Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # -*- coding: utf-8 -*-
- #folder settings
- OUTPUT = "output"
- DELIMITER = "-"
- MAIN = "%(author)s " + DELIMITER + " %(album)s"
- CD = MAIN + " CD%(cd)d"
- #file name
- FILE = "%(track)02.d " + DELIMITER + " %(name)s.%(ext)s"
- #extra tags
- COMMENT = ""
- URL = ""
- from sys import argv
- from os import listdir, makedirs
- from os.path import isfile, isdir, basename, join, exists
- from shutil import copy
- from subprocess import Popen, PIPE
- from operator import or_
- from getopt import getopt, GetoptError
- import re
- #globals
- album = ""
- author = ""
- year = 0
- genre = ""
- maxcd = 0
- #reg exp
- album_re = re.compile(r"(?P<author>[^\\" + DELIMITER + r"]+)" + DELIMITER + "\s*(?P<album>.+)", re.I)
- cd_re = re.compile(r"cd\s*(?P<cd>\d+)", re.I)
- title_re = re.compile(r"(?P<title>\d+)[^"+DELIMITER+"]+" + DELIMITER + "(?P<name>.+)", re.I)
- file_ext = re.compile(r"\.(mp3|ogg)", re.I)
- #helper
- def idv3v2(path, args=[]):
- if type(args) is not list:
- args = [args]
- cmd = ["id3v2"] + args + [path]
- proc = Popen(cmd, bufsize=-1, stdout=PIPE, stderr=PIPE)
- stdout, stderr = proc.communicate()
- if stderr.strip():
- print "ERROR: %s" % stderr.strip()
- return stdout.strip()
- def hasDir(path):
- return reduce(or_, [isdir(join(path,x)) for x in listdir(path)])
- def getDirs(path):
- dirs = []
- for x in listdir(path):
- if isdir(join(path, x)):
- dirs.append(x)
- return dirs
- def print_help():
- print "id3retag v0.1"
- print "Usage: [args] <directory>"
- print "Args:"
- print "--author=, --album=, --nocd, --year=, --genre=, --force"
- print "When no args are given info will be parsed from filenames"
- exit()
- #main
- def retagFolder(path, author, album, output, cd=0):
- track = 1
- for file in listdir(path):
- if not isfile(join(path, file)): continue
- m = file_ext.search(file)
- if m:
- ext = m.group(1)
- else:
- ext = None
- if not ext:
- copy(join(path, file), join(output, file))
- continue
- data = title_re.search(file).groupdict()
- name = data["name"].strip()
- track = int(data["title"])
- filename = FILE % {"track": track, "name": name, "ext": ext}
- print "\t" + filename
- dest = join(output, filename)
- copy(join(path, file), dest)
- idv3v2(dest, "-D")
- cmd = ["-a", author, "-A", album, "-t", name, "-T", str(track)]
- if COMMENT:
- cmd += ["-c", COMMENT]
- if URL:
- cmd += ["--WXXX", URL]
- if genre is not None:
- number = None
- try:
- number = int(genre)
- except :
- pass
- if number is not None:
- tag = number
- else:
- tag = genre
- cmd += ["-g", tag]
- if year:
- cmd += ["-y", year]
- if cd:
- cmd += ["--TPOS", "%s/%s" % (cd, maxcd)]
- idv3v2(dest, cmd)
- if __name__ == "__main__":
- argv = argv[1:]
- try:
- options, args = getopt(argv, 'a:g:nfy:',
- ["author=", "album=", "nocd", "year=", "genre=", "force"])
- for option, params in options:
- if option in ("-a", "--author"):
- author = params
- elif option in ("--album",):
- album = params
- elif option in ("--year", "-y"):
- year = params
- elif option in ("--genre", "-g"):
- genre = params
- elif option in ("-n","nocd"):
- maxcd = None
- if not len(args):
- print_help()
- except GetoptError, e:
- print e
- print_help()
- path = args[0]
- folder = basename(path)
- if not album or not author:
- data = album_re.search(path).groupdict()
- if not album: album = data["album"].strip()
- if not author: author = data["author"].strip()
- print "Album:", album
- print "Author:", author
- cds = {}
- if hasDir(path):
- folders = getDirs(path)
- for cd in folders:
- m = cd_re.search(cd).groupdict()
- cds[int(m["cd"])] = cd
- else:
- cds[0] = folder
- maxcd = len(cds)
- print "CDs:", maxcd
- numbers = sorted(cds.keys())
- for cd in numbers:
- if cd:
- output = join(OUTPUT, MAIN % {"author" : author, "album": album}, CD % {"author":author,"album": album, "cd": cd})
- input = join(path, cds[cd])
- else:
- output = join(OUTPUT, MAIN % {"author" : author, "album": album})
- input = path
- if not exists(output):
- makedirs(output)
- print "Writing CD:", output
- retagFolder(input, author, album, output, cd=cd)
Add Comment
Please, Sign In to add comment