]>
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
60 self
.audio_tmp
= "audio." + self
.codec2exts
[profile
.acodec
]
61 self
.video_tmp
= "video." + self
.codec2exts
[profile
.vcodec
]
63 def print_install_message(self
):
64 print >>sys
.stderr
, "Problem with command: %s", self
.name
66 print >>sys
.stderr
, "Try running:\n# aptitude install %s", self
.package
68 def check_command(self
, cmd
):
71 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
72 raise FatalException("Command '%s' is required" % cmd
)
74 def check_no_file(self
, path
):
75 if os
.path
.exists(path
):
76 raise FatalException("Output file '%s' exists." % path
)
78 def do_exec(self
, args
):
80 print " ".join(map(mkarg
, args
))
82 if subprocess
.Popen(args
).wait() != 0:
83 raise FatalException("Failure executing command: %s" % args
)
86 class MP4Box(Command
):
88 self
.check_command("MP4Box")
89 self
.check_no_file(self
.opts
.output
+ ".mp4")
95 fps
= midentify(self
.video_tmp
, "ID_VIDEO_FPS")
97 output
= self
.opts
.output
+ ".mp4"
101 "-add", self
.video_tmp
,
102 "-add", self
.audio_tmp
,
108 class MKVMerge(Command
):
110 self
.check_command("mkvmerge")
111 self
.check_no_file(self
.opts
.output
+ ".mkv")
117 fps
= midentify(self
.video_tmp
, "ID_VIDEO_FPS")
121 "-o", self
.opts
.output
+ ".mkv",
122 "--default-duration", "0:%sfps"%fps
,
129 class MencoderMux(Command
):
131 self
.check_command("mencoder")
132 self
.check_no_file(self
.opts
.output
+ ".avi")
137 "-o", self
.opts
.output
+ ".avi",
138 "-oac", "copy", "-ovc", "copy",
139 "-noskip", "-mc", "0",
140 "-audiofile", self
.audio_tmp
,
146 class Mencoder(Command
):
148 "xvid": "-xvidencopts",
149 "x264": "-x264encopts",
151 "mp3lame": "-lameopts",
154 def insert_options(self
, cmd
):
155 def try_opt(opt
, var
):
159 if self
.opts
.deinterlace
:
160 cmd
+= ["-vf-add", "pp=lb"]
161 if self
.opts
.detelecine
:
162 self
.opts
.ofps
= "24000/1001"
163 cmd
+= ["-vf-add", "pullup,softskip"]
164 try_opt("-fps", self
.opts
.ifps
)
165 try_opt("-ofps", self
.opts
.ofps
)
166 try_opt("-ss", self
.opts
.startpos
)
167 try_opt("-endpos", self
.opts
.endpos
)
168 try_opt("-dvd-device", self
.opts
.dvd
)
169 try_opt("-chapter", self
.opts
.chapter
)
170 try_opt("-aid", self
.opts
.audioid
)
171 try_opt("-sid", self
.opts
.subtitleid
)
172 try_opt("-vf-add", self
.opts
.vfilters
)
173 try_opt("-af-add", self
.opts
.afilters
)
174 cmd
+= ["-vf-add", "harddup"]
176 def subst_values(self
, cmd
, vpass
):
178 "vbitrate": self
.opts
.vbitrate
,
179 "abitrate": self
.opts
.abitrate
,
183 return [x
% subst
for x
in cmd
]
189 if self
.opts
.copyac3
:
195 cmd
+= ["mencoder", self
.opts
.input]
196 self
.insert_options(cmd
)
197 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
198 cmd
+= ["-oac", acodec
]
200 cmd
+= [self
.codec2opts
[p
.acodec
], p
.aopts
]
201 cmd
+= self
.profile
.extra1
+ self
.profile
.extra
202 cmd
= self
.subst_values(cmd
, vpass
=n
)
209 cmd
+= ["-o", self
.audio_tmp
, "-of", "rawaudio"]
214 cmd
+= ["-o", self
.video_tmp
, "-of", "rawvideo"]
218 self
.check_command("mencoder")
219 self
.check_no_file(self
.audio_tmp
)
220 self
.check_no_file(self
.video_tmp
)
223 self
.do_exec(self
.pass1())
224 self
.do_exec(self
.pass2())
228 class Profile(object):
229 def __init__(self
, commands
, **kwargs
):
230 self
.default_opts
= {
237 self
.commands
= commands
238 self
.__dict
__.update(kwargs
)
240 def __contains__(self
, keyname
):
241 return hasattr(self
, keyname
)
247 commands
=[Mencoder
, MKVMerge
],
249 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:subq=6:frameref=6:me=umh:partitions=all:bframes=4:b_adapt:qcomp=0.7:keyint=250",
251 aopts
="abr:br=%(abitrate)d",
256 commands
=[Mencoder
, MencoderMux
],
258 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
260 aopts
="abr:br=%(abitrate)d",
265 commands
=[Mencoder
, MP4Box
],
267 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
269 aopts
="br=%(abitrate)d:mpeg=4:object=2",
274 commands
=[Mencoder
, MP4Box
],
276 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
278 aopts
="br=%(abitrate)d:mpeg=4:object=2",
279 extra
=["-vf-add", "scale=480:-10"],
284 commands
=[Mencoder
, MP4Box
],
286 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",
288 aopts
="br=%(abitrate)d:mpeg=4:object=2",
289 extra
=["-vf-add", "scale=480:-10"],
290 extra2
=["-channels", "2", "-srate", "48000"],
295 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=640:-10"],
312 for profile_name
in profiles
.keys():
313 if sys
.argv
[0].find(profile_name
) >= 0:
316 profile_name
= "xvid"
318 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
319 parser
.add_option("--dvd", action
="store", dest
="dvd")
320 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
321 parser
.add_option("--detelecine", action
="store_true", dest
="detelecine")
322 parser
.add_option("--copyac3", action
="store_true", dest
="copyac3")
323 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
324 parser
.add_option("--afilters", action
="store", dest
="afilters")
325 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int")
326 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int")
327 parser
.add_option("--chapter", action
="store", dest
="chapter")
328 parser
.add_option("--ifps", action
="store", dest
="ifps")
329 parser
.add_option("--ofps", action
="store", dest
="ofps")
330 parser
.add_option("--startpos", action
="store", dest
="startpos")
331 parser
.add_option("--endpos", action
="store", dest
="endpos")
332 parser
.add_option("--audioid", action
="store", dest
="audioid")
333 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
334 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
335 parser
.add_option("--dump", action
="store_true", dest
="dump")
337 opts
, args
= parser
.parse_args(sys
.argv
[1:])
340 output
= os
.path
.splitext(os
.path
.basename(input))[0]
349 if "://" not in input:
350 opts
.input = os
.path
.abspath(input)
353 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
356 opts
.output
= os
.path
.abspath(output
)
367 profile
= profiles
[opts
.profile_name
]
369 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
372 # Pull in default option values from the profile
373 for key
, value
in profile
.default_opts
.iteritems():
374 if getattr(opts
, key
) is None:
375 setattr(opts
, key
, value
)
377 # Run in a temp dir so that multiple instances can be run simultaneously
378 tempdir
= tempfile
.mkdtemp()
384 for CommandClass
in profile
.commands
:
385 command
= CommandClass(profile
, opts
)
386 commands
.append(command
)
388 for command
in commands
:
391 except FatalException
, e
:
392 print >>sys
.stderr
, "Error:", str(e
)
397 shutil
.rmtree(tempdir
)
399 if __name__
== "__main__":