#!/usr/bin/env python # Copyright (c) 2009 Andreas Balogh # See LICENSE for details. """ create MeGUI jobs for Panasonic MPG files """ # system imports import logging import sys import os import getopt import re import xml.dom.minidom as xdm import xml.dom # local imports # constants # globals LOG = logging.getLogger() logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname).3s %(process)d:%(thread)d %(message)s", datefmt="%H:%M:%S") # definitions class Usage(Exception): pass class Error(Exception): pass def main(argv = [__name__]): try: # check for parameters LOG.info("starting '%s %s'", argv[0], " ".join(argv[1:])) script_name = os.path.basename(argv[0]) try: opts, args = getopt.getopt(argv[1:], "ht:j:z:", \ ["help", "template", "job_dir", "temp_dir"]) except getopt.error, err: raise Usage(err) LOG.debug("opts: %s, args: %s", opts, args) template = "job_divx_template.xml" job_dir = "C:\\Program Files\\megui\\jobs" temp_dir = "e:\\video" for o, a in opts: if o in ("-h", "--help"): usage(script_name) return 0 elif o in ("-t", "--template"): template = a elif o in ("-j", "--job_dir"): job_dir = a elif o in ("-z", "--temp_dir"): temp_dir = a src_dir = "D:\\Documents\\Raw\\Panasonic SDR-S7\\Video" dest_dir = "D:\\Documents\\My Videos\\Balogh" if len(args) == 2: src_dir = args[0] dest_dir = args[1] elif len(args) == 1: src_dir = args[0] elif len(args) > 2: raise Usage("too many arguments") # call method with appropriate arguments if src_dir and not os.path.exists(src_dir): raise Error("Source directory not found [%s], aborting" % (src_dir, )) if dest_dir and not os.path.exists(dest_dir): LOG.warn("Destination directory not found [%s]", dest_dir) LOG.info("Creating destination directory [%s]", dest_dir) os.makedirs(dest_dir) cli(src_dir, dest_dir, template, job_dir, temp_dir) LOG.info("Done.") return 0 except Error, err: LOG.error(err) return 1 except Usage, err: LOG.error(err) LOG.info("for usage use -h or --help") return 2 def cli(src_dir, dest_dir, template, job_dir, tmp_dir): """ command line interface """ if not os.path.exists(dest_dir): LOG.error("Destination directory not found [%s]", dest_dir) raise Error() for mpg_fn in os.listdir(src_dir): opts = { } root, ext = os.path.splitext(mpg_fn) # skip files which are not mpg video if ext.lower() != ".mpg": continue # determine file paths mpg_fp = os.path.join(src_dir, mpg_fn) avi_fp = os.path.join(dest_dir, root + ".avi") d2v_fp = os.path.join(tmp_dir, root + ".d2v") opts["mpg_input"] = mpg_fp opts["final_output"] = avi_fp opts["d2v_output"] = d2v_fp # if avi_fn older than mpg or doesn't exist # then create job xml if not os.path.exists(avi_fp): create_megui_job(template, job_dir, opts) def append_megui_joblist(jobname): joblist = "c:\\program files\\megui\\joblists.xml" # load the xml inf = open(joblist,"r") dom = xdm.parse(inf) inf.close() # add job220 to mainJobList JobListSerializer = dom.getElementsByTagName("JobListSerializer")[0] mainJobList = JobListSerializer.getElementsByTagName("mainJobList")[0] string = dom.createElement("string") text = dom.createTextNode(jobname) string.appendChild(text) mainJobList.appendChild(string) newline = dom.createTextNode("\n ") mainJobList.appendChild(newline) # save xml outf = open(joblist,"w") dom.writexml(outf) outf.close() def create_megui_job(template, job_dir, opts): # replace in template: mpg_input, d2v_output, final_output, jobnum jobnum = get_next_jobnumber(job_dir) LOG.info("creating job %i...", jobnum) jobfn = "job%i.xml" % (jobnum, ) jobname = "job%i" % (jobnum, ) opts["jobname"] = jobname tplf = open(template, "r") jobf = open(os.path.join(job_dir, jobfn), "w") try: for line in tplf: # line = raw_line.rstrip("\n\r") for k, v in opts.items(): kd = "".join(("$", k, "$")) line = line.replace(kd, v) jobf.write(line) finally: jobf.close() tplf.close() append_megui_joblist(jobname) JOB_REO = re.compile("job(\d+)\.xml") def get_next_jobnumber(job_dir): max_jn = 0 for job_fn in os.listdir(job_dir): job_mo = JOB_REO.match(job_fn) if job_mo: jn = int(job_mo.group(1)) if jn > max_jn: max_jn = jn return max_jn + 1 def usage(script_name): print print "usage: %s [-t template] [-j job_dir] [-z temp_dir] [src_dir [dst_dir]]" % (script_name,) print """ src_dir source directory of original MPG files dest_dir destination directory for compressed video options: -h, --help show this help message and exit -t, --template job template file -j, --job_dir megui job directory -z, --temp_dir working directory for megui """ if __name__ == "__main__": sys.exit(main(sys.argv))