要在Perl中将图稿图像添加到mp3文件,我应该使用哪个库?示例代码将不胜感激。
答案 0 :(得分:2)
使用此库:
http://metacpan.org/pod/MP3::Tag::ID3v2
#!/usr/local/bin/perl
use MP3::Tag;
use Image::Magick;
$file = “file.mp3″;
$jpg = “file.jpg”;
my $image = new Image::Magick;
if (my $x = $image->Read($jpg)) {
print “Couldn’t read image ‘$jpg’\n”;
}else{
$imagedata = $image->ImageToBlob(magick => jpg);
undef $image;
}
my $mp3 = MP3::Tag->new($file);
$mp3->get_tags();
$mp3->{ID3v2}->remove_tag() if exists $mp3->{ID3v2};
$mp3->{ID3v1}->remove_tag() if exists $mp3->{ID3v1};
my $id3v2 = $mp3->new_tag(“ID3v2″);
$id3v2->add_frame(“TALB”, $album);
$id3v2->add_frame(“TPE1″, $artist);
$id3v2->add_frame(“TIT2″, $title);
$id3v2->add_frame(“APIC”, chr(0×0), ‘image/jpg’, chr(0×0), ‘Cover (front)’, $imagedata);
$id3v2->write_tag();
my $id3v1 = $mp3->new_tag(“ID3v1″);
$id3v1->song($title);
$id3v1->artist($artist);
$id3v1->album($album);
$id3v1->write_tag();
$mp3->close();
有些人报告它会生成损坏的mp3,不知道它是否已经修复,但如果没有,请注释掉该行:
$flags = chr(128) if $tag_data =~ s/\xFF(?=[\x00\xE0-\xFF])/\xFF\x00/g;
它会正常工作。