Skip to content

Instantly share code, notes, and snippets.

@christiangenco
Created October 18, 2018 20:28
Show Gist options
  • Save christiangenco/a16fd397b6f532f92ff70d09f7a1242c to your computer and use it in GitHub Desktop.
Save christiangenco/a16fd397b6f532f92ff70d09f7a1242c to your computer and use it in GitHub Desktop.
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