Sunday, January 11, 2009

Renaming MP3 by ID tag

Ever tried to recover from an IPOD dump, and get a bunch of files with the useless names? Here's a script to fix that.
$ ls *.mp3 | ./rename.pl - | while read line; do OLD=`echo $line | cut -d, -f1`; NEW=`echo $line | cut -d, -f2`; mv "$OLD" "$NEW"; done

And here's the perl script:

#!/usr/bin/perl
#
#Your Love Means Everything.mp3: (tag v1.0)
#songname: Your Love Means Everything
#artist: Coldplay
#album: Varie
#year:
#comment:
#genre: Other(12)
#

while( <> )
{
chomp;
my %hsh = ();
my @info = `id3ed -i "$_"`;
foreach my $iline ( @info )
{
chomp $iline;
#print $iline . "\n";
if( $iline =~ /songname:\s+\b(.+?)$/ ){
$hsh{ song } = $1;
}
if( $iline =~ /artist:\s+\b(.+?)$/ ){
$hsh{ artist } = $1;
}
if( $iline =~ /album:\s+\b(.+?)$/ ){
$hsh{ album } = $1;
}
}

my $newname = "$hsh{ artist }" . "-" . "$hsh{ album }" . "-" . "$hsh{ song }" . ".mp3";
$newname =~ s/\s+/_/g;
print "$_,$newname\n";
}

No comments: