]> code.delx.au - transcoding/blobdiff - encode.py
Added detelecine option
[transcoding] / encode.py
index 1bc6c3a3ebf4c6f1b525c2bc4a8edc7af796c249..31b765fe72a1652b6529a61af3199065b340c341 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
@@ -26,10 +40,14 @@ class Command(object):
                        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(commands.mkarg, args))[1:]
+                       print " ".join(map(mkarg, args))
                else:
                        if subprocess.Popen(args).wait() != 0:
                                raise FatalException("Failure executing command: %s" % args)
@@ -45,6 +63,7 @@ class MP4Box(Command):
        def check(self):
                self.check_command("mencoder")
                self.check_command("MP4Box")
+               self.check_no_file(self.opts.output + ".mp4")
 
        def run(self):
                p = self.profile
@@ -60,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",
@@ -75,7 +108,12 @@ class Mencoder(Command):
                                cmd.append(opt)
                                cmd.append(var)
                if self.opts.deinterlace:
-                       cmd += ["-vf-add", "pp=ci"]
+                       cmd += ["-vf-add", "pp=lb"]
+               if self.opts.detelecine:
+                       self.opts.ofps = "24000/1001"
+                       cmd += ["-vf-add", "pullup,softskip"]
+               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)
@@ -122,6 +160,7 @@ class Mencoder(Command):
 
        def check(self):
                self.check_command("mencoder")
+               self.check_no_file(self.opts.output + ".avi")
 
        def run(self):
                self.do_exec(self.pass1())
@@ -155,6 +194,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],
@@ -199,6 +247,34 @@ profiles = {
                aopts="br=%(abitrate)d:mpeg=4:object=2",
                extra=["-vf-add", "scale=320:-10"],
        ),
+
+       "n97xvid" :
+       Profile(
+               commands=[Mencoder, MP4Box],
+               default_opts={
+                       "vbitrate": 1000,
+                       "abitrate": 96,
+               },
+               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"],
+       ),
+
+       "n97x264" :
+       Profile(
+               commands=[Mencoder, MP4Box],
+               default_opts={
+                       "vbitrate": 1000,
+                       "abitrate": 96,
+               },
+               vcodec="x264",
+               vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vbv_maxrate=2000:vbv_bufsize=2000:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto:level_idc=20",
+               acodec="faac",
+               aopts="br=%(abitrate)d:mpeg=4:object=2",
+               extra=["-vf-add", "scale=640:-10"],
+       ),
 }
 
 
@@ -214,11 +290,14 @@ def parse_args():
        parser = optparse.OptionParser(usage="%prog [options] input [output]")
        parser.add_option("--dvd", action="store", dest="dvd")
        parser.add_option("--deinterlace", action="store_true", dest="deinterlace")
+       parser.add_option("--detelecine", action="store_true", dest="detelecine")
        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")
@@ -280,7 +359,7 @@ def main():
                                command.run()
 
                except FatalException, e:
-                       print >>sys.stderr, "Error:", e.message
+                       print >>sys.stderr, "Error:", str(e)
                        sys.exit(1)
 
        finally: