]>
code.delx.au - transcoding/blob - encode.py
11 class FatalException(Exception):
15 if re
.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg
):
28 def midentify(source
, field
):
29 process
= subprocess
.Popen(
32 "-ao", "null", "-vo", "null",
33 "-frames", "0", "-identify",
35 stdout
=subprocess
.PIPE
,
36 stderr
=subprocess
.PIPE
,
38 for line
in process
.stdout
:
40 key
, value
= line
.split("=")
48 class Command(object):
57 def __init__(self
, profile
, opts
):
58 self
.profile
= profile
61 self
.audio_tmp
= "audio." + self
.codec2exts
[profile
.acodec
]
62 self
.video_tmp
= "video." + self
.codec2exts
[profile
.vcodec
]
67 def print_install_message(self
):
68 print >>sys
.stderr
, "Problem with command: %s", self
.name
70 print >>sys
.stderr
, "Try running:\n# aptitude install %s", self
.package
72 def check_command(self
, cmd
):
75 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
76 raise FatalException("Command '%s' is required" % cmd
)
78 def check_no_file(self
, path
):
79 if os
.path
.exists(path
):
80 raise FatalException("Output file '%s' exists." % path
)
82 def do_exec(self
, args
):
84 print " ".join(map(mkarg
, args
))
86 if subprocess
.Popen(args
).wait() != 0:
87 raise FatalException("Failure executing command: %s" % args
)
90 class MP4Box(Command
):
92 self
.check_command("MP4Box")
93 self
.check_no_file(self
.opts
.output
+ ".mp4")
99 fps
= midentify(self
.video_tmp
, "ID_VIDEO_FPS")
101 output
= self
.opts
.output
+ ".mp4"
105 "-add", self
.video_tmp
,
106 "-add", self
.audio_tmp
,
112 class MKVMerge(Command
):
114 self
.check_command("mkvmerge")
115 self
.check_no_file(self
.opts
.output
+ ".mkv")
121 fps
= midentify(self
.video_tmp
, "ID_VIDEO_FPS")
125 "-o", self
.opts
.output
+ ".mkv",
126 "--default-duration", "0:%sfps"%fps
,
133 class MencoderMux(Command
):
135 self
.check_command("mencoder")
136 self
.check_no_file(self
.opts
.output
+ ".avi")
141 "-o", self
.opts
.output
+ ".avi",
142 "-oac", "copy", "-ovc", "copy",
143 "-noskip", "-mc", "0",
144 "-audiofile", self
.audio_tmp
,
150 class Mencoder(Command
):
152 "xvid": "-xvidencopts",
153 "x264": "-x264encopts",
155 "mp3lame": "-lameopts",
159 if self
.opts
.copyac3
:
160 self
.profile
.acodec
= "copyac3"
161 self
.profile
.aopts
= None
164 def check_options(self
):
166 if o
.detelecine
and o
.ofps
:
167 raise FatalException("Cannot use --detelecine with --ofps")
168 if o
.deinterlace
and o
.detelecine
:
169 raise FatalException("Cannot use --detelecine with --deinterlace")
171 def insert_options(self
, cmd
):
173 def do_opt(opt
, var
):
181 cmd
+= ["-vf-add", "yadif"]
183 o
.ofps
= "24000/1001"
184 cmd
+= ["-vf-add", "pullup,softskip"]
188 cmd
+= ["-sb", str(o
.skipkb
* 1024)]
191 do_opt("-fps", o
.ifps
)
192 do_opt("-ofps", o
.ofps
)
193 do_opt("-ss", o
.startpos
)
194 do_opt("-endpos", o
.endpos
)
195 do_opt("-dvd-device", o
.dvd
)
196 do_opt("-chapter", o
.chapter
)
197 do_opt("-aid", o
.audioid
)
198 do_opt("-sid", o
.subtitleid
)
199 do_opt("-vf-add", o
.vfilters
)
200 do_opt("-af-add", o
.afilters
)
201 cmd
+= ["-vf-add", "harddup"]
203 def subst_values(self
, cmd
, vpass
):
205 "vbitrate": self
.opts
.vbitrate
,
206 "abitrate": self
.opts
.abitrate
,
210 return [x
% subst
for x
in cmd
]
216 if acodec
== "copyac3":
220 cmd
+= ["mencoder", self
.opts
.input]
221 self
.insert_options(cmd
)
222 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
223 cmd
+= ["-oac", acodec
]
225 cmd
+= [self
.codec2opts
[p
.acodec
], p
.aopts
]
226 cmd
+= self
.profile
.extra1
+ self
.profile
.extra
227 cmd
= self
.subst_values(cmd
, vpass
=n
)
234 cmd
+= ["-o", self
.audio_tmp
, "-of", "rawaudio"]
239 cmd
+= ["-o", self
.video_tmp
, "-of", "rawvideo"]
243 self
.check_command("mencoder")
244 self
.check_no_file(self
.audio_tmp
)
245 self
.check_no_file(self
.video_tmp
)
248 self
.do_exec(self
.pass1())
249 self
.do_exec(self
.pass2())
253 class Profile(object):
254 def __init__(self
, commands
, **kwargs
):
255 self
.default_opts
= {
262 self
.commands
= commands
263 self
.__dict
__.update(kwargs
)
265 def __contains__(self
, keyname
):
266 return hasattr(self
, keyname
)
272 commands
=[Mencoder
, MKVMerge
],
274 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:subq=6:frameref=6:me=umh:partitions=all:bframes=4:b_adapt:qcomp=0.7:keyint=250",
276 aopts
="abr:br=%(abitrate)d",
281 commands
=[Mencoder
, MencoderMux
],
283 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
285 aopts
="abr:br=%(abitrate)d",
290 commands
=[Mencoder
, MP4Box
],
292 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
294 aopts
="br=%(abitrate)d:mpeg=4:object=2",
299 commands
=[Mencoder
, MP4Box
],
301 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
303 aopts
="br=%(abitrate)d:mpeg=4:object=2",
304 extra
=["-vf-add", "scale=480:-10"],
309 commands
=[Mencoder
, MP4Box
],
311 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",
313 aopts
="br=%(abitrate)d:mpeg=4:object=2",
314 extra
=["-vf-add", "scale=480:-10"],
315 extra2
=["-channels", "2", "-srate", "48000"],
320 commands
=[Mencoder
, MP4Box
],
326 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
328 aopts
="br=%(abitrate)d:mpeg=4:object=2",
329 extra
=["-vf-add", "scale=640:-10"],
337 for profile_name
in profiles
.keys():
338 if sys
.argv
[0].find(profile_name
) >= 0:
341 profile_name
= "xvid"
343 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
344 parser
.add_option("--dvd", action
="store", dest
="dvd")
345 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
346 parser
.add_option("--detelecine", action
="store_true", dest
="detelecine")
347 parser
.add_option("--copyac3", action
="store_true", dest
="copyac3")
348 parser
.add_option("--mc", action
="store", dest
="mc", type="int")
349 parser
.add_option("--noskip", action
="store_true", dest
="noskip")
350 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
351 parser
.add_option("--afilters", action
="store", dest
="afilters")
352 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int")
353 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int")
354 parser
.add_option("--chapter", action
="store", dest
="chapter")
355 parser
.add_option("--ifps", action
="store", dest
="ifps")
356 parser
.add_option("--ofps", action
="store", dest
="ofps")
357 parser
.add_option("--skipkb", action
="store", dest
="skipkb", type="int")
358 parser
.add_option("--startpos", action
="store", dest
="startpos")
359 parser
.add_option("--endpos", action
="store", dest
="endpos")
360 parser
.add_option("--audioid", action
="store", dest
="audioid")
361 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
362 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
363 parser
.add_option("--dump", action
="store_true", dest
="dump")
365 opts
, args
= parser
.parse_args(sys
.argv
[1:])
368 output
= os
.path
.splitext(os
.path
.basename(input))[0]
377 if "://" not in input:
378 opts
.input = os
.path
.abspath(input)
381 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
384 opts
.output
= os
.path
.abspath(output
)
395 profile
= profiles
[opts
.profile_name
]
397 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
400 # Pull in default option values from the profile
401 for key
, value
in profile
.default_opts
.iteritems():
402 if getattr(opts
, key
) is None:
403 setattr(opts
, key
, value
)
405 # Run in a temp dir so that multiple instances can be run simultaneously
406 tempdir
= tempfile
.mkdtemp()
412 for CommandClass
in profile
.commands
:
413 command
= CommandClass(profile
, opts
)
414 commands
.append(command
)
416 for command
in commands
:
419 except FatalException
, e
:
420 print >>sys
.stderr
, "Error:", str(e
)
425 shutil
.rmtree(tempdir
)
427 if __name__
== "__main__":