Python script to tag mp3 files..

Here is the script:


import os
import eyed3
from eyed3.mp3 import isMp3File

# Script to tag the mp3 files, given a folder (and possibly sub-folders)
# containing mp3 files.
# This script will take the folder name and assign it as album, file name
# and assign it as title track, and a given string as the artist and album
# artist

def updateMp3Tags(fileName, album, artist, title):
      audiofile = eyed3.load(fileName)
      audiofile.tag.artist = unicode(artist)
      audiofile.tag.album = unicode(album)
      audiofile.tag.album_artist = unicode(artist)
      audiofile.tag.title = unicode(title)
      audiofile.tag.save()

def setupMp3Tags(fileName, dirName, topDir, artist):
      trackTitle = os.path.splitext(fileName)[0]
      album = os.path.relpath(dirName, topDir).replace("\\", "_")
      print "File: " + fileName + " title: " + trackTitle
      print "ALBUM: " + album + " artist: " + artist
      updateMp3Tags(os.path.join(dirName, fileName), album, artist, trackTitle)


def tagMp3Files(topDir, artist):
      if not os.path.exists(topDir):
            print topDir + " does not exist"
            return
      
      for dirName, subDirList, fileList in os.walk(topDir):
            print "Renaming files in === " + dirName + " ==="
            # Skip the files that are not mp3
            for fileName in fileList:
                  if not isMp3File(os.path.join(dirName, fileName)):
                        print "Skipping " + fileName + " ..."
                  else:
                        setupMp3Tags(fileName, dirName, topDir, artist)

                  
tagMp3Files(r"C:\kannadaAudio", "Vidyabhushana")

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s