From 36c120d0965bfa76300c108116f1df385bfe35e2 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Sat, 28 Feb 2009 22:39:33 +1100 Subject: [PATCH] MP4Box support in encode.py, profiles specify bitrate + more below * Ability to chain commands in a profile * Added MP4Box command * Removed all uses of mencoder's lavf, and replaced with MP4Box * Profiles can specify default bitrates as well as resolutions * Made output parameter optional, it can be intelligently inferred --- encode.py | 165 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 132 insertions(+), 33 deletions(-) diff --git a/encode.py b/encode.py index 371d27d..f99230a 100755 --- a/encode.py +++ b/encode.py @@ -8,7 +8,59 @@ import os import tempfile import shutil -class MencoderCommand(object): +class FatalException(Exception): + pass + +class Command(object): + def __init__(self, profile, opts): + self.profile = profile + self.opts = opts + + 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 do_exec(self, args): + if self.opts.dump: + print "".join(map(commands.mkarg, args))[1:] + else: + if subprocess.Popen(args).wait() != 0: + raise FatalException("Failure executing command: %s" % args) + + +class MP4Box(Command): + codec2exts = { + "xvid": "m4v", + "x264": "h264", + "faac": "aac", + } + + def check(self): + self.check_command("mencoder") + self.check_command("MP4Box") + + def run(self): + p = self.profile + video = "video.%s" % self.codec2exts[p.vcodec] + audio = "audio.%s" % self.codec2exts[p.acodec] + input = self.opts.output + ".avi" # From Mencoder command + output = self.opts.output + ".mp4" + mencoder = ["mencoder", input, "-ovc", "copy", "-oac", "copy", "-of"] + self.do_exec(["rm", "-f", output]) + self.do_exec(mencoder + ["rawvideo", "-o", video]) + self.do_exec(mencoder + ["rawaudio", "-o", audio]) + self.do_exec(["MP4Box", "-add", video, "-add", audio, output]) + self.do_exec(["rm", "-f", video, audio, input]) + + +class Mencoder(Command): codec2opts = { "lavc": "-lavcopts", "xvid": "-xvidencopts", @@ -17,10 +69,6 @@ class MencoderCommand(object): "mp3lame": "-lameopts", } - def __init__(self, profile, opts): - self.profile = profile - self.opts = opts - def insert_options(self, cmd): def try_opt(opt, var): if var is not None: @@ -42,7 +90,7 @@ class MencoderCommand(object): "vbitrate": self.opts.vbitrate, "abitrate": self.opts.abitrate, "input": self.opts.input, - "output": self.opts.output, + "output": self.opts.output + ".avi", "vpass": vpass, } @@ -55,6 +103,7 @@ class MencoderCommand(object): self.insert_options(cmd) cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts] cmd += ["-oac", "copy"] + cmd += self.profile.extra + self.profile.extra1 cmd = self.subst_values(cmd, vpass=1) return cmd @@ -67,23 +116,39 @@ class MencoderCommand(object): cmd += ["-oac", p.acodec, self.codec2opts[p.acodec], p.aopts] if self.opts.episode_name: cmd += ["-info", "name='%s'" % self.opts.episode_name] - cmd += self.profile.extra + cmd += self.profile.extra + self.profile.extra2 cmd = self.subst_values(cmd, vpass=2) return cmd + def check(self): + self.check_command("mencoder") + + def run(self): + self.do_exec(self.pass1()) + self.do_exec(self.pass2()) + + + class Profile(object): - def __init__(self, CommandClass, **kwargs): + def __init__(self, commands, **kwargs): + self.default_opts = { + "vbitrate": 1000, + "abitrate": 192, + } self.extra = [] - - self.CommandClass = CommandClass + self.extra1 = [] + self.extra2 = [] + self.commands = commands self.__dict__.update(kwargs) + def __contains__(self, keyname): return hasattr(self, keyname) + profiles = { "qt7" : Profile( - CommandClass=MencoderCommand, + 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", @@ -92,31 +157,46 @@ profiles = { "xvid" : Profile( - CommandClass=MencoderCommand, + commands=[Mencoder], vcodec="xvid", vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect", acodec="mp3lame", aopts="abr:br=%(abitrate)d", - extra=["-ffourcc", "DX50"], + extra2=["-ffourcc", "DX50"], ), "ipodxvid" : Profile( - CommandClass=MencoderCommand, + 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"], ), - "ipod264" : + "ipodx264" : Profile( - CommandClass=MencoderCommand, + 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:global_header:turbo", acodec="faac", aopts="br=%(abitrate)d:mpeg=4:object=2:raw", - extra=["-of", "lavf", "-lavfopts", "format=mp4", "-channels", "2", "-srate", "48000"] + extra2=["-channels", "2", "-srate", "48000"] + ), + + "nokiax264" : + Profile( + commands=[Mencoder, MP4Box], + default_opts={ + "vbitrate": 256, + "abitrate": 96, + }, + vcodec="x264", + vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto:global_header", + acodec="faac", + aopts="br=%(abitrate)d:mpeg=4:object=2:raw", + extra=["-vf-add", "scale=320:-10"], ), } @@ -130,13 +210,13 @@ def parse_args(): else: 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("--deinterlace", action="store_true", dest="deinterlace") 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", default=1000) - parser.add_option("--abitrate", action="store", dest="abitrate", type="int", default=192) + 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("--startpos", action="store", dest="startpos") parser.add_option("--endpos", action="store", dest="endpos") @@ -146,41 +226,60 @@ def parse_args(): parser.add_option("--episode-name", action="store", dest="episode_name") parser.add_option("--dump", action="store_true", dest="dump") try: - opts, (input, output) = parser.parse_args(sys.argv[1:]) + 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 except Exception: parser.print_usage() sys.exit(1) if "://" not in input: - opts.input = os.path.join(os.getcwd(), input) + opts.input = os.path.abspath(input) else: if opts.dvd: - opts.dvd = os.path.join(os.getcwd(), opts.dvd) + opts.dvd = os.path.abspath(opts.dvd) opts.input = input - opts.output = os.path.join(os.getcwd(), output) - return opts + opts.output = os.path.abspath(output) -def run(args, dump): - if dump: - print "".join(map(commands.mkarg, args))[1:] - else: - return subprocess.Popen(args).wait() + return opts def main(): opts = parse_args() + + # Find our profile try: 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) + + # Run in a temp dir so that multiple instances can be run simultaneously tempdir = tempfile.mkdtemp() try: os.chdir(tempdir) - cmd = profile.CommandClass(profile, opts) - if run(cmd.pass1(), opts.dump) == 0 or opts.dump: - run(cmd.pass2(), opts.dump) + + try: + commands = [] + for CommandClass in profile.commands: + command = CommandClass(profile, opts) + commands.append(command) + command.check() + for command in commands: + command.run() + + except FatalException, e: + print >>sys.stderr, "Error:", e.message + sys.exit(1) + finally: os.chdir("/") shutil.rmtree(tempdir) -- 2.39.2