#!/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",
}
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]
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):
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",
),
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"]
),
}
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.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):
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()