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.


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'

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.

for key in EasyID3.valid_keys.keys():
# 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)
            tags['tracknumber'] = '/{}'.format(total_track_num)
        if track_num:
            tags['tracknumber'] = '{}'.format(track_num)
    if total_disc_num:
        if disc_num:
            tags['discnumber'] = '{}/{}'.format(disc_num, total_disc_num)
            tags['discnumber'] = '/{}'.format(total_disc_num)
        if track_num:
            tags['discnumber'] = '{}'.format(disc_num)

The tag readout (display) is as follows.

def show_id3_tags(file_path):
    tags = EasyID3(file_path)

The tags are removed as follows.

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

def delete_all_id3_tag(file_path):
    tags = EasyID3(file_path)

Use as follows.

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


To edit ID3 tags directly, use the ID3 module.

from mutagen.id3 import ID3, TIT2

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

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

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