How to use mutagen to edit mp3 and other ID3 tags in Python

Money and Business

Python tag editing library, mutagen

The Python library mutagen can be used to edit the tags (metadata) of multimedia files such as mp3.

Mutagen is a Python module to handle audio metadata. It supports ASF, FLAC, MP4, Monkey’s Audio, MP3, Musepack, Ogg Opus, Ogg FLAC, Ogg Speex, Ogg Theora, Ogg Vorbis, True Audio, WavPack, OptimFROG, and AIFF audio files.

You can install it with pip.

$ pip install mutagen

Here is an example of editing an ID3 tag.

For more information about ID3, see the following link. The standard was originally created for mp3, but is now also applied to mp4 (m4a) and other non-mp3 files.

mutagen.easyid3

If you just want to read or write artist names, album names, track numbers, etc., it is easy to use the EasyID3 module.

from mutagen.easyid3 import EasyID3

To write a song title, do the following

path = 'example.mp3'
tags = EasyID3(path)
tags['title'] = 'new_title'
tags.save()

Only a limited number of tags can be edited to achieve a simple interface, but it is sufficient for basic use. The tags that can be edited can be seen below.
EasyID3.valid_keys.keys()

for key in EasyID3.valid_keys.keys():
    print(key)
# album
# bpm
# compilation
# composer
# copyright
# encodedby
# lyricist
# length
# media
# mood
# title
# version
# artist
# albumartist
# conductor
# arranger
# discnumber
# organization
# tracknumber
# author
# albumartistsort
# albumsort
# composersort
# artistsort
# titlesort
# isrc
# discsubtitle
# language
# genre
# date
# originaldate
# performer:*
# musicbrainz_trackid
# website
# replaygain_*_gain
# replaygain_*_peak
# musicbrainz_artistid
# musicbrainz_albumid
# musicbrainz_albumartistid
# musicbrainz_trmid
# musicip_puid
# musicip_fingerprint
# musicbrainz_albumstatus
# musicbrainz_albumtype
# releasecountry
# musicbrainz_discid
# asin
# performer
# barcode
# catalognumber
# musicbrainz_releasetrackid
# musicbrainz_releasegroupid
# musicbrainz_workid
# acoustid_fingerprint
# acoustid_id

It is useful to define a function.

The tags are written as follows. The total number of tracks (number of songs) is represented by the denominator of 'tracknumber'. The same is true for the number of discs.

def set_id3_tag(file_path, title=None, artist=None, albumartist=None, album=None, genre=None,
                track_num=None, total_track_num=None, disc_num=None, total_disc_num=None):
    tags = EasyID3(file_path)

    if title:
        tags['title'] = title
    if artist:
        tags['artist'] = artist
    if albumartist:
        tags['albumartist'] = albumartist
    if album:
        tags['album'] = album
    if genre:
        tags['genre'] = genre
    if total_track_num:
        if track_num:
            tags['tracknumber'] = '{}/{}'.format(track_num, total_track_num)
        else:
            tags['tracknumber'] = '/{}'.format(total_track_num)
    else:
        if track_num:
            tags['tracknumber'] = '{}'.format(track_num)
    if total_disc_num:
        if disc_num:
            tags['discnumber'] = '{}/{}'.format(disc_num, total_disc_num)
        else:
            tags['discnumber'] = '/{}'.format(total_disc_num)
    else:
        if track_num:
            tags['discnumber'] = '{}'.format(disc_num)

    tags.save()

The tag readout (display) is as follows.

def show_id3_tags(file_path):
    tags = EasyID3(file_path)
    print(tags.pprint())

The tags are removed as follows.

def delete_id3_tag(file_path, target_tag):
    tags = EasyID3(file_path)
    tags.pop(target_tag, None)
    tags.save()


def delete_all_id3_tag(file_path):
    tags = EasyID3(file_path)
    tags.delete()
    tags.save()

Use as follows.

set_id3_tag(path, albumartist='new_artist')
delete_id3_tag(path, 'discnumber')
show_id3_tags(path)

mutagen.id3

To edit ID3 tags directly, use the ID3 module.

from mutagen.id3 import ID3, TIT2

path = 'example.mp3'
tags = ID3(path)
print(tags.pprint())

tags.add(TIT2(encoding=3, text="new_title"))
tags.save()

To write, specify the tag ID as shown below.

  • song titles (TIT2)
  • Album name (TALB)

The tag IDs are summarized in the official documentation at the following link, but it is difficult to understand what kind of information they represent.

It may be easier to use the pprint() method to display the ID3 tags of an existing file to check for correspondence.

Copied title and URL