Skip to content

Instantly share code, notes, and snippets.

@zlalanne
Created July 27, 2013 21:59
Show Gist options
  • Save zlalanne/6096454 to your computer and use it in GitHub Desktop.
Save zlalanne/6096454 to your computer and use it in GitHub Desktop.
Renames an existing directory of MP3s to the format accepted by the music application Headphones
from sys import argv
from sys import exit
from mutagen.easyid3 import EasyID3
import os
import shutil
def main():
if len(argv) != 3:
print "Error: incorrect parameters"
exit(1)
script, startdir, destdir = argv
for dirpath, dirname, filenames in os.walk(startdir):
for filename in filenames:
if filename.endswith('.mp3'):
print "Parsing: " + filename
orig_file = os.path.join(dirpath, filename)
audio = EasyID3(orig_file)
date = audio['date'][0].encode("ascii")
artist = audio['artist'][0].encode("ascii")
album = audio['album'][0].encode("ascii")
year = date.split('-')[0].strip()
foldername = "%s - %s [%s]" % (artist, album, year)
newfolder = os.path.join(destdir, foldername)
if not os.path.exists(newfolder):
os.makedirs(newfolder)
shutil.move(orig_file, os.path.join(newfolder, filename))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment