]> code.delx.au - transcoding/blobdiff - encode.py
Better quoting for --dump
[transcoding] / encode.py
index 04ebfe834b674386c26daf3bba7799ebf7be1e8f..11e8ebfd12f873ab4f42cd50d68f0205098ad4ef 100755 (executable)
--- a/encode.py
+++ b/encode.py
@@ -1,16 +1,30 @@
 #!/usr/bin/env python
 
-import commands
 import optparse
+import re
 import subprocess
 import sys
 import os
-import tempfile
 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
+
 class Command(object):
        def __init__(self, profile, opts):
                self.profile = profile
@@ -33,7 +47,7 @@ class Command(object):
 
        def do_exec(self, args):
                if self.opts.dump:
-                       print "".join(map(commands.mkarg, args))[1:]
+                       print " ".join(map(mkarg, args))
                else:
                        if subprocess.Popen(args).wait() != 0:
                                raise FatalException("Failure executing command: %s" % args)
@@ -65,6 +79,20 @@ class MP4Box(Command):
                self.do_exec(["rm", "-f", video, audio, input])
 
 
+
+class MKVMerge(Command):
+       def check(self):
+               self.check_command("mkvmerge")
+               self.check_no_file(self.opts.output + ".mkv")
+
+       def run(self):
+               input = self.opts.output + ".avi" # From Mencoder command
+               output = self.opts.output + ".mkv"
+               self.do_exec(["mkvmerge", "-o", output, input])
+               self.do_exec(["rm", "-f", input])
+
+
+
 class Mencoder(Command):
        codec2opts = {
                "lavc": "-lavcopts",
@@ -80,7 +108,7 @@ class Mencoder(Command):
                                cmd.append(opt)
                                cmd.append(var)
                if self.opts.deinterlace:
-                       cmd += ["-vf-add", "pp=ci"]
+                       cmd += ["-vf-add", "pp=lb"]
                try_opt("-ss", self.opts.startpos)
                try_opt("-endpos", self.opts.endpos)
                try_opt("-dvd-device", self.opts.dvd)
@@ -161,6 +189,15 @@ profiles = {
                aopts="br=%(abitrate)d:mpeg=4:object=2",
        ),
 
+       "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],