Created
October 18, 2018 20:28
-
-
Save christiangenco/a16fd397b6f532f92ff70d09f7a1242c to your computer and use it in GitHub Desktop.
Organize and rename a folder of mp3s by album and title
This file contains hidden or 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
# 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