Bulk Tagging Music Files with Python and Mutagen
How to write Python scripts with Mutagen that embed ISRC codes, credits and cover art across hundreds of audio files without wiping what is already there.
Tagging files by hand works until the second release. After that every track ships with the same handful of fields — title, artist, album, year, ISRC, credits, artwork — and filling them one at a time is where the errors start: one wrong code, one missing cover, one folder renamed against the wrong master. A short Python script removes almost all of that, and it does not need to be clever.
The script I keep is deliberately boring: read what is already in the file, decide whether a field is empty, write only what is missing, and record what changed. Everything below follows from that order.
What actually travels inside the file
Tag data sits beside the audio, not inside it. MP3 carries an ID3 block, FLAC and Ogg carry Vorbis comments, M4A carries MP4 atoms, and WAV is the awkward case: some readers look for a RIFF INFO chunk, others look for an ID3 chunk, and some read neither. Handle each format as its own problem and let the library report what it found instead of trusting the file extension.
The ISRC deserves its own note. It is twelve characters — country, registrant, year, designation — and it identifies the recording, not the file. A re-export of the same take keeps the same code; a genuinely new recording gets a new one. Duplicate codes almost always arrive through copy-paste.
Read before you write
Open the file and print what is in it first. Mutagen's File() with easy=True gives a dict-like view across formats, and the most common failure I see is a script writing on top of a field that was already correct.
If the tag object is None, the file has no tag block yet, so add one before writing anything — otherwise the first save silently does nothing.
One file, done properly
import mutagen
audio = mutagen.File(path, easy=True)
if audio.tags is None:
audio.add_tags()
audio["title"] = title
audio.save()
Two details that bite later. Some fields are not in the default key list, so you may need to register one once before use: EasyID3.registerTextKey("isrc", "TSRC"). And Mutagen writes a newer ID3 revision by default while a few older readers still expect the older one, so check what your distributor's validator accepts rather than assuming.
Running the same pass across a folder
Wrap the single-file function in a loop over pathlib's glob, and give the script a dry-run flag that prints planned changes without saving. Collect failures with their filenames and skip them instead of crashing: a folder of two hundred files always contains at least one oddity, and you want the report rather than a traceback at file number nine.
The ledger that saves you a bad week
Append one line per change to a CSV or JSON log — timestamp, file, field, old value, new value, script version. Two things fall out of it. When a distributor rejects a track, you can see exactly what was written and when. And a uniqueness check over the ISRC column before upload catches the duplicate that a human eye misses every time.
Mistakes that quietly break a catalogue
- Rebuilding the tag block from scratch and dropping the cover art in the process.
- Minting a fresh ISRC for every export instead of every recording.
- Assuming downstream tools will read WAV tags the way your editor does.
- Renaming files in the same run that writes tags, before you have checked the values.
- Running the script twice with no log, so the second run cannot see what the first did.
Build the single-file version first, open the result in two different players, then add the loop. The order matters: a bug in the per-file function gives you one bad tag, and a bug in the loop gives you two hundred.
— Nam Hoai (Nam Hoai Dat Mo)