]> code.delx.au - transcoding/commitdiff
Massive refactoring to enable direct use of x264, lame & faac encoders
authorJames Bunton <jamesbunton@fastmail.fm>
Wed, 15 Sep 2010 14:39:52 +0000 (00:39 +1000)
committerJames Bunton <jamesbunton@fastmail.fm>
Wed, 15 Sep 2010 14:39:52 +0000 (00:39 +1000)
encode.py

index a15193074eac521d60dfd2aa847883daeb344c67..5312e33e68931a65f77cad108dc0a24ea122400e 100755 (executable)
--- a/encode.py
+++ b/encode.py
@@ -49,103 +49,128 @@ def append_cmd(cmd, opt, var):
                cmd.append(opt)
                cmd.append(str(var))
 
+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)]
+
+       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)
 
-class Command(object):
-       codec2exts = {
-               "xvid": "m4v",
-               "x264": "h264",
-               "faac": "aac",
-               "mp3lame": "mp3",
-               "copyac3": "ac3",
-       }
 
+class Command(object):
        def __init__(self, profile, opts):
                self.profile = profile
                self.opts = opts
+               self.__process = None
                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):
+       def do_exec(self, args, wait=True):
                if self.opts.dump:
                        print " ".join(map(mkarg, args))
                else:
-                       if subprocess.Popen(args).wait() != 0:
-                               raise FatalException("Failure executing command: %s" % args)
+                       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 check(self):
+       def init(self):
                self.check_command("MP4Box")
                self.check_no_file(self.opts.output + ".mp4")
 
        def run(self):
-               if self.opts.dump:
+               o = self.opts
+               p = self.profile
+
+               if o.dump:
                        fps = "???"
                else:
-                       fps = midentify(self.video_tmp, "ID_VIDEO_FPS")
+                       fps = midentify(p.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
+                       "-add", p.video_tmp,
+                       "-add", p.audio_tmp,
+                       o.output + ".mp4"
                ])
 
 
 
 class MKVMerge(Command):
-       def check(self):
+       def init(self):
                self.check_command("mkvmerge")
                self.check_no_file(self.opts.output + ".mkv")
 
        def run(self):
-               if self.opts.dump:
+               o = self.opts
+               p = self.profile
+
+               if o.dump:
                        fps = "???"
                else:
-                       fps = midentify(self.video_tmp, "ID_VIDEO_FPS")
+                       fps = midentify(p.video_tmp, "ID_VIDEO_FPS")
 
                self.do_exec([
                        "mkvmerge",
-                       "-o", self.opts.output + ".mkv",
+                       "-o", o.output + ".mkv",
                        "--default-duration", "0:%sfps"%fps,
-                       self.video_tmp,
-                       self.audio_tmp,
+                       p.video_tmp,
+                       p.audio_tmp,
                ])
 
 
 
 class MencoderFixRemux(Command):
        def init(self):
+               self.check_command("mencoder")
+               self.check_no_file("remux.avi")
+
                orig = self.opts
-               self.opts = optparse.Values(orig.__dict__)
+               self.opts = duplicate_opts(orig)
                orig.input = "remux.avi"
                orig.dvd = orig.chapter = orig.startpos = orig.endpos = None
 
-       def check(self):
-               self.check_command("mencoder")
-               self.check_no_file("remux.avi")
-
        def run(self):
                o = self.opts
                cmd = [
@@ -164,21 +189,70 @@ class MencoderFixRemux(Command):
 
 
 
-class MencoderMux(Command):
-       def check(self):
-               self.check_command("mencoder")
-               self.check_no_file(self.opts.output + ".tmp.avi")
 
+
+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):
-               self.do_exec([
-                       "mencoder",
-                       "-o", self.opts.output + ".avi",
-                       "-oac", "copy", "-ovc", "copy",
-                       "-noskip", "-mc", "0",
-                       "-audiofile", self.audio_tmp,
-                       self.video_tmp,
-               ])
+               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.extra
+               self.do_exec(cmd, wait=False)
+
+
+class X264(Command):
+       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)
+
+
+class Lame(Command):
+       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)
+
+
+class Faac(Command):
+       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)
 
 
 class Mencoder(Command):
@@ -190,171 +264,116 @@ class Mencoder(Command):
        }
 
        def init(self):
-               if self.opts.copyac3:
-                       self.profile.acodec = "copyac3"
-                       self.profile.aopts = None
-               self.check_options()
-
-       def check_options(self):
-               o = self.opts
-               if o.detelecine and o.ofps:
-                       raise FatalException("Cannot use --detelecine with --ofps")
-               if o.deinterlace and o.detelecine:
-                       raise FatalException("Cannot use --detelecine with --deinterlace")
-       
-       def insert_options(self, cmd):
                o = self.opts
-               do_opt = partial(append_cmd, cmd)
+               p = self.profile
 
-               if o.deinterlace:
-                       cmd += ["-vf-add", "yadif"]
-               if o.detelecine:
-                       o.ofps = "24000/1001"
-                       cmd += ["-vf-add", "pullup,softskip"]
-               if o.noskip:
-                       cmd += ["-noskip"]
-               if o.skipkb:
-                       cmd += ["-sb", str(o.skipkb * 1024)]
-
-               do_opt("-mc", o.mc)
-               do_opt("-fps", o.ifps)
-               do_opt("-ofps", o.ofps)
-               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)
-               cmd += ["-vf-add", "harddup"]
+               self.check_command("mencoder")
+               self.check_no_file(o.output + ".avi")
 
-       def subst_values(self, cmd, vpass):
-               subst = {
-                       "vbitrate": self.opts.vbitrate,
-                       "abitrate": self.opts.abitrate,
-                       "vpass": vpass,
-               }
+               p.video_tmp = o.output + ".avi"
+               p.audio_tmp = o.output + ".avi"
 
-               return [x % subst for x in cmd]
+               if o.deinterlace and o.detelecine:
+                       raise FatalException("Cannot use --detelecine with --deinterlace")
 
-       def passn(self, n):
+       def run(self):
+               o = self.opts
                p = self.profile
 
-               acodec = p.acodec
-               if acodec == "copyac3":
-                       acodec = "copy"
-
                cmd = []
-               cmd += ["mencoder", self.opts.input]
-               self.insert_options(cmd)
+               cmd += ["mencoder", o.input]
+               insert_mplayer_options(cmd, o)
+               cmd += ["-vf-add", "harddup"]
                cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
-               cmd += ["-oac", acodec]
+               cmd += ["-oac", p.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)
+               cmd += self.profile.extra
+               cmd += ["-o", self.opts.output + ".avi"]
 
-               return cmd
+               self.do_exec(cmd)
 
 
-       def pass1(self):
-               cmd = self.passn(1)
-               cmd += ["-o", self.audio_tmp, "-of", "rawaudio"]
-               return cmd
+class MencoderDemux(Command):
+       codec2exts = {
+               "xvid": "m4v",
+               "x264": "h264",
+               "faac": "aac",
+               "mp3lame": "mp3",
+               "copyac3": "ac3",
+       }
 
-       def pass2(self):
-               cmd = self.passn(2)
-               cmd += ["-o", self.video_tmp, "-of", "rawvideo"]
-               return cmd
+       def init(self):
+               o = self.opts
+               p = self.profile
 
-       def check(self):
                self.check_command("mencoder")
-               self.check_no_file(self.audio_tmp)
-               self.check_no_file(self.video_tmp)
-
+               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):
-               self.do_exec(self.pass1())
-               self.do_exec(self.pass2())
+               o = self.opts
+               p = self.profile
+
+               cmd = ["mencoder", "-ovc", "copy", "-oac", "copy", o.output + ".avi"]
+               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.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)
 
+class Wait(object):
+       def __init__(self, commands):
+               self.commands = commands[:]
+       
+       def run(self):
+               for command in self.commands:
+                       command.wait()
+
+
 
 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",
+               commands=[MPlayer, X264, Lame, Wait, MKVMerge],
+               x264opts=["--preset", "veryslow", "--crf", "19"],
+               lameopts=["--preset", "extreme"],
        ),
 
        "xvid" :
        Profile(
-               commands=[Mencoder, MencoderMux],
+               commands=[Mencoder],
                vcodec="xvid",
-               vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
+               vopts="fixed_quant=2:vhq=4:autoaspect",
                acodec="mp3lame",
-               aopts="abr:br=%(abitrate)d",
+               aopts="cbr:br=128",
        ),
 
        "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"],
+               commands=[MPlayer, X264, Faac, Wait, MP4Box],
+               x264opts=["--crf", "19", "--bframes", "1"],
+               faacopts=["-q", "100", "--mpeg-vers", "4"],
        ),
 
        "nokia-n97" :
        Profile(
-               commands=[Mencoder, MP4Box],
-               default_opts={
-                       "vbitrate": 256,
-                       "abitrate": 64,
-               },
+               commands=[Mencoder, MencoderDemux, MP4Box],
                vcodec="xvid",
-               vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
+               vopts="bitrate=256:vhq=4:autoaspect:max_bframes=0",
                acodec="faac",
-               aopts="br=%(abitrate)d:mpeg=4:object=2",
+               aopts="br=64:mpeg=4:object=2",
                extra=["-vf-add", "scale=640:-10"],
        ),
 }
@@ -374,16 +393,12 @@ def parse_args():
        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("--copyac3", action="store_true", dest="copyac3")
        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("--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("--skipkb", action="store", dest="skipkb", type="int")
        parser.add_option("--startpos", action="store", dest="startpos")
        parser.add_option("--endpos", action="store", dest="endpos")
@@ -403,7 +418,7 @@ def parse_args():
        except Exception:
                parser.print_usage()
                sys.exit(1)
-       
+
        if "://" not in input:
                opts.input = os.path.abspath(input)
        else:
@@ -427,11 +442,6 @@ def main():
                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)
-
        # Run in a temp dir so that multiple instances can be run simultaneously
        tempdir = tempfile.mkdtemp()
        try:
@@ -442,9 +452,11 @@ def main():
                        if opts.fixmux:
                                profile.commands.insert(0, MencoderFixRemux)
                        for CommandClass in profile.commands:
-                               command = CommandClass(profile, opts)
+                               if Command in CommandClass.__bases__:
+                                       command = CommandClass(profile, opts)
+                               else:
+                                       command = CommandClass(commands)
                                commands.append(command)
-                               command.check()
                        for command in commands:
                                command.run()