Skip to content

Instantly share code, notes, and snippets.

@meshivam
Created August 30, 2015 14:49
Show Gist options
  • Save meshivam/8ab97f500f8cabf11ff1 to your computer and use it in GitHub Desktop.
Save meshivam/8ab97f500f8cabf11ff1 to your computer and use it in GitHub Desktop.
A Ruby script to fix names of iPod recovered files in file system.
require "id3tag"
require "fileutils"
puts "enter absolute path to music folder:"
indir = gets.chomp
# Example: "/home/shivam/Music/Music/F01"
indir = indir.sub(/\/$/, "") + "/*"
puts "enter extraction path (absolute):"
outdir = gets.chomp
# Example: "/tmp/songs"
outdir = outdir.sub(/\/$/, "") + "/"
files = Dir[indir]
files.reject! { |file| file !~ /.mp3/ }
meta_array = []
files.each { |file|
hash = {}
ID3Tag.read(File.open(file, "rb")) do |tag|
hash[:artist] = tag.artist
hash[:title] = tag.title
hash[:album] = tag.album
hash[:path] = file
end
meta_array << hash
}
albums = meta_array.group_by { |a| a[:album] }
begin
albums.each { |album|
album_path = outdir + album[0].gsub(/\//,"-")
FileUtils.mkdir_p album_path
album.last.each { |hash|
title = (hash[:title] || "Unknown Track #{rand(1000)}").gsub(/\//,"-")
FileUtils.cp hash[:path], (album_path + "/" + title).sub(/\.mp3$/,"") + ".mp3"
}
}
end
rescue => e
puts e.inspect
puts @hash.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment