Niraj Zade   Home  Blog  Notes  Tools 

Automatically update titles in mp3 files' metadata

Tags: everyday engineering  

I have maintained a curated collection of songs since around 15 years now. I don't trust my songs and mixes to always stay available on youtube, spotify etc.

I grade songs depending on how "fast" they make me feel from 0 to 4. And I rename the mp3 file based on that grade

Example - <grade>-<song title>

  • 1-Avoure - Aura.mp3
  • 0-Faasle.mp3
  • 2-Don't You Worry Child - Swedish House Mafia.mp3

I want this naming system to show up on all my music players.

  • I use strawberry music player on laptop. It browses songs by directory and filenames. So no issue with that.
  • I use Musicolet on the phone, and this app shows the songs by the "title" field in the mp3 metadata. Not by the filename.

So, to get proper song titles on my phone, I need to copy the mp3's filename into the mp3's metadata's title field.

There are apps like EzTag etc to do this in bulk. But you have to manually open the app, select songs, and then make it copy filenames into tags. It is still a manual step. And I don't like repeating manual work.

Solution - python script

Again, python to the rescue. As always.

Here is the script. It is dead simple.

from pathlib import Path
from mutagen.easyid3 import EasyID3
mp3_files = list(Path(".").rglob("*.mp3"))

for mp3_file in mp3_files:
    metadata = EasyID3(mp3_file)
    metadata['title']=mp3_file.stem
    print(metadata['title'])
    metadata.save()

Here is the makefile

SHELL = /bin/bash
set-tags:
    source ~/venv/mp3tagger/bin/activate && python3 main.py
create-venv:
    ( \
        mkdir -p ~/venv/;\
        python3.10 -m venv ~/venv/mp3tagger;\
        source ~/venv/mp3tagger/bin/activate;\
        pip install -r requirements.txt\
    )

A cron job automatically runs make set-tags in this directory everyday, and my mp3's titles are always set - fresh and perfect.