Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Help with reading MP3's

by StarkRavingCalm (Sexton)
on Aug 24, 2011 at 23:52 UTC ( #922231=perlquestion: print w/ replies, xml ) Need Help??
StarkRavingCalm has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I am trying to find a script to read .mp3s and their tags. I have no need to write or re-write tags.

My first step is to have script read mp3's but for some reason, I cannot get one to work from any that I have found. I have included an example from your site.

Script hangs with no output or, if using strict, throws error of:

"Global symbol "@info" requires explicit package name at"

NOTE: Features I would like to have are:

List all mp3 files and their tags as well as any without tags (this should say something like "no tag found" or something)

List files missing certain tag elements (i.e. photo)

Here's the code:


#!/usr/bin/perl -w use MP3::Tag; # define how autoinfo tries to get information # default: # MP3::Tag->config("autoinfo","ID3v2","ID3v1","filename"); # don't use ID3v2: # MP3::Tag->config("autoinfo","ID3v1","filename"); while (<STDIN>) { chomp; if (my $mp3=MP3::Tag->new($_)) { print "$_ (Tags: ", join(", ",$mp3->get_tags),")\n"; @info=$mp3->autoinfo; print "* Song: $info[0]\n"; print "* Track: $info[1]\n"; print "* Artist: $info[2]\n"; print "* Album: $info[3]\n"; print "* Comment: $info[4]\n"; } }

Comment on Help with reading MP3's
Download Code
Replies are listed 'Best First'.
Re: Help with reading MP3's
by GrandFather (Sage) on Aug 25, 2011 at 04:49 UTC

    Try adding a failure message to your 'open':

    my $file = 'file.mp3'; my $mp3 = MP3::Tag->new($file) or die "Can't open $file: $!\n";

    Although it doesn't seem to be documented, MP3::Tag->new seems to set $! on failure. Very likely you have a mismatch between to current directory and the directory you think is current when the script runs.

    True laziness is hard work

      Same results.

      NOTE: If I just put in a filename.mp3 or *.mp3 (mp3's are in the same directory as script), I get "No file or directory."

      If I give it an absolute path, it hangs. Using strict gives the following: "Global symbol "@info" requires explicit package name at"

        Using strict gives the following: "Global symbol "@info" requires explicit package name at"

        Yep! Because that line should say:
        my @info=$mp3->autoinfo;

        Your script reads the filenames from the standard input (keyboard) I assume. Are you inputting the full paths or correct relative paths?

        Wildcards in your input will NOT be expanded, so don't try those.

        If under windows, you need to pay special to spaces and such in the filename. Better still, surround your inputs with quotes marks

Re: Help with reading MP3's
by toolic (Bishop) on Aug 25, 2011 at 00:16 UTC
    What output do you get with this?
    use MP3::Tag; use warnings; use strict; use Data::Dumper; my $file = shift; my $mp3 = MP3::Tag->new($file); $mp3->get_tags(); print Dumper($mp3);
    Basic debugging checklist

    Did you try mp3info2?


      "Can't call method "get_tags" on an undefined value"

      I haven't tried mp3info2 as all the script I have found use MP3Tag

        Let's assume you have an mp3 file named "file.mp3".
        use MP3::Tag; use warnings; use strict; use Data::Dumper; my $file = 'file.mp3'; my $mp3 = MP3::Tag->new($file); $mp3->get_tags(); print Dumper($mp3);
        Substitute "file.mp3" with whatever your mp3 file name is.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://922231]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2015-11-26 17:33 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (705 votes), past polls