]> code.delx.au - transcoding/blobdiff - encode.py
batchrun.py --jobs now starts a new jobs immediately after one finishes
[transcoding] / encode.py
index 5312e33e68931a65f77cad108dc0a24ea122400e..769baf881e0812ad60fb206cec2f21e0d4a152a3 100755 (executable)
--- a/encode.py
+++ b/encode.py
@@ -53,27 +53,28 @@ def duplicate_opts(opts):
        return optparse.Values(opts.__dict__)
 
 def insert_mplayer_options(cmd, o):
-       do_opt = partial(append_cmd, cmd)
-
-       if o.deinterlace:
-               cmd += ["-vf-add", "yadif"]
-       if o.detelecine:
-               cmd += ["-vf-add", "pullup,softskip"]
-       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("-fps", o.ifps)
        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-add", o.vfilters)
-       do_opt("-af-add", o.afilters)
+       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):
@@ -161,33 +162,28 @@ class MKVMerge(Command):
 
 
 
-class MencoderFixRemux(Command):
+class MencoderLossless(Command):
        def init(self):
                self.check_command("mencoder")
-               self.check_no_file("remux.avi")
+               self.check_no_file("lossless.avi")
 
-               orig = self.opts
-               self.opts = duplicate_opts(orig)
-               orig.input = "remux.avi"
-               orig.dvd = orig.chapter = orig.startpos = orig.endpos = None
+               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 = [
-                       "mencoder",
-                       "-o", "remux.avi",
-                       "-oac", "copy", "-ovc", "copy",
-                       "-mc", "0.1",
-                       o.input,
-               ]
-               do_opt = partial(append_cmd, cmd)
-               do_opt("-dvd-device", o.dvd)
-               do_opt("-chapter", o.chapter)
-               do_opt("-ss", o.startpos)
-               do_opt("-endpos", o.endpos)
-               self.do_exec(cmd)
-
-
+               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)
 
 
 
@@ -206,10 +202,26 @@ class MPlayer(Command):
                cmd += ["-vo", "yuv4mpeg:file=video.y4m"]
                cmd += ["-ao", "pcm:waveheader:file=audio.wav"]
                insert_mplayer_options(cmd, self.opts)
-               cmd += self.profile.extra
+               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")
@@ -273,22 +285,20 @@ class Mencoder(Command):
                p.video_tmp = o.output + ".avi"
                p.audio_tmp = o.output + ".avi"
 
-               if o.deinterlace and o.detelecine:
-                       raise FatalException("Cannot use --detelecine with --deinterlace")
-
        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.extra
+               cmd += self.profile.mplayeropts
                cmd += ["-o", self.opts.output + ".avi"]
 
                self.do_exec(cmd)
@@ -318,6 +328,7 @@ class MencoderDemux(Command):
                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"])
@@ -326,7 +337,6 @@ class MencoderDemux(Command):
 
 class Profile(object):
        def __init__(self, commands, **kwargs):
-               self.extra = []
                self.commands = commands
                self.__dict__.update(kwargs)
 
@@ -344,16 +354,25 @@ class Wait(object):
 
 
 profiles = {
-       "x264" :
+       "x264/lame" :
        Profile(
                commands=[MPlayer, X264, Lame, Wait, MKVMerge],
-               x264opts=["--preset", "veryslow", "--crf", "19"],
-               lameopts=["--preset", "extreme"],
+               mplayeropts=[],
+               x264opts=["--preset", "veryslow", "--crf", "20"],
+               lameopts=["--preset", "medium"],
        ),
 
-       "xvid" :
+       "x264/copyac3" :
+       Profile(
+               commands=[MPlayer, X264, Wait, MencoderCopyAC3, MKVMerge],
+               mplayeropts=["-nosound"],
+               x264opts=["--preset", "veryslow", "--crf", "20"],
+       ),
+
+       "xvid/lame" :
        Profile(
                commands=[Mencoder],
+               mplayeropts=["-ffourcc", "DX50"],
                vcodec="xvid",
                vopts="fixed_quant=2:vhq=4:autoaspect",
                acodec="mp3lame",
@@ -363,21 +382,29 @@ profiles = {
        "apple-quicktime" :
        Profile(
                commands=[MPlayer, X264, Faac, Wait, MP4Box],
-               x264opts=["--crf", "19", "--bframes", "1"],
+               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=256:vhq=4:autoaspect:max_bframes=0",
+               vopts="bitrate=384:vhq=4:autoaspect:max_bframes=0",
                acodec="faac",
                aopts="br=64:mpeg=4:object=2",
-               extra=["-vf-add", "scale=640:-10"],
        ),
 }
 
+mappings = {
+       "x264": "x264/lame",
+       "xvid": "xvid/lame",
+}
+for x, y in mappings.iteritems():
+       profiles[x] = profiles[y]
+
 
 
 
@@ -386,19 +413,17 @@ def parse_args():
                if sys.argv[0].find(profile_name) >= 0:
                        break
        else:
-               profile_name = "xvid"
+               profile_name = "xvid/lame"
 
        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("--fixmux", action="store_true", dest="fixmux")
        parser.add_option("--mc", action="store", dest="mc", type="int")
        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("--ifps", action="store", dest="ifps")
        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")
@@ -427,6 +452,7 @@ def parse_args():
                opts.input = input
 
        opts.output = os.path.abspath(output)
+       opts.mplayer_done = False
 
        return opts
 
@@ -449,8 +475,8 @@ def main():
 
                try:
                        commands = []
-                       if opts.fixmux:
-                               profile.commands.insert(0, MencoderFixRemux)
+                       if opts.detelecine:
+                               profile.commands.insert(0, MencoderLossless)
                        for CommandClass in profile.commands:
                                if Command in CommandClass.__bases__:
                                        command = CommandClass(profile, opts)