rename mp3s from id3s

#!/usr/bin/perl
require 5;
use strict;
use Image::ExifTool ‘ImageInfo’;

my $trackdir = $ARGV[0];
opendir(DIR, $trackdir) || die(“unable to open directory $trackdir”);
my @tracks = grep { /.mp3/i } readdir DIR;
closedir DIR;
my $exifTool = new Image::ExifTool;

foreach (@tracks) {
my $filename = “$trackdir/$_”;

$exifTool->ExtractInfo($filename);

my $title = $exifTool->GetValue(‘Title’);
my $album = $exifTool->GetValue(‘Album’);
my $artist = $exifTool->GetValue(‘Artist’);
my $genre = $exifTool->GetValue(‘Genre’);
my $track = $exifTool->GetValue(‘Track’);

$track =~ /[^0-9]*([0-9]+)[^0-9]*/;
$track = $1;

print STDERR “$artist $album $title $genre $track\n”;

$track = sprintf(“%02d”, $track);
my $newname = “$artist-$album-$track-$title”;
$newname =~ s/[^A-Za-z0-9\-\.]+/_/g;
rename(“$trackdir/$_”,”${trackdir}/${newname}.mp3″);

}

Leave a Reply