Created
March 31, 2021 23:34
-
-
Save btm/e7280ac64feac531e4dd17898ad9a652 to your computer and use it in GitHub Desktop.
Script to rename Google Takeout Youtube music uploads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# terrible script for reading the music-uploads-metadata.csv file that Google Takeout includes when you download | |
# Youtube Music 'uploads' (which were Google Play Music songs/uploads). | |
# | |
# tries to identify the corresponding file and add artist/album/track to the mp3 tags | |
# use another tool like MediaMonkey afterward to rename the files, add cover art, etc. | |
# | |
# It's hard to separate tracks with the same name. Deal with them by hand. | |
# | |
# This worked for about 90% of my mp3s. Your mileage may vary. No promises or guarantees. | |
# This is hacked together. Works for me. Use at your own risk. | |
require "csv" | |
require "mp3info" | |
require "fileutils" | |
require "digest" | |
# remove/replace problematic characters for directories | |
def fix_dir(path) | |
path.tr(':/?', '') | |
end | |
music = CSV.read("music-uploads-metadata.csv") | |
# row[0] == track | |
# row[1] == album | |
# row[2] == artist | |
# row[3] == length seconds | |
music.each do |row| | |
# debugging loop conditional | |
# if row[0] == "Beat Crazy" | |
title = row[0] | |
album = row[1] | |
artist = row[2] | |
path1 = title + ".mp3" | |
path2 = title.tr("'/&?;:*\"", "_") + ".mp3" | |
path3 = path2[0...50] + ".mp3" | |
path4 = if title[-1] == '.' || title[-1] == "'" | |
title.chop + "_.mp3" | |
else | |
"" | |
end | |
path5 = path2.gsub(".mp3", "(1).mp3") | |
path6 = path2.gsub(".mp3", "(3).mp3") | |
path7 = path2.gsub(".mp3", "(2).mp3") | |
if File.exist?(path1) | |
mp3path = path1 | |
elsif File.exist?(path2) | |
mp3path = path2 | |
elsif File.exist?(path3) | |
mp3path = path3 | |
elsif File.exist?(path4) | |
mp3path = path4 | |
elsif File.exist?(path5) | |
mp3path = path5 | |
elsif File.exist?(path6) | |
mp3path = path6 | |
elsif File.exist?(path7) | |
mp3path = path7 | |
else | |
next | |
end | |
puts "* #{artist} - #{album} - #{title}" | |
Mp3Info.open(mp3path) do |mp3| | |
if mp3.tag.title.nil? | |
mp3.tag.title = title | |
mp3.tag.album = album | |
mp3.tag.artist = artist | |
puts " #{mp3path}: set title/album/artist" | |
else | |
puts " #{mp3path}: title already set, skipping" | |
end | |
end | |
# move and rename the file | |
album_dir = fix_dir("#{artist} - #{album}") | |
file_name = fix_dir("#{artist} - #{title}.mp3").tr('*"', '_') | |
Dir.mkdir(album_dir) unless Dir.exist?(album_dir) | |
destpath = "#{album_dir}/#{file_name}" | |
if File.exist?(destpath) | |
puts " #{mp3path}: '#{destpath}' ALREADY EXISTS!!!!" | |
puts " *** New File: #{Digest::SHA256.file(mp3path).hexdigest}" | |
puts " *** Existing File: #{Digest::SHA256.file(destpath).hexdigest}" | |
else | |
FileUtils.mv(mp3path, destpath) | |
puts " #{mp3path}: moved to '#{destpath}'" | |
end | |
# debugging loop end | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment