+class FatalException(Exception):
+ pass
+
+def mkarg(arg):
+ if re.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg):
+ return arg
+
+ if "'" not in arg:
+ return "'%s'" % arg
+ out = "\""
+ for c in arg:
+ if c in "\\$\"`":
+ out += "\\"
+ out += c
+ out += "\""
+ return out
+
+def midentify(source, field):
+ process = subprocess.Popen(
+ [
+ "mplayer", source,
+ "-ao", "null", "-vo", "null",
+ "-frames", "0", "-identify",
+ ],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ )
+ for line in process.stdout:
+ try:
+ key, value = line.split("=")
+ except ValueError:
+ continue
+ if key == field:
+ return value.strip()
+
+def append_cmd(cmd, opt, var):
+ if var is not None:
+ cmd.append(opt)
+ cmd.append(str(var))
+
+def duplicate_opts(opts):
+ return optparse.Values(opts.__dict__)
+
+def insert_mplayer_options(cmd, o):
+ if o.mplayer_done:
+ return
+
+ do_opt = partial(append_cmd, cmd)
+ do_opt("-mc", o.mc)
+ do_opt("-ss", o.startpos)
+ do_opt("-endpos", o.endpos)
+ do_opt("-dvd-device", o.dvd)
+ do_opt("-chapter", o.chapter)
+ do_opt("-aid", o.audioid)
+ do_opt("-sid", o.subtitleid)
+ do_opt("-vf", o.vfilters)
+ do_opt("-af", o.afilters)
+
+ if o.deinterlace:
+ cmd += ["-vf-pre", "yadif"]
+ if o.detelecine:
+ cmd += ["-vf-pre", "pullup,softskip", "-ofps", "24000/1001"]
+ if o.noskip:
+ cmd += ["-noskip"]
+ if o.skipkb:
+ cmd += ["-sb", str(o.skipkb * 1024)]
+
+
+class Command(object):
+ def __init__(self, profile, opts):
+ self.profile = profile
+ self.opts = opts
+ self.__process = None
+ self.init()
+
+ def init(self):
+ pass
+
+ def check_command(self, cmd):
+ if self.opts.dump:
+ return
+ if subprocess.Popen(["which", cmd], stdout=open("/dev/null", "w")).wait() != 0:
+ raise FatalException("Command '%s' is required" % cmd)
+
+ def check_no_file(self, path):
+ if os.path.exists(path):
+ raise FatalException("Output file '%s' exists." % path)
+
+ def do_exec(self, args, wait=True):
+ if self.opts.dump:
+ print " ".join(map(mkarg, args))
+ else:
+ self.__process = subprocess.Popen(args)
+ self.__args = args
+ if wait:
+ self.wait()
+
+ def wait(self):
+ if self.__process == None:
+ return
+ if self.__process.wait() != 0:
+ raise FatalException("Failure executing command: %s" % self.__args)
+ self.__process = None
+
+
+class MP4Box(Command):
+ def init(self):
+ self.check_command("MP4Box")
+ self.check_no_file(self.opts.output + ".mp4")
+
+ def run(self):
+ o = self.opts
+ p = self.profile
+
+ if o.dump:
+ fps = "???"
+ else:
+ fps = midentify(p.video_tmp, "ID_VIDEO_FPS")
+
+ self.do_exec([
+ "MP4Box",
+ "-fps", fps,
+ "-add", p.video_tmp,
+ "-add", p.audio_tmp,
+ o.output + ".mp4"
+ ])
+
+
+
+class MKVMerge(Command):
+ def init(self):
+ self.check_command("mkvmerge")
+ self.check_no_file(self.opts.output + ".mkv")
+
+ def run(self):
+ o = self.opts
+ p = self.profile
+
+ if o.dump:
+ fps = "???"
+ else:
+ fps = midentify(p.video_tmp, "ID_VIDEO_FPS")
+
+ self.do_exec([
+ "mkvmerge",
+ "-o", o.output + ".mkv",
+ "--default-duration", "0:%sfps"%fps,
+ p.video_tmp,
+ p.audio_tmp,
+ ])
+
+
+
+class MencoderLossless(Command):
+ def init(self):
+ self.check_command("mencoder")
+ self.check_no_file("lossless.avi")
+
+ ofut = self.opts
+ self.opts = duplicate_opts(ofut)
+ ofut.input = "lossless.avi"
+ ofut.mplayer_done = True
+
+ def run(self):
+ fifo = False
+ if fifo:
+ os.mkfifo("lossless.avi")
+ o = self.opts
+ cmd = []
+ cmd += ["mencoder", self.opts.input, "-o", "lossless.avi"]
+ cmd += ["-noconfig", "all"]
+ cmd += ["-oac", "copy", "-ovc", "lavc", "-lavcopts", "vcodec=ffv1:autoaspect"]
+ insert_mplayer_options(cmd, self.opts)
+ cmd += ["-vf-add", "harddup"]
+ self.do_exec(cmd, wait=not fifo)
+
+
+
+class MPlayer(Command):
+ def init(self):
+ self.check_command("mplayer")
+ self.check_no_file("video.y4m")
+ self.check_no_file("audio.wav")
+
+ def run(self):
+ os.mkfifo("video.y4m")
+ os.mkfifo("audio.wav")
+ cmd = []
+ cmd += ["mplayer", self.opts.input]
+ cmd += ["-benchmark", "-noconsolecontrols", "-noconfig", "all"]
+ cmd += ["-vo", "yuv4mpeg:file=video.y4m"]
+ cmd += ["-ao", "pcm:waveheader:file=audio.wav"]
+ insert_mplayer_options(cmd, self.opts)
+ cmd += self.profile.mplayeropts
+ self.do_exec(cmd, wait=False)
+
+
+class MencoderCopyAC3(Command):
+ def init(self):
+ self.check_command("mplayer")
+ self.check_no_file("audio.ac3")
+ self.profile.audio_tmp = "audio.ac3"
+
+ def run(self):
+ cmd = []
+ cmd += ["mencoder", self.opts.input]
+ cmd += ["-noconfig", "all"]
+ cmd += ["-ovc", "copy", "-oac", "copy"]
+ cmd += ["-of", "rawaudio", "-o", "audio.ac3"]
+ insert_mplayer_options(cmd, self.opts)
+ self.do_exec(cmd)
+
+
+class X264(Command):
+ def init(self):
+ self.check_command("x264")
+ self.profile.video_tmp = "video.h264"
+
+ def run(self):
+ p = self.profile
+ cmd = []
+ cmd += ["x264", "--no-progress"]
+ cmd += p.x264opts
+ cmd += ["-o", p.video_tmp]
+ cmd += ["video.y4m"]
+ self.do_exec(cmd, wait=False)
+
+
+class Lame(Command):
+ def init(self):
+ self.check_command("lame")
+ self.profile.audio_tmp = "audio.mp3"
+
+ def run(self):
+ p = self.profile
+ cmd = []
+ cmd += ["lame", "--quiet"]
+ cmd += p.lameopts
+ cmd += ["audio.wav"]
+ cmd += [p.audio_tmp]
+ self.do_exec(cmd, wait=False)
+
+
+class Faac(Command):
+ def init(self):
+ self.check_command("faac")
+ self.profile.audio_tmp = "audio.aac"
+
+ def run(self):
+ p = self.profile
+ cmd = []
+ cmd += ["faac"]
+ cmd += ["-o", p.audio_tmp]
+ cmd += p.faacopts
+ cmd += ["audio.wav"]
+ self.do_exec(cmd, wait=False)
+
+
+class Mencoder(Command):