++ed by:
1 non-PAUSE user
Author image Ilya Zakharevich


mp3info2 - get/set MP3 tags; uses MP3::Tag to get default values.


  # Print the information in tags and autodeduced info
  mp3info2 *.mp3

  # In addition, set the year field to 1981
  mp3info2 -y 1981 *.mp3

  # Same without printout of information, recursively in the current directory
  mp3info2 -R -p "" -y 1981 .

  # Do not deduce any field, print the info from the tags only
  mp3info2 -C autoinfo=ID3v2,ID3v1 *.mp3

  # Get the artist from CDDB_File, autodeduce other info, write it to tags
  mp3info2 -C artist=CDDB_File -u *.mp3

  # For the title, prefer information from .inf file; autodeduce and update
  mp3info2 -C title=Inf,ID3v2,ID3v1,filename -u *.mp3

  # Same, and get the author from CDDB file
  mp3info2 -C "#title=Inf,ID3v2,ID3v1,filename#artist=CDDB_File" -u *.mp3

  # Write a script for conversion of .wav to .mp3 autodeducing tags
  mp3info2 -p "lame -h --vbr-new --tt '%t' --tn %n --ta '%a' --tc '%c' --tl '%l' --ty '%y' '%f'\n" *.wav >xxx.sh


The program prints a message summarizing tag info (obtained via MP3::Tag module) for specified files.

It may also update the information in MP3 tags. This happens in three different cases.

  • If the information supplied in command-line options t a l y g c n differs from the content of the corresponding ID3 tags (or there is no corresponding ID3 tags).

  • If options -d or -F were given.

  • if MP3::Tag obtains the info from other means than MP3 tags, and -u forces the update of the ID3 tags.

(All ways are disabled by -D option.) ID3v2 tag is written if needed.

The option -u writes (updates) the fetched information to the MP3 ID3 tags. This option is assumed if tag elements are set via command-line options. (This option may be overridden by -D option.) If -2 option is also given, forces write of ID3v2 tag even if the info fits the ID3v1 tag (in addition, this option enable update of personal name fields according to values of translate_person and person_frames configuration settings).

The option -p prints a message using the next argument as format (by default \\, \t, \n are replaced by backslash, tab and newline; governed by the value of -E option); see "interpolate" in MP3::Tag for details of the format of sprintf()-like escapes. If no option -p is given, message in default format will be emitted. The value of option -e is the encoding used for the output; if the value is 1, system-specific encoding is guessed; if the value is 2 or 3, then, command line arguments are assumed to be in the guessed encoding; for value 2 the encoding of -p output is not set.

With option -D (dry run) no update is performed.

Use options

  t a l y g c n

to overwrite the information (title artist album year genre comment track-number) obtained via MP3::Tag heuristics (-u switch is implied if any one of these arguments differs from what would be found otherwise; use -D switch to disable auto-update). By default, the values of these options are not %-interpolated; this may be changed by -E option.

The option -d should contain the comma-separated list of ID3v2 frames to delete. A frame specification is the same as what might be given to "%{...}" frame interpolation command, e.g., TIT3, COMM03, COMM(fra)[short title]. In addition, if the list contains ID3v1 or ID3v2, whole tags will be deleted.

Likewise, the option -F allows setting of arbitrary ID3v2 frames: if one needs to set one frame, use the directive FRAME_spec=VALUE:

  -F TIT2=The_new_Title

If one needs to set more than one frame, separate the directives with arbitrary non-alphanumeric character repeated 3 times, and add the same character at the start:

  -F "~TIT2=The new Title~~~TXXX[TIT2-prev]=The old title"

By default, the values are %-interpolated; this can be changed by option -E.

The option -P is a very powerful generalization of what can be done by options -F, -d, and -t -a -l -y -g -c -n. The value should contain the parse recipes. They become the configuration item parse_data of MP3::Tag; eventually this information is processed by MP3::Tag::ParseData module (if the latter present in the chain of heuristics; see option -C). The option is split into [$flag, $string, @patterns] on its first non-alphanumeric character; if multiple options are needed, one should separate them by this character repeated 3 times. (See examples: EXAMPLES.)

If option -G is specified, the file names on the command line are considered as glob patterns. This may be useful if the maximal command-line length is too low. With the option -R arguments can be directories, which are searched recursively for audio (default *.mp3) files to process; use option -r to reset the regular expression to look for (the default is (?:\.mp3$)).

The option -E controls expansion of escape characters. It should contain the letters of the command-line options where \\, \n, \t are interpolated; one can append the letters of t a l y g c n F options requiring %-interpolation after the separator /i: (for -F, only the values are interpolated). The default value is p/i:Fp: only -p is \-interpolated, and only -F and -p are subject to %-interpolation.

If the option -@ is given, all characters @ in the options are replaced by %. This may be convenient if the shell treats % specially (e.g., DOSISH shells).

If option -I is given, no guessworking for artist field is performed on typeout.

The option -C sets MP3::Tag configuration data (separated by commas; the first comma can be replaced by = sign) as MP3::Tag-config()> would do. (To call config() multiple times, separate the parts by arbitrary non-alphanumeric character, and repeat this character in the start of -C option.) Note that since ParseData is used to inject the user-specified tag fields (such as -a "A. U. Thor"), usually it should be kept in the autoinfo configuration (and related fields author etc).

Extra translation

If a module Music_Translate_Fields is available, it is loaded. It may define methods translate_artist etc which would be used by MP3::Tag (via corresponding configuration settings).


Only the -P option is complicated enough to deserve comments...

For a (silly) example, one can replace -a Homer -t Iliad by

  -P mz=Homer=%a===mz=Iliad=%t

A less silly example is forcing a particular way of parsing a file name via

  -P "im=%{d0}/%f=%a/%n %t.%e"

It is broken into

 flags          string          pattern1
 "im"           "%{d0}/%f"      "%a/%n %t.%e"

The flag letters stand for interpolate, must_match. This interpolates the string "%{d0}/%f" and parses the result (which is the file name with one level of the directory part preserved) using the given pattern; thus the directory name becomes the artist, the leading numeric part - the track number, and the rest of the file name (without extension) - the title. Note that since multiple patterns are allowed, one can similarly allow for multiple formats of the names, e.g.

  -P "im=%{d0}/%f=%a/%n %t.%e=%a/%t (%y).%e"

allows for the file basename to be also of the form "TITLE (YEAR)". An alternative way to obtain the same results is

  -P "im=%{d0}=%a===im=%f=%n %t.%e=%t (%y).%e"

which corresponds to two recipies:

 flags          string          pattern1        pattern2
 "im"           "%{d0}"         "%a"
 "im"           "%f"            "%n %t.%e"      "%t (%y).%e"

Of course, one could use

 "im"           "%B"            "%n %t"         "%t (%y)"

as a replacement for the second one.

Note that it may be more readable to set artist to %{d0} by an explicit asignment, with arguments similar to

  -E "p/i:Fpa" -a "%{d0}"

(this value of -E requests %-interpolation of the option -a in addition to the default \-interpolation of -p, and %-interpolation of -F and -p).

To give more examples,

  -P "if=%D/.comment=%c"

will read comment from the file .comment in the directory of the audio file;

  -P "ifn=%D/.comment=%c"

has similar effect if the file .comment has one-line comments, one per track (this assumes the the track number can be found by other means).

Suppose that a file Parts in a directory of MP3 files has the following format: it has a preamble, then has a short paragraph of information per audio file, preceeded by the track number and dot:


   12. Rezitativ.
   (Pizarro, Rocco)

   13. Duett: jetzt, Alter, jetzt hat es Eile, (Pizarro, Rocco)


The following command puts this info into the title of the ID3 tag (provided the audio file names are informative enough so that MP3::Tag can deduce the track number):

 mp3info2 -u -C parse_split='\n(?=\d+\.)' -P 'fl;Parts;%=n. %t'

If this paragraph of information has the form TITLE (COMMENT) with the COMMENT part being optional, then use

 mp3info2 -u -C parse_split='\n(?=\d+\.)' -P 'fl;Parts;%=n. %t (%c);%=n. %t'

If you want to remove a dot or a comma got into the end of the title, use

 mp3info2 -u -C parse_split='\n(?=\d+\.)' \
   -P 'fl;Parts;%=n. %t (%c);%=n. %t;;;iR;%t;%t[.,]$'

The second pattern of this invocation is converted to

  ['iR', '%t' => '%t[.,]$']

which essentially applies the substitution s/(.*)[.,]$/$1/s to the title.

Now suppose that in addition to Parts, we have a text file Comment with additional info; we want to put this info into the comment field after what is extracted from TITLE (COMMENT); separate these two parts of the comment by an empty line:

 mp3info2 -E C -C '#parse_split=\n(?=\d+\.)#parse_join=\n\n' \
  -P 'f;Comment;%c;;;fl;Parts;%=n. %t;;;i;%t///%c;%t (%c)///%c;;;iR;%t;%t[.,]$'

This assumes that the title and the comment do not contain '///' as a substring. Explanation: the first pattern of -P,

  ['f', 'Comment' => '%c'],

reads comment from the file Comment into the comment field; the second,

  ['fl', 'Parts'  => '%=n. %t'],

reads a chunk of Parts into the title field. The third one

  ['i', '%t///%c' => '%t (%c)///%c']

rearranges the title and comment provided the title is of the form TITLE (COMMENT). (The configuration option parse_join takes care of separating two chunks of comment corresponding to two occurences of %c on the right hand side.)

Finally, the fourth pattern is the same as in the preceeding example; it removes spurious punctuation at the end of the title.

More examples: removing string "with violin" from the start of the comment field (removing comment altogether if nothing remains):

  mp3info2 -u -P 'iz;%c;with violin%c' *.mp3

setting the artist field without letting auto-update feature deduce other fields from other sources;

  mp3info2 -C autoinfo=ParseData -a "A. U. Thor" *.mp3

setting a comment field unless it it already present:

  mp3info2 -u -P 'i;%c///with piano;///%c' *.mp3

The last example shows how to actually write "programs" in the language of the -P option: the example gives a conditional assignment. With user variables (as in %{U8}) for temporaries, and a possibility to use regular expressions, one could provide arbitrary programmatic logic. Of course, at some level of complexity one should better switch to direct interfacing with MP3::Tag Perl module (use the code of this Perl script as an example!).

Here is a typical task setting "advanced" id3v2 frames: composer (TCOM), orchestra (TPE2), conductor (TPE3). We assume a directory tree which contains MP3 files tagged with the following conventions: artist is actually a composer; comment is of one of two forms:

  Performers; Orchestra; Conductor
  Orchestra; Conductor

To set the specific MP3 frames via -P rules, use

  mp3info2 -@P "mi/@a/@{TCOM}///mi/@c/@{U1}; @{TPE2}; @{TPE3}/@{TPE2}; @{TPE3}" -R .

With -F options, this can be simplified as

  mp3info2 -@F "TCOM=@a" -P "mi/@c/@{U1}; @{TPE2}; @{TPE3}/@{TPE2}; @{TPE3}" -R .

To copy ID3 tags of MP3 files in the current directory to files in directory /tmp/mp3 with the extension .tag (and print "progress report"), use

  mp3info2 -C autoinfo=ParseData,ID3v2,ID3v1 -p "@N@E\n" \
        -@P "bODi,@{ID3v2}@{ID3v1},/tmp/mp3/@N.tag" -R .

Since we did not use z flag, MP3 files without tags are skipped.

Now suppose that there are two parallel file hierarchies of audio files, and of lyrics: audio files are in audio/dir_name/audio_name.mp3 with corresponding lyrics file in text/dir_name/audio_name.mp3. To attach lyrics to MP3 files (in COMM frame with description lyrics in language eng), call

  mp3info2 -@P "fim;../text/@{d0}/@B.txt;@{COMM(eng)[lyrics]}" -Ru .

inside the directory audio. (Change fim to Ffim to ignore the audio files for which the corresponding text file does not exist.) (Of course, to follow the specifications, one should have used the field "%{USLT(eng)[]}" instead of "%{COMM(eng)[lyrics]}"). With -F option, one could set the USLT frame as

  mp3info2 -@F "USLT(eng)[]=@{I(fim)../text/@{d0}/@B.txt}" -Ru .

Finish by a very simple example: all that the pattern

  -P 'i;%t;%t'

does is removal of trailing and leading blanks from the title (which is deduced by other means).


This tool is loosely modeled on the program mp3info; it is "mostly" backward compatible, and allows a very significant superset of functionality. Known backward incompatibilities are:

  -G -h -r -d

Missing functionality:

  -f -F -i -x

Incompatible %-escapes:

  %e %E         - absolutely different semantic
  %v            - has no trailing 0s
  %q            - has fractional part
  %r            - is a number, not a word "Variable" for VBR
  %u            - is one less (in presence of descriptor frame only?)

Missing %-escapes:

  %b %G

Backslash escapes: only \\, \n, \t supported.


With -e 1, 2 or 3, this script may consult environment variables LC_CTYPE, LC_ALL, LANG to deduce the current encoding. No other environment variables are directly read by this script.

Note however, that MP3::Tag module has a rich set of defaults for encoding settings settable by environment variables; see "ENVIRONMENT" in MP3::Tag. So these variables will (indirectly) affect how this script works.


Ilya Zakharevich <cpan@ilyaz.org>.


MP3::Tag, MP3::Tag::ParseData, audio_rename