MP4

Read and write MPEG-4 audio files with iTunes metadata.

This module will read MPEG-4 audio information and metadata, as found in Apple’s MP4 (aka M4A, M4B, M4P) files.

There is no official specification for this format. The source code for TagLib, FAAD, and various MPEG specifications at

were all consulted.

MP4

class mutagen.mp4.MP4(filename)

Bases: mutagen.FileType

An MPEG-4 audio file, probably containing AAC.

If more than one track is present in the file, the first is used. Only audio (‘soun’) tracks will be read.

Variables:
class mutagen.mp4.MP4Tags

Bases: mutagen._util.DictProxy, mutagen.Metadata

Dictionary containing Apple iTunes metadata list key/values.

Keys are four byte identifiers, except for freeform (‘—-‘) keys. Values are usually unicode strings, but some atoms have a special structure:

Text values (multiple values per key are supported):

  • ‘\xa9nam’ – track title
  • ‘\xa9alb’ – album
  • ‘\xa9ART’ – artist
  • ‘aART’ – album artist
  • ‘\xa9wrt’ – composer
  • ‘\xa9day’ – year
  • ‘\xa9cmt’ – comment
  • ‘desc’ – description (usually used in podcasts)
  • ‘purd’ – purchase date
  • ‘\xa9grp’ – grouping
  • ‘\xa9gen’ – genre
  • ‘\xa9lyr’ – lyrics
  • ‘purl’ – podcast URL
  • ‘egid’ – podcast episode GUID
  • ‘catg’ – podcast category
  • ‘keyw’ – podcast keywords
  • ‘\xa9too’ – encoded by
  • ‘cprt’ – copyright
  • ‘soal’ – album sort order
  • ‘soaa’ – album artist sort order
  • ‘soar’ – artist sort order
  • ‘sonm’ – title sort order
  • ‘soco’ – composer sort order
  • ‘sosn’ – show sort order
  • ‘tvsh’ – show name

Boolean values:

  • ‘cpil’ – part of a compilation
  • ‘pgap’ – part of a gapless album
  • ‘pcst’ – podcast (iTunes reads this only on import)

Tuples of ints (multiple values per key are supported):

  • ‘trkn’ – track number, total tracks
  • ‘disk’ – disc number, total discs

Others:

  • ‘tmpo’ – tempo/BPM, 16 bit int
  • ‘covr’ – cover artwork, list of MP4Cover objects (which are tagged strs)
  • ‘gnre’ – ID3v1 genre. Not supported, use ‘\xa9gen’ instead.

The freeform ‘—-‘ frames use a key in the format ‘—-:mean:name’ where ‘mean’ is usually ‘com.apple.iTunes’ and ‘name’ is a unique identifier for this frame. The value is a str, but is probably text that can be decoded as UTF-8. Multiple values per key are supported.

MP4 tag data cannot exist outside of the structure of an MP4 file, so this class should not be manually instantiated.

Unknown non-text tags and tags that failed to parse will be written back as is.

delete(filename)

Remove the metadata from the given filename.

save(filename)

Save the metadata to the given filename.

class mutagen.mp4.MP4Info

MPEG-4 stream information.

Attributes:

  • bitrate – bitrate in bits per second, as an int

  • length – file length in seconds, as a float

  • channels – number of audio channels

  • sample_rate – audio sampling rate in Hz

  • bits_per_sample – bits per sample

  • codec (string):
    • if starting with "mp4a" uses an mp4a audio codec (see the codec parameter in rfc6381 for details e.g. "mp4a.40.2")
    • for everything else see a list of possible values at http://www.mp4ra.org/codecs.html

    e.g. "mp4a", "alac", "mp4a.40.2", "ac-3" etc.

  • codec_description (string):

    Name of the codec used (ALAC, AAC LC, AC-3...). Values might change in the future, use for display purposes only.

class mutagen.mp4.MP4Cover(data, imageformat=<AtomDataType.JPEG: 13>)

A cover artwork.

Attributes:

  • imageformat – format of the image (either FORMAT_JPEG or FORMAT_PNG)
class mutagen.mp4.MP4FreeForm(data, dataformat=<AtomDataType.UTF8: 1>, version=0)

A freeform value.

Attributes:

  • dataformat – format of the data (see AtomDataType)
mutagen.mp4.Open(filename)
mutagen.mp4.delete(filename)

Remove tags from a file.

EasyMP4

class mutagen.easymp4.EasyMP4(filename)

Bases: mutagen.mp4.MP4

Like MP4, but uses EasyMP4Tags for tags.

Variables:
classmethod RegisterKey(key, getter=None, setter=None, deleter=None, lister=None)

Register a new key mapping.

A key mapping is four functions, a getter, setter, deleter, and lister. The key may be either a string or a glob pattern.

The getter, deleted, and lister receive an MP4Tags instance and the requested key name. The setter also receives the desired value, which will be a list of strings.

The getter, setter, and deleter are used to implement __getitem__, __setitem__, and __delitem__.

The lister is used to implement keys(). It should return a list of keys that are actually in the MP4 instance, provided by its associated getter.

classmethod RegisterTextKey(key, atomid)

Register a text key.

If the key you need to register is a simple one-to-one mapping of MP4 atom name to EasyMP4Tags key, then you can use this function:

EasyMP4Tags.RegisterTextKey("artist", "©ART")
class mutagen.easymp4.EasyMP4Tags

Bases: mutagen._util.DictMixin, mutagen.Metadata

A file with MPEG-4 iTunes metadata.

Like Vorbis comments, EasyMP4Tags keys are case-insensitive ASCII strings, and values are a list of Unicode strings (and these lists are always of length 0 or 1).

If you need access to the full MP4 metadata feature set, you should use MP4, not EasyMP4.

classmethod RegisterFreeformKey(key, name, mean='com.apple.iTunes')

Register a text key.

If the key you need to register is a simple one-to-one mapping of MP4 freeform atom (—-) and name to EasyMP4Tags key, then you can use this function:

EasyMP4Tags.RegisterFreeformKey(
    "musicbrainz_artistid", "MusicBrainz Artist Id")
classmethod RegisterIntKey(key, atomid, min_value=0, max_value=65535)

Register a scalar integer key.

classmethod RegisterKey(key, getter=None, setter=None, deleter=None, lister=None)

Register a new key mapping.

A key mapping is four functions, a getter, setter, deleter, and lister. The key may be either a string or a glob pattern.

The getter, deleted, and lister receive an MP4Tags instance and the requested key name. The setter also receives the desired value, which will be a list of strings.

The getter, setter, and deleter are used to implement __getitem__, __setitem__, and __delitem__.

The lister is used to implement keys(). It should return a list of keys that are actually in the MP4 instance, provided by its associated getter.

classmethod RegisterTextKey(key, atomid)

Register a text key.

If the key you need to register is a simple one-to-one mapping of MP4 atom name to EasyMP4Tags key, then you can use this function:

EasyMP4Tags.RegisterTextKey("artist", "©ART")
pprint()

Print tag key=value pairs.

Table Of Contents

Previous topic

APEv2

Next topic

ASF

This Page