Google

Campaign0

Saturday, February 28, 2009

Or, Let Picard ID Your Music

If you'd prefer a bit more control over your music id'ing than the previously posted ruby script, then Picard should fit the bill. I'm using it in conjunction with the noted script.

http://musicbrainz.org/doc/PicardTagger

From the site:

Picard is the next generation MusicBrainz tagging application. This new tagging concept is album oriented, as opposed to track/file oriented like the ClassicTagger was. Picard is written in Python, which is a cross-platform language, and makes use of cross-platform libraries - this allows the same code to run both on Windows, Linux and Mac OS X.

Let Ruby ID Your Music

Tender Lovemaking » Blog Archive » Identifying unknown music with Ruby

A quick intro noted above from the author of the earworm gem. A really cool piece of work for identifying music files.

Here's how I am using it -- note that I'm also using rbrainz, another really neat piece of code.. This code needs to be a bit neater/more functional, but it was a quick solution to a bunch of mp3's pulled down from spinner.com's and daytrotter.com's mp3 of the day pages.

What it does:
Walks a file hierarchy finding *.mp3/*.MP3 files.
Renames the file based on the id3 tags in the file.
Relocates the file into my Music directory in the proper file hierarchy based on the id3 tags.
If it can't id the file via id3 tags, it tries to id the file via rbrainz, and update the tags
If it can, it relocates the file appropriately
If it can't it dumps the file into Music/undertermined -- I can come back next month and try the id again.


require 'rubygems'
require 'id3lib'
require 'earworm'
require 'rbrainz'
include MusicBrainz

class String
def alnum_ws_sanitize()
self.gsub!(/[^\/[:alnum:]\s]/, '')
end
end

def printAndDescend(pattern)
#we keep track of the directories, to be used in the second, recursive part of this function
directories=[]
Dir['*'].sort.each do |name|
if File.file?(name) and name[pattern]
# Load a tag from a file
tag = ID3Lib::Tag.new(File.expand_path(name))
#p tag
musicdir="/home/rthompso/Music/"
if tag.title and tag.album and tag.artist
dirpath = musicdir + tag.artist.strip + "/" + tag.album.strip
dirpath.alnum_ws_sanitize

puts dirpath
if !File.directory?(dirpath)
FileUtils.mkdir_p(dirpath)
end

# if !File.directory?(tag.album)
# FileUtils.mkdir_p tag.album
# end

tag.title.alnum_ws_sanitize
if !File.exists?(tag.title)
dest = dirpath.strip + "/" + tag.title.strip.tr('/()','-') + ".mp3"
FileUtils.mv(File.expand_path(name), dest)
end
puts "Title: " + tag.title
puts "Album: " + tag.album
puts "Artist: " + tag.artist
else
# puts "Title: " + tag.title
# puts "Album: " + tag.album
# puts "Artist: " + tag.artist
puts File.expand_path(name)
ew = Earworm::Client.new('9fd161da6c15d840fc25f0df0fac7b1f')
begin
info = ew.identify(:file => File.expand_path(name))
rescue => e
FileUtils.mv(File.expand_path(name), "/home/rthompso/Music/undetermined")
puts "ew.identify failed"
puts(File.expand_path(name))
next
end
q = MusicBrainz::Webservice::Query.new
results = q.get_tracks(Webservice::TrackFilter.new(:puid => info.puid_list))
if results.count
begin
tag.title = results[0].entity.title
tag.album = results[0].entity.releases.entries
tag.artist = results[0].entity.artist
tag.update!
#puts results[0].score

puts results[0].entity.title
puts results[0].entity.releases.entries
puts results[0].entity.artist
puts "going to set tags now"
rescue
FileUtils.mv(File.expand_path(name), "/home/rthompso/Music/undetermined")
puts "musicbrainz failed"
puts(File.expand_path(name))
end
else
FileUtils.mv(File.expand_path(name), "/home/rthompso/Music/undetermined")
puts(File.expand_path(name))

end
end

elsif File.directory?(name)
directories << name
end
end
directories.each do |name|
#don't descend into . or .. on linux
Dir.chdir(name){printAndDescend(pattern)} if !Dir.pwd[File.expand_path(name)]
end
end

#print all ruby files
printAndDescend(/.+\.mp3$/)
printAndDescend(/.+\.MP3$/)