MP3::Info - Manipulate / fetch info from MP3 audio files
#!perl -w
use MP3::Info;
my $file = 'Pearls_Before_Swine.mp3';
set_mp3tag($file, 'Pearls Before Swine', q"77's",
'Sticks and Stones', '1990',
q"(c) 1990 77's LTD.", 'rock & roll');
my $tag = get_mp3tag($file) or die "No TAG info";
$tag->{GENRE} = 'rock';
set_mp3tag($file, $tag);
my $info = get_mp3info($file);
printf "$file length is %d:%d\n", $info->{MM}, $info->{SS};
my $mp3 = new MP3::Info $file;
$mp3->title('Perls Before Swine');
printf "$file length is %s, title is %s\n",
$mp3->time, $mp3->title;
use_mp3_utf8([STATUS])Will only be able to it on if Unicode::String is available. ID3v2 tags will be converted to UTF-8 according to the encoding specified in each tag; ID3v1 tags will be assumed Latin-1 and converted to UTF-8.
Function returns status (TRUE/FALSE). If no argument is supplied, or an unaccepted argument is supplied, function merely returns status.
This function is not exported by default, but may be exported
with the :utf8 export tag.
use_winamp_genres()@mp3_genres and %mp3_genres
(adds 68 additional genres to the default list of 80).
This is a separate function because these are non-standard
genres, but they are included because they are widely used.
You can import the data structures with one of:
use MP3::Info qw(:genres);
use MP3::Info qw(:DEFAULT :genres);
use MP3::Info qw(:all);
1 for ID3v1,
2 for ID3v2, and ALL for both.
For ID3v1, removes last 128 bytes from file if those last 128 bytes begin with the text 'TAG'. File will be 128 bytes shorter.
For ID3v2, removes ID3v2 tag. Because an ID3v2 tag is at the beginning of the file, we rewrite the file after removing the tag data. The buffer for rewriting the file is 4MB. BUFFER (in bytes) ca change the buffer size.
Returns the number of bytes removed, or -1 if no tag removed, or undef if there is an error.
Fields are TITLE, ARTIST, ALBUM, YEAR, COMMENT, GENRE. All fields have
a 30-byte limit, except for YEAR, which has a four-byte limit, and GENRE,
which is one byte in the file. The GENRE passed in the function is a
case-insensitive text string representing a genre found in @mp3_genres.
Will accept either a list of values, or a hashref of the type
returned by get_mp3tag.
If TRACKNUM is present (for ID3v1.1), then the COMMENT field can only be 28 bytes.
ID3v2 support may come eventually. Note that if you set a tag on a file
with ID3v2, the set tag will be for ID3v1[.1] only, and if you call
get_mp3_tag on the file, it will show you the (unchanged) ID3v2 tags,
unless you specify ID3v1.
set_mp3tag.
If VERSION is 1, the information is taken from the ID3v1 tag (if present).
If VERSION is 2, the information is taken from the ID3v2 tag (if present).
If VERSION is not supplied, or is false, the ID3v1 tag is read if present, and
then, if present, the ID3v2 tag information will override any existing ID3v1
tag info.
If the ID3v2 version is older than ID3v2.2.0 or newer than ID3v2.3.0, it will not be read (and a warning will be issued if -w is on).
If RAW_V2 is false or not supplied and VERSION is 2, only the tags
corresponding to ID3v1 tags are returned, with the same keys in the returned
hashref.
If RAW_V2 is true and VERSION is 2, get_mp3tag will return a hash
of tag four-character IDs and their data, without stripping text encoding and
language code information. Tag IDs and their meanings are in the global hash
(not exported) %v2_tag_names.
my $tag = get_mp3tag('mysong.mp3', 2, 1); # ID3v2, raw ID3v2 tags
for (keys %$tag) {
printf "%s => %s\n", $MP3::Info::v2_tag_names{$_}, $tag->{$_};
}
Strings returned will be in Latin-1, unless UTF-8 is specified (use_mp3_utf8), unless RAW_V2 is specified, in which no charset manipulation is done.
Also returns a VERSION key, containing the ID3 version used for the returned
data (if VERSION argument is 0, may contain two versions).
VERSION MPEG audio version (1, 2, 2.5)
LAYER MPEG layer description (1, 2, 3)
STEREO boolean for audio is in stereo
VBR boolean for variable bitrate
BITRATE bitrate in kbps (average for VBR files)
FREQUENCY frequency in kHz
SIZE bytes in audio stream
SECS total seconds
MM minutes
SS leftover seconds
MS leftover milliseconds
TIME time in MM:SS
COPYRIGHT boolean for audio is copyrighted
PADDING boolean for MP3 frames are padded
MODE channel mode (0 = stereo, 1 = joint stereo,
2 = dual channel, 3 = single channel)
FRAMES approximate number of frames
FRAME_LENGTH approximate length of a frame
VBR_SCALE VBR scale from VBR header
If you find a bug, please send me a patch. If you cannot figure out why it does not work for you, please put the MP3 file in a place where I can get it (preferably via FTP) and send me mail regarding where I can get the file, with a detailed description of the problem.
If I download the file, after debugging the problem I will not keep the MP3 file if it is not legal for me to have it. Just let me know if it is legal for me to keep it or not.
I think I might just use Matt DiMeo's MPEG::ID3v2Tag; the problem is that right now it requires 5.005, and MacPerl uses 5.004 (for now).
Also make it work better with ID3v2.2 tags, and make a more reasonable guess at which comment to use if there's more than one.
Add some support for ID3v2.4.0 and Unicode strings in tags;
see use_mp3_utf8. (Ben Gertzfield)
Add TAGVERSION to get_mp3tag result.
Don't get FRAME_LENGTH if no FRAMES (Woodrow Hill).
Fix bug where get_mp3tag would return an empty hashref instead
of undef if ID3v1 tag is asked for, and there is no ID3v1 tag, but
there is an ID3v2 tag. (Stuart)
Made all functions optionally accept filehandle in place of filename.
Remove all croaks/dies and replace with simple returns or carps/warns. (Jeffrey Sumler)
Fix various input data problems, bad warnings, division by zero, etc.
Undef $/ in set_mp3tag() so caller can't mess up the print.
Fix bitrate if ID == 0 and VBR. (Kyle Farrell, Per Bolmstedt)
Split off _get_info() from get_mp3info(), so, eventually, programmers
can access that functionality without passing in a file or filehandle.
Not supported yet, but available for playing.
Added total frames, leftover milliseconds, and formatted time.
Fixed sample frequency for MPEG 2.5 files (perhaps not including VBR, though ... see bug above).
Add in some additional genres. (Peter Marschall)
Added ID3v2 tag removal. (Ronan Waide) NOTE: this is DANGEROUS. It is tested, but needs more testing. The file is rewritten entirely. Lots of data moving around.
Added ID3v2.2.0 tag reading. (Ronan Waide, Kee Hinckley)
Changed ID3v2 tag recognition to only match [A-Z0-9] instead of \w. (Christoph Oberauer)
Fixed VERSION in get_mp3info to properly return 2 when appropriate. (Bogdan Surdu)
Added VBR support. Average bitrate is returned as BITRATE, and minutes and seconds (MM and SS) should be accurate. (Andy Waite for pointer to MP3Ext)
Made time calculation better overall.
Made MP3 header validation routines more comprehensive. (Matthew Sachs for pointer to xmms source)
Changed name to MP3::Info (with wrapper still named MP3::Info).
get_mp3tag. Thanks much
to Tom Brown.
Cleaned up a bit, prepare for impending ID3v2 support.
NOTE: truncate() broken in some builds of ActivePerl (517, maybe
others). No changes to module to fix problem. (Brian Goodwin)
Added 255 (\xFF) as default genre. (Andrew Phillips)
I think I fixed bug relating to spaces in ID3v2 headers. (Tom Brown)
Fix small unnoticable bug where ID3v2 offset is tag size plus 10, not just tag size.
Not publickly released.
get_mp3tag (was
using spaces for padding before ... now trailing whitespace and
all nulls are removed before returning tag info).
Made tests more extensive (more for my own sanity when making all these changes than to make sure it works on other platforms and machines :).
get_mp3info
much faster and much more reliable, and added recognition of ID3v2
headers. (Tom Brown)
get_mp3tag that changed value of $_ in caller
(Todd Hanneken).
%winamp_genres having the wrong numbers
(Matthew Sachs).
remove_mp3tag. Addeed VERSION to the hash returned by
get_mp3info, and fixed a bug where STEREO was not being set correctly.
Export all genre data structures on request. Added use_winamp_genres
to use WinAmp genres. (Roland Steinbach)
Added a $MPEG::MP3Info::try_harder ($MP3::Info::try_harder) variable
that will try harder to find the MP3 header in a file. False by default.
Can take a long time to fail, but should find most headers at any offsets
if set to true.
Thanks to Matthew Sachs for his input and fixes, and for mp3tools.
MPEG::MP3Tag to MPEG::MP3Info, because it does
more than just TAG stuff now.
Made header stuff even more reliable. Lots of help and testing from Meng Weng Wong again. :-)
If you have an MP3 that has the header begin at an odd place like byte 761, then I suggest you strip out the junk before the header begins. :-)
get_mp3info. Thanks again to mp3tool source from
Johann Lindvall, because I basically stole it straight (after
converting it from C to Perl, of course).
I did everything I could to find the header info, but if anyone has valid MP3 files that are not recognized, or has suggestions for improvement of the algorithms, let me know.
unpack.
(Meng Weng Wong)
Edward Allen <allenej@c51844-a.spokn1.wa.home.com>, Vittorio Bertola <v.bertola@vitaminic.com>, Michael Blakeley <mike@blakeley.com>, Per Bolmstedt <tomten@kol14.com>, Tony Bowden <tony@tmtm.com>, Tom Brown <thecap@usa.net>, Sergio Camarena <scamarena@users.sourceforge.net>, Chris Dawson <cdawson@webiphany.com>, Luke Drumm <lukedrumm@mypad.com>, Kyle Farrell <kyle@cantametrix.com>, Ben Gertzfield <che@debian.org>, Brian Goodwin <brian@fuddmain.com>, Todd Hanneken <thanneken@hds.harvard.edu>, Woodrow Hill <asim@mindspring.com>, Kee Hinckley <nazgul@somewhere.com>, Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>, Peter Kovacs <kovacsp@egr.uri.edu>, Johann Lindvall, Peter Marschall <peter.marschall@mayn.de>, Trond Michelsen <mike@crusaders.no>, Dave O'Neill <dave@nexus.carleton.ca>, Christoph Oberauer <christoph.oberauer@sbg.ac.at>, Andrew Phillips <asp@wasteland.org>, David Reuteler <reuteler@visi.com>, Matthew Sachs <matthewg@zevils.com>, Hermann Schwaerzler <Hermann.Schwaerzler@uibk.ac.at>, Chris Sidi <sidi@angband.org>, Roland Steinbach <roland@support-system.com>, Stuart <schneis@users.sourceforge.net>, Jeffery Sumler <jsumler@mediaone.net>, Predrag Supurovic <mpgtools@dv.co.yu>, Bogdan Surdu <tim@go.ro>, Pass F. B. Travis <pftravis@bellsouth.net>, Tobias Wagener <tobias@wagener.nu>, Ronan Waide <waider@stepstone.ie>, Andy Waite <andy@mailroute.com>, Ken Williams <ken@forum.swarthmore.edu>, Meng Weng Wong <mengwong@pobox.com>.
Chris Nandor <pudge@pobox.com>, http://pudge.net/
Copyright (c) 1998-2002 Chris Nandor. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License, distributed with Perl.
http://sourceforge.net/projects/mp3-info/
http://www.zevils.com/linux/mp3tools/
http://www.dv.co.yu/mpgscript/mpgtools.htm
http://www.dv.co.yu/mpgscript/mpeghdr.htm
http://www.dtek.chalmers.se/~d2linjo/mp3/mp3tool.html
http://www.id3.org/
http://www.xingtech.com/support/partner_developer/mp3/vbr_sdk/
http://rupert.informatik.uni-stuttgart.de/~mutschml/MP3ext/
http://www.xmms.org/
v1.01, Tuesday, February 26, 2002