0

How can i read the ID3v2 tag from the provided MP3 and print all information in Perl? Sample Code will be appreciated

sT0n3
  • 125
  • 2
  • 13

2 Answers2

3

It seems that you have many different options:

All these modules provide an example in their documentation, so it will be easy for you to get started.

So from all of these, I attempted to give it a try at Audio::TagLib, but personally I spent half-an-hour trying to install the library and module, so I quit and then checked MP3::Tag which worked immediately like a charm.

So here's a small example that I tested successfully:

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
psxls
  • 6,807
  • 6
  • 30
  • 50
  • 1
    Of course. For example `MP3::Tag` does not have any dependencies to external libraries. It is pure Perl. So if you are curious, you can check its [source code](http://cpansearch.perl.org/src/ILYAZ/MP3-Tag-1.13/lib/MP3/Tag/ID3v2.pm) and see how it is done. You can also read the official [ID3v2 specification](http://id3lib.sourceforge.net/id3/develop.html) to get you started. – psxls Nov 12 '13 at 13:38
0

Some thing that worked for me might not be the best solution.

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);
sT0n3
  • 125
  • 2
  • 13