Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Organize and rename a folder of mp3s by album and title
# usage:
# ruby organize_mp3s.rb /path/to/mp3/directory/
require 'shellwords'
def clean filename
return "" unless filename
filename.gsub(/[^0-9A-Za-z.\- ]/, '_')
end
Dir.glob(File.join(ARGV.first, "*").each{|path|
info = `id3info #{path.shellescape}`.unpack("C*").pack("U*")
if info.include?("TIT2")
id3 = Hash[info.scan(/^=== ([^ ]+).*\: (.+)$/)]
title = id3["TIT2"]
album = id3["TALB"]
track = id3["TRCK"]
destination = "#{clean album}/#{clean track}. #{clean title}#{File.extname(path)}"
destination = File.join(File.dirname(path), destination)
`mkdir -p #{File.dirname(destination).shellescape}`
`mv #{path.shellescape} #{destination.shellescape}`
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.