Question

Im trying to save the image data from am id3 tag to an jpg file

My actual script reads the image data and writes it to a file, whichs code looks exact like the original image file, but if i open it it looks like that: http://i.imgur.com/opq3Gqb.jpg Original picture: http://i.imgur.com/ZZGLBmK.jpg

I use the following code:

use MP3::Tag;
use strict;
use warnings;

    my $filepath = "test.mp3";

    my $mp3 = MP3::Tag->new($filepath);
    $mp3->get_tags();

    my $id3v2_tagdata   = $mp3->{ID3v2};
    my $info            = $id3v2_tagdata->get_frame("APIC");
    my $imgdata         = $$info{'_Data'};

     $mp3->close();

open (COVER, ">test.jpeg");
print COVER "$imgdata";
close (COVER); 

Wheres my mistake?

Was it helpful?

Solution

Might need to open the filehandle in binary mode:

Straight from the binmode documentation: "In other words: regardless of platform, use binmode() on binary data, like images, for example."

use strict;
use warnings;
use autodie;

...

open my $fh, '>:raw', 'test.jpeg';
binmode $fh;
print $fh $imgdata;
close $fh;

Read PerlIO for more information on opening files in different modes. I included both '>:raw' and binmode $fh in the above code to increase familiarity, but these indicators are actaully equivalent as you'd read in PerlIO. So feel free to use just one or the other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top