Skip to content

Instantly share code, notes, and snippets.

@rohitrangan
Created October 23, 2012 11:16
Show Gist options
  • Save rohitrangan/3938251 to your computer and use it in GitHub Desktop.
Save rohitrangan/3938251 to your computer and use it in GitHub Desktop.
Modifies the metadata of all mp3's in the given directory

mp3TagModifier

Requirements

  • eyeD3 v0.6.18.
  • Python 2.7
  • Mac OSX, any version of Linux. (Specific parts of the program can be used on Windows)

Instructions

  • Download mp3TagModifier.py.
  • Go to the directory containing mp3TagModifier.py and execute the command chmod u+x ./mp3TagModifier.py in the Shell.
  • For help with the command, execute "./mp3TagModifier.py --help"
#!/usr/bin/env python
# Program to modify the tags present in all the mp3 files given
# in a directory. This program assumes that the directory's name
# in which the mp3 is present is the album name of the mp3. More
# information about the program can be gleaned from the help.
import os
import eyeD3
import argparse
def getArgParser():
ver = "%(prog)s 1.0"
usage = "%(prog)s [options] dir_name"
description = "Modify mp3 tags of all files present in the given "\
"directory. The artist name is required for this to work."
parser = argparse.ArgumentParser(usage=usage, description=description)
parser.add_argument("-p", "--print-information", action="store_true",
help="To print all the information of the mp3 files in the "
"given directory.", dest="print_info")
parser.add_argument("-a", "--album", action="store",
dest="album", help="Replace ALBUM "
"with the album name.", metavar="ALBUM")
parser.add_argument("-A", "--artist", action="store",
dest="artist", help="Replace ARTIST with the "
"artist's name.", metavar="ARTIST")
parser.add_argument("-y", "--year", action="store",
type=int, dest="year", help="Replace YEAR with "
"the year.", metavar="YEAR")
parser.add_argument("-g", "--genre", action="store",
dest="genre", help="Replace GENRE with the genre "
"of the music.", metavar="GENRE")
parser.add_argument("-d", "--disc-number", action="store",
type=int, dest="disc_num", help="Replace DISCNUM "
"with the disc number.", metavar="DISCNUM")
parser.add_argument("-v", "--verbose", action="store_true",
help="Display more information.")
parser.add_argument("--version", action="version",
version=ver)
parser.add_argument("dir_name", help="The Directory in "
"which the mp3's are present.")
return parser
# Unnecessary, but has a useful function in getAllInformation.
class SubTag(eyeD3.Tag):
def __init__(self, fileName=None):
eyeD3.Tag.__init__(self, fileName)
def getAllInformation(self):
info = {"Title": self.getTitle(),
"Artist": self.getArtist(),
"Album": self.getAlbum(),
"Track Number": self.getTrackNum(),
"Disc Number": self.getDiscNum(),
"Year": self.getYear(),
"Genre": self.getGenre().getName(),}
return info
# Here, info is a dictionary in the format of getAllInformation.
def printInformation(info):
for key in info:
if key == "Disc Number" or key == "Track Number":
if info[key][0] != None and info[key][1] != None:
print "{}: {} of {}".format(key, info[key][0], info[key][1])
elif info[key][1] == None and info[key][0] != None:
print "{}: {}".format(key, info[key][0])
elif info[key][0] == None and info[key][1] == None:
print "{}: ".format(key)
else:
print "{}: {}".format(key, info[key])
# Returns a list of all mp3's in the given directory.
def listmp3Dir(dirname):
all_files = [os.path.normcase(f) for f in os.listdir(dirname)]
mp3files = [os.path.join(dirname, f) for f in all_files\
if os.path.splitext(f)[1] == ".mp3"]
return mp3files
# The following functions are self-explanatory.
def getAlbumNameFromPath(path):
return path.split('/')[-2]
def getTitleFromPath(path):
return os.path.splitext(path.split('/')[-1])[0]
# Program starts execution here.
def main():
argParser = getArgParser()
args = argParser.parse_args()
album, title, artist, year, genre = None, None, None, None, None
disc_num = None
if args.print_info:
mp3files = listmp3Dir(args.dir_name)
if len(mp3files) == 0:
print "No mp3 files present in the given directory."
exit()
for f in mp3files:
tag = SubTag()
tag.link(f)
print "\nFile: {}\n".format(f)
printInformation(tag.getAllInformation())
return
if args.album:
album = args.album
if args.artist:
artist = args.artist
if args.year:
year = args.year
if args.genre:
genre = args.genre
if args.disc_num:
disc_num = args.disc_num
mp3files = listmp3Dir(args.dir_name)
if len(mp3files) == 0:
print "No mp3 files present in the given directory."
return
for f in mp3files:
tag = SubTag()
tag.link(f)
if album != None:
tag.setAlbum(album)
tag.update()
else:
tag.setAlbum(getAlbumNameFromPath(f))
tag.update()
if artist != None:
tag.setArtist(artist)
tag.update()
if title != None:
tag.setTitle(title)
tag.update()
else:
tag.setTitle(getTitleFromPath(f))
tag.update()
if year != None:
tag.setDate(year)
tag.update()
if genre != None:
tag.setGenre(genre)
tag.update()
if disc_num != None:
tag.setDiscNum((disc_num, None))
tag.update()
if args.verbose:
print "\nNew Information for file {} : \n".format(f)
printInformation(tag.getAllInformation())
print "Metadata for all the mp3's have been updated."
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment