Automated MP3 tags updation with python

Had a wonderful song collection of Old hindi songs, in the format of “somenumber title (moviename).mp3”. Had to find way to update the ID3 tag info. Opensource to help yet again.

Installed a module named python-id3, Using yum install python-id3. Then wrote a small script in python using regular expressions, glob and the newly installed MP3 ID3 tagging module

#!/bin/env python

import os, glob, re, sys
from ID3 import *

def strip(str):
	return str.lstrip().rstrip()

os.chdir('/media/Carrington/Music/old-hindi-hit')
mp3s = glob.glob('*.mp3')

r = re.compile('([\d]+(([\s])*?[\w\s]+)\s?(\((.*?)\)?)?)\.mp3', re.I)

i = 1
for mp3 in mp3s:
	print str(i) + ": " + mp3
	gr = r.match(mp3)
	#print gr.groups()

	id3info = ID3(open(mp3, 'r+b'), mp3)
	title = ''
	album = ''
	try:
		if (len(gr.groups()) >= 4):
			print "Title: %s, Album: %s" % (strip(gr.group(2)), strip(gr.group(5)))
			title = strip(gr.group(2))
			album = strip(gr.group(5))
		else:
			print "Title: %s" % (strip(gr.group(2)))
			title = strip(gr.group(2))
		
		id3info.title = title
		id3info.album = album
		id3info.genre = 'Old Hindi'
		id3info.artist = ''
		id3info.write()
	except:
		print sys.exc_info()
	
	i = i + 1


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.