Max Baker — A Man with a Hand

iTunes – Setting the Compilation Flag on MP3’s using Perl

Update

I’ve consolidated this script into a new one. GO HERE : http://warped.org/blog/2009/05/03/itunes-fixing-id3-tags-in-mp3s-take-two/

Overview

If you have an album that’s a compilation or DJ mix, then Cover Flow gets confused and does not group all the tracks together into an album because there is a different “Artist” for each track.

iTunes gets around this by setting it’s own custom flag in the ID3 tag : the TCMP frame in ID3v2 to be specific.

The Script

The following script uses MP3::Tag to set the TCMP flag on all the MP3’s in the current directory, or the files specified on the command line.

#!/usr/bin/perl -w
# mp3_make_comp
# Max Baker
# 4/29/09
#
# This script will set the I-Tunes Compilation Tag (TCMP)
# on Files passed to it.   If no files are passed, it works on *.mp3 in the current directory.
#
 
use MP3::Tag;
 
unless (scalar @ARGV) {
    @ARGV = glob("*.mp3");
}
 
foreach my $f (@ARGV) {
    next unless -r $f;
    add_comp($f);
}
 
sub add_comp {
    my $file = shift;
    my $mp3 = MP3::Tag->new($file);
    $mp3->config('write_v24' => 1);
 
    # scan file for existing tags
    $mp3->get_tags;
 
    unless (exists $mp3->{ID3v2}) {
        $mp3->new_tag("ID3v2");
    }
 
    # check for existing tag
    my ($info, $name, @rest) = $mp3->{ID3v2}->get_frame("TCMP");
    if (defined($info)) {
        print "$file : TCMP=$info already set.\n";
        return;
    }   
 
    print "$file : Setting TCMP=1\n";
    $mp3->{ID3v2}->add_frame("TCMP", "1") or die "$file : Adding TCMP frame failed.\n";
    $mp3->{ID3v2}->write_tag;
    $mp3->close();
}

Prerequisites

I managed to find a bug in MP3::Tag, so you may have to do the patch found here.

$data = pack("N", length($data)) . compress $data
unless $frame->flags->{unchanged};

Becomes:

$data = pack("N", length($data)) . compress $data
unless $frame->{flags}->{unchanged};

On line 585 of ID3v2.pm.

Next Up : Copying ID3v1 tags to ID3v2 tags.

Share and Enjoy:
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Blogosphere News
  • email
  • LinkedIn
  • Print
  • Reddit
  • Slashdot
  • Add to favorites
  • RSS
  • Technorati
  • PDF

2 Responses to “iTunes – Setting the Compilation Flag on MP3’s using Perl”

Trackbacks

  1. iTunes - Copying ID3 Tags in your MP3s from ID3v1 to ID3v2 in Perl | One hand clapping
  2. iTunes - Fixing ID3 tags in MP3’s - Take two | One hand clapping

Leave a Reply