Skip to content

Instantly share code, notes, and snippets.

@ViktorOgnev
Created October 20, 2017 13:41
Show Gist options
  • Save ViktorOgnev/6e1d0679445c607147b0c28f1647d309 to your computer and use it in GitHub Desktop.
Save ViktorOgnev/6e1d0679445c607147b0c28f1647d309 to your computer and use it in GitHub Desktop.
Edit mp3 names via mutagen
import os
import re
from mutagen import easyid3
path = '/Users/name/Downloads/Book'
def compf(s: str) -> int:
num = next(re.finditer(r"\d+_\d+", s)).group()
track, disc = [int(i) for i in num.split('_')]
order = track + disc if disc == 46 else track + disc**2
return order
def reorder_book():
l = sorted(os.listdir(path), key=compf)
for i, name in enumerate(l):
dest = os.path.join(path, f'nudge_{i}.mp3')
src = os.path.join(path, name)
print(src, '->', dest)
os.rename(src, dest)
def edit_tags():
for name in os.listdir(path):
mp3file = easyid3.EasyID3(os.path.join(path, name))
mp3file['title'] = name
mp3file.save()
if __name__ == 'main':
reorder_book()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment