如何从提供的MP3中读取ID3v2标签并在Perl中打印所有信息?示例代码将不胜感激
答案 0 :(得分:3)
您似乎有很多不同的选择:
所有这些模块都在其文档中提供了一个示例,因此您可以轻松开始使用。
所有这些,我尝试尝试使用Audio :: TagLib,但个人我花了半个小时试图安装库和模块,所以我放弃了然后检查MP3 ::标签,它就像魅力一样立即起作用。
所以这是我成功测试的一个小例子:
use strict;
use warnings;
use MP3::Tag;
use Data::Dumper;
my $mp3 = MP3::Tag->new("anthony_rother-phobos.mp3");
$mp3->get_tags();
my $id3v2 = $mp3->{ID3v2} if exists $mp3->{ID3v2};
print Dumper($id3v2); #returns an MP3::Tag::ID3v2=HASH object
答案 1 :(得分:0)
对我有用的一些东西可能不是最好的解决方案。
my $myFile = shift or die "Usage: perl task3 <file.mp3>\n";
open myMP3File, "<$myFile" or die "Error! cant open file \n";#open mode read
binmode(myMP3File); #read in binary mode.
#read file and place it in buffer string
my $length = 512;
read (myMP3File, my $buffer, $length);
print "Displaying ID3v2 Header for " .$myFile.": \n";
my $tagHeader = substr($buffer, 0, 10);#first 10 bytes.
my ($IDtag, $version, $revision, $flag, $size) = unpack('A3 h h h N4',$tagHeader);
print "TagID : $IDtag\n";
print "Version : $version\n";
print "Revision : $revision\n";
print "Flags : $flag\n";
print "Size : $size\n";
my $len = 0;
my $ptr1 = 0;
my $ptr2 = 0;
#Reading frames after header
while (1)
{
#reading 10 bytes for each frame and adding 10 bytes for next frame
$ptr1 += 10+$len;
$ptr2 = $ptr1+10;
#reading frame header contains 4bytes frame ID,4 bytes frame size, 2 bytes flags
my $frameHeader = substr($buffer,$ptr1,10);
# A null/space padding string, N 16/32 bit value(big-ending) , h hexadecimal string
my($frameID, $frameSize, $flags) = unpack('A4 N4 h2',$frameHeader);
#TALB:album-name,TCON:content-type,TIT2:title,TPE1:Artist, TRCK:Track Number,TYER: year
if (($frameID eq 'TALB') || ($frameID eq 'TCON') || ($frameID eq 'TIT2') || ($frameID eq 'TPE1') || ($frameID eq 'TRCK') || ($frameID eq 'TYER'))
{
my $readFrame = substr($buffer, $ptr2, $frameSize);#reading frame content
my $myFrame = unpack('A*($frameSize)', $readFrame);
print "$frameID : $myFrame \n";#frame info
$len=$frameSize;#save pointer location.
}
else
{ die "Ends Here \n"; }
}
close(myMP3File);