]>
code.delx.au - transcoding/blob - encode.py
11 class FatalException(Exception):
14 class Command(object):
15 def __init__(self
, profile
, opts
):
16 self
.profile
= profile
19 def print_install_message(self
):
20 print >>sys
.stderr
, "Problem with command: %s", self
.name
22 print >>sys
.stderr
, "Try running:\n# aptitude install %s", self
.package
24 def check_command(self
, cmd
):
27 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
28 raise FatalException("Command '%s' is required" % cmd
)
30 def check_no_file(self
, path
):
31 if os
.path
.exists(path
):
32 raise FatalException("Output file '%s' exists." % path
)
34 def do_exec(self
, args
):
36 print "".join(map(commands
.mkarg
, args
))[1:]
38 if subprocess
.Popen(args
).wait() != 0:
39 raise FatalException("Failure executing command: %s" % args
)
42 class MP4Box(Command
):
50 self
.check_command("mencoder")
51 self
.check_command("MP4Box")
52 self
.check_no_file(self
.opts
.output
+ ".mp4")
56 video
= "video.%s" % self
.codec2exts
[p
.vcodec
]
57 audio
= "audio.%s" % self
.codec2exts
[p
.acodec
]
58 input = self
.opts
.output
+ ".avi" # From Mencoder command
59 output
= self
.opts
.output
+ ".mp4"
60 mencoder
= ["mencoder", input, "-ovc", "copy", "-oac", "copy", "-of"]
61 self
.do_exec(["rm", "-f", output
])
62 self
.do_exec(mencoder
+ ["rawvideo", "-o", video
])
63 self
.do_exec(mencoder
+ ["rawaudio", "-o", audio
])
64 self
.do_exec(["MP4Box", "-add", video
, "-add", audio
, output
])
65 self
.do_exec(["rm", "-f", video
, audio
, input])
68 class Mencoder(Command
):
71 "xvid": "-xvidencopts",
72 "x264": "-x264encopts",
74 "mp3lame": "-lameopts",
77 def insert_options(self
, cmd
):
78 def try_opt(opt
, var
):
82 if self
.opts
.deinterlace
:
83 cmd
+= ["-vf-add", "pp=ci"]
84 try_opt("-ss", self
.opts
.startpos
)
85 try_opt("-endpos", self
.opts
.endpos
)
86 try_opt("-dvd-device", self
.opts
.dvd
)
87 try_opt("-chapter", self
.opts
.chapter
)
88 try_opt("-aid", self
.opts
.audioid
)
89 try_opt("-sid", self
.opts
.subtitleid
)
90 try_opt("-vf-add", self
.opts
.vfilters
)
91 try_opt("-af", self
.opts
.afilters
)
93 def subst_values(self
, cmd
, vpass
):
95 "vbitrate": self
.opts
.vbitrate
,
96 "abitrate": self
.opts
.abitrate
,
97 "input": self
.opts
.input,
98 "output": self
.opts
.output
+ ".avi",
102 return [x
% subst
for x
in cmd
]
107 cmd
+= ["mencoder", "%(input)s", "-o", "/dev/null"]
108 self
.insert_options(cmd
)
109 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
110 cmd
+= ["-oac", "copy"]
111 cmd
+= self
.profile
.extra
+ self
.profile
.extra1
112 cmd
= self
.subst_values(cmd
, vpass
=1)
118 cmd
+= ["mencoder", "%(input)s", "-o", "%(output)s"]
119 self
.insert_options(cmd
)
120 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
121 cmd
+= ["-oac", p
.acodec
, self
.codec2opts
[p
.acodec
], p
.aopts
]
122 if self
.opts
.episode_name
:
123 cmd
+= ["-info", "name='%s'" % self
.opts
.episode_name
]
124 cmd
+= self
.profile
.extra
+ self
.profile
.extra2
125 cmd
= self
.subst_values(cmd
, vpass
=2)
129 self
.check_command("mencoder")
130 self
.check_no_file(self
.opts
.output
+ ".avi")
133 self
.do_exec(self
.pass1())
134 self
.do_exec(self
.pass2())
138 class Profile(object):
139 def __init__(self
, commands
, **kwargs
):
140 self
.default_opts
= {
147 self
.commands
= commands
148 self
.__dict
__.update(kwargs
)
150 def __contains__(self
, keyname
):
151 return hasattr(self
, keyname
)
157 commands
=[Mencoder
, MP4Box
],
159 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
161 aopts
="br=%(abitrate)d:mpeg=4:object=2",
168 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
170 aopts
="abr:br=%(abitrate)d",
171 extra2
=["-ffourcc", "DX50"],
176 commands
=[Mencoder
, MP4Box
],
178 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
180 aopts
="br=%(abitrate)d:mpeg=4:object=2",
181 extra
=["-vf-add", "scale=480:-10"],
186 commands
=[Mencoder
, MP4Box
],
188 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",
190 aopts
="br=%(abitrate)d:mpeg=4:object=2",
191 extra
=["-vf-add", "scale=480:-10"],
192 extra2
=["-channels", "2", "-srate", "48000"],
197 commands
=[Mencoder
, MP4Box
],
203 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto",
205 aopts
="br=%(abitrate)d:mpeg=4:object=2",
206 extra
=["-vf-add", "scale=320:-10"],
211 commands
=[Mencoder
, MP4Box
],
217 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
219 aopts
="br=%(abitrate)d:mpeg=4:object=2",
220 extra
=["-vf-add", "scale=640:-10"],
225 commands
=[Mencoder
, MP4Box
],
231 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vbv_maxrate=2000:vbv_bufsize=2000:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto:level_idc=20",
233 aopts
="br=%(abitrate)d:mpeg=4:object=2",
234 extra
=["-vf-add", "scale=640:-10"],
242 for profile_name
in profiles
.keys():
243 if sys
.argv
[0].find(profile_name
) >= 0:
246 profile_name
= "xvid"
248 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
249 parser
.add_option("--dvd", action
="store", dest
="dvd")
250 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
251 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
252 parser
.add_option("--afilters", action
="store", dest
="afilters")
253 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int")
254 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int")
255 parser
.add_option("--chapter", action
="store", dest
="chapter")
256 parser
.add_option("--startpos", action
="store", dest
="startpos")
257 parser
.add_option("--endpos", action
="store", dest
="endpos")
258 parser
.add_option("--audioid", action
="store", dest
="audioid")
259 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
260 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
261 parser
.add_option("--episode-name", action
="store", dest
="episode_name")
262 parser
.add_option("--dump", action
="store_true", dest
="dump")
264 opts
, args
= parser
.parse_args(sys
.argv
[1:])
267 output
= os
.path
.splitext(os
.path
.basename(input))[0]
276 if "://" not in input:
277 opts
.input = os
.path
.abspath(input)
280 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
283 opts
.output
= os
.path
.abspath(output
)
292 profile
= profiles
[opts
.profile_name
]
294 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
297 # Pull in default option values from the profile
298 for key
, value
in profile
.default_opts
.iteritems():
299 if getattr(opts
, key
) is None:
300 setattr(opts
, key
, value
)
302 # Run in a temp dir so that multiple instances can be run simultaneously
303 tempdir
= tempfile
.mkdtemp()
309 for CommandClass
in profile
.commands
:
310 command
= CommandClass(profile
, opts
)
311 commands
.append(command
)
313 for command
in commands
:
316 except FatalException
, e
:
317 print >>sys
.stderr
, "Error:", e
.message
322 shutil
.rmtree(tempdir
)
324 if __name__
== "__main__":