ID3v2 cover art insertion with Perl?

ID3v2 cover art insertion with Perl?

am 02.04.2008 08:21:46 von Seppo Ingalsuo

Hi,

I'm struggling with my flac->mp3 music library maintenance script. based
on some net articles I have used MP3::Tag to attach cover art this way

use constant HEADER => ( chr(0x0) , "image/jpeg" , chr(0x0), "");

$img = new Image::Magick;
if (! $img->Read("$flacdir/cover.jpg")) {
$id3v2->add_frame( "APIC", HEADER, $img->ImageToBlob(magick => 'jpg'));
}

There is also setting

MP3::Tag->config("id3v23_unsync_size_w" => "TRUE");

See e.g. http://perl.thiesen.org/scripts/id3image

But I get corrupted cover art images in Amarok. iTunes and Windows Media
Player seem to work with it. MediaTomb uPnP server + PS3 doesn't find
the pictures. I'd like to get MediaTomb that uses taglib for metadata to
work.

There are Perl bindings for taglib in Audio::TagLib in CPAN. I'm able to
generate basic ID3v1 tags with it but nothing else since the
documentation is missing/minimal and I'm far from fluent in Perl to get
the information from Audio::TagLib source code. Any pointers how to do
proper ID3v2 APIC tags with Perl would be really helpful.

I found this but it seems to generate only ID3v1 tags based on "id3v2
-l" output and there is no example of
Audio::TagLib::ID3v2::AttachedPictureFrame

http://www.mfischer.com/2007/11/21/mp3-metadata-tagging/

BR,
Seppo

Re: ID3v2 cover art insertion with Perl?

am 02.04.2008 13:02:12 von Seppo Ingalsuo

Seppo Ingalsuo wrote:

> There are Perl bindings for taglib in Audio::TagLib in CPAN. I'm able to
> generate basic ID3v1 tags with it but nothing else since the
> documentation is missing/minimal and I'm far from fluent in Perl to get
> the information from Audio::TagLib source code. Any pointers how to do
> proper ID3v2 APIC tags with Perl would be really helpful.

As response to myself this seems to work ... :^)

my $mp3 = Audio::TagLib::MPEG::File->new($outfile);
my $id3v2 = $mp3->ID3v2Tag(1);
$id3v2->setArtist(Audio::TagLib::String->new($artist));
$id3v2->setAlbum(Audio::TagLib::String->new($album));
$id3v2->setTitle(Audio::TagLib::String->new($title));
$id3v2->setYear($date);
$id3v2->setTrack($track);
$id3v2->setGenre(Audio::TagLib::String->new($genre));
if (open(PICFILE, "$flacdir/cover.jpg")) {
$filesize = (-s PICFILE);
binmode(PICFILE);
read(PICFILE, $imgdata, $filesize);
close(PICFILE);
my $imgbv = Audio::TagLib::ByteVector->new();
$imgbv->setData($imgdata,$filesize);
my $bv = Audio::TagLib::ByteVector->new("APIC");
my $field =
Audio::TagLib::ID3v2::AttachedPictureFrame->new($bv, "UTF8");
$field->setPicture($imgbv);
$field->setTextEncoding("UTF8");
$field->setMimeType(Audio::TagLib::String->new("image/jpeg") );
$field->setType("Other");
$field->setDescription(Audio::TagLib::String->new("Cover
(front)"));
$id3v2->addFrame($field);
}
$mp3->save();

BR,
Seppo