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

Dear Monks,

I am currently trying to retrieve the Album Art from my MP3 files using the module MP3::Tag.

I currently have the following code after reading the documentation :

use MP3::Tag; my $mp3 = MP3::Tag->new("E:/My Music/File.mp3") or die “Can’t open Fil +e”; # Get the tags from the MP3 $mp3->get_tags; # Get the ID3 V2 tag where the picture is saved $id3v2 = $mp3->{ID3v2}; # Get the APIC frame from the Tag (The Image) which # returns a hash ref $Pic = $id3v2->get_frame("APIC"); # returns print "Content-type: image/jpeg\n\n"; print $Pic->{_Data};

If I just print $Pic->{_Data} to a text file with Binmode set, the file is not recognised as a valid image, however there is loads of binary data in the file.

My aim is for the script to return the Album Art image from a selected MP3 to the Browser.

I found the following documentation on CPAN http://search.cpan.org/~ilyaz/MP3-Tag-0.96/ID3v2-Data.pod but it only gives information on how to access the image data and not how to print it. I have searched the net and there is lots of examples on how to set the Album Art, but none to get it and print it to a browser.

Any one have any ideas / pointers?

Thanks,

Al

Replies are listed 'Best First'.
Re: Get MP3 Album Art with MP3::Tag
by true (Pilgrim) on Jul 21, 2005 at 00:59 UTC
    You almost had it. Below example works for me. Set binmode to your outfit after you open the handle. Also, you don't need to print the content-type header unless you are printing to STDOUT (like through a browser).
    use MP3::Tag; my $mp3 = MP3::Tag->new("withimage.mp3") or die "no File"; # Get the tags from the MP3 $mp3->get_tags; $id3v2 = $mp3->{ID3v2}; # Get the APIC frame from the Tag (The Image) which # returns a hash ref my $Pic = $id3v2->get_frame("APIC"); # returns open (SAVE, ">out.jpg"); binmode SAVE; print SAVE $Pic->{_Data}; close SAVE;
    jtrue
      Thanks for the reply!

      I was hoping to be able to print out to a browser directly to save having to create files?

      I thought that the following would achieve this, but when loaded from a Web Server the image only shows as the Red X , meaning it is corrupt.

      print "Content-type: image/jpeg\n\n"; print $Pic->{_Data};

      Any ideas? I will try the output to a file later on to see if I can get any further.

      Alistair

        You have to set binmode to STDOUT just like you did to the file. This example works off Windows running Apache2 and ActiveState perl 5.8
        #!C:/Perl/bin/perl use MP3::Tag; my $mp3 = MP3::Tag->new("withimage.mp3") or die "no File"; $mp3->get_tags; $id3v2 = $mp3->{ID3v2}; my $Pic = $id3v2->get_frame("APIC"); # returns print "Content-type:image/jpeg\r\n\r\n"; binmode STDOUT; print STDOUT $Pic->{_Data};
        jtrue