Commit "FS#10468 - USB HID: Show keypad mode on screen"
[kugel-rb.git] / tools / mp3info.pm
blobd9007772667318f2d93a1449911dbcc0c88bcb4d
1 package mp3info;
3 require 5.006;
5 use overload;
6 use strict;
7 use Carp;
9 use vars qw(
10 @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $REVISION
11 @mp3_genres %mp3_genres @winamp_genres %winamp_genres $try_harder
12 @t_bitrate @t_sampling_freq @frequency_tbl %v1_tag_fields
13 @v1_tag_names %v2_tag_names %v2_to_v1_names $AUTOLOAD
14 @mp3_info_fields %rva2_channel_types
17 @ISA = 'Exporter';
18 @EXPORT = qw(
19 set_mp3tag get_mp3tag get_mp3info remove_mp3tag
20 use_winamp_genres, use_mp3_utf8
22 @EXPORT_OK = qw(@mp3_genres %mp3_genres use_mp3_utf8);
23 %EXPORT_TAGS = (
24 genres => [qw(@mp3_genres %mp3_genres)],
25 utf8 => [qw(use_mp3_utf8)],
26 all => [@EXPORT, @EXPORT_OK]
29 # $Id$
30 ($REVISION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
31 $VERSION = '1.20';
33 =pod
35 =head1 NAME
37 MP3::Info - Manipulate / fetch info from MP3 audio files
39 =head1 SYNOPSIS
41 #!perl -w
42 use MP3::Info;
43 my $file = 'Pearls_Before_Swine.mp3';
44 set_mp3tag($file, 'Pearls Before Swine', q"77's",
45 'Sticks and Stones', '1990',
46 q"(c) 1990 77's LTD.", 'rock & roll');
48 my $tag = get_mp3tag($file) or die "No TAG info";
49 $tag->{GENRE} = 'rock';
50 set_mp3tag($file, $tag);
52 my $info = get_mp3info($file);
53 printf "$file length is %d:%d\n", $info->{MM}, $info->{SS};
55 =cut
58 my $c = -1;
59 # set all lower-case and regular-cased versions of genres as keys
60 # with index as value of each key
61 %mp3_genres = map {($_, ++$c, lc, $c)} @mp3_genres;
63 # do it again for winamp genres
64 $c = -1;
65 %winamp_genres = map {($_, ++$c, lc, $c)} @winamp_genres;
68 =pod
70 my $mp3 = new MP3::Info $file;
71 $mp3->title('Perls Before Swine');
72 printf "$file length is %s, title is %s\n",
73 $mp3->time, $mp3->title;
76 =head1 DESCRIPTION
78 =over 4
80 =item $mp3 = MP3::Info-E<gt>new(FILE)
82 OOP interface to the rest of the module. The same keys
83 available via get_mp3info and get_mp3tag are available
84 via the returned object (using upper case or lower case;
85 but note that all-caps "VERSION" will return the module
86 version, not the MP3 version).
88 Passing a value to one of the methods will set the value
89 for that tag in the MP3 file, if applicable.
91 =cut
93 sub new {
94 my($pack, $file) = @_;
96 my $info = get_mp3info($file) or return undef;
97 my $tags = get_mp3tag($file) || { map { ($_ => undef) } @v1_tag_names };
98 my %self = (
99 FILE => $file,
100 TRY_HARDER => 0
103 @self{@mp3_info_fields, @v1_tag_names, 'file'} = (
104 @{$info}{@mp3_info_fields},
105 @{$tags}{@v1_tag_names},
106 $file
109 return bless \%self, $pack;
112 sub can {
113 my $self = shift;
114 return $self->SUPER::can(@_) unless ref $self;
115 my $name = uc shift;
116 return sub { $self->$name(@_) } if exists $self->{$name};
117 return undef;
120 sub AUTOLOAD {
121 my($self) = @_;
122 (my $name = uc $AUTOLOAD) =~ s/^.*://;
124 if (exists $self->{$name}) {
125 my $sub = exists $v1_tag_fields{$name}
126 ? sub {
127 if (defined $_[1]) {
128 $_[0]->{$name} = $_[1];
129 set_mp3tag($_[0]->{FILE}, $_[0]);
131 return $_[0]->{$name};
133 : sub {
134 return $_[0]->{$name}
137 no strict 'refs';
138 *{$AUTOLOAD} = $sub;
139 goto &$AUTOLOAD;
141 } else {
142 carp(sprintf "No method '$name' available in package %s.",
143 __PACKAGE__);
147 sub DESTROY {
152 =item use_mp3_utf8([STATUS])
154 Tells MP3::Info to (or not) return TAG info in UTF-8.
155 TRUE is 1, FALSE is 0. Default is TRUE, if available.
157 Will only be able to turn it on if Encode is available. ID3v2
158 tags will be converted to UTF-8 according to the encoding specified
159 in each tag; ID3v1 tags will be assumed Latin-1 and converted
160 to UTF-8.
162 Function returns status (TRUE/FALSE). If no argument is supplied,
163 or an unaccepted argument is supplied, function merely returns status.
165 This function is not exported by default, but may be exported
166 with the C<:utf8> or C<:all> export tag.
168 =cut
170 my $unicode_module = eval { require Encode; require Encode::Guess };
171 my $UNICODE = use_mp3_utf8($unicode_module ? 1 : 0);
173 sub use_mp3_utf8 {
174 my($val) = @_;
175 if ($val == 1) {
176 if ($unicode_module) {
177 $UNICODE = 1;
178 $Encode::Guess::NoUTFAutoGuess = 1;
180 } elsif ($val == 0) {
181 $UNICODE = 0;
183 return $UNICODE;
186 =pod
188 =item use_winamp_genres()
190 Puts WinAmp genres into C<@mp3_genres> and C<%mp3_genres>
191 (adds 68 additional genres to the default list of 80).
192 This is a separate function because these are non-standard
193 genres, but they are included because they are widely used.
195 You can import the data structures with one of:
197 use MP3::Info qw(:genres);
198 use MP3::Info qw(:DEFAULT :genres);
199 use MP3::Info qw(:all);
201 =cut
203 sub use_winamp_genres {
204 %mp3_genres = %winamp_genres;
205 @mp3_genres = @winamp_genres;
206 return 1;
209 =pod
211 =item remove_mp3tag (FILE [, VERSION, BUFFER])
213 Can remove ID3v1 or ID3v2 tags. VERSION should be C<1> for ID3v1
214 (the default), C<2> for ID3v2, and C<ALL> for both.
216 For ID3v1, removes last 128 bytes from file if those last 128 bytes begin
217 with the text 'TAG'. File will be 128 bytes shorter.
219 For ID3v2, removes ID3v2 tag. Because an ID3v2 tag is at the
220 beginning of the file, we rewrite the file after removing the tag data.
221 The buffer for rewriting the file is 4MB. BUFFER (in bytes) ca
222 change the buffer size.
224 Returns the number of bytes removed, or -1 if no tag removed,
225 or undef if there is an error.
227 =cut
229 sub remove_mp3tag {
230 my($file, $version, $buf) = @_;
231 my($fh, $return);
233 $buf ||= 4096*1024; # the bigger the faster
234 $version ||= 1;
236 if (not (defined $file && $file ne '')) {
237 $@ = "No file specified";
238 return undef;
241 if (not -s $file) {
242 $@ = "File is empty";
243 return undef;
246 if (ref $file) { # filehandle passed
247 $fh = $file;
248 } else {
249 if (not open $fh, '+<', $file) {
250 $@ = "Can't open $file: $!";
251 return undef;
255 binmode $fh;
257 if ($version eq 1 || $version eq 'ALL') {
258 seek $fh, -128, 2;
259 my $tell = tell $fh;
260 if (<$fh> =~ /^TAG/) {
261 truncate $fh, $tell or carp "Can't truncate '$file': $!";
262 $return += 128;
266 if ($version eq 2 || $version eq 'ALL') {
267 my $v2h = _get_v2head($fh);
268 if ($v2h) {
269 local $\;
270 seek $fh, 0, 2;
271 my $eof = tell $fh;
272 my $off = $v2h->{tag_size};
274 while ($off < $eof) {
275 seek $fh, $off, 0;
276 read $fh, my($bytes), $buf;
277 seek $fh, $off - $v2h->{tag_size}, 0;
278 print $fh $bytes;
279 $off += $buf;
282 truncate $fh, $eof - $v2h->{tag_size}
283 or carp "Can't truncate '$file': $!";
284 $return += $v2h->{tag_size};
288 _close($file, $fh);
290 return $return || -1;
294 =pod
296 =item set_mp3tag (FILE, TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE [, TRACKNUM])
298 =item set_mp3tag (FILE, $HASHREF)
300 Adds/changes tag information in an MP3 audio file. Will clobber
301 any existing information in file.
303 Fields are TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE. All fields have
304 a 30-byte limit, except for YEAR, which has a four-byte limit, and GENRE,
305 which is one byte in the file. The GENRE passed in the function is a
306 case-insensitive text string representing a genre found in C<@mp3_genres>.
308 Will accept either a list of values, or a hashref of the type
309 returned by C<get_mp3tag>.
311 If TRACKNUM is present (for ID3v1.1), then the COMMENT field can only be
312 28 bytes.
314 ID3v2 support may come eventually. Note that if you set a tag on a file
315 with ID3v2, the set tag will be for ID3v1[.1] only, and if you call
316 C<get_mp3tag> on the file, it will show you the (unchanged) ID3v2 tags,
317 unless you specify ID3v1.
319 =cut
321 sub set_mp3tag {
322 my($file, $title, $artist, $album, $year, $comment, $genre, $tracknum) = @_;
323 my(%info, $oldfh, $ref, $fh);
324 local %v1_tag_fields = %v1_tag_fields;
326 # set each to '' if undef
327 for ($title, $artist, $album, $year, $comment, $tracknum, $genre,
328 (@info{@v1_tag_names}))
329 {$_ = defined() ? $_ : ''}
331 ($ref) = (overload::StrVal($title) =~ /^(?:.*\=)?([^=]*)\((?:[^\(]*)\)$/)
332 if ref $title;
333 # populate data to hashref if hashref is not passed
334 if (!$ref) {
335 (@info{@v1_tag_names}) =
336 ($title, $artist, $album, $year, $comment, $tracknum, $genre);
338 # put data from hashref into hashref if hashref is passed
339 } elsif ($ref eq 'HASH') {
340 %info = %$title;
342 # return otherwise
343 } else {
344 carp(<<'EOT');
345 Usage: set_mp3tag (FILE, TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE [, TRACKNUM])
346 set_mp3tag (FILE, $HASHREF)
348 return undef;
351 if (not (defined $file && $file ne '')) {
352 $@ = "No file specified";
353 return undef;
356 if (not -s $file) {
357 $@ = "File is empty";
358 return undef;
361 # comment field length 28 if ID3v1.1
362 $v1_tag_fields{COMMENT} = 28 if $info{TRACKNUM};
365 # only if -w is on
366 if ($^W) {
367 # warn if fields too long
368 foreach my $field (keys %v1_tag_fields) {
369 $info{$field} = '' unless defined $info{$field};
370 if (length($info{$field}) > $v1_tag_fields{$field}) {
371 carp "Data too long for field $field: truncated to " .
372 "$v1_tag_fields{$field}";
376 if ($info{GENRE}) {
377 carp "Genre `$info{GENRE}' does not exist\n"
378 unless exists $mp3_genres{$info{GENRE}};
382 if ($info{TRACKNUM}) {
383 $info{TRACKNUM} =~ s/^(\d+)\/(\d+)$/$1/;
384 unless ($info{TRACKNUM} =~ /^\d+$/ &&
385 $info{TRACKNUM} > 0 && $info{TRACKNUM} < 256) {
386 carp "Tracknum `$info{TRACKNUM}' must be an integer " .
387 "from 1 and 255\n" if $^W;
388 $info{TRACKNUM} = '';
392 if (ref $file) { # filehandle passed
393 $fh = $file;
394 } else {
395 if (not open $fh, '+<', $file) {
396 $@ = "Can't open $file: $!";
397 return undef;
401 binmode $fh;
402 $oldfh = select $fh;
403 seek $fh, -128, 2;
404 # go to end of file if no tag, beginning of file if tag
405 seek $fh, (<$fh> =~ /^TAG/ ? -128 : 0), 2;
407 # get genre value
408 $info{GENRE} = $info{GENRE} && exists $mp3_genres{$info{GENRE}} ?
409 $mp3_genres{$info{GENRE}} : 255; # some default genre
411 local $\;
412 # print TAG to file
413 if ($info{TRACKNUM}) {
414 print pack 'a3a30a30a30a4a28xCC', 'TAG', @info{@v1_tag_names};
415 } else {
416 print pack 'a3a30a30a30a4a30C', 'TAG', @info{@v1_tag_names[0..4, 6]};
419 select $oldfh;
421 _close($file, $fh);
423 return 1;
426 =pod
428 =item get_mp3tag (FILE [, VERSION, RAW_V2])
430 Returns hash reference containing tag information in MP3 file. The keys
431 returned are the same as those supplied for C<set_mp3tag>, except in the
432 case of RAW_V2 being set.
434 If VERSION is C<1>, the information is taken from the ID3v1 tag (if present).
435 If VERSION is C<2>, the information is taken from the ID3v2 tag (if present).
436 If VERSION is not supplied, or is false, the ID3v1 tag is read if present, and
437 then, if present, the ID3v2 tag information will override any existing ID3v1
438 tag info.
440 If RAW_V2 is C<1>, the raw ID3v2 tag data is returned, without any manipulation
441 of text encoding. The key name is the same as the frame ID (ID to name mappings
442 are in the global %v2_tag_names).
444 If RAW_V2 is C<2>, the ID3v2 tag data is returned, manipulating for Unicode if
445 necessary, etc. It also takes multiple values for a given key (such as comments)
446 and puts them in an arrayref.
448 If the ID3v2 version is older than ID3v2.2.0 or newer than ID3v2.4.0, it will
449 not be read.
451 Strings returned will be in Latin-1, unless UTF-8 is specified (L<use_mp3_utf8>),
452 (unless RAW_V2 is C<1>).
454 Also returns a TAGVERSION key, containing the ID3 version used for the returned
455 data (if TAGVERSION argument is C<0>, may contain two versions).
457 =cut
459 sub get_mp3tag {
460 my ($file, $ver, $raw_v2, $find_ape) = @_;
461 my ($tag, $v2h, $fh);
463 my $v1 = {};
464 my $v2 = {};
465 my $ape = {};
466 my %info = ();
467 my @array = ();
469 $raw_v2 ||= 0;
470 $ver = !$ver ? 0 : ($ver == 2 || $ver == 1) ? $ver : 0;
472 if (not (defined $file && $file ne '')) {
473 $@ = "No file specified";
474 return undef;
477 my $filesize = -s $file;
479 if (!$filesize) {
480 $@ = "File is empty";
481 return undef;
484 if (ref $file) { # filehandle passed
485 $fh = $file;
486 } else {
487 if (not open $fh, '<', $file) {
488 $@ = "Can't open $file: $!";
489 return undef;
493 binmode $fh;
495 # Try and find an APE Tag - this is where FooBar2k & others
496 # store ReplayGain information
497 if ($find_ape) {
499 $ape = _parse_ape_tag($fh, $filesize, \%info);
502 if ($ver < 2) {
504 $v1 = _get_v1tag($fh, \%info);
506 if ($ver == 1 && !$v1) {
507 _close($file, $fh);
508 $@ = "No ID3v1 tag found";
509 return undef;
513 if ($ver == 2 || $ver == 0) {
514 ($v2, $v2h) = _get_v2tag($fh);
517 if (!$v1 && !$v2 && !$ape) {
518 _close($file, $fh);
519 $@ = "No ID3 tag found";
520 return undef;
523 if (($ver == 0 || $ver == 2) && $v2) {
525 if ($raw_v2 == 1 && $ver == 2) {
527 %info = %$v2;
529 $info{'TAGVERSION'} = $v2h->{'version'};
531 } else {
533 _parse_v2tag($raw_v2, $v2, \%info);
535 if ($ver == 0 && $info{'TAGVERSION'}) {
536 $info{'TAGVERSION'} .= ' / ' . $v2h->{'version'};
537 } else {
538 $info{'TAGVERSION'} = $v2h->{'version'};
543 unless ($raw_v2 && $ver == 2) {
544 foreach my $key (keys %info) {
545 if (defined $info{$key}) {
546 $info{$key} =~ s/\000+.*//g;
547 $info{$key} =~ s/\s+$//;
551 for (@v1_tag_names) {
552 $info{$_} = '' unless defined $info{$_};
556 if (keys %info && exists $info{'GENRE'} && ! defined $info{'GENRE'}) {
557 $info{'GENRE'} = '';
560 _close($file, $fh);
562 return keys %info ? {%info} : undef;
565 sub _get_v1tag {
566 my ($fh, $info) = @_;
568 seek $fh, -128, 2;
569 read($fh, my $tag, 128);
571 if (!defined($tag) || $tag !~ /^TAG/) {
573 return 0;
576 if (substr($tag, -3, 2) =~ /\000[^\000]/) {
578 (undef, @{$info}{@v1_tag_names}) =
579 (unpack('a3a30a30a30a4a28', $tag),
580 ord(substr($tag, -2, 1)),
581 $mp3_genres[ord(substr $tag, -1)]);
583 $info->{'TAGVERSION'} = 'ID3v1.1';
585 } else {
587 (undef, @{$info}{@v1_tag_names[0..4, 6]}) =
588 (unpack('a3a30a30a30a4a30', $tag),
589 $mp3_genres[ord(substr $tag, -1)]);
591 $info->{'TAGVERSION'} = 'ID3v1';
594 if ($UNICODE) {
596 # Save off the old suspects list, since we add
597 # iso-8859-1 below, but don't want that there
598 # for possible ID3 v2.x parsing below.
599 my $oldSuspects = $Encode::Encoding{'Guess'}->{'Suspects'};
601 for my $key (keys %{$info}) {
603 next unless $info->{$key};
605 # Try and guess the encoding.
606 my $value = $info->{$key};
607 my $icode = Encode::Guess->guess($value);
609 unless (ref($icode)) {
611 # Often Latin1 bytes are
612 # stuffed into a 1.1 tag.
613 Encode::Guess->add_suspects('iso-8859-1');
615 while (length($value)) {
617 $icode = Encode::Guess->guess($value);
619 last if ref($icode);
621 # Remove garbage and retry
622 # (string is truncated in the
623 # middle of a multibyte char?)
624 $value =~ s/(.)$//;
628 $info->{$key} = Encode::decode(ref($icode) ? $icode->name : 'iso-8859-1', $info->{$key});
631 Encode::Guess->set_suspects(keys %{$oldSuspects});
634 return 1;
637 sub _parse_v2tag {
638 my ($raw_v2, $v2, $info) = @_;
640 # Make sure any existing TXXX flags are an array.
641 # As we might need to append comments to it below.
642 if ($v2->{'TXXX'} && ref($v2->{'TXXX'}) ne 'ARRAY') {
644 $v2->{'TXXX'} = [ $v2->{'TXXX'} ];
647 # J.River Media Center sticks RG tags in comments.
648 # Ugh. Make them look like TXXX tags, which is really what they are.
649 if (ref($v2->{'COMM'}) eq 'ARRAY' && grep { /Media Jukebox/ } @{$v2->{'COMM'}}) {
651 for my $comment (@{$v2->{'COMM'}}) {
653 if ($comment =~ /Media Jukebox/) {
655 # we only want one null to lead.
656 $comment =~ s/^\000+//g;
658 push @{$v2->{'TXXX'}}, "\000$comment";
663 my $hash = $raw_v2 == 2 ? { map { ($_, $_) } keys %v2_tag_names } : \%v2_to_v1_names;
665 for my $id (keys %$hash) {
667 next if !exists $v2->{$id};
669 if ($id =~ /^UFID?$/) {
671 my @ufid_list = split(/\0/, $v2->{$id});
673 $info->{$hash->{$id}} = $ufid_list[1] if ($#ufid_list > 0);
675 } elsif ($id =~ /^RVA[D2]?$/) {
677 # Expand these binary fields. See the ID3 spec for Relative Volume Adjustment.
678 if ($id eq 'RVA2') {
680 # ID is a text string
681 ($info->{$hash->{$id}}->{'ID'}, my $rvad) = split /\0/, $v2->{$id};
683 my $channel = $rva2_channel_types{ ord(substr($rvad, 0, 1, '')) };
685 $info->{$hash->{$id}}->{$channel}->{'REPLAYGAIN_TRACK_GAIN'} =
686 sprintf('%f', _grab_int_16(\$rvad) / 512);
688 my $peakBytes = ord(substr($rvad, 0, 1, ''));
690 if (int($peakBytes / 8)) {
692 $info->{$hash->{$id}}->{$channel}->{'REPLAYGAIN_TRACK_PEAK'} =
693 sprintf('%f', _grab_int_16(\$rvad) / 512);
696 } elsif ($id eq 'RVAD' || $id eq 'RVA') {
698 my $rvad = $v2->{$id};
699 my $flags = ord(substr($rvad, 0, 1, ''));
700 my $desc = ord(substr($rvad, 0, 1, ''));
702 # iTunes appears to be the only program that actually writes
703 # out a RVA/RVAD tag. Everyone else punts.
704 for my $type (qw(REPLAYGAIN_TRACK_GAIN REPLAYGAIN_TRACK_PEAK)) {
706 for my $channel (qw(RIGHT LEFT)) {
708 my $val = _grab_uint_16(\$rvad) / 256;
710 # iTunes uses a range of -255 to 255
711 # to be -100% (silent) to 100% (+6dB)
712 if ($val == -255) {
713 $val = -96.0;
714 } else {
715 $val = 20.0 * log(($val+255)/255)/log(10);
718 $info->{$hash->{$id}}->{$channel}->{$type} = $flags & 0x01 ? $val : -$val;
723 } elsif ($id =~ /^A?PIC$/) {
725 my $pic = $v2->{$id};
727 # if there is more than one picture, just grab the first one.
728 if (ref($pic) eq 'ARRAY') {
729 $pic = (@$pic)[0];
732 use bytes;
734 my $valid_pic = 0;
735 my $pic_len = 0;
736 my $pic_format = '';
738 # look for ID3 v2.2 picture
739 if ($pic && $id eq 'PIC') {
741 # look for ID3 v2.2 picture
742 my ($encoding, $format, $picture_type, $description) = unpack 'Ca3CZ*', $pic;
743 $pic_len = length($description) + 1 + 5;
745 # skip extra terminating null if unicode
746 if ($encoding) { $pic_len++; }
748 if ($pic_len < length($pic)) {
749 $valid_pic = 1;
750 $pic_format = $format;
753 } elsif ($pic && $id eq 'APIC') {
755 # look for ID3 v2.3 picture
756 my ($encoding, $format) = unpack 'C Z*', $pic;
758 $pic_len = length($format) + 2;
760 if ($pic_len < length($pic)) {
762 my ($picture_type, $description) = unpack "x$pic_len C Z*", $pic;
764 $pic_len += 1 + length($description) + 1;
766 # skip extra terminating null if unicode
767 if ($encoding) { $pic_len++; }
769 $valid_pic = 1;
770 $pic_format = $format;
774 # Proceed if we have a valid picture.
775 if ($valid_pic && $pic_format) {
777 my ($data) = unpack("x$pic_len A*", $pic);
779 if (length($data) && $pic_format) {
781 $info->{$hash->{$id}} = {
782 'DATA' => $data,
783 'FORMAT' => $pic_format,
788 } else {
789 my $data1 = $v2->{$id};
791 # this is tricky ... if this is an arrayref,
792 # we want to only return one, so we pick the
793 # first one. but if it is a comment, we pick
794 # the first one where the first charcter after
795 # the language is NULL and not an additional
796 # sub-comment, because that is most likely to be
797 # the user-supplied comment
798 if (ref $data1 && !$raw_v2) {
799 if ($id =~ /^COMM?$/) {
800 my($newdata) = grep /^(....\000)/, @{$data1};
801 $data1 = $newdata || $data1->[0];
802 } elsif ($id !~ /^(?:TXXX?|PRIV)$/) {
803 # We can get multiple User Defined Text frames in a mp3 file
804 $data1 = $data1->[0];
808 $data1 = [ $data1 ] if ! ref $data1;
810 for my $data (@$data1) {
811 # TODO : this should only be done for certain frames;
812 # using RAW still gives you access, but we should be smarter
813 # about how individual frame types are handled. it's not
814 # like the list is infinitely long.
815 $data =~ s/^(.)//; # strip first char (text encoding)
816 my $encoding = $1;
817 my $desc;
819 # Comments & Unsyncronized Lyrics have the same format.
820 if ($id =~ /^(COM[M ]?|USLT)$/) { # space for iTunes brokenness
822 $data =~ s/^(?:...)//; # strip language
825 if ($UNICODE) {
827 if ($encoding eq "\001" || $encoding eq "\002") { # UTF-16, UTF-16BE
828 # text fields can be null-separated lists;
829 # UTF-16 therefore needs special care
831 # foobar2000 encodes tags in UTF-16LE
832 # (which is apparently illegal)
833 # Encode dies on a bad BOM, so it is
834 # probably wise to wrap it in an eval
835 # anyway
836 $data = eval { Encode::decode('utf16', $data) } || Encode::decode('utf16le', $data);
838 } elsif ($encoding eq "\003") { # UTF-8
840 # make sure string is UTF8, and set flag appropriately
841 $data = Encode::decode('utf8', $data);
843 } elsif ($encoding eq "\000") {
845 # Only guess if it's not ascii.
846 if ($data && $data !~ /^[\x00-\x7F]+$/) {
848 # Try and guess the encoding, otherwise just use latin1
849 my $dec = Encode::Guess->guess($data);
851 if (ref $dec) {
852 $data = $dec->decode($data);
853 } else {
854 # Best try
855 $data = Encode::decode('iso-8859-1', $data);
860 } else {
862 # If the string starts with an
863 # UTF-16 little endian BOM, use a hack to
864 # convert to ASCII per best-effort
865 my $pat;
866 if ($data =~ s/^\xFF\xFE//) {
867 $pat = 'v';
868 } elsif ($data =~ s/^\xFE\xFF//) {
869 $pat = 'n';
872 if ($pat) {
873 $data = pack 'C*', map {
874 (chr =~ /[[:ascii:]]/ && chr =~ /[[:print:]]/)
875 ? $_
876 : ord('?')
877 } unpack "$pat*", $data;
881 # We do this after decoding so we could be certain we're dealing
882 # with 8-bit text.
883 if ($id =~ /^(COM[M ]?|USLT)$/) { # space for iTunes brokenness
885 $data =~ s/^(.*?)\000//; # strip up to first NULL(s),
886 # for sub-comments (TODO:
887 # handle all comment data)
888 $desc = $1;
890 } elsif ($id =~ /^TCON?$/) {
892 my ($index, $name);
894 # Turn multiple nulls into a single.
895 $data =~ s/\000+/\000/g;
897 # Handle the ID3v2.x spec -
899 # just an index number, possibly
900 # paren enclosed - referer to the v1 genres.
901 if ($data =~ /^ \(? (\d+) \)?\000?$/sx) {
903 $index = $1;
905 # Paren enclosed index with refinement.
906 # (4)Eurodisco
907 } elsif ($data =~ /^ \( (\d+) \)\000? ([^\(].+)$/x) {
909 ($index, $name) = ($1, $2);
911 # List of indexes: (37)(38)
912 } elsif ($data =~ /^ \( (\d+) \)\000?/x) {
914 my @genres = ();
916 while ($data =~ s/^ \( (\d+) \)\000?//x) {
918 push @genres, $mp3_genres[$1];
921 $data = \@genres;
924 # Text based genres will fall through.
925 if ($name && $name ne "\000") {
926 $data = $name;
927 } elsif (defined $index) {
928 $data = $mp3_genres[$index];
932 if ($raw_v2 == 2 && $desc) {
933 $data = { $desc => $data };
936 if ($raw_v2 == 2 && exists $info->{$hash->{$id}}) {
938 if (ref $info->{$hash->{$id}} eq 'ARRAY') {
939 push @{$info->{$hash->{$id}}}, $data;
940 } else {
941 $info->{$hash->{$id}} = [ $info->{$hash->{$id}}, $data ];
944 } else {
946 # User defined frame
947 if ($id eq 'TXXX') {
949 my ($key, $val) = split(/\0/, $data);
950 $info->{uc($key)} = $val;
952 } elsif ($id eq 'PRIV') {
954 my ($key, $val) = split(/\0/, $data);
955 $info->{uc($key)} = unpack('v', $val);
957 } else {
959 $info->{$hash->{$id}} = $data;
967 sub _get_v2tag {
968 my($fh) = @_;
969 my($off, $end, $myseek, $v2, $v2h, $hlen, $num, $wholetag);
971 $v2 = {};
972 $v2h = _get_v2head($fh) or return;
974 if ($v2h->{major_version} < 2) {
975 carp "This is $v2h->{version}; " .
976 "ID3v2 versions older than ID3v2.2.0 not supported\n"
977 if $^W;
978 return;
981 # use syncsafe bytes if using version 2.4
982 # my $bytesize = ($v2h->{major_version} > 3) ? 128 : 256;
984 # alas, that's what the spec says, but iTunes and others don't syncsafe
985 # the length, which breaks MP3 files with v2.4 tags longer than 128 bytes,
986 # like every image file.
987 my $bytesize = 256;
989 if ($v2h->{major_version} == 2) {
990 $hlen = 6;
991 $num = 3;
992 } else {
993 $hlen = 10;
994 $num = 4;
997 $off = $v2h->{ext_header_size} + 10;
998 $end = $v2h->{tag_size} + 10; # should we read in the footer too?
1000 seek $fh, $v2h->{offset}, 0;
1001 read $fh, $wholetag, $end;
1003 $wholetag =~ s/\xFF\x00/\xFF/gs if $v2h->{unsync};
1005 $myseek = sub {
1006 my $bytes = substr($wholetag, $off, $hlen);
1007 return unless $bytes =~ /^([A-Z0-9]{$num})/
1008 || ($num == 4 && $bytes =~ /^(COM )/); # stupid iTunes
1009 my($id, $size) = ($1, $hlen);
1010 my @bytes = reverse unpack "C$num", substr($bytes, $num, $num);
1012 for my $i (0 .. ($num - 1)) {
1013 $size += $bytes[$i] * $bytesize ** $i;
1016 my $flags = {};
1017 if ($v2h->{major_version} > 3) {
1018 my @bits = split //, unpack 'B16', substr($bytes, 8, 2);
1019 $flags->{frame_unsync} = $bits[14];
1020 $flags->{data_len_indicator} = $bits[15];
1023 return($id, $size, $flags);
1026 while ($off < $end) {
1027 my($id, $size, $flags) = &$myseek or last;
1029 my $bytes = substr($wholetag, $off+$hlen, $size-$hlen);
1031 my $data_len;
1032 if ($flags->{data_len_indicator}) {
1033 $data_len = 0;
1034 my @data_len_bytes = reverse unpack 'C4', substr($bytes, 0, 4);
1035 $bytes = substr($bytes, 4);
1036 for my $i (0..3) {
1037 $data_len += $data_len_bytes[$i] * 128 ** $i;
1041 # perform frame-level unsync if needed (skip if already done for whole tag)
1042 $bytes =~ s/\xFF\x00/\xFF/gs if $flags->{frame_unsync} && !$v2h->{unsync};
1044 # if we know the data length, sanity check it now.
1045 if ($flags->{data_len_indicator} && defined $data_len) {
1046 carp "Size mismatch on $id\n" unless $data_len == length($bytes);
1049 if (exists $v2->{$id}) {
1050 if (ref $v2->{$id} eq 'ARRAY') {
1051 push @{$v2->{$id}}, $bytes;
1052 } else {
1053 $v2->{$id} = [$v2->{$id}, $bytes];
1055 } else {
1056 $v2->{$id} = $bytes;
1058 $off += $size;
1061 return($v2, $v2h);
1065 =pod
1067 =item get_mp3info (FILE)
1069 Returns hash reference containing file information for MP3 file.
1070 This data cannot be changed. Returned data:
1072 VERSION MPEG audio version (1, 2, 2.5)
1073 LAYER MPEG layer description (1, 2, 3)
1074 STEREO boolean for audio is in stereo
1076 VBR boolean for variable bitrate
1077 BITRATE bitrate in kbps (average for VBR files)
1078 FREQUENCY frequency in kHz
1079 SIZE bytes in audio stream
1080 OFFSET bytes offset that stream begins
1082 SECS total seconds
1083 MM minutes
1084 SS leftover seconds
1085 MS leftover milliseconds
1086 TIME time in MM:SS
1088 COPYRIGHT boolean for audio is copyrighted
1089 PADDING boolean for MP3 frames are padded
1090 MODE channel mode (0 = stereo, 1 = joint stereo,
1091 2 = dual channel, 3 = single channel)
1092 FRAMES approximate number of frames
1093 FRAME_LENGTH approximate length of a frame
1094 VBR_SCALE VBR scale from VBR header
1096 On error, returns nothing and sets C<$@>.
1098 =cut
1100 sub get_mp3info {
1101 my($file) = @_;
1102 my($off, $byte, $eof, $h, $tot, $fh);
1104 if (not (defined $file && $file ne '')) {
1105 $@ = "No file specified";
1106 return undef;
1109 if (not -s $file) {
1110 $@ = "File is empty";
1111 return undef;
1114 if (ref $file) { # filehandle passed
1115 $fh = $file;
1116 } else {
1117 if (not open $fh, '<', $file) {
1118 $@ = "Can't open $file: $!";
1119 return undef;
1123 $off = 0;
1124 $tot = 8192;
1126 # Let the caller change how far we seek in looking for a header.
1127 if ($try_harder) {
1128 $tot *= $try_harder;
1131 binmode $fh;
1132 seek $fh, $off, 0;
1133 read $fh, $byte, 4;
1135 if ($off == 0) {
1136 if (my $v2h = _get_v2head($fh)) {
1137 $tot += $off += $v2h->{tag_size};
1138 seek $fh, $off, 0;
1139 read $fh, $byte, 4;
1143 $h = _get_head($byte);
1144 my $is_mp3 = _is_mp3($h);
1146 # the head wasn't where we were expecting it.. dig deeper.
1147 unless ($is_mp3) {
1149 # do only one read - it's _much_ faster
1150 $off++;
1151 seek $fh, $off, 0;
1152 read $fh, $byte, $tot;
1154 my $i;
1156 # now walk the bytes looking for the head
1157 for ($i = 0; $i < $tot; $i++) {
1159 last if ($tot - $i) < 4;
1161 my $head = substr($byte, $i, 4) || last;
1163 next if (ord($head) != 0xff);
1165 $h = _get_head($head);
1166 $is_mp3 = _is_mp3($h);
1167 last if $is_mp3;
1170 # adjust where we are for _get_vbr()
1171 $off += $i;
1173 if ($off > $tot && !$try_harder) {
1174 _close($file, $fh);
1175 $@ = "Couldn't find MP3 header (perhaps set " .
1176 '$MP3::Info::try_harder and retry)';
1177 return undef;
1181 my $vbr = _get_vbr($fh, $h, \$off);
1183 seek $fh, 0, 2;
1184 $eof = tell $fh;
1185 seek $fh, -128, 2;
1186 $eof -= 128 if <$fh> =~ /^TAG/ ? 1 : 0;
1188 _close($file, $fh);
1190 $h->{size} = $eof - $off;
1191 $h->{offset} = $off;
1193 return _get_info($h, $vbr);
1196 sub _get_info {
1197 my($h, $vbr) = @_;
1198 my $i;
1200 # No bitrate or sample rate? Something's wrong.
1201 unless ($h->{bitrate} && $h->{fs}) {
1202 return {};
1205 $i->{VERSION} = $h->{IDR} == 2 ? 2 : $h->{IDR} == 3 ? 1 :
1206 $h->{IDR} == 0 ? 2.5 : 0;
1207 $i->{LAYER} = 4 - $h->{layer};
1208 $i->{VBR} = defined $vbr ? 1 : 0;
1210 $i->{COPYRIGHT} = $h->{copyright} ? 1 : 0;
1211 $i->{PADDING} = $h->{padding_bit} ? 1 : 0;
1212 $i->{STEREO} = $h->{mode} == 3 ? 0 : 1;
1213 $i->{MODE} = $h->{mode};
1215 $i->{SIZE} = $vbr && $vbr->{bytes} ? $vbr->{bytes} : $h->{size};
1216 $i->{OFFSET} = $h->{offset};
1218 my $mfs = $h->{fs} / ($h->{ID} ? 144000 : 72000);
1219 $i->{FRAMES} = int($vbr && $vbr->{frames}
1220 ? $vbr->{frames}
1221 : $i->{SIZE} / ($h->{bitrate} / $mfs)
1224 if ($vbr) {
1225 $i->{VBR_SCALE} = $vbr->{scale} if $vbr->{scale};
1226 $h->{bitrate} = $i->{SIZE} / $i->{FRAMES} * $mfs;
1227 if (not $h->{bitrate}) {
1228 $@ = "Couldn't determine VBR bitrate";
1229 return undef;
1233 $h->{'length'} = ($i->{SIZE} * 8) / $h->{bitrate} / 10;
1234 $i->{SECS} = $h->{'length'} / 100;
1235 $i->{MM} = int $i->{SECS} / 60;
1236 $i->{SS} = int $i->{SECS} % 60;
1237 $i->{MS} = (($i->{SECS} - ($i->{MM} * 60) - $i->{SS}) * 1000);
1238 # $i->{LF} = ($i->{MS} / 1000) * ($i->{FRAMES} / $i->{SECS});
1239 # int($i->{MS} / 100 * 75); # is this right?
1240 $i->{TIME} = sprintf "%.2d:%.2d", @{$i}{'MM', 'SS'};
1242 $i->{BITRATE} = int $h->{bitrate};
1243 # should we just return if ! FRAMES?
1244 $i->{FRAME_LENGTH} = int($h->{size} / $i->{FRAMES}) if $i->{FRAMES};
1245 $i->{FREQUENCY} = $frequency_tbl[3 * $h->{IDR} + $h->{sampling_freq}];
1247 return $i;
1250 sub _get_head {
1251 my($byte) = @_;
1252 my($bytes, $h);
1254 $bytes = _unpack_head($byte);
1255 @$h{qw(IDR ID layer protection_bit
1256 bitrate_index sampling_freq padding_bit private_bit
1257 mode mode_extension copyright original
1258 emphasis version_index bytes)} = (
1259 ($bytes>>19)&3, ($bytes>>19)&1, ($bytes>>17)&3, ($bytes>>16)&1,
1260 ($bytes>>12)&15, ($bytes>>10)&3, ($bytes>>9)&1, ($bytes>>8)&1,
1261 ($bytes>>6)&3, ($bytes>>4)&3, ($bytes>>3)&1, ($bytes>>2)&1,
1262 $bytes&3, ($bytes>>19)&3, $bytes
1265 $h->{bitrate} = $t_bitrate[$h->{ID}][3 - $h->{layer}][$h->{bitrate_index}];
1266 $h->{fs} = $t_sampling_freq[$h->{IDR}][$h->{sampling_freq}];
1268 return $h;
1271 sub _is_mp3 {
1272 my $h = $_[0] or return undef;
1273 return ! ( # all below must be false
1274 $h->{bitrate_index} == 0
1276 $h->{version_index} == 1
1278 ($h->{bytes} & 0xFFE00000) != 0xFFE00000
1280 !$h->{fs}
1282 !$h->{bitrate}
1284 $h->{bitrate_index} == 15
1286 !$h->{layer}
1288 $h->{sampling_freq} == 3
1290 $h->{emphasis} == 2
1292 !$h->{bitrate_index}
1294 ($h->{bytes} & 0xFFFF0000) == 0xFFFE0000
1296 ($h->{ID} == 1 && $h->{layer} == 3 && $h->{protection_bit} == 1)
1297 # mode extension should only be applicable when mode = 1
1298 # however, failing just becuase mode extension is used when unneeded is a bit strict
1299 # ||
1300 #($h->{mode_extension} != 0 && $h->{mode} != 1)
1304 sub _vbr_seek {
1305 my $fh = shift;
1306 my $off = shift;
1307 my $bytes = shift;
1308 my $n = shift || 4;
1310 seek $fh, $$off, 0;
1311 read $fh, $$bytes, $n;
1313 $$off += $n;
1316 sub _get_vbr {
1317 my($fh, $h, $roff) = @_;
1318 my($off, $bytes, @bytes, %vbr);
1320 $off = $$roff;
1322 $off += 4;
1324 if ($h->{ID}) { # MPEG1
1325 $off += $h->{mode} == 3 ? 17 : 32;
1326 } else { # MPEG2
1327 $off += $h->{mode} == 3 ? 9 : 17;
1330 _vbr_seek($fh, \$off, \$bytes);
1331 return unless $bytes eq 'Xing';
1333 _vbr_seek($fh, \$off, \$bytes);
1334 $vbr{flags} = _unpack_head($bytes);
1336 if ($vbr{flags} & 1) {
1337 _vbr_seek($fh, \$off, \$bytes);
1338 $vbr{frames} = _unpack_head($bytes);
1341 if ($vbr{flags} & 2) {
1342 _vbr_seek($fh, \$off, \$bytes);
1343 $vbr{bytes} = _unpack_head($bytes);
1346 if ($vbr{flags} & 4) {
1347 _vbr_seek($fh, \$off, \$bytes, 100);
1348 # Not used right now ...
1349 # $vbr{toc} = _unpack_head($bytes);
1352 if ($vbr{flags} & 8) { # (quality ind., 0=best 100=worst)
1353 _vbr_seek($fh, \$off, \$bytes);
1354 $vbr{scale} = _unpack_head($bytes);
1355 } else {
1356 $vbr{scale} = -1;
1359 $$roff = $off;
1360 return \%vbr;
1363 sub _get_v2head {
1364 my $fh = $_[0] or return;
1365 my($v2h, $bytes, @bytes);
1366 $v2h->{offset} = 0;
1368 # check first three bytes for 'ID3'
1369 seek $fh, 0, 0;
1370 read $fh, $bytes, 3;
1372 # TODO: add support for tags at the end of the file
1373 if ($bytes eq 'RIF' || $bytes eq 'FOR') {
1374 _find_id3_chunk($fh, $bytes) or return;
1375 $v2h->{offset} = tell $fh;
1376 read $fh, $bytes, 3;
1379 return unless $bytes eq 'ID3';
1381 # get version
1382 read $fh, $bytes, 2;
1383 $v2h->{version} = sprintf "ID3v2.%d.%d",
1384 @$v2h{qw[major_version minor_version]} =
1385 unpack 'c2', $bytes;
1387 # get flags
1388 read $fh, $bytes, 1;
1389 my @bits = split //, unpack 'b8', $bytes;
1390 if ($v2h->{major_version} == 2) {
1391 $v2h->{unsync} = $bits[7];
1392 $v2h->{compression} = $bits[8];
1393 $v2h->{ext_header} = 0;
1394 $v2h->{experimental} = 0;
1395 } else {
1396 $v2h->{unsync} = $bits[7];
1397 $v2h->{ext_header} = $bits[6];
1398 $v2h->{experimental} = $bits[5];
1399 $v2h->{footer} = $bits[4] if $v2h->{major_version} == 4;
1402 # get ID3v2 tag length from bytes 7-10
1403 $v2h->{tag_size} = 10; # include ID3v2 header size
1404 $v2h->{tag_size} += 10 if $v2h->{footer};
1405 read $fh, $bytes, 4;
1406 @bytes = reverse unpack 'C4', $bytes;
1407 foreach my $i (0 .. 3) {
1408 # whoaaaaaa nellllllyyyyyy!
1409 $v2h->{tag_size} += $bytes[$i] * 128 ** $i;
1412 # get extended header size
1413 $v2h->{ext_header_size} = 0;
1414 if ($v2h->{ext_header}) {
1415 read $fh, $bytes, 4;
1416 @bytes = reverse unpack 'C4', $bytes;
1418 # use syncsafe bytes if using version 2.4
1419 my $bytesize = ($v2h->{major_version} > 3) ? 128 : 256;
1420 for my $i (0..3) {
1421 $v2h->{ext_header_size} += $bytes[$i] * $bytesize ** $i;
1425 return $v2h;
1428 sub _find_id3_chunk {
1429 my($fh, $filetype) = @_;
1430 my($bytes, $size, $tag, $pat, $mat);
1432 read $fh, $bytes, 1;
1433 if ($filetype eq 'RIF') { # WAV
1434 return 0 if $bytes ne 'F';
1435 $pat = 'a4V';
1436 $mat = 'id3 ';
1437 } elsif ($filetype eq 'FOR') { # AIFF
1438 return 0 if $bytes ne 'M';
1439 $pat = 'a4N';
1440 $mat = 'ID3 ';
1442 seek $fh, 12, 0; # skip to the first chunk
1444 while ((read $fh, $bytes, 8) == 8) {
1445 ($tag, $size) = unpack $pat, $bytes;
1446 return 1 if $tag eq $mat;
1447 seek $fh, $size, 1;
1450 return 0;
1453 sub _unpack_head {
1454 unpack('l', pack('L', unpack('N', $_[0])));
1457 sub _grab_int_16 {
1458 my $data = shift;
1459 my $value = unpack('s',substr($$data,0,2));
1460 $$data = substr($$data,2);
1461 return $value;
1464 sub _grab_uint_16 {
1465 my $data = shift;
1466 my $value = unpack('S',substr($$data,0,2));
1467 $$data = substr($$data,2);
1468 return $value;
1471 sub _grab_int_32 {
1472 my $data = shift;
1473 my $value = unpack('V',substr($$data,0,4));
1474 $$data = substr($$data,4);
1475 return $value;
1478 sub _parse_ape_tag {
1479 my ($fh, $filesize, $info) = @_;
1481 my $ape_tag_id = 'APETAGEX';
1483 seek $fh, -256, 2;
1484 read($fh, my $tag, 256);
1485 my $pre_tag = substr($tag, 0, 128, '');
1487 # Try and bail early if there's no ape tag.
1488 if (substr($pre_tag, 96, 8) ne $ape_tag_id && substr($tag, 96, 8) ne $ape_tag_id) {
1490 seek($fh, 0, 0);
1491 return 0;
1494 my $id3v1_tag_size = 128;
1495 my $ape_tag_header_size = 32;
1496 my $lyrics3_tag_size = 10;
1497 my $tag_offset_start = 0;
1498 my $tag_offset_end = 0;
1500 seek($fh, (0 - $id3v1_tag_size - $ape_tag_header_size - $lyrics3_tag_size), 2);
1502 read($fh, my $ape_footer_id3v1, $id3v1_tag_size + $ape_tag_header_size + $lyrics3_tag_size);
1504 if (substr($ape_footer_id3v1, (length($ape_footer_id3v1) - $id3v1_tag_size - $ape_tag_header_size), 8) eq $ape_tag_id) {
1506 $tag_offset_end = $filesize - $id3v1_tag_size;
1508 } elsif (substr($ape_footer_id3v1, (length($ape_footer_id3v1) - $ape_tag_header_size), 8) eq $ape_tag_id) {
1510 $tag_offset_end = $filesize;
1513 seek($fh, $tag_offset_end - $ape_tag_header_size, 0);
1515 read($fh, my $ape_footer_data, 32);
1517 my $ape_footer = _parse_ape_header_or_footer($ape_footer_data);
1519 if (keys %{$ape_footer}) {
1521 my $ape_tag_data = '';
1523 if ($ape_footer->{'flags'}->{'header'}) {
1525 seek($fh, ($tag_offset_end - $ape_footer->{'tag_size'} - $ape_tag_header_size), 0);
1527 $tag_offset_start = tell($fh);
1529 read($fh, $ape_tag_data, $ape_footer->{'tag_size'} + $ape_tag_header_size);
1531 } else {
1533 $tag_offset_start = $tag_offset_end - $ape_footer->{'tag_size'};
1535 seek($fh, $tag_offset_start, 0);
1537 read($fh, $ape_tag_data, $ape_footer->{'tag_size'});
1540 my $ape_header_data = substr($ape_tag_data, 0, $ape_tag_header_size, '');
1541 my $ape_header = _parse_ape_header_or_footer($ape_header_data);
1543 for (my $c = 0; $c < $ape_header->{'tag_items'}; $c++) {
1545 # Loop through the tag items
1546 my $tag_len = _grab_int_32(\$ape_tag_data);
1547 my $tag_flags = _grab_int_32(\$ape_tag_data);
1549 $ape_tag_data =~ s/^(.*?)\0//;
1551 my $tag_item_key = uc($1 || 'UNKNOWN');
1553 $info->{$tag_item_key} = substr($ape_tag_data, 0, $tag_len, '');
1557 seek($fh, 0, 0);
1559 return 1;
1562 sub _parse_ape_header_or_footer {
1563 my $bytes = shift;
1564 my %data = ();
1566 if (substr($bytes, 0, 8, '') eq 'APETAGEX') {
1568 $data{'version'} = _grab_int_32(\$bytes);
1569 $data{'tag_size'} = _grab_int_32(\$bytes);
1570 $data{'tag_items'} = _grab_int_32(\$bytes);
1571 $data{'global_flags'} = _grab_int_32(\$bytes);
1573 # trim the reseved bytes
1574 _grab_int_32(\$bytes);
1575 _grab_int_32(\$bytes);
1577 $data{'flags'}->{'header'} = ($data{'global_flags'} & 0x80000000) ? 1 : 0;
1578 $data{'flags'}->{'footer'} = ($data{'global_flags'} & 0x40000000) ? 1 : 0;
1579 $data{'flags'}->{'is_header'} = ($data{'global_flags'} & 0x20000000) ? 1 : 0;
1582 return \%data;
1585 sub _close {
1586 my($file, $fh) = @_;
1587 unless (ref $file) { # filehandle not passed
1588 close $fh or carp "Problem closing '$file': $!";
1592 BEGIN {
1593 @mp3_genres = (
1594 'Blues',
1595 'Classic Rock',
1596 'Country',
1597 'Dance',
1598 'Disco',
1599 'Funk',
1600 'Grunge',
1601 'Hip-Hop',
1602 'Jazz',
1603 'Metal',
1604 'New Age',
1605 'Oldies',
1606 'Other',
1607 'Pop',
1608 'R&B',
1609 'Rap',
1610 'Reggae',
1611 'Rock',
1612 'Techno',
1613 'Industrial',
1614 'Alternative',
1615 'Ska',
1616 'Death Metal',
1617 'Pranks',
1618 'Soundtrack',
1619 'Euro-Techno',
1620 'Ambient',
1621 'Trip-Hop',
1622 'Vocal',
1623 'Jazz+Funk',
1624 'Fusion',
1625 'Trance',
1626 'Classical',
1627 'Instrumental',
1628 'Acid',
1629 'House',
1630 'Game',
1631 'Sound Clip',
1632 'Gospel',
1633 'Noise',
1634 'AlternRock',
1635 'Bass',
1636 'Soul',
1637 'Punk',
1638 'Space',
1639 'Meditative',
1640 'Instrumental Pop',
1641 'Instrumental Rock',
1642 'Ethnic',
1643 'Gothic',
1644 'Darkwave',
1645 'Techno-Industrial',
1646 'Electronic',
1647 'Pop-Folk',
1648 'Eurodance',
1649 'Dream',
1650 'Southern Rock',
1651 'Comedy',
1652 'Cult',
1653 'Gangsta',
1654 'Top 40',
1655 'Christian Rap',
1656 'Pop/Funk',
1657 'Jungle',
1658 'Native American',
1659 'Cabaret',
1660 'New Wave',
1661 'Psychadelic',
1662 'Rave',
1663 'Showtunes',
1664 'Trailer',
1665 'Lo-Fi',
1666 'Tribal',
1667 'Acid Punk',
1668 'Acid Jazz',
1669 'Polka',
1670 'Retro',
1671 'Musical',
1672 'Rock & Roll',
1673 'Hard Rock',
1676 @winamp_genres = (
1677 @mp3_genres,
1678 'Folk',
1679 'Folk-Rock',
1680 'National Folk',
1681 'Swing',
1682 'Fast Fusion',
1683 'Bebop',
1684 'Latin',
1685 'Revival',
1686 'Celtic',
1687 'Bluegrass',
1688 'Avantgarde',
1689 'Gothic Rock',
1690 'Progressive Rock',
1691 'Psychedelic Rock',
1692 'Symphonic Rock',
1693 'Slow Rock',
1694 'Big Band',
1695 'Chorus',
1696 'Easy Listening',
1697 'Acoustic',
1698 'Humour',
1699 'Speech',
1700 'Chanson',
1701 'Opera',
1702 'Chamber Music',
1703 'Sonata',
1704 'Symphony',
1705 'Booty Bass',
1706 'Primus',
1707 'Porn Groove',
1708 'Satire',
1709 'Slow Jam',
1710 'Club',
1711 'Tango',
1712 'Samba',
1713 'Folklore',
1714 'Ballad',
1715 'Power Ballad',
1716 'Rhythmic Soul',
1717 'Freestyle',
1718 'Duet',
1719 'Punk Rock',
1720 'Drum Solo',
1721 'Acapella',
1722 'Euro-House',
1723 'Dance Hall',
1724 'Goa',
1725 'Drum & Bass',
1726 'Club-House',
1727 'Hardcore',
1728 'Terror',
1729 'Indie',
1730 'BritPop',
1731 'Negerpunk',
1732 'Polsk Punk',
1733 'Beat',
1734 'Christian Gangsta Rap',
1735 'Heavy Metal',
1736 'Black Metal',
1737 'Crossover',
1738 'Contemporary Christian',
1739 'Christian Rock',
1740 'Merengue',
1741 'Salsa',
1742 'Thrash Metal',
1743 'Anime',
1744 'JPop',
1745 'Synthpop',
1748 @t_bitrate = ([
1749 [0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256],
1750 [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
1751 [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160]
1753 [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448],
1754 [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384],
1755 [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
1758 @t_sampling_freq = (
1759 [11025, 12000, 8000],
1760 [undef, undef, undef], # reserved
1761 [22050, 24000, 16000],
1762 [44100, 48000, 32000]
1765 @frequency_tbl = map { $_ ? eval "${_}e-3" : 0 }
1766 map { @$_ } @t_sampling_freq;
1768 @mp3_info_fields = qw(
1769 VERSION
1770 LAYER
1771 STEREO
1773 BITRATE
1774 FREQUENCY
1775 SIZE
1776 OFFSET
1777 SECS
1781 TIME
1782 COPYRIGHT
1783 PADDING
1784 MODE
1785 FRAMES
1786 FRAME_LENGTH
1787 VBR_SCALE
1790 %rva2_channel_types = (
1791 0x00 => 'OTHER',
1792 0x01 => 'MASTER',
1793 0x02 => 'FRONT_RIGHT',
1794 0x03 => 'FRONT_LEFT',
1795 0x04 => 'BACK_RIGHT',
1796 0x05 => 'BACK_LEFT',
1797 0x06 => 'FRONT_CENTER',
1798 0x07 => 'BACK_CENTER',
1799 0x08 => 'SUBWOOFER',
1802 %v1_tag_fields =
1803 (TITLE => 30, ARTIST => 30, ALBUM => 30, COMMENT => 30, YEAR => 4);
1805 @v1_tag_names = qw(TITLE ARTIST ALBUM YEAR COMMENT TRACKNUM GENRE);
1807 %v2_to_v1_names = (
1808 # v2.2 tags
1809 'TT2' => 'TITLE',
1810 'TP1' => 'ARTIST',
1811 'TAL' => 'ALBUM',
1812 'TYE' => 'YEAR',
1813 'COM' => 'COMMENT',
1814 'TRK' => 'TRACKNUM',
1815 'TCO' => 'GENRE', # not clean mapping, but ...
1816 # v2.3 tags
1817 'TIT2' => 'TITLE',
1818 'TPE1' => 'ARTIST',
1819 'TALB' => 'ALBUM',
1820 'TYER' => 'YEAR',
1821 'COMM' => 'COMMENT',
1822 'TRCK' => 'TRACKNUM',
1823 'TCON' => 'GENRE',
1824 # v2.3 tags - needed for MusicBrainz
1825 'UFID' => 'Unique file identifier',
1826 'TXXX' => 'User defined text information frame',
1829 %v2_tag_names = (
1830 # v2.2 tags
1831 'BUF' => 'Recommended buffer size',
1832 'CNT' => 'Play counter',
1833 'COM' => 'Comments',
1834 'CRA' => 'Audio encryption',
1835 'CRM' => 'Encrypted meta frame',
1836 'ETC' => 'Event timing codes',
1837 'EQU' => 'Equalization',
1838 'GEO' => 'General encapsulated object',
1839 'IPL' => 'Involved people list',
1840 'LNK' => 'Linked information',
1841 'MCI' => 'Music CD Identifier',
1842 'MLL' => 'MPEG location lookup table',
1843 'PIC' => 'Attached picture',
1844 'POP' => 'Popularimeter',
1845 'REV' => 'Reverb',
1846 'RVA' => 'Relative volume adjustment',
1847 'SLT' => 'Synchronized lyric/text',
1848 'STC' => 'Synced tempo codes',
1849 'TAL' => 'Album/Movie/Show title',
1850 'TBP' => 'BPM (Beats Per Minute)',
1851 'TCM' => 'Composer',
1852 'TCO' => 'Content type',
1853 'TCR' => 'Copyright message',
1854 'TDA' => 'Date',
1855 'TDY' => 'Playlist delay',
1856 'TEN' => 'Encoded by',
1857 'TFT' => 'File type',
1858 'TIM' => 'Time',
1859 'TKE' => 'Initial key',
1860 'TLA' => 'Language(s)',
1861 'TLE' => 'Length',
1862 'TMT' => 'Media type',
1863 'TOA' => 'Original artist(s)/performer(s)',
1864 'TOF' => 'Original filename',
1865 'TOL' => 'Original Lyricist(s)/text writer(s)',
1866 'TOR' => 'Original release year',
1867 'TOT' => 'Original album/Movie/Show title',
1868 'TP1' => 'Lead artist(s)/Lead performer(s)/Soloist(s)/Performing group',
1869 'TP2' => 'Band/Orchestra/Accompaniment',
1870 'TP3' => 'Conductor/Performer refinement',
1871 'TP4' => 'Interpreted, remixed, or otherwise modified by',
1872 'TPA' => 'Part of a set',
1873 'TPB' => 'Publisher',
1874 'TRC' => 'ISRC (International Standard Recording Code)',
1875 'TRD' => 'Recording dates',
1876 'TRK' => 'Track number/Position in set',
1877 'TSI' => 'Size',
1878 'TSS' => 'Software/hardware and settings used for encoding',
1879 'TT1' => 'Content group description',
1880 'TT2' => 'Title/Songname/Content description',
1881 'TT3' => 'Subtitle/Description refinement',
1882 'TXT' => 'Lyricist/text writer',
1883 'TXX' => 'User defined text information frame',
1884 'TYE' => 'Year',
1885 'UFI' => 'Unique file identifier',
1886 'ULT' => 'Unsychronized lyric/text transcription',
1887 'WAF' => 'Official audio file webpage',
1888 'WAR' => 'Official artist/performer webpage',
1889 'WAS' => 'Official audio source webpage',
1890 'WCM' => 'Commercial information',
1891 'WCP' => 'Copyright/Legal information',
1892 'WPB' => 'Publishers official webpage',
1893 'WXX' => 'User defined URL link frame',
1895 # v2.3 tags
1896 'AENC' => 'Audio encryption',
1897 'APIC' => 'Attached picture',
1898 'COMM' => 'Comments',
1899 'COMR' => 'Commercial frame',
1900 'ENCR' => 'Encryption method registration',
1901 'EQUA' => 'Equalization',
1902 'ETCO' => 'Event timing codes',
1903 'GEOB' => 'General encapsulated object',
1904 'GRID' => 'Group identification registration',
1905 'IPLS' => 'Involved people list',
1906 'LINK' => 'Linked information',
1907 'MCDI' => 'Music CD identifier',
1908 'MLLT' => 'MPEG location lookup table',
1909 'OWNE' => 'Ownership frame',
1910 'PCNT' => 'Play counter',
1911 'POPM' => 'Popularimeter',
1912 'POSS' => 'Position synchronisation frame',
1913 'PRIV' => 'Private frame',
1914 'RBUF' => 'Recommended buffer size',
1915 'RVAD' => 'Relative volume adjustment',
1916 'RVRB' => 'Reverb',
1917 'SYLT' => 'Synchronized lyric/text',
1918 'SYTC' => 'Synchronized tempo codes',
1919 'TALB' => 'Album/Movie/Show title',
1920 'TBPM' => 'BPM (beats per minute)',
1921 'TCOM' => 'Composer',
1922 'TCON' => 'Content type',
1923 'TCOP' => 'Copyright message',
1924 'TDAT' => 'Date',
1925 'TDLY' => 'Playlist delay',
1926 'TENC' => 'Encoded by',
1927 'TEXT' => 'Lyricist/Text writer',
1928 'TFLT' => 'File type',
1929 'TIME' => 'Time',
1930 'TIT1' => 'Content group description',
1931 'TIT2' => 'Title/songname/content description',
1932 'TIT3' => 'Subtitle/Description refinement',
1933 'TKEY' => 'Initial key',
1934 'TLAN' => 'Language(s)',
1935 'TLEN' => 'Length',
1936 'TMED' => 'Media type',
1937 'TOAL' => 'Original album/movie/show title',
1938 'TOFN' => 'Original filename',
1939 'TOLY' => 'Original lyricist(s)/text writer(s)',
1940 'TOPE' => 'Original artist(s)/performer(s)',
1941 'TORY' => 'Original release year',
1942 'TOWN' => 'File owner/licensee',
1943 'TPE1' => 'Lead performer(s)/Soloist(s)',
1944 'TPE2' => 'Band/orchestra/accompaniment',
1945 'TPE3' => 'Conductor/performer refinement',
1946 'TPE4' => 'Interpreted, remixed, or otherwise modified by',
1947 'TPOS' => 'Part of a set',
1948 'TPUB' => 'Publisher',
1949 'TRCK' => 'Track number/Position in set',
1950 'TRDA' => 'Recording dates',
1951 'TRSN' => 'Internet radio station name',
1952 'TRSO' => 'Internet radio station owner',
1953 'TSIZ' => 'Size',
1954 'TSRC' => 'ISRC (international standard recording code)',
1955 'TSSE' => 'Software/Hardware and settings used for encoding',
1956 'TXXX' => 'User defined text information frame',
1957 'TYER' => 'Year',
1958 'UFID' => 'Unique file identifier',
1959 'USER' => 'Terms of use',
1960 'USLT' => 'Unsychronized lyric/text transcription',
1961 'WCOM' => 'Commercial information',
1962 'WCOP' => 'Copyright/Legal information',
1963 'WOAF' => 'Official audio file webpage',
1964 'WOAR' => 'Official artist/performer webpage',
1965 'WOAS' => 'Official audio source webpage',
1966 'WORS' => 'Official internet radio station homepage',
1967 'WPAY' => 'Payment',
1968 'WPUB' => 'Publishers official webpage',
1969 'WXXX' => 'User defined URL link frame',
1971 # v2.4 additional tags
1972 # note that we don't restrict tags from 2.3 or 2.4,
1973 'ASPI' => 'Audio seek point index',
1974 'EQU2' => 'Equalisation (2)',
1975 'RVA2' => 'Relative volume adjustment (2)',
1976 'SEEK' => 'Seek frame',
1977 'SIGN' => 'Signature frame',
1978 'TDEN' => 'Encoding time',
1979 'TDOR' => 'Original release time',
1980 'TDRC' => 'Recording time',
1981 'TDRL' => 'Release time',
1982 'TDTG' => 'Tagging time',
1983 'TIPL' => 'Involved people list',
1984 'TMCL' => 'Musician credits list',
1985 'TMOO' => 'Mood',
1986 'TPRO' => 'Produced notice',
1987 'TSOA' => 'Album sort order',
1988 'TSOP' => 'Performer sort order',
1989 'TSOT' => 'Title sort order',
1990 'TSST' => 'Set subtitle',
1992 # grrrrrrr
1993 'COM ' => 'Broken iTunes comments',
1999 __END__
2001 =pod
2003 =back
2005 =head1 TROUBLESHOOTING
2007 If you find a bug, please send me a patch (see the project page in L<"SEE ALSO">).
2008 If you cannot figure out why it does not work for you, please put the MP3 file in
2009 a place where I can get it (preferably via FTP, or HTTP, or .Mac iDisk) and send me
2010 mail regarding where I can get the file, with a detailed description of the problem.
2012 If I download the file, after debugging the problem I will not keep the MP3 file
2013 if it is not legal for me to have it. Just let me know if it is legal for me to
2014 keep it or not.
2017 =head1 TODO
2019 =over 4
2021 =item ID3v2 Support
2023 Still need to do more for reading tags, such as using Compress::Zlib to decompress
2024 compressed tags. But until I see this in use more, I won't bother. If something
2025 does not work properly with reading, follow the instructions above for
2026 troubleshooting.
2028 ID3v2 I<writing> is coming soon.
2030 =item Get data from scalar
2032 Instead of passing a file spec or filehandle, pass the
2033 data itself. Would take some work, converting the seeks, etc.
2035 =item Padding bit ?
2037 Do something with padding bit.
2039 =item Test suite
2041 Test suite could use a bit of an overhaul and update. Patches very welcome.
2043 =over 4
2045 =item *
2047 Revamp getset.t. Test all the various get_mp3tag args.
2049 =item *
2051 Test Unicode.
2053 =item *
2055 Test OOP API.
2057 =item *
2059 Test error handling, check more for missing files, bad MP3s, etc.
2061 =back
2063 =item Other VBR
2065 Right now, only Xing VBR is supported.
2067 =back
2070 =head1 THANKS
2072 Edward Allen,
2073 Vittorio Bertola,
2074 Michael Blakeley,
2075 Per Bolmstedt,
2076 Tony Bowden,
2077 Tom Brown,
2078 Sergio Camarena,
2079 Chris Dawson,
2080 Anthony DiSante,
2081 Luke Drumm,
2082 Kyle Farrell,
2083 Jeffrey Friedl,
2084 brian d foy,
2085 Ben Gertzfield,
2086 Brian Goodwin,
2087 Todd Hanneken,
2088 Todd Harris,
2089 Woodrow Hill,
2090 Kee Hinckley,
2091 Roman Hodek,
2092 Ilya Konstantinov,
2093 Peter Kovacs,
2094 Johann Lindvall,
2095 Alex Marandon,
2096 Peter Marschall,
2097 michael,
2098 Trond Michelsen,
2099 Dave O'Neill,
2100 Christoph Oberauer,
2101 Jake Palmer,
2102 Andrew Phillips,
2103 David Reuteler,
2104 John Ruttenberg,
2105 Matthew Sachs,
2106 scfc_de,
2107 Hermann Schwaerzler,
2108 Chris Sidi,
2109 Roland Steinbach,
2110 Brian S. Stephan,
2111 Stuart,
2112 Dan Sully,
2113 Jeffery Sumler,
2114 Predrag Supurovic,
2115 Bogdan Surdu,
2116 Pierre-Yves Thoulon,
2117 tim,
2118 Pass F. B. Travis,
2119 Tobias Wagener,
2120 Ronan Waide,
2121 Andy Waite,
2122 Ken Williams,
2123 Ben Winslow,
2124 Meng Weng Wong.
2127 =head1 CURRENT AUTHOR
2129 Dan Sully E<lt>dan | at | slimdevices.comE<gt> & Slim Devices, Inc.
2131 =head1 AUTHOR EMERITUS
2133 Chris Nandor E<lt>pudge@pobox.comE<gt>, http://pudge.net/
2135 =head1 COPYRIGHT AND LICENSE
2137 Copyright (c) 2006 Dan Sully & Slim Devices, Inc. All rights reserved.
2139 Copyright (c) 1998-2005 Chris Nandor. All rights reserved.
2141 This program is free software; you can redistribute it and/or modify it under
2142 the same terms as Perl itself.
2144 =head1 SEE ALSO
2146 =over 4
2148 =item Slim Devices
2150 http://www.slimdevices.com/
2152 =item mp3tools
2154 http://www.zevils.com/linux/mp3tools/
2156 =item mpgtools
2158 http://www.dv.co.yu/mpgscript/mpgtools.htm
2159 http://www.dv.co.yu/mpgscript/mpeghdr.htm
2161 =item mp3tool
2163 http://www.dtek.chalmers.se/~d2linjo/mp3/mp3tool.html
2165 =item ID3v2
2167 http://www.id3.org/
2169 =item Xing Variable Bitrate
2171 http://www.xingtech.com/support/partner_developer/mp3/vbr_sdk/
2173 =item MP3Ext
2175 http://rupert.informatik.uni-stuttgart.de/~mutschml/MP3ext/
2177 =item Xmms
2179 http://www.xmms.org/
2182 =back
2184 =cut