Bitacoreando

26/1/2007

Fix MP3 Tags with Rake

I'm using Rake to do all kinds of system tasks, half to learn and half because it's a great system tool. One itch that I wanted to scratch was to fix lots of MP3 albums, that dont' have the correct track number and lack embedded cover artwork. Fixing them by hand in Itunes (specially the track number) is quite tedious, so I came up with this little snippet to fix it. Enjoy.
#
# Rakefile to modify tag information
# in MP3 files.
#
# (c) Rubén Marrero (ruben-snippets@lingo.com.mx)
#
# This work is licensed under the Creative Commons Attribution 2.5 License. 
# To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ 
# or send a letter to Creative Commons, 543 Howard Street, 5th Floor, 
# San Francisco, California, 94105, USA.
#
# === ABOUT THIS SNIPPET ===
# This rakefile fixes some ID3 tags of a given set of MP3 files; Currently fixes:
# * :ACPI, by adding a "cover.jpg" file to each song
# * :TCON (genre) by deleting it. It's always wrong, anyway
# * :TRK (track number) by extracting the track number from the filename
#
# === INSTALLATION ===
# Needs the following gems already installed in your system:
#
# id3lib (http://id3lib-ruby.rubyforge.org)
# 
# Just copy to the top of your music collection to a file named Rakefile  
#
# === USAGE ==
# 1. cd down to the folder that contains the MP3 files to fix. Copy the cover
#  of the album to that same folder with the name "cover.jpg"
#
# 2. Execute as 
#
# rake fixmp3 					# to fix all mp3 files inside the folder, or
# rake fixmp3 filename.mp3 filename2.mp3 	# to fix selected files.
# 
# Currently fixes files with filenames in the form:
#
# 01-i_will_follow_192_lame_presetstandart.mp3
# 01_-_afraid_(feat_attilude)_192_lame_presetstandart_ex.mp3
#
#
# === WARRANTY ===
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#


require 'rubygems'
require 'id3lib'
desc "Fixes allofmp3 songs to include track number and cover. Check code for usage"
task :fixmp3 do
cd Rake.original_dir

ARGV.shift
files  = (ARGV.length == 0) ? Dir.glob("*.mp3") : ARGV
files.sort!
	cover = {
	  :id          => :APIC,
	  :mimetype    => 'image/jpeg',
	  :picturetype => 3,
	  :description => 'Album cover',
	  :textenc     => 0,
	  :data        => File.read('cover.jpg')
	}
	files.each do |song|
	 asong = File.basename(song)
	 track_no = /^(..)[-|_]/.match(asong)[1].to_i
	 tag = ID3Lib::Tag.new(song)
	 #Clean stuff...
	 # Remove all covers
	 tag.delete_if{ |frame| frame[:id] == :APIC }
	 # Remove all genres
	 tag.delete_if{ |frame| frame[:id] == :TCON }

	 # add cover.jpg to the song
	 tag < < cover
	 # add track nnumberfrom filename
	 tag.track=track_no

	 tag.update!
	puts "
Filename: #{asong} 
Title: #{tag.title} 
Album: #{tag.album}
Track: #{tag.track}
	"

	end


end #task
Filed under: Bitacoreando — ruben @ 3:10 pm

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress