MP3 renumbering script
iTunes is decent enough, primarily for keeping up with podcasts on the ipod, but it's terrible at mass renaming/renumbering. It doesn't come up often, but just enough. I have plenty of multi-disc albums (audio books, mainly), only some of which are in CDDB. So I end up with some tracks with iTunes' default "Track NN" and others with "N-MM" or "Nm", where m/M could be alpha or numeric. Bothers the TypeA in me! I tried MediaMonkey, but it doesn't seem to recognize the disc number (ID3 frame: TPOS), otherwise it appears to have a decent scripting ability.
So Perl to the rescue! (Ruby would have been easier, but the current ID3 library only supports read-write for v1.1 tags). An hour later (mostly due to remembering the syntax), I had the following code. (Downloadable code)
<code>
#!/usr/bin/perl -w
# use module
use strict;
use MP3::Tag;
use File::Glob ':glob';
my $dir = shift || ".";
die "Invalid directory specified.\n" unless -d $dir;
chdir $dir;
my @files = bsd_glob('*.mp3');
foreach my $filename (@files)
{
print "Filename: $filename\n";
# create new MP3-Tag object
my $mp3 = MP3::Tag->new($filename);
# get tag information
$mp3->get_tags();
# if ID3v2 tags exists
if (exists $mp3->{ID3v2})
{
# get a list of frames as a hash reference
my $id3 = $mp3->{ID3v2};
my $frames = $id3->get_frame_ids();
print "Title: " . $id3->get_frame('TIT2') . "\n";
my $disc = $id3->get_frame('TPOS');
my ($discno,$dtotal) = split(/\//, $disc);
print "Disc: $disc\n";
my $track = $id3->get_frame('TRCK');
my ($trackno,$ttotal) = split(/\//, $track);
print "Track: $track\n";
my $maxdisclen = length($dtotal);
my $maxtracklen = length($ttotal);
my $newtitle = sprintf "%.*u-%.*u",
$maxdisclen, $discno,
$maxtracklen, $trackno;
print "New Title: $newtitle\n";
$id3->change_frame('TIT2', $newtitle);
$id3->write_tag();
}
# clean up
$mp3->close();
}
exit 0;
</code>
Comments
Cool topic! ;)
Cool topic! ;)
Just ran across this program,
Just ran across this program, which is only a little more featured than my script. Maybe a better Google search would have turned it up first, but where's the fun in that?!http://www.id3renamer.com/
Post new comment