]> code.delx.au - transcoding/blob - encode.py
Improved MencoderDemux
[transcoding] / encode.py
1 #!/usr/bin/env python
2
3 from functools import partial
4 import optparse
5 import re
6 import subprocess
7 import sys
8 import os
9 import shutil
10 import tempfile
11
12 class FatalException(Exception):
13 pass
14
15 def mkarg(arg):
16 if re.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg):
17 return arg
18
19 if "'" not in arg:
20 return "'%s'" % arg
21 out = "\""
22 for c in arg:
23 if c in "\\$\"`":
24 out += "\\"
25 out += c
26 out += "\""
27 return out
28
29 def midentify(source, field):
30 process = subprocess.Popen(
31 [
32 "mplayer", source,
33 "-ao", "null", "-vo", "null",
34 "-frames", "0", "-identify",
35 ],
36 stdout=subprocess.PIPE,
37 stderr=subprocess.PIPE,
38 )
39 for line in process.stdout:
40 try:
41 key, value = line.split("=")
42 except ValueError:
43 continue
44 if key == field:
45 return value.strip()
46
47 def append_cmd(cmd, opt, var):
48 if var is not None:
49 cmd.append(opt)
50 cmd.append(str(var))
51
52 def duplicate_opts(opts):
53 return optparse.Values(opts.__dict__)
54
55 def insert_mplayer_options(cmd, o):
56 do_opt = partial(append_cmd, cmd)
57
58 if o.deinterlace:
59 cmd += ["-vf-add", "yadif"]
60 if o.noskip:
61 cmd += ["-noskip"]
62 if o.skipkb:
63 cmd += ["-sb", str(o.skipkb * 1024)]
64
65 do_opt("-mc", o.mc)
66 do_opt("-fps", o.ifps)
67 do_opt("-ss", o.startpos)
68 do_opt("-endpos", o.endpos)
69 do_opt("-dvd-device", o.dvd)
70 do_opt("-chapter", o.chapter)
71 do_opt("-aid", o.audioid)
72 do_opt("-sid", o.subtitleid)
73 do_opt("-vf-add", o.vfilters)
74 do_opt("-af-add", o.afilters)
75
76
77 class Command(object):
78 def __init__(self, profile, opts):
79 self.profile = profile
80 self.opts = opts
81 self.__process = None
82 self.init()
83
84 def init(self):
85 pass
86
87 def check_command(self, cmd):
88 if self.opts.dump:
89 return
90 if subprocess.Popen(["which", cmd], stdout=open("/dev/null", "w")).wait() != 0:
91 raise FatalException("Command '%s' is required" % cmd)
92
93 def check_no_file(self, path):
94 if os.path.exists(path):
95 raise FatalException("Output file '%s' exists." % path)
96
97 def do_exec(self, args, wait=True):
98 if self.opts.dump:
99 print " ".join(map(mkarg, args))
100 else:
101 self.__process = subprocess.Popen(args)
102 self.__args = args
103 if wait:
104 self.wait()
105
106 def wait(self):
107 if self.__process == None:
108 return
109 if self.__process.wait() != 0:
110 raise FatalException("Failure executing command: %s" % self.__args)
111 self.__process = None
112
113
114 class MP4Box(Command):
115 def init(self):
116 self.check_command("MP4Box")
117 self.check_no_file(self.opts.output + ".mp4")
118
119 def run(self):
120 o = self.opts
121 p = self.profile
122
123 if o.dump:
124 fps = "???"
125 else:
126 fps = midentify(p.video_tmp, "ID_VIDEO_FPS")
127
128 self.do_exec([
129 "MP4Box",
130 "-fps", fps,
131 "-add", p.video_tmp,
132 "-add", p.audio_tmp,
133 o.output + ".mp4"
134 ])
135
136
137
138 class MKVMerge(Command):
139 def init(self):
140 self.check_command("mkvmerge")
141 self.check_no_file(self.opts.output + ".mkv")
142
143 def run(self):
144 o = self.opts
145 p = self.profile
146
147 if o.dump:
148 fps = "???"
149 else:
150 fps = midentify(p.video_tmp, "ID_VIDEO_FPS")
151
152 self.do_exec([
153 "mkvmerge",
154 "-o", o.output + ".mkv",
155 "--default-duration", "0:%sfps"%fps,
156 p.video_tmp,
157 p.audio_tmp,
158 ])
159
160
161
162 class MencoderFixRemux(Command):
163 def init(self):
164 self.check_command("mencoder")
165 self.check_no_file("remux.avi")
166
167 orig = self.opts
168 self.opts = duplicate_opts(orig)
169 orig.input = "remux.avi"
170 orig.dvd = orig.chapter = orig.startpos = orig.endpos = None
171
172 def run(self):
173 o = self.opts
174 cmd = [
175 "mencoder",
176 "-o", "remux.avi",
177 "-oac", "copy", "-ovc", "copy",
178 "-mc", "0.1",
179 o.input,
180 ]
181 do_opt = partial(append_cmd, cmd)
182 do_opt("-dvd-device", o.dvd)
183 do_opt("-chapter", o.chapter)
184 do_opt("-ss", o.startpos)
185 do_opt("-endpos", o.endpos)
186 self.do_exec(cmd)
187
188
189
190
191
192 class MPlayer(Command):
193 def init(self):
194 self.check_command("mplayer")
195 self.check_no_file("video.y4m")
196 self.check_no_file("audio.wav")
197
198 def run(self):
199 os.mkfifo("video.y4m")
200 os.mkfifo("audio.wav")
201 cmd = []
202 cmd += ["mplayer", self.opts.input]
203 cmd += ["-benchmark", "-noconsolecontrols", "-noconfig", "all"]
204 cmd += ["-vo", "yuv4mpeg:file=video.y4m"]
205 cmd += ["-ao", "pcm:waveheader:file=audio.wav"]
206 insert_mplayer_options(cmd, self.opts)
207 cmd += self.profile.extra
208 self.do_exec(cmd, wait=False)
209
210
211 class MencoderCopyAC3(Command):
212 def init(self):
213 self.check_command("mplayer")
214 self.check_no_file("audio.ac3")
215 self.profile.audio_tmp = "audio.ac3"
216
217 def run(self):
218 cmd = []
219 cmd += ["mencoder", self.opts.input]
220 cmd += ["-noconfig", "all"]
221 cmd += ["-ovc", "copy", "-oac", "copy"]
222 cmd += ["-of", "rawaudio", "-o", "audio.ac3"]
223 insert_mplayer_options(cmd, self.opts)
224 cmd += self.profile.extra
225 self.do_exec(cmd, wait=False)
226
227
228 class X264(Command):
229 def init(self):
230 self.check_command("x264")
231 self.profile.video_tmp = "video.h264"
232
233 def run(self):
234 p = self.profile
235 cmd = []
236 cmd += ["x264", "--no-progress"]
237 cmd += p.x264opts
238 cmd += ["-o", p.video_tmp]
239 cmd += ["video.y4m"]
240 self.do_exec(cmd, wait=False)
241
242
243 class Lame(Command):
244 def init(self):
245 self.check_command("lame")
246 self.profile.audio_tmp = "audio.mp3"
247
248 def run(self):
249 p = self.profile
250 cmd = []
251 cmd += ["lame", "--quiet"]
252 cmd += p.lameopts
253 cmd += ["audio.wav"]
254 cmd += [p.audio_tmp]
255 self.do_exec(cmd, wait=False)
256
257
258 class Faac(Command):
259 def init(self):
260 self.check_command("faac")
261 self.profile.audio_tmp = "audio.aac"
262
263 def run(self):
264 p = self.profile
265 cmd = []
266 cmd += ["faac"]
267 cmd += ["-o", p.audio_tmp]
268 cmd += p.faacopts
269 cmd += ["audio.wav"]
270 self.do_exec(cmd, wait=False)
271
272
273 class SwallowAudio(Command):
274 def run(self):
275 self.do_exec(["dd", "if=audio.wav", "of=/dev/null"], wait=False)
276
277
278 class Mencoder(Command):
279 codec2opts = {
280 "xvid": "-xvidencopts",
281 "x264": "-x264encopts",
282 "faac": "-faacopts",
283 "mp3lame": "-lameopts",
284 }
285
286 def init(self):
287 o = self.opts
288 p = self.profile
289
290 self.check_command("mencoder")
291 self.check_no_file(o.output + ".avi")
292
293 p.video_tmp = o.output + ".avi"
294 p.audio_tmp = o.output + ".avi"
295
296 def run(self):
297 o = self.opts
298 p = self.profile
299
300 cmd = []
301 cmd += ["mencoder", o.input]
302 cmd += ["-noconfig", "all"]
303 insert_mplayer_options(cmd, o)
304 cmd += ["-vf-add", "harddup"]
305 cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
306 cmd += ["-oac", p.acodec]
307 if p.aopts:
308 cmd += [self.codec2opts[p.acodec], p.aopts]
309 cmd += self.profile.extra
310 cmd += ["-o", self.opts.output + ".avi"]
311
312 self.do_exec(cmd)
313
314
315 class MencoderDemux(Command):
316 codec2exts = {
317 "xvid": "m4v",
318 "x264": "h264",
319 "faac": "aac",
320 "mp3lame": "mp3",
321 "copyac3": "ac3",
322 }
323
324 def init(self):
325 o = self.opts
326 p = self.profile
327
328 self.check_command("mencoder")
329 p.audio_tmp = "audio." + self.codec2exts[p.acodec]
330 p.video_tmp = "video." + self.codec2exts[p.vcodec]
331 self.check_no_file(p.audio_tmp)
332 self.check_no_file(p.video_tmp)
333
334 def run(self):
335 o = self.opts
336 p = self.profile
337
338 cmd = ["mencoder", "-ovc", "copy", "-oac", "copy", o.output + ".avi"]
339 cmd += ["-noconfig", "all", "-noskip", "-mc", "0"]
340 self.do_exec(cmd + ["-of", "rawaudio", "-o", p.audio_tmp])
341 self.do_exec(cmd + ["-of", "rawvideo", "-o", p.video_tmp])
342 self.do_exec(["rm", "-f", o.output + ".avi"])
343
344
345
346 class Profile(object):
347 def __init__(self, commands, **kwargs):
348 self.extra = []
349 self.commands = commands
350 self.__dict__.update(kwargs)
351
352 def __contains__(self, keyname):
353 return hasattr(self, keyname)
354
355 class Wait(object):
356 def __init__(self, commands):
357 self.commands = commands[:]
358
359 def run(self):
360 for command in self.commands:
361 command.wait()
362
363
364
365 profiles = {
366 "x264/lame" :
367 Profile(
368 commands=[MPlayer, X264, Lame, Wait, MKVMerge],
369 x264opts=["--preset", "veryslow", "--crf", "19"],
370 lameopts=["--preset", "extreme"],
371 ),
372
373 "x264/copyac3" :
374 Profile(
375 commands=[MPlayer, X264, SwallowAudio, Wait, MencoderCopyAC3, MKVMerge],
376 x264opts=["--preset", "veryslow", "--crf", "19"],
377 lameopts=["--preset", "extreme"],
378 ),
379
380 "xvid/lame" :
381 Profile(
382 commands=[Mencoder],
383 vcodec="xvid",
384 vopts="fixed_quant=2:vhq=4:autoaspect",
385 acodec="mp3lame",
386 aopts="cbr:br=128",
387 ),
388
389 "apple-quicktime" :
390 Profile(
391 commands=[MPlayer, X264, Faac, Wait, MP4Box],
392 x264opts=["--crf", "19", "--bframes", "1"],
393 faacopts=["-q", "100", "--mpeg-vers", "4"],
394 ),
395
396 "nokia-n97" :
397 Profile(
398 commands=[Mencoder, MencoderDemux, MP4Box],
399 vcodec="xvid",
400 vopts="bitrate=256:vhq=4:autoaspect:max_bframes=0",
401 acodec="faac",
402 aopts="br=64:mpeg=4:object=2",
403 extra=["-vf-add", "scale=640:-10"],
404 ),
405 }
406
407 mappings = {
408 "x264", "x264/lame",
409 "xvid", "xvid/lame",
410 }
411 for x, y in mappings.iteritems():
412 profiles[x] = profiles[y]
413
414
415
416
417 def parse_args():
418 for profile_name in profiles.keys():
419 if sys.argv[0].find(profile_name) >= 0:
420 break
421 else:
422 profile_name = "xvid/lame"
423
424 parser = optparse.OptionParser(usage="%prog [options] input [output]")
425 parser.add_option("--dvd", action="store", dest="dvd")
426 parser.add_option("--deinterlace", action="store_true", dest="deinterlace")
427 parser.add_option("--fixmux", action="store_true", dest="fixmux")
428 parser.add_option("--mc", action="store", dest="mc", type="int")
429 parser.add_option("--noskip", action="store_true", dest="noskip")
430 parser.add_option("--vfilters", action="store", dest="vfilters")
431 parser.add_option("--afilters", action="store", dest="afilters")
432 parser.add_option("--chapter", action="store", dest="chapter")
433 parser.add_option("--ifps", action="store", dest="ifps")
434 parser.add_option("--skipkb", action="store", dest="skipkb", type="int")
435 parser.add_option("--startpos", action="store", dest="startpos")
436 parser.add_option("--endpos", action="store", dest="endpos")
437 parser.add_option("--audioid", action="store", dest="audioid")
438 parser.add_option("--subtitleid", action="store", dest="subtitleid")
439 parser.add_option("--profile", action="store", dest="profile_name", default=profile_name)
440 parser.add_option("--dump", action="store_true", dest="dump")
441 try:
442 opts, args = parser.parse_args(sys.argv[1:])
443 if len(args) == 1:
444 input = args[0]
445 output = os.path.splitext(os.path.basename(input))[0]
446 elif len(args) == 2:
447 input, output = args
448 else:
449 raise ValueError
450 except Exception:
451 parser.print_usage()
452 sys.exit(1)
453
454 if "://" not in input:
455 opts.input = os.path.abspath(input)
456 else:
457 if opts.dvd:
458 opts.dvd = os.path.abspath(opts.dvd)
459 opts.input = input
460
461 opts.output = os.path.abspath(output)
462
463 return opts
464
465 def main():
466 os.nice(1)
467
468 opts = parse_args()
469
470 # Find our profile
471 try:
472 profile = profiles[opts.profile_name]
473 except KeyError:
474 print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name
475 sys.exit(1)
476
477 # Run in a temp dir so that multiple instances can be run simultaneously
478 tempdir = tempfile.mkdtemp()
479 try:
480 os.chdir(tempdir)
481
482 try:
483 commands = []
484 if opts.fixmux:
485 profile.commands.insert(0, MencoderFixRemux)
486 for CommandClass in profile.commands:
487 if Command in CommandClass.__bases__:
488 command = CommandClass(profile, opts)
489 else:
490 command = CommandClass(commands)
491 commands.append(command)
492 for command in commands:
493 command.run()
494
495 except FatalException, e:
496 print >>sys.stderr, "Error:", str(e)
497 sys.exit(1)
498
499 finally:
500 os.chdir("/")
501 shutil.rmtree(tempdir)
502
503 if __name__ == "__main__":
504 main()
505