Skip to content

Instantly share code, notes, and snippets.

@reikop
Created December 29, 2017 11:11
Show Gist options
  • Save reikop/cd42bdc23adffa342b905ddfda824545 to your computer and use it in GitHub Desktop.
Save reikop/cd42bdc23adffa342b905ddfda824545 to your computer and use it in GitHub Desktop.
Rename MP3 files based on ID3 tag
#!/usr/bin/env python2
import taglib
import os
import os.path
filenames = []
for root, dirs, files in os.walk("."):
def fullpath(x):
return os.path.join(root, x)
def mp3(x):
return x.endswith(".mp3")
filenames.extend(map(fullpath, filter(mp3, files)))
for filename in filenames:
song = taglib.File(filename)
def t(k):
if k.upper() in song.tags:
v = song.tags[k.upper()][0]
if "NUMBER" in k.upper() and "/" in v:
v = map(int, v.split("/"))
v = "".join(map(lambda x: "%02d" % x, v))
return v
else:
return None
newname = u" - ".join(filter(lambda x: x is not None, map(t, ["artist", "album", "discnumber", "tracknumber", "title"])))
os.rename(filename, newname + ".mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment