man MP3::Tag () - Module for reading tags of MP3 audio files

NAME

MP3::Tag - Module for reading tags of MP3 audio files

SYNOPSIS

  use MP3::Tag;

  $mp3 = MP3::Tag->new($filename);

  # get some information about the file in the easiest way
  ($title, $track, $artist, $album, $comment, $year, $genre) = $mp3->autoinfo();
  $comment = $mp3->comment();

  # or have a closer look on the tags

  # scan file for existing tags
  $mp3->get_tags;

  if (exists $mp3->{ID3v1}) {
      # read some information from the tag
      $id3v1 = $mp3->{ID3v1};  # $id3v1 is only a shortcut for $mp3->{ID3v1}
      print $id3v1->title;

      # change the tag contents
      $id3v1->all("Song","Artist","Album",2001,"Comment",10,"Top 40");
      $id3v1->write_tag;
  }

  if (exists $mp3->{ID3v2}) {
      # read some information from the tag
      ($name, $info) = $mp3->{ID3v2}->get_frame("TIT2");
      # delete the tag completely from the file
      $mp3->{ID3v2}->remove_tag;
  } else {
      # create a new tag
      $mp3->new_tag("ID3v2");
      $mp3->{ID3v2}->add_frame("TALB", "Album title");
      $mp3->{ID3v2}->write_tag;
  }

  $mp3->close();

AUTHORS

Thomas Geffert, thg@users.sourceforge.net Ilya Zakharevich, ilyaz@cpan.org

DESCRIPTION

Tag is a wrapper module to read different tags of mp3 files. It provides an easy way to access the functions of seperate modules which do the handling of reading/writing the tags itself.

At the moment MP3::Tag::ID3v1 and MP3::Tag::ID3v2 are supported for read and write; MP3::Tag::Inf, MP3::Tag::CDDB_File, MP3::Tag::File, MP3::Tag::LastResort are supported for read access (the information obtained by parsing CDDB files, .inf file and the filename).

new()
 $mp3 = MP3::Tag->new($filename);
Creates a mp3-object, which can be used to retrieve/set different tags.
get_tags()
  [old name: getTags() . The old name is still available, but its use is not advised]
  @tags = $mp3->get_tags;
Checks which tags can be found in the mp3-object. It returns a list CW@tags which contains strings identifying the found tags, like ID3v1, ID3v2, Inf, or CDDB_File (the last but one if the .inf information file with the same basename as MP3 file is found). Each found tag can then be accessed with CW$mp3->{tagname} , where tagname is a string returned by get_tags ; Use the information found in MP3::Tag::ID3v1, MP3::Tag::ID3v2 and MP3::Tag::Inf, MP3::Tag::CDDB_File to see what you can do with the tags.
new_tag()
  [old name: newTag() . The old name is still available, but its use is not advised]
  $tag = $mp3->new_tag($tagname);
Creates a new tag of the given type CW$tagname. You can access it then with CW$mp3->{$tagname}. At the moment ID3v1 and ID3v2 are supported as tagname. Returns an tag-object: CW$mp3->{$tagname}.
close()
  $mp3->close;
You can use close() to explicitly close a file. Normally this is done automatically by the module, so that you do not need to do this.
genres()
  $allgenres = $mp3->genres;
  $genreName = $mp3->genres($genreID);
  $genreID   = $mp3->genres($genreName);
Returns a list of all genres (reference to an array), or the according name or id to a given id or name. This function is only a shortcut to MP3::Tag::ID3v1->genres. This can be also called as MP3::Tag->genres;
autoinfo()
  ($title, $track, $artist, $album, $comment, $year, $genre) = $mp3->autoinfo();
  $info_hashref = $mp3->autoinfo();
autoinfo() returns information about the title, track number, artist, album name, the file comment, the year and genre. It can get this information from an ID3v1-tag, an ID3v2-tag, from CDDB file, from .inf-file, and from the filename itself. It will as default first try to find a ID3v2-tag to get this information. If this cannot be found it tries to find a ID3v1-tag, then to read an CDDB file, an .inf-file, and if these are not present either, it will use the filename to retrieve the title, track number, artist, album name. The comment, year and genre are found differently, via the CWcomment, CWyear and CWgenre methods. You can change the order of lookup with the config() command. autoinfo() returns an array with the information or a hashref. The hash has four keys 'title', 'track', 'artist' and 'album' where the information is stored. If comment, year or genre are found, the hash will have keys 'comment' and/or 'year' and/or 'genre' too. If an optional argument CW'from' is given, the returned values (title, track number, artist, album name, the file comment, the year and genre) are array references with the first element being the value, the second the tag (CWID3v2 or CWID3v1 or CWInf or CWCDDB_File or CWfilename) from which it is taken. (Deprecated name 'song' can be used instead of 'title' as well.)
comment()
  $comment = $mp3->comment();           # empty string unless found
comment() returns comment information. It can get this information from an ID3v1-tag, or an ID3v2-tag (from CWCOMM frame with empty <short> field), CDDB file (from CWEXTD or CWEXTT fields), or .inf-file (from CWTrackcomment field). It will as default first try to find a ID3v2-tag to get this information. If no comment is found there, it tries to find it in a ID3v1-tag, if none present, will try CDDB file, then .inf-file. It returns an empty string if no comment is found. You can change the order of this with the config() command. If an optional argument CW'from' is given, returns an array reference with the first element being the value, the second the tag (ID3v2 or ID3v1) from which the value is taken.
year()
  $year = $mp3->year();         # empty string unless found
year() returns the year information. It can get this information from an ID3v2-tag, or ID3v1-tag, or .inf-file, or filename. It will as default first try to find a ID3v2-tag to get this information. If no year is found there, it tries to find it in a ID3v1-tag, if none present, will try CDDB file, then .inf-file, then by parsing the file name. It returns an empty string if no year is found. You can change the order of this with the config() command. If an optional argument CW'from' is given, returns an array reference with the first element being the value, the second the tag (ID3v2 or ID3v1 or filename) from which the value is taken.
comment_collection(), comment_track(), title_track(). author_collection()
access the corresponding fields returned by parse() method of CDDB_File.
genre()
  $genre = $mp3->genre();               # empty string unless found
genre() returns the genre string. It can get this information from an ID3v2-tag or ID3v1-tag. It will as default first try to find a ID3v2-tag to get this information. If no genre is found there, it tries to find it in a ID3v1-tag, if none present, will try .inf-file, It returns an empty string if no genre is found. You can change the order of this with the config() command. If an optional argument CW'from' is given, returns an array reference with the first element being the value, the second the tag (ID3v2 or ID3v1 or filename) from which the value is taken.
config
  MP3::Tag->config(item => value1, value2...);  # Set options globally
  $mp3->config(item => value1, value2...);      # Set object options
When object options are first time set or get, the global options are propagated into object options. (So if global options are changed later, these changes are not inherited.) Possible items are:
autoinfo
Configure the order in which ID3v1-, ID3v2-tag and filename are used by autoinfo. Options can be ID3v1, ID3v2, CDDB_File, Inf, filename. The order in which they are given to config also sets the order how they are used by autoinfo. If an option is not present, it will not be used by autoinfo (and other auto-methods if the specific overriding config command were not issued).
  $mp3->config("autoinfo","ID3v1","ID3v2","filename");
sets the order to check first ID3v1, then ID3v2 and at last the Filename
  $mp3->config("autoinfo","ID3v1","filename","ID3v2");
sets the order to check first ID3v1, then the Filename and last ID3v2. As the filename will be always present ID3v2 will here never be checked.
  $mp3->config("autoinfo","ID3v1","ID3v2");
sets the order to check first ID3v1, then ID3v2. The filename will never be used.
title artist album year comment track genre
Configure the order in which ID3v1- and ID3v2-tag are used by the corresponding methods (e.g., comment()). Options can be ID3v1, ID3v2, Inf, CDDB_File, filename. The order in which they are given to config also sets the order how they are used by comment(). If an option is not present, then CWautoinfo option will be used instead.
extension
regular expression to match the file extension (including the dot). The default is to match 1..4 letter extensions which are not numbers.
parse_data
the data used by MP3::Tag::ParseData handler; each option is an array reference of the form CW[$flag, $string, $pattern1, ...]. All the options are processed in the following way: patterns are matched against CW$string until one of them succeeds; the information obtained from later options takes precedence over the information obtained from earlier ones.
parse_split
The regular expression to split the data when parsing with CWn or CWl flags.
parse_filename_ignore_case
If true (default), calling parse() and parse_rex() with match-filename escapes (such as CW%=D) matches case-insensitively.
parse_filename_merge_dots
If true (default), calling parse() and parse_rex() with match-filename escapes (such as CW%=D) does not distinguish a dot and many consequent dots.
parse_join
string to put between multiple occurences of a tag in a parse pattern; defaults to CW'; '. E.g., parsing CW'1988-1992, Homer (LP)' with pattern CW'%c, %a (%c)' results in comment set to CW'1988-1992; LP' with the default value of CWparse_join.
v2title
Configure the elements of ID3v2-tag which are used by ID3v2::title(). Options can be TIT1, TIT2, TIT3; the present values are combined. If an option is not present, it will not be used by ID3v2::title().
cddb_files
List of files to look for in the directory of MP3 file to get CDDB info.
year_is_timestamp
If TRUE (default) parse() will match complicated timestamps against CW%y; for example, CW2001-10-23--30,2002-02-28 is a range from 23rd to 30th of October 2001, and 28th of February of 2002. According to ISO, CW-- can be replaced by CW/ as well. For convenience, the leading 0 can be omited from the fields which ISO requires to be 2-digit.
comment_remove_date
When extracting the date from comment fields, remove the recognized portion even if it is human readable (e.g., CWRecorded on 2014-3-23) if TRUE. Current default: FALSE.
id3v2_frame_empty_ok
When setting the individual id3v2 frames via ParseData, do not remove the frames set to an empty string. Default 0 (empty means 'remove').
translate_*
A subroutine used to munch a field CW* (out of CWtitle track artist album comment year genre) Takes two arguments: the MP3::Tag object, and the current value of the field. The second argument may also have the form CW[value, handler], where CWhandler is the string indentifying the handler which returned the value.
id3v2_missing_fatal
If TRUE, interpolating ID3v2 frames (e.g., by CW%{TCOM}) when the ID3v2 tags is missing is a fatal error. If false (default), in such cases interpolation results in an empty string.
parse_minmatch
may be 0, 1, or a list of CW%-escapes (matching any string) which should matched non-greedily by parse() and friends. E.g., parsing CW'Adagio - Andante - Piano Sonata' via CW'%t - %l' gives different results for the settings 0 and 1; note that greediness of CW%l does not matter, thus the value of 1 is equivalent for the value of CWt for this particular pattern.
*
Later there will be probably more things to configure.
get_config
  $opt = $mp3->get_config("item");
When object options are first time set or get, the global options are propagated into object options. (So if global options are changed later, these changes are not inherited.)
pure_filetags
  $data = $mp3->pure_filetags()->autoinfo;
Configures CW$mp3 to not read anything except the pure ID3v2 or ID3v1 tags, and do not postprocess them. Returns the object reference itself to simplify chaining of method calls.
get_user
  $data = $mp3->get_user($n);   # n-th piece of user scratch space
Queries an entry in a scratch array ($n=3 corresponds to CW%{U3}).
set_user
  $mp3->set_user($n, $data);    # n-th piece of user scratch space
Sets an entry in a scratch array ($n=3 corresponds to CW%{U3}).
set_id3v2_frame
  $mp3->set_id3v2_frame($name, @values);
When called with only CW$name as the argument, removes the specified frame (if it existed). Otherwise sets the frame passing the specified CW@values to the add_frame() function of MP3::Tag::ID3v2.
get_id3v2_frames
  ($descr, @frames) = $mp3->get_id3v2_frames($fname);
Returns the specified frame(s); has the same API as MP3::Tag::ID3v2::get_frames, but also returns undef if no ID3v2 tag is present.
is_id3v2_modified
  $frame = $mp3->is_id3v2_modified();
Returns TRUE if ID3v2 tag exists and was modified after creation.
select_id3v2_frame
  $frame = $mp3->select_id3v2_frame($fname, $descrs, $langs [, $VALUE]);
Returns the specified frame(s); has the same API as MP3::Tag::ID3v2::frame_select (args are frame name, list of wanted Descriptors, list of wanted Languages, and possibly the new contents - with CWundef meaning deletion). For read-only access it returns CWundef if no ID3v2 tag is present.
have_id3v2_frame
  $have_it = $mp3->have_id3v2_frame($fname, $descrs, $langs);
Returns TRUE the specified frame(s) exist; has the same API as MP3::Tag::ID3v2::frame_have (args are frame name, list of wanted Descriptors, list of wanted Languages).
get_id3v2_frame_ids
  $h = $mp3->get_id3v2_frame_ids();
  print "  $_ => $h{$_}" for keys %$h;
Returns a hash reference with the short names of ID3v2 frames present in the tag as keys (and long description of the meaning as values), or FALSE if no ID3v2 tag is present. See MP3::Tags::ID3v2::get_frame_ids for details.
interpolate
  $string = $mp3->interpolate($pattern)
interpolates CW%-escapes in CW$pattern using the information from CW$mp3 tags. The syntax of escapes is similar to this of sprintf():
  % [ [FLAGS] MINWIDTH] [.MAXWIDTH] ESCAPE
The only recognized FLAGS are CW- (to denote left-alignment inside MINWIDTH- wide field), CW' ' (SPACE), and CW0 (denoting the fill character to use), as well as an arbitrary character in parentheses (which becomes the fill character). MINWIDTH and MAXWIDTH should be numbers. The one-letter ESCAPEs are replaced by
                % => literal '%'
                t => title
                a => artist
                l => album
                y => year
                g => genre
                c => comment
                n => track
                f => filename without the directory path
                F => filename with the directory path
                D => the directory path of the filename
                E => file extension
                e => file extension without the leading dot
                A => absolute filename without extension
                B => filename without the directory part and extension
                N => filename as originally given without extension
                v       mpeg_version
                L       mpeg_layer_roman
                r       bitrate_kbps
                q       frequency_kHz
                Q       frequency_Hz
                S       total_secs_int
                m       total_mins
                s       leftover_secs
                C       is_copyrighted_YN
                p       frames_padded_YN
                o       channel_mode
                u       frames
Additionally, ESCAPE can be a string (with all backslashes and curlies escaped) enclosed in curly braces CW{}. The interpretation is the following:
*
CWdNUMBER is replaced by NUMBER-th component of the directory name (with 0 corresponding to the last component).
*
CWUNUMBER is replaced by NUMBER-th component of the user scratch array.
*
CWDNUMBER is replaced by the directory name with NUMBER components stripped.
*
Names of ID3v2 frames are replaced by their text values (empty for missing frames).
*
If string starts with CWFNAME:: if frame FNAME does not exists, the escape is ignored; otherwise the rest of the string is reinterpreted (after stripping backslashes from backslashes and curlies).
*
String starting with CW!FNAME: are treated similarly with inverted test.
*
String starting with LETTERCW: or CW!LETTERCW: are treated similarly to ID3v2 conditionals, but the condition is that the corresponding escape expands to non-empty string.
*
Strings CWaC, CWtT, CWcC, CWcT are replaced by the collection artist, track title, collection comment, and track comment as obtained from CDDB_File.
*
Strings CWID3v1 and CWID3v2 are replaced by the whole ID3v1/2 tag.
*
Strings of the form CWFRAM(list,of,languages)[description]' are replaced by the first FRAM frame with the descriptor description in the specified comma-separated list of languages. Instead of a language (ID3v2 uses lowercase 3-char ISO-639-2 language notations) one can use a string of the form CW#Number; e.g., CW#4 means 4th FRAM frame, or FRAM04. Empty string for the language means any language.) Works as a condition for conditional interpolation too. Any one of the list of languages and the disription can be omitted; this means that either the frame FRAM has no language or descriptor associated, or no restriction should be applied. Unknown language should be denoted as CWXXX (in uppercase!). The language match is case-insensitive. The default for the fill character is SPACE. Fill character should preceed CW- if both are given. Example:
   Title: %(/)-12.12t%{TIT3:; TIT3 is %\{TIT3\}}%{!TIT3:. No TIT3 is present}
will result in
   Title: TITLE///////; TIT3 is Op. 16
if title is CWTITLE, and TIT3 is CWOp. 16, and
   Title: TITLE///////. No TIT3 is present
if title is CWTITLE, but TIT3 is not present.
  Fat content: %{COMM(eng,fra,fre,rus,)[FatContent]}
will print the comment field with Description CWFatContent prefering the description in English to one in French, Russian, or any other language (in this order). (I do not know which one of terminology/bibliography codes for Frech is used, so for safety include both.) Parse CW$string according to the regular expression CW$pattern with CW%-escapes CW%%, %a, %t, %l, %y, %g, %c, %n, %e, %E. The meaning of escapes is the same as for interpolate. Also supported are escapes CW%=a, %=t, %=l, %=y, %=g, %=c, %=n, %=e, %=E, %=A, %=B, %=D, %=f, %=F, %=N, %={WHATEVER}; they match substrings which are actual values of artist/title/etc (CW%=n also matches leading 0s; actual file-name matches ignore the difference between CW/ and CW\, between one and multiple consequent dots (if configuration variable CWparse_filename_merge_dots is true (default)) and are case-insensitive if configuration variable CWparse_filename_ignore_case is true (default); moreover, <%n>, <%y>, <%=n>, <%=y> will not match if the string-to-match is adjacent to a digit). Returns false on failure, a hash reference with parsed fields otherwise. The escapes CW%{U<number>} and escapes of the forms CW%{ABCD}, CW%{ABCD<number>} match any string, and corresponds to the hash key inside braces; here CWABCD is a 4-letter word possibly followed by 2-digit number (as in names of ID3v2 tags), or what can be put in CW'%{FRAM(lang,list)[description]}'.
  $res = $mp3->parse_rex(qr<^%a - %t\.\w{1,4}$>, $mp3->filename_nodir) or die;
  $author = $res->{author};
2-digit numbers are allowed for the track number (the leading 0 is stripped); 4-digit years in the range 1000..2999 are allowed for year. Alternatively, if option year_is_timestamp is TRUE (default), year may be a range of timestamps in the format understood by ID3v2 method year() (see year in MP3::Tag::ID3v2). Currently the regular expressions with capturing parens are not supported.
parse_rex_prepare($pattern)
Returns a data structure which later can be used by parse_rex_match(). These two are equivalent:
  $mp3->parse_rex($pattern, $data);
  $mp3->parse_rex_match($mp3->parse_rex_prepare($pattern), $data);
This call constitutes the slow part of the parse_rex() call; it makes sense to factor out this step if the parse_rex() with the same CW$pattern is called against multiple CW$data. Matches CW$data against a data structure returned by parse_rex_prepare(). These two are equivalent:
  $mp3->parse_rex($pattern, $data);
  $mp3->parse_rex_match($mp3->parse_rex_prepare($pattern), $data);
Parse CW$string according to the string CW$pattern with CW%-escapes CW%%, %a, %t, %l, %y, %g, %c, %n, %e, %E. The meaning of escapes is the same as for interpolate. See parse_rex($pattern, CW$string) for more details. Returns false on failure, a hash reference with parsed fields otherwise.
  $res = $mp3->parse("%a - %t.mp3", $mp3->filename_nodir) or die;
  $author = $res->{author};
2-digit numbers are allowed for the track number; 4-digit years in the range 1000..2999 are allowed for year.
parse_prepare($pattern)
Returns a data structure which later can be used by parse_rex_match(). This is a counterpart of parse_rex_prepare() used with non-regular-expression patterns. These two are equivalent:
  $mp3->parse($pattern, $data);
  $mp3->parse_rex_match($mp3->parse_prepare($pattern), $data);
This call constitutes the slow part of the parse() call; it makes sense to factor out this step if the parse() with the same CW$pattern is called against multiple CW$data.
filename()
abs_filename()
filename_nodir()
filename_noextension()
filename_nodir_noextension()
abs_filename_noextension()
dirname([$strip_levels])
filename_extension()
filename_extension_nodot()
dir_component([$level])
  $filename = $mp3->filename();
  $abs_filename = $mp3->abs_filename();
  $filename_nodir = $mp3->filename_nodir();
  $abs_dirname = $mp3->dirname();
  $abs_dirname = $mp3->dirname(0);
  $abs_parentdir = $mp3->dirname(1);
  $last_dir_component = $mp3->dir_component(0);
Return the name of the audio file: either as given to the new() method, or absolute, or directory-less, or originally given without extension, or directory-less without extension, or absolute without extension, or the directory part of the fullname only, or filename extension (with dot included, or not). The extension is calculated using the config() value CWextension. The dirname() method takes an optional argument: the number of directory components to strip; the CWdir_component($level) method returns one component of the directory (to get the last use 0 as CW$level; this is the default if no CW$level is specified).
mpeg_version()
mpeg_layer()
mpeg_layer_roman()
is_stereo()
is_vbr()
bitrate_kbps()
frequency_Hz()
frequency_kHz()
size_bytes()
total_secs()
total_secs_int()
total_mins()
leftover_secs()
leftover_msec()
time_mm_ss()
is_copyrighted()
is_copyrighted_YN()
frames_padded()
frames_padded_YN()
channel_mode_int()
frames()
frame_len()
vbr_scale()
These methods return the information about the contents of the MP3 file. Useing these methods requires that the module MP3::Info is installed. Since these calls are redirectoed to the module MP3::Info, the returned info is subject to the same restrictions as the method get_mp3info() of this module; in particular, the information about the frame number and frame length is only approximate vbr_scale() is from the VBR header; total_secs() is not necessarily an integer, but total_secs_int() is; time_mm_ss() has format CWMM:SS; the CW*_YN flavors return the value as a string Yes or No; mpeg_layer_roman() returns the value as a roman numeral; channel_mode() takes values in CW'stereo', 'joint stereo', 'dual channel', 'mono'.
  $mp3 = MP3::Tag->new($filename);
  $mp3->update_tags();                  # Fetches the info, and updates tags
This method updates ID3v1 and ID3v2 tags (the latter only if needed) with the the information about title, artist, album, year, comment, track, genre from the hash reference CW$data. The format of CW$data is the same as one returned from autoinfo() (with or without the optional argument 'from'). The fields which are marked as coming from ID3v1 or ID3v2 tags are not updated when written to the same tag. If CW$data is not defined or missing, CWautoinfo('from') is called to obtain the data. Returns the object reference itself to simplify chaining of method calls.

EXAMPLE SCRIPTS

Some example scripts come with this module:

mp3info2
perform command line manipulation of audio tags (and more!);
audio_rename
rename audio files according to associated tags (and more!);
type_mp3_dir
write LaTeX files suitable for CD covers and normal-size sheet descriptions of hierarchy of audio files;
mp3_total_time
Calculate total duration of audio files;
eat_wav_mp3_header
remove WAV headers from MP3 files in WAV containers. (Last two do not use these modules!) Some more examples:
  # Convert from one (non-standard-conforming!) encoding to another
  perl -MMP3::Tag -MEncode -wle '
    my @fields = qw(artist album title comment);
    for my $f (@ARGV) {
      print $f;
      my $t = MP3::Tag->new($f) or die;
      $t->update_tags(
        { map { $_ => encode "cp1251", decode "koi8-r", $t->$_() }, @fields }
      );
    }' list_of_audio_files

SEE ALSO

MP3::Tag::ID3v1, MP3::Tag::ID3v2, MP3::Tag::File, MP3::Tag::ParseData, MP3::Tag::Inf, MP3::Tag::CDDB_File.

COPYRIGHT

Copyright (c) 2000-2004 Thomas Geffert, Ilya Zakharevich. 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.