#/usr/bin/perl -w use strict; use Getopt::Std; use MP3::Info; use File::Find; my ($file, @dirs, $tmp, %mp3s, @duplicates, @lines, %options, $artist, $album, $title); getopts("f:m:rdh", \%options); if ($options{h}) { print <<'eof'; -f file: where to save the mp3 list -m dir: mp3 directory -r: rename the mp3 files according to there id3 tags -d: delete duplicates from the list of mp3s -h: this help document eof exit; } $file = $options{f} || "/Library/Webserver/Documents/mp3.info"; @dirs = $options{m} || ("/Volumes/Storage Drive/Music/"); find(\&id3info, @dirs); sub id3info { return unless $File::Find::name =~ /^.+?\.[Mm][Pp]3$/; $tmp = get_mp3tag($File::Find::name); unless (exists $mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}) { %{$mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}} = ( 'PATH' => $File::Find::name, 'TRACK' => $$tmp{'TRACK'}, 'TIME' => $$tmp{'TIME'}, 'SIZE' => $$tmp{'SIZE'}, 'BITRATE' => $$tmp{'BITRATE'} ); } else { if ($mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}{'SIZE'} < $$tmp{'SIZE'}) { push @duplicates, $mp3s{$artist}{$album}{$title}{'PATH'}; %{$mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}} = ( 'PATH' => $File::Find::name, 'TRACK' => $$tmp{'TRACK'}, 'TIME' => $$tmp{'TIME'}, 'SIZE' => $$tmp{'SIZE'}, 'BITRATE' => $$tmp{'BITRATE'} ); } else { push @duplicates, $File::Find::name; } } } if ($options{d}) { delete_duplicates(\@duplicates); } if ($options{r}) { rename_mp3s(\%mp3s); } foreach $artist (keys %mp3s) { foreach $album (keys %{ $mp3s{$artist} }) { foreach $title (keys %{ $mp3s{$artist}{$album} }) { push @lines, print $artist . '::' . $album . '::' . $title . '::' . $mp3s{$artist}{$album}{$title}{'PATH'} . "\n"; } } } open (FILE, ">$file") or die ("Can't open $file: $!\n"); foreach (@lines) { print FILE $_; } close (FILE); sub rename_mp3s { my ($mp3s) = @_; my ($artist, $album, $title); foreach $artist (keys %mp3s) { foreach $album (keys %{ $mp3s{$artist} }) { foreach $title (keys %{ $mp3s{$artist}{$album} }) { $mp3s{$artist}{$album}{$title}{'PATH'} =~ /^(.+\/)/; rename $mp3s{$artist}{$album}{$title}{'PATH'}, $1 . $artist . ' - ' . $title . '.mp3'; } } } } sub delete_duplicates { my ($duplicates) = @_; foreach (@$duplicates) { unlink($_) or warn "Couldn't delete $_: $!\n"; } }