Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i'm working on a script that basically just reads in my mp3 directory and makes a list of my mp3s, but frankly, filenames are ugly. winamp has a feature to generate an html playlist that lists the song title and artist instead of the filename, but i'd rather have more control over it. does anyone know how to extract the id3 tag info out of the file?

Replies are listed 'Best First'.
Re: title/artist info
by dws (Chancellor) on Jul 27, 2002 at 07:41 UTC
    does anyone know how to extract the id3 tag info out of the file?

    MP3::Info

Re: title/artist info
by dree (Monsignor) on Jul 27, 2002 at 12:19 UTC
    You can also use tagged.

    Here a simple mp3 command line renamer that use File::Find to traverse the given directory structure:
    use strict; use File::Find; use MP3::Tag; my $dir=$ARGV[0]; die "use mp3ren.pl directory_name" if (!$dir); find (\&wanted, "$dir"); sub wanted { if (&match("$File::Find::name")) { &rename_MP3($File::Find::name); } } sub match { my $filename=$_[0]; if (-d $filename) { return 0; } my $mp3 = MP3::Tag->new($filename); if (defined $mp3) { $mp3->get_tags; if (exists $mp3->{ID3v1}) { return 1; } else { print "$filename is not mp3 or no ID3v1 tag\n"; return 0; } } } sub rename_MP3 { my $fullname_mp3=$_[0]; my $mp3 = MP3::Tag->new($fullname_mp3); my $new_mp3_name; $mp3->get_tags; my $song=$mp3->{ID3v1}->song; my $artist=$mp3->{ID3v1}->artist; if ($artist && $song) { $new_mp3_name="$artist--$song"; } elsif ($song) { $new_mp3_name="$song"; } elsif ($artist) { $new_mp3_name="$artist--untitled"; } else { $new_mp3_name=""; } if ($new_mp3_name) { $new_mp3_name=~s#[\\|\?|\*|\||\:|"|<|>|/]+##g; $new_mp3_name.=".mp3"; $new_mp3_name="$File::Find::dir/$new_mp3_name"; if ($fullname_mp3 eq $new_mp3_name) { print "Error renaming $fullname_mp3 to $new_mp3_name: same nam +e\n" } elsif (!rename("$fullname_mp3","$new_mp3_name")) { print "Error renaming $fullname_mp3 to $new_mp3_name: $!\n" } else { print "Renamed $fullname_mp3 to $new_mp3_name\n" } } else { print "Error renaming $fullname_mp3: no ID3v1 tag\n" } }