#!/usr/bin/env python
-import optparse, subprocess, sys
-
-codecs = {
-"x264":
-[
- "mencoder", "%(input)s", "-o", "%(output)s",
- "-vf", "%(filters)s",
- "-ovc", "x264", "-x264encopts", "pass=%(pass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:bframes=1:direct_pred=auto",
- "-oac", "faac", "-faacopts", "br=%(abitrate)d:mpeg=4:object=2", "-channels", "2", "-srate", "48000",
-],
-
-"xvid":
-[
- "mencoder", "%(input)s", "-o", "%(output)s",
- "-ffourcc", "DX50",
- "-vf", "%(filters)s",
- "-ovc", "xvid", "-xvidencopts", "pass=%(pass)d:bitrate=%(vbitrate)d:vhq=4",
- "-oac", "mp3lame", "-lameopts", "abr:br=%(abitrate)d",
-],
+import optparse
+import re
+import subprocess
+import sys
+import os
+import shutil
+import tempfile
+
+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()
+
+
+
+class Command(object):
+ codec2exts = {
+ "xvid": "m4v",
+ "x264": "h264",
+ "faac": "aac",
+ "mp3lame": "mp3",
+ "copyac3": "ac3",
+ }
+
+ def __init__(self, profile, opts):
+ self.profile = profile
+ self.opts = opts
+ self.init()
+ self.audio_tmp = "audio." + self.codec2exts[profile.acodec]
+ self.video_tmp = "video." + self.codec2exts[profile.vcodec]
+
+ def init(self):
+ pass
+
+ def print_install_message(self):
+ print >>sys.stderr, "Problem with command: %s", self.name
+ if self.package:
+ print >>sys.stderr, "Try running:\n# aptitude install %s", self.package
+
+ 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):
+ if self.opts.dump:
+ print " ".join(map(mkarg, args))
+ else:
+ if subprocess.Popen(args).wait() != 0:
+ raise FatalException("Failure executing command: %s" % args)
+
+
+class MP4Box(Command):
+ def check(self):
+ self.check_command("MP4Box")
+ self.check_no_file(self.opts.output + ".mp4")
+
+ def run(self):
+ if self.opts.dump:
+ fps = "???"
+ else:
+ fps = midentify(self.video_tmp, "ID_VIDEO_FPS")
+
+ output = self.opts.output + ".mp4"
+ self.do_exec([
+ "MP4Box",
+ "-fps", fps,
+ "-add", self.video_tmp,
+ "-add", self.audio_tmp,
+ output
+ ])
+
+
+
+class MKVMerge(Command):
+ def check(self):
+ self.check_command("mkvmerge")
+ self.check_no_file(self.opts.output + ".mkv")
+
+ def run(self):
+ if self.opts.dump:
+ fps = "???"
+ else:
+ fps = midentify(self.video_tmp, "ID_VIDEO_FPS")
+
+ self.do_exec([
+ "mkvmerge",
+ "-o", self.opts.output + ".mkv",
+ "--default-duration", "0:%sfps"%fps,
+ self.video_tmp,
+ self.audio_tmp,
+ ])
+
+
+
+class MencoderMux(Command):
+ def check(self):
+ self.check_command("mencoder")
+ self.check_no_file(self.opts.output + ".avi")
+
+ def run(self):
+ self.do_exec([
+ "mencoder",
+ "-o", self.opts.output + ".avi",
+ "-oac", "copy", "-ovc", "copy",
+ "-noskip", "-mc", "0",
+ "-audiofile", self.audio_tmp,
+ self.video_tmp,
+ ])
+
+
+
+class Mencoder(Command):
+ codec2opts = {
+ "xvid": "-xvidencopts",
+ "x264": "-x264encopts",
+ "faac": "-faacopts",
+ "mp3lame": "-lameopts",
+ }
+
+ def init(self):
+ if self.opts.copyac3:
+ self.profile.acodec = "copyac3"
+ self.profile.aopts = None
+
+ def insert_options(self, cmd):
+ def try_opt(opt, var):
+ if var is not None:
+ cmd.append(opt)
+ cmd.append(var)
+ if self.opts.deinterlace:
+ cmd += ["-vf-add", "pp=lb"]
+ if self.opts.detelecine:
+ self.opts.ofps = "24000/1001"
+ cmd += ["-vf-add", "pullup,softskip"]
+ if self.opts.copyac3:
+ cmd += ["-noskip", "-mc", "0"]
+ try_opt("-fps", self.opts.ifps)
+ try_opt("-ofps", self.opts.ofps)
+ try_opt("-ss", self.opts.startpos)
+ try_opt("-endpos", self.opts.endpos)
+ try_opt("-dvd-device", self.opts.dvd)
+ try_opt("-chapter", self.opts.chapter)
+ try_opt("-aid", self.opts.audioid)
+ try_opt("-sid", self.opts.subtitleid)
+ try_opt("-vf-add", self.opts.vfilters)
+ try_opt("-af-add", self.opts.afilters)
+ cmd += ["-vf-add", "harddup"]
+
+ def subst_values(self, cmd, vpass):
+ subst = {
+ "vbitrate": self.opts.vbitrate,
+ "abitrate": self.opts.abitrate,
+ "vpass": vpass,
+ }
+
+ return [x % subst for x in cmd]
+
+ def passn(self, n):
+ p = self.profile
+
+ acodec = p.acodec
+ if acodec == "copyac3":
+ acodec = "copy"
+
+ cmd = []
+ cmd += ["mencoder", self.opts.input]
+ self.insert_options(cmd)
+ cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
+ cmd += ["-oac", acodec]
+ if p.aopts:
+ cmd += [self.codec2opts[p.acodec], p.aopts]
+ cmd += self.profile.extra1 + self.profile.extra
+ cmd = self.subst_values(cmd, vpass=n)
+
+ return cmd
+
+
+ def pass1(self):
+ cmd = self.passn(1)
+ cmd += ["-o", self.audio_tmp, "-of", "rawaudio"]
+ return cmd
+
+ def pass2(self):
+ cmd = self.passn(2)
+ cmd += ["-o", self.video_tmp, "-of", "rawvideo"]
+ return cmd
+
+ def check(self):
+ self.check_command("mencoder")
+ self.check_no_file(self.audio_tmp)
+ self.check_no_file(self.video_tmp)
+
+ def run(self):
+ self.do_exec(self.pass1())
+ self.do_exec(self.pass2())
+
+
+
+class Profile(object):
+ def __init__(self, commands, **kwargs):
+ self.default_opts = {
+ "vbitrate": 1000,
+ "abitrate": 192,
+ }
+ self.extra = []
+ self.extra1 = []
+ self.extra2 = []
+ self.commands = commands
+ self.__dict__.update(kwargs)
+
+ def __contains__(self, keyname):
+ return hasattr(self, keyname)
+
+
+profiles = {
+ "x264" :
+ Profile(
+ commands=[Mencoder, MKVMerge],
+ vcodec="x264",
+ vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:subq=6:frameref=6:me=umh:partitions=all:bframes=4:b_adapt:qcomp=0.7:keyint=250",
+ acodec="mp3lame",
+ aopts="abr:br=%(abitrate)d",
+ ),
+
+ "xvid" :
+ Profile(
+ commands=[Mencoder, MencoderMux],
+ vcodec="xvid",
+ vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
+ acodec="mp3lame",
+ aopts="abr:br=%(abitrate)d",
+ ),
+
+ "apple-quicktime" :
+ Profile(
+ commands=[Mencoder, MP4Box],
+ vcodec="x264",
+ vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
+ acodec="faac",
+ aopts="br=%(abitrate)d:mpeg=4:object=2",
+ ),
+
+ "ipod-xvid" :
+ Profile(
+ commands=[Mencoder, MP4Box],
+ vcodec="xvid",
+ vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
+ acodec="faac",
+ aopts="br=%(abitrate)d:mpeg=4:object=2",
+ extra=["-vf-add", "scale=480:-10"],
+ ),
+
+ "ipod-x264" :
+ Profile(
+ commands=[Mencoder, MP4Box],
+ vcodec="x264",
+ vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vbv_maxrate=1500:vbv_bufsize=2000:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto:level_idc=30:turbo",
+ acodec="faac",
+ aopts="br=%(abitrate)d:mpeg=4:object=2",
+ extra=["-vf-add", "scale=480:-10"],
+ extra2=["-channels", "2", "-srate", "48000"],
+ ),
+
+ "nokia-n97" :
+ Profile(
+ commands=[Mencoder, MP4Box],
+ default_opts={
+ "vbitrate": 256,
+ "abitrate": 64,
+ },
+ vcodec="xvid",
+ vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
+ acodec="faac",
+ aopts="br=%(abitrate)d:mpeg=4:object=2",
+ extra=["-vf-add", "scale=640:-10"],
+ ),
}
-def parseArgs():
- for codec in codecs.keys():
- if sys.argv[0].find(codec) >= 0:
+
+
+def parse_args():
+ for profile_name in profiles.keys():
+ if sys.argv[0].find(profile_name) >= 0:
break
else:
- codec = "x264"
+ profile_name = "xvid"
- parser = optparse.OptionParser(usage="%prog [options] input output")
+ parser = optparse.OptionParser(usage="%prog [options] input [output]")
parser.add_option("--dvd", action="store", dest="dvd")
- parser.add_option("--filters", action="store", dest="filters", default="denoise3d")
- parser.add_option("--vbitrate", action="store", dest="vbitrate", type="int", default=1000)
- parser.add_option("--abitrate", action="store", dest="abitrate", type="int", default=128)
+ parser.add_option("--deinterlace", action="store_true", dest="deinterlace")
+ parser.add_option("--detelecine", action="store_true", dest="detelecine")
+ parser.add_option("--copyac3", action="store_true", dest="copyac3")
+ parser.add_option("--vfilters", action="store", dest="vfilters")
+ parser.add_option("--afilters", action="store", dest="afilters")
+ parser.add_option("--vbitrate", action="store", dest="vbitrate", type="int")
+ parser.add_option("--abitrate", action="store", dest="abitrate", type="int")
parser.add_option("--chapter", action="store", dest="chapter")
+ parser.add_option("--ifps", action="store", dest="ifps")
+ parser.add_option("--ofps", action="store", dest="ofps")
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("--codec", action="store", dest="codec", default=codec)
+ 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, (input, output) = parser.parse_args(sys.argv[1:])
- except:
+ 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)
- return opts, codec, input, output
-
-def run(args, dump):
- if dump:
- print " ".join(args)
+ if "://" not in input:
+ opts.input = os.path.abspath(input)
else:
- subprocess.Popen(args).wait()
+ if opts.dvd:
+ opts.dvd = os.path.abspath(opts.dvd)
+ opts.input = input
+
+ opts.output = os.path.abspath(output)
+
+ return opts
def main():
- opts, codec, input, output = parseArgs()
+ os.nice(1)
+
+ opts = parse_args()
+
+ # Find our profile
try:
- cmd = codecs[codec]
- except:
- print >>sys.stderr, "Codec '%s' not found!" % codec
+ profile = profiles[opts.profile_name]
+ except KeyError:
+ print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name
sys.exit(1)
+ # Pull in default option values from the profile
+ for key, value in profile.default_opts.iteritems():
+ if getattr(opts, key) is None:
+ setattr(opts, key, value)
- def insertOpt(opt, var):
- if var:
- cmd.insert(1, var)
- cmd.insert(1, opt)
- insertOpt("-ss", opts.startpos)
- insertOpt("-endpos", opts.endpos)
- insertOpt("-dvd-device", opts.dvd)
- insertOpt("-chapter", opts.chapter)
- insertOpt("-aid", opts.audioid)
-
- subst = {
- "vbitrate": opts.vbitrate,
- "abitrate": opts.abitrate,
- "filters": opts.filters,
- "input": input,
- }
+ # Run in a temp dir so that multiple instances can be run simultaneously
+ tempdir = tempfile.mkdtemp()
+ try:
+ os.chdir(tempdir)
+
+ try:
+ commands = []
+ for CommandClass in profile.commands:
+ command = CommandClass(profile, opts)
+ commands.append(command)
+ command.check()
+ for command in commands:
+ command.run()
- # Pass 1
- subst["pass"] = 1
- subst["output"] = "/dev/null"
- run([x % subst for x in cmd], opts.dump)
+ except FatalException, e:
+ print >>sys.stderr, "Error:", str(e)
+ sys.exit(1)
- # Pass 2
- subst["pass"] = 2
- subst["output"] = output
- run([x % subst for x in cmd], opts.dump)
+ finally:
+ os.chdir("/")
+ shutil.rmtree(tempdir)
if __name__ == "__main__":
main()