Problem is, I have over 700~ mp3 files, and most of them have non-descriptive one word title. Some I knew right off the bat, but most of them I don't (I use 'random' mix option when playing them). Now, for me to listen to each one of them and categorize them to properly would've taken me more than one weekend.
And so I thought, wouldn't it be cool to write a small Python script to pull out the meta data (ie. artist, album, year, etc) and just based it from there? Yep, what a better way brush up on your non-existent programming fu than to write a small Pytho utility :-) And so here's the script I wrote, not the most elegant but it works (sorta :-))
Note: For reference, check out Dive Into Python book (chapter 5-6). That's where I got the baseline code. But now (except for stripnull function), the code hardly resemble the original one.
#!/usr/bin/python
# Author: Gene Ordanza
# Email: gene.ordanza AT gmail.com
# Description: Ugly hack on how to pull the metadata out of mp3 files that uses
# the ID3v1 TAG formatting scheme. mp3meta will also create a file called
# MetaFile.txt in your current directory.
# Usage: mp3meta<directory> <directory>
# Where:is optional. If no directory was given, mp3meta
# looks in the current directory and walks through all subdirectory.
# Note: A great reference on how to pull ID3v1 data from mp3 file can be
# found on Chap 5-6 Dive Into Python book Chap 5-6, you can also find it online
# at http://diveintopython.org/
import sys
import os
def stripnulls(data):
string = data.replace('\00','').strip()
return string[:23]
def writeMetatoFile(mp3ObjectList):
filename = 'MetaFile.txt'
line_width = 80
field_data = '%-5s%-24s%-24s%-24s%-4s\n'
f1, f2, f3, f4, f5 = ('\nNo.', 'Title', 'Artist', 'Album', 'Year\n')
if os.path.exists(filename):
file = open(filename, 'a')
else:
file = open(filename, 'w')
file.write('%s%s' % (' '*31, 'MP3 File Metadata\n'))
file.write('=' * line_width)
file.write(field_data % (f1, f2, f3, f4, f5))
file.write('-' * line_width)
file.write('\n')
for mp3file in mp3ObjectList:
file.write(field_data % (mp3file['count'], mp3file['Title'],\
mp3file['Artist'], mp3file['Album'], mp3file['Year']))
file.close()
class MP3Meta(dict):
count = 0
def __init__(self, name):
self['name'] = name
self.__class__.count += 1
self['count'] = MP3Meta.count
metaData = { 'Title' : (3, 33, stripnulls),
'Artist': (33, 63, stripnulls),
'Album' : (63, 93, stripnulls),
'Year' : (93, 97, stripnulls),
'Comment': (97, 126, stripnulls),
'Genre' : (127, 128, ord)}
def usage():
sys.stderr.write("""
Usage: mp3meta [directory]
Where [directory] is optional. If you did not specify one, it will
start at your current directory and traverse all subdirectory.
""")
def getMetaData(listofMP3):
mp3ObjectList = []
for filename in listofMP3:
mp3file = MP3Meta(filename)
try:
metafile = open(filename, 'rb', 0)
metafile.seek(-128, 2)
mp3data = metafile.read(128)
if mp3data[:3] == 'TAG':
for tag, (start, end, cleanup) in mp3file.metaData.items():
mp3file[tag] = cleanup(mp3data[start:end])
mp3ObjectList.append(mp3file)
else:
(null, title) = os.path.split(filename)
(truncated, null) = os.path.splitext(title)
mp3file['Title'] = truncated[:23]
mp3file['Artist'] = ''
mp3file['Album'] = '** Non-ID3v1.0 Format **'
mp3file['Year'] = ''
mp3file['Comment'] = ''
mp3file['Genre'] = ''
mp3ObjectList.append(mp3file)
metafile.close()
except IOError: pass
writeMetatoFile(mp3ObjectList)
def findMP3(dummy, dirname, fnames):
extension = '.mp3'
mp3List = []
for fname in fnames:
if fname.endswith(extension):
path = os.path.join(dirname, fname)
mp3List.append(path)
getMetaData(mp3List)
def main():
if len(sys.argv) == 1:
directory = os.path.abspath('.')
os.path.walk(directory, findMP3, None)
if len(sys.argv) == 2:
directory = sys.argv[1]
if os.path.isdir(directory):
os.path.walk(directory, findMP3, None)
else:
usage()
if __name__ == '__main__':
main()

No comments:
Post a Comment