X-Git-Url: https://code.delx.au/transcoding/blobdiff_plain/684581e782e309a155b79eba9abc9c8c13096777..07133ca24761d5e4c6ddebcebfa46681e827e02f:/encode.py diff --git a/encode.py b/encode.py index 0d6df19..371d27d 100755 --- a/encode.py +++ b/encode.py @@ -1,13 +1,19 @@ #!/usr/bin/env python -import commands, optparse, subprocess, sys, os +import commands +import optparse +import subprocess +import sys +import os +import tempfile +import shutil class MencoderCommand(object): codec2opts = { "lavc": "-lavcopts", "xvid": "-xvidencopts", "x264": "-x264encopts", - "faac": "-faaccopts", + "faac": "-faacopts", "mp3lame": "-lameopts", } @@ -15,26 +21,29 @@ class MencoderCommand(object): self.profile = profile self.opts = opts - def insertOptions(self, cmd): - def tryOpt(opt, var): + def insert_options(self, cmd): + def try_opt(opt, var): if var is not None: cmd.append(opt) cmd.append(var) - tryOpt("-ss", self.opts.startpos) - tryOpt("-endpos", self.opts.endpos) - tryOpt("-dvd-device", self.opts.dvd) - tryOpt("-chapter", self.opts.chapter) - tryOpt("-aid", self.opts.audioid) - tryOpt("-sid", self.opts.subtitleid) - tryOpt("-vf", self.opts.vfilters) - tryOpt("-af", self.opts.afilters) - - def substValues(self, cmd): + if self.opts.deinterlace: + cmd += ["-vf-add", "pp=ci"] + try_opt("-ss", self.opts.startpos) + try_opt("-endpos", self.opts.endpos) + try_opt("-dvd-device", self.opts.dvd) + try_opt("-chapter", self.opts.chapter) + try_opt("-aid", self.opts.audioid) + try_opt("-sid", self.opts.subtitleid) + try_opt("-vf-add", self.opts.vfilters) + try_opt("-af", self.opts.afilters) + + def subst_values(self, cmd, vpass): subst = { "vbitrate": self.opts.vbitrate, "abitrate": self.opts.abitrate, "input": self.opts.input, "output": self.opts.output, + "vpass": vpass, } return [x % subst for x in cmd] @@ -43,36 +52,40 @@ class MencoderCommand(object): p = self.profile cmd = [] cmd += ["mencoder", "%(input)s", "-o", "/dev/null"] - self.insertOptions(cmd) - cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], "pass=1:"+p.vopts] + self.insert_options(cmd) + cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts] cmd += ["-oac", "copy"] - cmd = self.substValues(cmd) + cmd = self.subst_values(cmd, vpass=1) return cmd def pass2(self): p = self.profile cmd = [] cmd += ["mencoder", "%(input)s", "-o", "%(output)s"] - self.insertOptions(cmd) - cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], "pass=2:"+p.vopts] + self.insert_options(cmd) + cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts] 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.substValues(cmd) + cmd = self.subst_values(cmd, vpass=2) return cmd class Profile(object): def __init__(self, CommandClass, **kwargs): + self.extra = [] + self.CommandClass = CommandClass self.__dict__.update(kwargs) + def __contains__(self, keyname): + return hasattr(self, keyname) profiles = { "qt7" : Profile( CommandClass=MencoderCommand, vcodec="x264", - vopts="bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto", + 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", ), @@ -81,26 +94,29 @@ profiles = { Profile( CommandClass=MencoderCommand, vcodec="xvid", - vopts="bitrate=%(vbitrate)d:vhq=4:autoaspect", + vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect", acodec="mp3lame", aopts="abr:br=%(abitrate)d", extra=["-ffourcc", "DX50"], ), + "ipodxvid" : Profile( CommandClass=MencoderCommand, vcodec="xvid", - vopts="bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0", + vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0", acodec="faac", aopts="br=%(abitrate)d:mpeg=4:object=2", ), + "ipod264" : Profile( CommandClass=MencoderCommand, vcodec="x264", - vopts="bitrate=%(vbitrate)d:me=umh:threads=auto:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto", + 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",# "-channels", "2", "-srate", "48000", + aopts="br=%(abitrate)d:mpeg=4:object=2:raw", + extra=["-of", "lavf", "-lavfopts", "format=mp4", "-channels", "2", "-srate", "48000"] ), } @@ -116,6 +132,7 @@ 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("--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) @@ -134,8 +151,14 @@ def parse_args(): parser.print_usage() sys.exit(1) - opts.input = input - opts.output = output + if "://" not in input: + opts.input = os.path.join(os.getcwd(), input) + else: + if opts.dvd: + opts.dvd = os.path.join(os.getcwd(), opts.dvd) + opts.input = input + + opts.output = os.path.join(os.getcwd(), output) return opts def run(args, dump): @@ -149,12 +172,18 @@ def main(): try: profile = profiles[opts.profile_name] except KeyError: - print >>sys.stderr, "Profile '%s' not found!" % profile_name + print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name sys.exit(1) - cmd = profile.CommandClass(profile, opts) - if run(cmd.pass1(), opts.dump) == 0 or opts.dump: - run(cmd.pass2(), opts.dump) + 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) + finally: + os.chdir("/") + shutil.rmtree(tempdir) if __name__ == "__main__": main()