Skip to content

Instantly share code, notes, and snippets.

@sinewalker
Created April 26, 2017 09:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinewalker/c636025bfc4bf3cc3e9992f212a40afa to your computer and use it in GitHub Desktop.
Save sinewalker/c636025bfc4bf3cc3e9992f212a40afa to your computer and use it in GitHub Desktop.
use eyed3 to set ID3 tags on MP3 files
# I used this code within an IPython session to clean up all the missing ID3 tags from my MP3 collection.
# They had gone missing years ago when I down-sampled them to fit on an old phone, and then lost the originals.
# Fortunately I named the files themselves with the basic details (artist, date, title and so on) so it was possible to
# recover the tags... It sat on my to-do list for *years* but now I finally did it.
# I used the Python library "EyeD3" (get it?): https://pypi.python.org/pypi/eyeD3
# This requires Python 2.7, which has some interesting quirks for Unicode, a bit of a pain since I had named my MP3s
# with utf8 characters. What I've come up with *mostly* works. When it doesn't I had to resort to manually editing (using
# Clementine).
import eyed3
import os
import os.path
import glob
#In ipython: cd ~/Music/some_artist
artist=u'Some Artist'
#note the glob to select folders for tagging MP3s
# This expects music tracks to be stored within album folders, and to be named like this:
#
# Artist/YYYY - Album Name/nn - Song Title.mp3
#
# It will pick out the relevant parts of these names for use in the ID3 tags.
for folder in glob.glob("*"):
(year,album)=unicode(os.path.split(os.path.abspath(folder))[-1],"utf8").split(" - ")
print year, album
year=eyed3.core.Date(int(year))
for file in glob.glob(os.path.join(folder,"*.mp3")):
(track_num,title)=unicode(os.path.splitext(os.path.basename(file))[0],"utf8").split(" - ")
song=eyed3.load(file).tag
song.track_num=int(track_num)
song.title=title
song.album=album
song.artist=artist
song.recording_date=year
song.save()
# Sometimes my MP3s had no tags set *at all*. There's probably a way to initialise tags with eyed3, but I found that
# the easiest was to load an album with Clementine and set a tag for all the files -- e.g. the Artist tag. After that
# then the above code works.
# In some rare cases I had to do an album at a time:
#cd ~/Music/Funky_artist/1974 - Some Album That breaks above code
artist=u'Funky Artist'
year=eyed3.core.Date(1974)
album=u'Some Album That breaks above code'
for file in glob.glob("*.mp3"):
song=eyed3.load(file).tag
(track_num,title)=unicode(os.path.splitext(file)[0],"utf8").split(" - ")
song.track_num=int(track_num)
song.title=title
song.album=album
song.artist=artist
song.recording_date=year
song.save()
# And that takes care of my missing ID3 tags.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment