MP3 renumbering script

Tagged:

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-&gt;new($filename);
 
  # get tag information
  $mp3-&gt;get_tags();
 
  # if ID3v2 tags exists
  if (exists $mp3-&gt;{ID3v2})
  {
      # get a list of frames as a hash reference
      my $id3 = $mp3-&gt;{ID3v2};
      my $frames = $id3-&gt;get_frame_ids();
 
      print "Title: " . $id3-&gt;get_frame('TIT2') . "\n";
      my $disc =  $id3-&gt;get_frame('TPOS');
      my ($discno,$dtotal) = split(/\//, $disc);
      print "Disc: $disc\n";
      my $track = $id3-&gt;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-&gt;change_frame('TIT2', $newtitle);
      $id3-&gt;write_tag();
  }
 
  # clean up
  $mp3-&gt;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

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>, <shell>, <c>, <drupal6>, <java>, <javascript>, <objc>, <perl>, <php>, <python>, <rails>, <ruby>, <sql>, <xmlcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.