Next post: Image that dynamically fills container, keeping aspect ratio

Writing ID3 v2.3 mp3 metadata in Python

I've been using Mutagen to read/write audio metadata (tags for mp3, ogg, flac, m4a/mp4) in Python.

By default it writes mp3 tags using ID3 v2.4. For my purposes, though, I wanted to use 2.3, which has more widespread support (for example, at the time this article was written, Windows 8.1 won't show information in Explorer for 2.4 tags).

Mutagen does have 2.3 support but it is cumbersome, and so I wrote a wrapper with an interface that is much easier to use:

from easypythonmutagen import EasyPythonMutagen
o = EasyPythonMutagen('file.mp3', use_id3_v23=True)
o.set('title', u'title with non-ascii character: \u0107')
o.save()
I've uploaded the source, and several tests, here (released under the GPLv3).

Other features of EasyPythonMutagen
  • The same EasyPythonMutagen class works for mp3, m4a, flac formats, the calling code does not need to be concerned with differences in the internal tag names between id3 and m4a.
  • You won't need to catch exceptions in case the mp3 doesn't have an id3 tag yet, the tag will just be automatically added.
  • Provides the get_empirical_bitrate() method to get the actual bitrate as opposed to the "stated" bitrate.
  • Provides the get_audio_duration() method returning number of seconds.
  • Intentionally disallows adding unrecognized fields, whereas in Mutagen a typo like setting o['artistt'] will return success.
  • Added a few fields, like 'Composer' and 'Website' for m4a.