Skip to content

Instantly share code, notes, and snippets.

@imerr
Created August 15, 2014 23:19
Show Gist options
  • Save imerr/36c35ef71ff2b4828c82 to your computer and use it in GitHub Desktop.
Save imerr/36c35ef71ff2b4828c82 to your computer and use it in GitHub Desktop.
Python Bulk Music Metadata Script
#!/usr/bin/python
'''
Parses meta data from filenames using a simple regex
This was done for my specific case where files where like "[Gerne] - Artist - Filename (feat. Otherartist)"
You'll probably have to adjust the regex for your use case
Uses mutagen for meta-info stuff
'''
import sys
import os
import re
from mutagen.mp4 import MP4
if len(sys.argv) < 2:
print "Usage:"
print os.path.basename(sys.argv[0]), "<filename.m4a> [filename2.m4a filename3.m4a ...]"
exit()
for filename in sys.argv[1:]:
if not os.path.exists(filename):
print "Specified file (" + filename + ") does not exist"
exit()
print filename
infore = re.compile("(?:\[([^\]]+)\] - )?([^-]+) - ([^\[^\(]+)(?:\(feat. ([^)]+)\))? (?:.*)?")
info = infore.findall(os.path.basename(filename))[0]
if len(info) != 4:
print "Invalid info: ", info
exit()
meta = MP4(filename)
meta['\xa9nam'] = info[2]
meta['\xa9ART'] = info[1]
if info[3]:
meta['\xa9ART'] += " feat. "+info[3]
if info[0]:
meta['\xa9gen'] = info[0]
meta.save(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment