#
# 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
-
Rubén Marrero
30-something, Mexico City
Technology / Politics / Culture / Society / Business

Thanks for this sweet script! Just what I’ve been looking for