import tempfile
class FatalException(Exception):
- pass
+ 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
+ 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()
+ 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))
+ if var is not None:
+ cmd.append(opt)
+ cmd.append(str(var))
def duplicate_opts(opts):
- return optparse.Values(opts.__dict__)
+ 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)]
+ 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
+ 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 init(self):
+ self.check_command("MP4Box")
+ self.check_no_file(self.opts.output + ".mp4")
- def run(self):
- o = self.opts
- p = self.profile
+ def run(self):
+ o = self.opts
+ p = self.profile
- if o.dump:
- fps = "???"
- else:
- fps = midentify(p.video_tmp, "ID_VIDEO_FPS")
+ 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"
- ])
+ 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 init(self):
+ self.check_command("mkvmerge")
+ self.check_no_file(self.opts.output + ".mkv")
- def run(self):
- o = self.opts
- p = self.profile
+ def run(self):
+ o = self.opts
+ p = self.profile
- if o.dump:
- fps = "???"
- else:
- fps = midentify(p.video_tmp, "ID_VIDEO_FPS")
+ 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,
- ])
+ 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)
+ 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)
+ 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)
+ 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 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)
+ 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 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)
+ 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 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)
+ 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):
- codec2opts = {
- "xvid": "-xvidencopts",
- "x264": "-x264encopts",
- "faac": "-faacopts",
- "mp3lame": "-lameopts",
- }
-
- def init(self):
- o = self.opts
- p = self.profile
-
- self.check_command("mencoder")
- self.check_no_file(o.output + ".avi")
-
- p.video_tmp = o.output + ".avi"
- p.audio_tmp = o.output + ".avi"
-
- def run(self):
- o = self.opts
- p = self.profile
-
- cmd = []
- cmd += ["mencoder", o.input]
- cmd += ["-noconfig", "all"]
- insert_mplayer_options(cmd, o)
- cmd += ["-vf-add", "harddup"]
- cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
- cmd += ["-oac", p.acodec]
- if p.aopts:
- cmd += [self.codec2opts[p.acodec], p.aopts]
- cmd += self.profile.mplayeropts
- cmd += ["-o", self.opts.output + ".avi"]
-
- self.do_exec(cmd)
+ codec2opts = {
+ "xvid": "-xvidencopts",
+ "x264": "-x264encopts",
+ "faac": "-faacopts",
+ "mp3lame": "-lameopts",
+ }
+
+ def init(self):
+ o = self.opts
+ p = self.profile
+
+ self.check_command("mencoder")
+ self.check_no_file(o.output + ".avi")
+
+ p.video_tmp = o.output + ".avi"
+ p.audio_tmp = o.output + ".avi"
+
+ def run(self):
+ o = self.opts
+ p = self.profile
+
+ cmd = []
+ cmd += ["mencoder", o.input]
+ cmd += ["-noconfig", "all"]
+ insert_mplayer_options(cmd, o)
+ cmd += ["-vf-add", "harddup"]
+ cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
+ cmd += ["-oac", p.acodec]
+ if p.aopts:
+ cmd += [self.codec2opts[p.acodec], p.aopts]
+ cmd += self.profile.mplayeropts
+ cmd += ["-o", self.opts.output + ".avi"]
+
+ self.do_exec(cmd)
class MencoderDemux(Command):
- codec2exts = {
- "xvid": "m4v",
- "x264": "h264",
- "faac": "aac",
- "mp3lame": "mp3",
- "copyac3": "ac3",
- }
-
- def init(self):
- o = self.opts
- p = self.profile
-
- self.check_command("mencoder")
- p.audio_tmp = "audio." + self.codec2exts[p.acodec]
- p.video_tmp = "video." + self.codec2exts[p.vcodec]
- self.check_no_file(p.audio_tmp)
- self.check_no_file(p.video_tmp)
-
- def run(self):
- o = self.opts
- p = self.profile
-
- cmd = ["mencoder", "-ovc", "copy", "-oac", "copy", o.output + ".avi"]
- cmd += ["-noconfig", "all", "-noskip", "-mc", "0"]
- self.do_exec(cmd + ["-of", "rawaudio", "-o", p.audio_tmp])
- self.do_exec(cmd + ["-of", "rawvideo", "-o", p.video_tmp])
- self.do_exec(["rm", "-f", o.output + ".avi"])
+ codec2exts = {
+ "xvid": "m4v",
+ "x264": "h264",
+ "faac": "aac",
+ "mp3lame": "mp3",
+ "copyac3": "ac3",
+ }
+
+ def init(self):
+ o = self.opts
+ p = self.profile
+
+ self.check_command("mencoder")
+ p.audio_tmp = "audio." + self.codec2exts[p.acodec]
+ p.video_tmp = "video." + self.codec2exts[p.vcodec]
+ self.check_no_file(p.audio_tmp)
+ self.check_no_file(p.video_tmp)
+
+ def run(self):
+ o = self.opts
+ p = self.profile
+
+ cmd = ["mencoder", "-ovc", "copy", "-oac", "copy", o.output + ".avi"]
+ cmd += ["-noconfig", "all", "-noskip", "-mc", "0"]
+ self.do_exec(cmd + ["-of", "rawaudio", "-o", p.audio_tmp])
+ self.do_exec(cmd + ["-of", "rawvideo", "-o", p.video_tmp])
+ self.do_exec(["rm", "-f", o.output + ".avi"])
class Profile(object):
- def __init__(self, commands, **kwargs):
- self.commands = commands
- self.__dict__.update(kwargs)
+ def __init__(self, commands, **kwargs):
+ self.commands = commands
+ self.__dict__.update(kwargs)
- def __contains__(self, keyname):
- return hasattr(self, keyname)
+ def __contains__(self, keyname):
+ return hasattr(self, keyname)
class Wait(object):
- def __init__(self, commands):
- self.commands = commands[:]
-
- def run(self):
- for command in self.commands:
- command.wait()
+ def __init__(self, commands):
+ self.commands = commands[:]
+
+ def run(self):
+ for command in self.commands:
+ command.wait()
profiles = {
- "x264/lame" :
- Profile(
- commands=[MPlayer, X264, Lame, Wait, MKVMerge],
- mplayeropts=[],
- x264opts=["--preset", "veryslow", "--crf", "20"],
- lameopts=["--preset", "medium"],
- ),
-
- "x264/copyac3" :
- Profile(
- commands=[MPlayer, X264, Wait, MencoderCopyAC3, MKVMerge],
- mplayeropts=["-nosound"],
- x264opts=["--preset", "veryslow", "--crf", "20"],
- ),
-
- "x264/lame/fast" :
- Profile(
- commands=[MPlayer, X264, Lame, Wait, MKVMerge],
- mplayeropts=[],
- x264opts=["--preset", "fast", "--crf", "23"],
- lameopts=["--preset", "medium"],
- ),
-
- "xvid/lame" :
- Profile(
- commands=[Mencoder],
- mplayeropts=["-ffourcc", "DX50"],
- vcodec="xvid",
- vopts="fixed_quant=2:vhq=4:autoaspect",
- acodec="mp3lame",
- aopts="cbr:br=128",
- ),
-
- "apple-quicktime" :
- Profile(
- commands=[MPlayer, X264, Faac, Wait, MP4Box],
- mplayeropts=[],
- x264opts=["--crf", "20", "--bframes", "1"],
- faacopts=["-q", "100", "--mpeg-vers", "4"],
- ),
-
- "nokia-n97" :
- Profile(
- commands=[Mencoder, MencoderDemux, MP4Box],
- mplayeropts=["-vf-add", "scale=640:-10"],
- vcodec="xvid",
- vopts="bitrate=384:vhq=4:autoaspect:max_bframes=0",
- acodec="faac",
- aopts="br=64:mpeg=4:object=2",
- ),
+ "x264/lame" :
+ Profile(
+ commands=[MPlayer, X264, Lame, Wait, MKVMerge],
+ mplayeropts=[],
+ x264opts=["--preset", "veryslow", "--crf", "20"],
+ lameopts=["--preset", "medium"],
+ ),
+
+ "x264/copyac3" :
+ Profile(
+ commands=[MPlayer, X264, Wait, MencoderCopyAC3, MKVMerge],
+ mplayeropts=["-nosound"],
+ x264opts=["--preset", "veryslow", "--crf", "20"],
+ ),
+
+ "x264/lame/fast" :
+ Profile(
+ commands=[MPlayer, X264, Lame, Wait, MKVMerge],
+ mplayeropts=[],
+ x264opts=["--preset", "fast", "--crf", "23"],
+ lameopts=["--preset", "medium"],
+ ),
+
+ "xvid/lame" :
+ Profile(
+ commands=[Mencoder],
+ mplayeropts=["-ffourcc", "DX50"],
+ vcodec="xvid",
+ vopts="fixed_quant=2:vhq=4:autoaspect",
+ acodec="mp3lame",
+ aopts="cbr:br=128",
+ ),
+
+ "apple-quicktime" :
+ Profile(
+ commands=[MPlayer, X264, Faac, Wait, MP4Box],
+ mplayeropts=[],
+ x264opts=["--crf", "20", "--bframes", "1"],
+ faacopts=["-q", "100", "--mpeg-vers", "4"],
+ ),
+
+ "nokia-n97" :
+ Profile(
+ commands=[Mencoder, MencoderDemux, MP4Box],
+ mplayeropts=["-vf-add", "scale=640:-10"],
+ vcodec="xvid",
+ vopts="bitrate=384:vhq=4:autoaspect:max_bframes=0",
+ acodec="faac",
+ aopts="br=64:mpeg=4:object=2",
+ ),
}
mappings = {
- "x264": "x264/lame",
- "xvid": "xvid/lame",
+ "x264": "x264/lame",
+ "xvid": "xvid/lame",
}
for x, y in mappings.iteritems():
- profiles[x] = profiles[y]
+ profiles[x] = profiles[y]
def parse_args():
- for profile_name in profiles.keys():
- if sys.argv[0].find(profile_name) >= 0:
- break
- else:
- profile_name = "xvid/lame"
-
- parser = optparse.OptionParser(usage="%prog [options] input [output]")
- parser.add_option("--dvd", action="store", dest="dvd")
- parser.add_option("--fixmux", action="store_true", dest="fixmux")
- parser.add_option("--deinterlace", action="store_true", dest="deinterlace")
- parser.add_option("--detelecine", action="store_true", dest="detelecine")
- parser.add_option("--mc", action="store", dest="mc", type="float")
- parser.add_option("--noskip", action="store_true", dest="noskip")
- parser.add_option("--vfilters", action="store", dest="vfilters")
- parser.add_option("--afilters", action="store", dest="afilters")
- parser.add_option("--chapter", action="store", dest="chapter")
- parser.add_option("--skipkb", action="store", dest="skipkb", type="int")
- parser.add_option("--startpos", action="store", dest="startpos")
- parser.add_option("--endpos", action="store", dest="endpos")
- parser.add_option("--audioid", action="store", dest="audioid")
- parser.add_option("--subtitleid", action="store", dest="subtitleid")
- parser.add_option("--profile", action="store", dest="profile_name", default=profile_name)
- parser.add_option("--dump", action="store_true", dest="dump")
- try:
- opts, args = parser.parse_args(sys.argv[1:])
- if len(args) == 1:
- input = args[0]
- output = os.path.splitext(os.path.basename(input))[0]
- elif len(args) == 2:
- input, output = args
- else:
- raise ValueError
- except Exception:
- parser.print_usage()
- sys.exit(1)
-
- if "://" not in input:
- opts.input = os.path.abspath(input)
- else:
- if opts.dvd:
- opts.dvd = os.path.abspath(opts.dvd)
- opts.input = input
-
- opts.output = os.path.abspath(output)
- opts.mplayer_done = False
-
- return opts
+ for profile_name in profiles.keys():
+ if sys.argv[0].find(profile_name) >= 0:
+ break
+ else:
+ profile_name = "xvid/lame"
+
+ parser = optparse.OptionParser(usage="%prog [options] input [output]")
+ parser.add_option("--dvd", action="store", dest="dvd")
+ parser.add_option("--fixmux", action="store_true", dest="fixmux")
+ parser.add_option("--deinterlace", action="store_true", dest="deinterlace")
+ parser.add_option("--detelecine", action="store_true", dest="detelecine")
+ parser.add_option("--mc", action="store", dest="mc", type="float")
+ parser.add_option("--noskip", action="store_true", dest="noskip")
+ parser.add_option("--vfilters", action="store", dest="vfilters")
+ parser.add_option("--afilters", action="store", dest="afilters")
+ parser.add_option("--chapter", action="store", dest="chapter")
+ parser.add_option("--skipkb", action="store", dest="skipkb", type="int")
+ parser.add_option("--startpos", action="store", dest="startpos")
+ parser.add_option("--endpos", action="store", dest="endpos")
+ parser.add_option("--audioid", action="store", dest="audioid")
+ parser.add_option("--subtitleid", action="store", dest="subtitleid")
+ parser.add_option("--profile", action="store", dest="profile_name", default=profile_name)
+ parser.add_option("--dump", action="store_true", dest="dump")
+ try:
+ opts, args = parser.parse_args(sys.argv[1:])
+ if len(args) == 1:
+ input = args[0]
+ output = os.path.splitext(os.path.basename(input))[0]
+ elif len(args) == 2:
+ input, output = args
+ else:
+ raise ValueError
+ except Exception:
+ parser.print_usage()
+ sys.exit(1)
+
+ if "://" not in input:
+ opts.input = os.path.abspath(input)
+ else:
+ if opts.dvd:
+ opts.dvd = os.path.abspath(opts.dvd)
+ opts.input = input
+
+ opts.output = os.path.abspath(output)
+ opts.mplayer_done = False
+
+ return opts
def main():
- os.nice(1)
-
- opts = parse_args()
-
- # Find our profile
- try:
- profile = profiles[opts.profile_name]
- except KeyError:
- print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name
- sys.exit(1)
-
- # Run in a temp dir so that multiple instances can be run simultaneously
- tempdir = tempfile.mkdtemp()
- try:
- os.chdir(tempdir)
-
- try:
- commands = []
- if opts.fixmux or opts.detelecine:
- profile.commands.insert(0, MencoderLossless)
- for CommandClass in profile.commands:
- if Command in CommandClass.__bases__:
- command = CommandClass(profile, opts)
- else:
- command = CommandClass(commands)
- commands.append(command)
- for command in commands:
- command.run()
-
- except FatalException, e:
- print >>sys.stderr, "Error:", str(e)
- sys.exit(1)
-
- finally:
- os.chdir("/")
- shutil.rmtree(tempdir)
+ os.nice(1)
+
+ opts = parse_args()
+
+ # Find our profile
+ try:
+ profile = profiles[opts.profile_name]
+ except KeyError:
+ print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name
+ sys.exit(1)
+
+ # Run in a temp dir so that multiple instances can be run simultaneously
+ tempdir = tempfile.mkdtemp()
+ try:
+ os.chdir(tempdir)
+
+ try:
+ commands = []
+ if opts.fixmux or opts.detelecine:
+ profile.commands.insert(0, MencoderLossless)
+ for CommandClass in profile.commands:
+ if Command in CommandClass.__bases__:
+ command = CommandClass(profile, opts)
+ else:
+ command = CommandClass(commands)
+ commands.append(command)
+ for command in commands:
+ command.run()
+
+ except FatalException, e:
+ print >>sys.stderr, "Error:", str(e)
+ sys.exit(1)
+
+ finally:
+ os.chdir("/")
+ shutil.rmtree(tempdir)
if __name__ == "__main__":
- main()
+ main()
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
+ 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 fail(line_count, msg):
- raise Exception(msg + " on line %d" % line_count)
+ raise Exception(msg + " on line %d" % line_count)
def convert_frame_to_sample(frame):
- return frame * AUDIO_SAMPLE_RATE / VIDEO_FPS
+ return frame * AUDIO_SAMPLE_RATE / VIDEO_FPS
def run_cmd(cmd):
- print "$", " ".join(map(mkarg, cmd))
- if DRY_RUN:
- return
- print
- ret = subprocess.Popen(cmd).wait()
- if ret != 0:
- print >>sys.stderr, "Failed on command", cmd
- raise Exception("Command returned non-zero: " + str(ret))
+ print "$", " ".join(map(mkarg, cmd))
+ if DRY_RUN:
+ return
+ print
+ ret = subprocess.Popen(cmd).wait()
+ if ret != 0:
+ print >>sys.stderr, "Failed on command", cmd
+ raise Exception("Command returned non-zero: " + str(ret))
def read_file_contents(filename):
- try:
- f = open(filename)
- data = f.read().strip()
- f.close()
- return data
- except IOError:
- return None
+ try:
+ f = open(filename)
+ data = f.read().strip()
+ f.close()
+ return data
+ except IOError:
+ return None
def explode_video_to_png(source):
- image_cache_desc = os.path.join(TMP_DIR, "image_cache.txt")
- image_cache_dir = os.path.join(TMP_DIR, "image_cache")
-
- # Do nothing if the current cache is what we need
- current_cache = read_file_contents(image_cache_desc)
- if source == current_cache:
- return image_cache_dir
-
- # Remove if necessary
- if os.path.exists(image_cache_dir):
-### print "Confirm removal of image cache:", current_cache
-### ok = raw_input("(Y/n) ")
-### if ok != "Y":
-### print "Exiting..."
-### sys.exit(2)
- shutil.rmtree(image_cache_dir)
-
- cmd = [
- "mplayer",
- "-vo", "png:outdir=%s" % image_cache_dir,
- "-nosound",
- "-noconsolecontrols",
- "-noconfig", "user",
- "-benchmark",
- source,
- ]
- run_cmd(cmd)
-
- # Cache has been created, save the description
- f = open(image_cache_desc, "w")
- f.write(source)
- f.close()
-
- return image_cache_dir
+ image_cache_desc = os.path.join(TMP_DIR, "image_cache.txt")
+ image_cache_dir = os.path.join(TMP_DIR, "image_cache")
+
+ # Do nothing if the current cache is what we need
+ current_cache = read_file_contents(image_cache_desc)
+ if source == current_cache:
+ return image_cache_dir
+
+ # Remove if necessary
+ if os.path.exists(image_cache_dir):
+### print "Confirm removal of image cache:", current_cache
+### ok = raw_input("(Y/n) ")
+### if ok != "Y":
+### print "Exiting..."
+### sys.exit(2)
+ shutil.rmtree(image_cache_dir)
+
+ cmd = [
+ "mplayer",
+ "-vo", "png:outdir=%s" % image_cache_dir,
+ "-nosound",
+ "-noconsolecontrols",
+ "-noconfig", "user",
+ "-benchmark",
+ source,
+ ]
+ run_cmd(cmd)
+
+ # Cache has been created, save the description
+ f = open(image_cache_desc, "w")
+ f.write(source)
+ f.close()
+
+ return image_cache_dir
def explode_video_to_wav(source):
- audio_cache_desc = os.path.join(TMP_DIR, "audio_cache.txt")
- audio_cache_file = os.path.join(TMP_DIR, "audio_cache.wav")
-
- # Do nothing if the current cache is what we need
- if source == read_file_contents(audio_cache_desc):
- return audio_cache_file
-
- cmd = [
- "mencoder",
- "-oac", "pcm",
- "-ovc", "copy",
- "-of", "rawaudio",
- "-o", audio_cache_file + ".raw",
- source,
- ]
- run_cmd(cmd)
-
- cmd = [
- "sox",
- "-r", str(AUDIO_SAMPLE_RATE), "-b", "16", "-e", "signed-integer",
- audio_cache_file + ".raw",
- audio_cache_file,
- ]
- run_cmd(cmd)
-
- # Cache has been created, save the description
- f = open(audio_cache_desc, "w")
- f.write(source)
- f.close()
-
- return audio_cache_file
+ audio_cache_desc = os.path.join(TMP_DIR, "audio_cache.txt")
+ audio_cache_file = os.path.join(TMP_DIR, "audio_cache.wav")
+
+ # Do nothing if the current cache is what we need
+ if source == read_file_contents(audio_cache_desc):
+ return audio_cache_file
+
+ cmd = [
+ "mencoder",
+ "-oac", "pcm",
+ "-ovc", "copy",
+ "-of", "rawaudio",
+ "-o", audio_cache_file + ".raw",
+ source,
+ ]
+ run_cmd(cmd)
+
+ cmd = [
+ "sox",
+ "-r", str(AUDIO_SAMPLE_RATE), "-b", "16", "-e", "signed-integer",
+ audio_cache_file + ".raw",
+ audio_cache_file,
+ ]
+ run_cmd(cmd)
+
+ # Cache has been created, save the description
+ f = open(audio_cache_desc, "w")
+ f.write(source)
+ f.close()
+
+ return audio_cache_file
def apply_audio_effects(source, dest, crop_start, crop_end, audio_normalize):
- cmd = [
- "sox",
- source,
- dest,
- ]
- if audio_normalize:
- cmd += ["gain", "-n"]
- if crop_start and crop_end:
- c = convert_frame_to_sample
- cmd += ["trim", "%ds" % c(crop_start), "%ds" % c(crop_end - crop_start)]
- run_cmd(cmd)
+ cmd = [
+ "sox",
+ source,
+ dest,
+ ]
+ if audio_normalize:
+ cmd += ["gain", "-n"]
+ if crop_start and crop_end:
+ c = convert_frame_to_sample
+ cmd += ["trim", "%ds" % c(crop_start), "%ds" % c(crop_end - crop_start)]
+ run_cmd(cmd)
def apply_single_image_effects(source_file, dest_file, color_matrix):
- cmd = [
- "convert",
- source_file,
- "-color-matrix", color_matrix,
- dest_file,
- ]
- run_cmd(cmd)
+ cmd = [
+ "convert",
+ source_file,
+ "-color-matrix", color_matrix,
+ dest_file,
+ ]
+ run_cmd(cmd)
def apply_image_effects(source_dir, crop_start, crop_end, color_matrix):
- dest_dir = os.path.join(TMP_DIR, "image_processed")
- if os.path.exists(dest_dir):
- shutil.rmtree(dest_dir)
- os.mkdir(dest_dir)
-
- inframe = crop_start
- outframe = 0
- while inframe <= crop_end:
- source_file = os.path.join(source_dir, str(inframe+1).zfill(8) + ".png")
- dest_file = os.path.join(dest_dir, str(outframe+1).zfill(8) + ".png")
- if color_matrix:
- apply_single_image_effects(source_file, dest_file, color_matrix)
- else:
- os.link(source_file, dest_file)
- inframe += 1
- outframe += 1
-
- return dest_dir
+ dest_dir = os.path.join(TMP_DIR, "image_processed")
+ if os.path.exists(dest_dir):
+ shutil.rmtree(dest_dir)
+ os.mkdir(dest_dir)
+
+ inframe = crop_start
+ outframe = 0
+ while inframe <= crop_end:
+ source_file = os.path.join(source_dir, str(inframe+1).zfill(8) + ".png")
+ dest_file = os.path.join(dest_dir, str(outframe+1).zfill(8) + ".png")
+ if color_matrix:
+ apply_single_image_effects(source_file, dest_file, color_matrix)
+ else:
+ os.link(source_file, dest_file)
+ inframe += 1
+ outframe += 1
+
+ return dest_dir
def combine_audio_video(audio_file, image_dir, dest):
- cmd = [
- "mencoder",
- "mf://%s/*.png" % image_dir,
- "-audiofile", audio_file,
- "-force-avi-aspect", ASPECT_RATIO,
- "-vf", "harddup",
- "-af", "channels=1",
- "-ovc", "lavc",
- "-lavcopts", "vcodec=ffv1:ilme:ildct",
- "-oac", "pcm",
- "-o", dest,
- ]
- run_cmd(cmd)
+ cmd = [
+ "mencoder",
+ "mf://%s/*.png" % image_dir,
+ "-audiofile", audio_file,
+ "-force-avi-aspect", ASPECT_RATIO,
+ "-vf", "harddup",
+ "-af", "channels=1",
+ "-ovc", "lavc",
+ "-lavcopts", "vcodec=ffv1:ilme:ildct",
+ "-oac", "pcm",
+ "-o", dest,
+ ]
+ run_cmd(cmd)
class Job(object):
- def __init__(self):
- self.source = None
- self.dest = None
- self.crop_start = None
- self.crop_end = None
- self.color_matrix = None
- self.audio_normalize = True
-
- def set_source(self, arg):
- self.source = os.path.join(SOURCE_DIR, arg)
-
- def set_dest(self, arg):
- self.dest = os.path.join(DEST_DIR, arg)
- if not self.dest.endswith(".avi"):
- self.dest += ".avi"
-
- def set_crop(self, arg):
- a, b = arg.split("-")
- self.crop_start = int(a)
- self.crop_end = int(b)
-
- def set_colormatrix(self, arg):
- [float(x) for x in arg.split(" ") if x] # check it's valid
- self.color_matrix = arg
-
- def set_whitecolor(self, arg):
- arg = arg.split(" ")
- color = arg[0]
- r = 0xff / int(color[0:2], 16)
- g = 0xff / int(color[2:4], 16)
- b = 0xff / int(color[4:6], 16)
- # don't change the brightness
- avg = (r + g + b) / 3
- if (avg - 1) > 0.02:
- diff = avg - 1.0
- r -= diff
- g -= diff
- b -= diff
- if len(arg) == 2:
- brightness = float(arg[1])
- r *= brightness
- g *= brightness
- b *= brightness
- self.set_colormatrix("%.3f 0 0 0 %.3f 0 0 0 %.3f" % (r, g, b))
-
- def set_audionormalize(self, arg):
- self.audio_normalize = int(arg)
-
- def validate(self, line_count, unique):
- if self.dest in unique:
- fail(line_count, "Non-unique output file: " + self.dest)
- if self.source is None:
- fail(line_count, "Missing source")
- if self.dest is None:
- fail(line_count, "Missing dest")
- if not os.path.isfile(self.source):
- fail(line_count, "Unable to find source: " + self.source)
-
- def is_done(self):
- return os.path.isfile(self.dest)
-
- def run(self):
- image_cache_dir = explode_video_to_png(self.source)
- image_dir = apply_image_effects(image_cache_dir, self.crop_start, self.crop_end, self.color_matrix)
-
- audio_cache_file = explode_video_to_wav(self.source)
- audio_file = os.path.join(TMP_DIR, "audio_processed.wav")
- apply_audio_effects(audio_cache_file, audio_file, self.crop_start, self.crop_end, self.audio_normalize)
-
- combine_audio_video(audio_file, image_dir, self.dest+".tmp")
- os.rename(self.dest+".tmp", self.dest)
-
- def __str__(self):
- return "Job :: %s (%s)" % (self.dest, self.source)
+ def __init__(self):
+ self.source = None
+ self.dest = None
+ self.crop_start = None
+ self.crop_end = None
+ self.color_matrix = None
+ self.audio_normalize = True
+
+ def set_source(self, arg):
+ self.source = os.path.join(SOURCE_DIR, arg)
+
+ def set_dest(self, arg):
+ self.dest = os.path.join(DEST_DIR, arg)
+ if not self.dest.endswith(".avi"):
+ self.dest += ".avi"
+
+ def set_crop(self, arg):
+ a, b = arg.split("-")
+ self.crop_start = int(a)
+ self.crop_end = int(b)
+
+ def set_colormatrix(self, arg):
+ [float(x) for x in arg.split(" ") if x] # check it's valid
+ self.color_matrix = arg
+
+ def set_whitecolor(self, arg):
+ arg = arg.split(" ")
+ color = arg[0]
+ r = 0xff / int(color[0:2], 16)
+ g = 0xff / int(color[2:4], 16)
+ b = 0xff / int(color[4:6], 16)
+ # don't change the brightness
+ avg = (r + g + b) / 3
+ if (avg - 1) > 0.02:
+ diff = avg - 1.0
+ r -= diff
+ g -= diff
+ b -= diff
+ if len(arg) == 2:
+ brightness = float(arg[1])
+ r *= brightness
+ g *= brightness
+ b *= brightness
+ self.set_colormatrix("%.3f 0 0 0 %.3f 0 0 0 %.3f" % (r, g, b))
+
+ def set_audionormalize(self, arg):
+ self.audio_normalize = int(arg)
+
+ def validate(self, line_count, unique):
+ if self.dest in unique:
+ fail(line_count, "Non-unique output file: " + self.dest)
+ if self.source is None:
+ fail(line_count, "Missing source")
+ if self.dest is None:
+ fail(line_count, "Missing dest")
+ if not os.path.isfile(self.source):
+ fail(line_count, "Unable to find source: " + self.source)
+
+ def is_done(self):
+ return os.path.isfile(self.dest)
+
+ def run(self):
+ image_cache_dir = explode_video_to_png(self.source)
+ image_dir = apply_image_effects(image_cache_dir, self.crop_start, self.crop_end, self.color_matrix)
+
+ audio_cache_file = explode_video_to_wav(self.source)
+ audio_file = os.path.join(TMP_DIR, "audio_processed.wav")
+ apply_audio_effects(audio_cache_file, audio_file, self.crop_start, self.crop_end, self.audio_normalize)
+
+ combine_audio_video(audio_file, image_dir, self.dest+".tmp")
+ os.rename(self.dest+".tmp", self.dest)
+
+ def __str__(self):
+ return "Job :: %s (%s)" % (self.dest, self.source)
def main(frames):
- jobs = []
- unique = set()
-
- f = open(frames)
-
- job = None
- count = 0
-
- def append_job():
- if job is None:
- return
- job.validate(count, unique)
- if job.is_done():
- print "Skipping", job
- else:
- jobs.append(job)
-
- for line in f:
- count += 1
- line = line.strip()
- if line.startswith("#"):
- continue
- if not line:
- if job is not None:
- append_job()
- job = None
- continue
-
- if job is None:
- job = Job()
- cmd, arg = line.split(" ", 1)
- f = getattr(job, "set_"+cmd, None)
- if not f:
- fail(count, "Invalid command: " + cmd)
- try:
- f(arg)
- except Exception, e:
- fail(count, str(e))
-
- # trailing job...
- append_job()
-
- # optimise image and audio cache usage, use the current cache first if it exists
- current_image_cache = read_file_contents(os.path.join(TMP_DIR, "image_cache.txt"))
- jobs.sort(key=lambda job: (job.source if job.source != current_image_cache else "", job.dest))
- for job in jobs:
- print "\n\n\nStarted job:", job, "\n\n"
- job.run()
+ jobs = []
+ unique = set()
+
+ f = open(frames)
+
+ job = None
+ count = 0
+
+ def append_job():
+ if job is None:
+ return
+ job.validate(count, unique)
+ if job.is_done():
+ print "Skipping", job
+ else:
+ jobs.append(job)
+
+ for line in f:
+ count += 1
+ line = line.strip()
+ if line.startswith("#"):
+ continue
+ if not line:
+ if job is not None:
+ append_job()
+ job = None
+ continue
+
+ if job is None:
+ job = Job()
+ cmd, arg = line.split(" ", 1)
+ f = getattr(job, "set_"+cmd, None)
+ if not f:
+ fail(count, "Invalid command: " + cmd)
+ try:
+ f(arg)
+ except Exception, e:
+ fail(count, str(e))
+
+ # trailing job...
+ append_job()
+
+ # optimise image and audio cache usage, use the current cache first if it exists
+ current_image_cache = read_file_contents(os.path.join(TMP_DIR, "image_cache.txt"))
+ jobs.sort(key=lambda job: (job.source if job.source != current_image_cache else "", job.dest))
+ for job in jobs:
+ print "\n\n\nStarted job:", job, "\n\n"
+ job.run()
if __name__ == "__main__":
- try:
- frames = sys.argv[1]
- SOURCE_DIR = sys.argv[2]
- DEST_DIR = sys.argv[3]
- TMP_DIR = sys.argv[4]
- except IndexError:
- print >>sys.stderr, "Usage: %s frames.txt source_dir dest_dir tmp_dir" % sys.argv[0]
- sys.exit(1)
-
- main(frames)
+ try:
+ frames = sys.argv[1]
+ SOURCE_DIR = sys.argv[2]
+ DEST_DIR = sys.argv[3]
+ TMP_DIR = sys.argv[4]
+ except IndexError:
+ print >>sys.stderr, "Usage: %s frames.txt source_dir dest_dir tmp_dir" % sys.argv[0]
+ sys.exit(1)
+
+ main(frames)