]>
code.delx.au - transcoding/blob - encode.py
9300ceaee175191d82757f39b9e21783d446ea78
3 from functools
import partial
12 class FatalException(Exception):
16 if re
.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg
):
29 def midentify(source
, field
):
30 process
= subprocess
.Popen(
33 "-ao", "null", "-vo", "null",
34 "-frames", "0", "-identify",
36 stdout
=subprocess
.PIPE
,
37 stderr
=subprocess
.PIPE
,
39 for line
in process
.stdout
:
41 key
, value
= line
.split("=")
47 def append_cmd(cmd
, opt
, var
):
52 def duplicate_opts(opts
):
53 return optparse
.Values(opts
.__dict
__)
55 def insert_mplayer_options(cmd
, o
):
59 do_opt
= partial(append_cmd
, cmd
)
62 cmd
+= ["-vf-pre", "yadif"]
64 cmd
+= ["-vf-pre", "pullup,softskip", "-ofps", "24000/1001"]
68 cmd
+= ["-sb", str(o
.skipkb
* 1024)]
71 do_opt("-ss", o
.startpos
)
72 do_opt("-endpos", o
.endpos
)
73 do_opt("-dvd-device", o
.dvd
)
74 do_opt("-chapter", o
.chapter
)
75 do_opt("-aid", o
.audioid
)
76 do_opt("-sid", o
.subtitleid
)
77 do_opt("-vf", o
.vfilters
)
78 do_opt("-af", o
.afilters
)
81 class Command(object):
82 def __init__(self
, profile
, opts
):
83 self
.profile
= profile
91 def check_command(self
, cmd
):
94 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
95 raise FatalException("Command '%s' is required" % cmd
)
97 def check_no_file(self
, path
):
98 if os
.path
.exists(path
):
99 raise FatalException("Output file '%s' exists." % path
)
101 def do_exec(self
, args
, wait
=True):
103 print " ".join(map(mkarg
, args
))
105 self
.__process
= subprocess
.Popen(args
)
111 if self
.__process
== None:
113 if self
.__process
.wait() != 0:
114 raise FatalException("Failure executing command: %s" % self
.__args
)
115 self
.__process
= None
118 class MP4Box(Command
):
120 self
.check_command("MP4Box")
121 self
.check_no_file(self
.opts
.output
+ ".mp4")
130 fps
= midentify(p
.video_tmp
, "ID_VIDEO_FPS")
142 class MKVMerge(Command
):
144 self
.check_command("mkvmerge")
145 self
.check_no_file(self
.opts
.output
+ ".mkv")
154 fps
= midentify(p
.video_tmp
, "ID_VIDEO_FPS")
158 "-o", o
.output
+ ".mkv",
159 "--default-duration", "0:%sfps"%fps
,
166 class MencoderLossless(Command
):
168 self
.check_command("mencoder")
169 self
.check_no_file("lossless.avi")
172 self
.opts
= duplicate_opts(ofut
)
173 ofut
.input = "lossless.avi"
174 ofut
.mplayer_done
= True
179 os
.mkfifo("lossless.avi")
182 cmd
+= ["mencoder", self
.opts
.input, "-o", "lossless.avi"]
183 cmd
+= ["-noconfig", "all"]
184 cmd
+= ["-oac", "copy", "-ovc", "lavc", "-lavcopts", "vcodec=ffv1:autoaspect"]
185 insert_mplayer_options(cmd
, self
.opts
)
186 cmd
+= ["-vf-add", "harddup"]
187 self
.do_exec(cmd
, wait
=not fifo
)
191 class MPlayer(Command
):
193 self
.check_command("mplayer")
194 self
.check_no_file("video.y4m")
195 self
.check_no_file("audio.wav")
198 os
.mkfifo("video.y4m")
199 os
.mkfifo("audio.wav")
201 cmd
+= ["mplayer", self
.opts
.input]
202 cmd
+= ["-benchmark", "-noconsolecontrols", "-noconfig", "all"]
203 cmd
+= ["-vo", "yuv4mpeg:file=video.y4m"]
204 cmd
+= ["-ao", "pcm:waveheader:file=audio.wav"]
205 insert_mplayer_options(cmd
, self
.opts
)
206 cmd
+= self
.profile
.mplayeropts
207 self
.do_exec(cmd
, wait
=False)
210 class MencoderCopyAC3(Command
):
212 self
.check_command("mplayer")
213 self
.check_no_file("audio.ac3")
214 self
.profile
.audio_tmp
= "audio.ac3"
218 cmd
+= ["mencoder", self
.opts
.input]
219 cmd
+= ["-noconfig", "all"]
220 cmd
+= ["-ovc", "copy", "-oac", "copy"]
221 cmd
+= ["-of", "rawaudio", "-o", "audio.ac3"]
222 insert_mplayer_options(cmd
, self
.opts
)
228 self
.check_command("x264")
229 self
.profile
.video_tmp
= "video.h264"
234 cmd
+= ["x264", "--no-progress"]
236 cmd
+= ["-o", p
.video_tmp
]
238 self
.do_exec(cmd
, wait
=False)
243 self
.check_command("lame")
244 self
.profile
.audio_tmp
= "audio.mp3"
249 cmd
+= ["lame", "--quiet"]
253 self
.do_exec(cmd
, wait
=False)
258 self
.check_command("faac")
259 self
.profile
.audio_tmp
= "audio.aac"
265 cmd
+= ["-o", p
.audio_tmp
]
268 self
.do_exec(cmd
, wait
=False)
271 class Mencoder(Command
):
273 "xvid": "-xvidencopts",
274 "x264": "-x264encopts",
276 "mp3lame": "-lameopts",
283 self
.check_command("mencoder")
284 self
.check_no_file(o
.output
+ ".avi")
286 p
.video_tmp
= o
.output
+ ".avi"
287 p
.audio_tmp
= o
.output
+ ".avi"
294 cmd
+= ["mencoder", o
.input]
295 cmd
+= ["-noconfig", "all"]
296 insert_mplayer_options(cmd
, o
)
297 cmd
+= ["-vf-add", "harddup"]
298 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
299 cmd
+= ["-oac", p
.acodec
]
301 cmd
+= [self
.codec2opts
[p
.acodec
], p
.aopts
]
302 cmd
+= self
.profile
.mplayeropts
303 cmd
+= ["-o", self
.opts
.output
+ ".avi"]
308 class MencoderDemux(Command
):
321 self
.check_command("mencoder")
322 p
.audio_tmp
= "audio." + self
.codec2exts
[p
.acodec
]
323 p
.video_tmp
= "video." + self
.codec2exts
[p
.vcodec
]
324 self
.check_no_file(p
.audio_tmp
)
325 self
.check_no_file(p
.video_tmp
)
331 cmd
= ["mencoder", "-ovc", "copy", "-oac", "copy", o
.output
+ ".avi"]
332 cmd
+= ["-noconfig", "all", "-noskip", "-mc", "0"]
333 self
.do_exec(cmd
+ ["-of", "rawaudio", "-o", p
.audio_tmp
])
334 self
.do_exec(cmd
+ ["-of", "rawvideo", "-o", p
.video_tmp
])
335 self
.do_exec(["rm", "-f", o
.output
+ ".avi"])
339 class Profile(object):
340 def __init__(self
, commands
, **kwargs
):
341 self
.commands
= commands
342 self
.__dict
__.update(kwargs
)
344 def __contains__(self
, keyname
):
345 return hasattr(self
, keyname
)
348 def __init__(self
, commands
):
349 self
.commands
= commands
[:]
352 for command
in self
.commands
:
360 commands
=[MPlayer
, X264
, Lame
, Wait
, MKVMerge
],
362 x264opts
=["--preset", "veryslow", "--crf", "20"],
363 lameopts
=["--preset", "medium"],
368 commands
=[MPlayer
, X264
, Wait
, MencoderCopyAC3
, MKVMerge
],
369 mplayeropts
=["-nosound"],
370 x264opts
=["--preset", "veryslow", "--crf", "20"],
376 mplayeropts
=["-ffourcc", "DX50"],
378 vopts
="fixed_quant=2:vhq=4:autoaspect",
385 commands
=[MPlayer
, X264
, Faac
, Wait
, MP4Box
],
387 x264opts
=["--crf", "20", "--bframes", "1"],
388 faacopts
=["-q", "100", "--mpeg-vers", "4"],
393 commands
=[Mencoder
, MencoderDemux
, MP4Box
],
394 mplayeropts
=["-vf-add", "scale=640:-10"],
396 vopts
="bitrate=384:vhq=4:autoaspect:max_bframes=0",
398 aopts
="br=64:mpeg=4:object=2",
406 for x
, y
in mappings
.iteritems():
407 profiles
[x
] = profiles
[y
]
413 for profile_name
in profiles
.keys():
414 if sys
.argv
[0].find(profile_name
) >= 0:
417 profile_name
= "xvid/lame"
419 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
420 parser
.add_option("--dvd", action
="store", dest
="dvd")
421 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
422 parser
.add_option("--detelecine", action
="store_true", dest
="detelecine")
423 parser
.add_option("--mc", action
="store", dest
="mc", type="int")
424 parser
.add_option("--noskip", action
="store_true", dest
="noskip")
425 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
426 parser
.add_option("--afilters", action
="store", dest
="afilters")
427 parser
.add_option("--chapter", action
="store", dest
="chapter")
428 parser
.add_option("--skipkb", action
="store", dest
="skipkb", type="int")
429 parser
.add_option("--startpos", action
="store", dest
="startpos")
430 parser
.add_option("--endpos", action
="store", dest
="endpos")
431 parser
.add_option("--audioid", action
="store", dest
="audioid")
432 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
433 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
434 parser
.add_option("--dump", action
="store_true", dest
="dump")
436 opts
, args
= parser
.parse_args(sys
.argv
[1:])
439 output
= os
.path
.splitext(os
.path
.basename(input))[0]
448 if "://" not in input:
449 opts
.input = os
.path
.abspath(input)
452 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
455 opts
.output
= os
.path
.abspath(output
)
456 opts
.mplayer_done
= False
467 profile
= profiles
[opts
.profile_name
]
469 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
472 # Run in a temp dir so that multiple instances can be run simultaneously
473 tempdir
= tempfile
.mkdtemp()
480 profile
.commands
.insert(0, MencoderLossless
)
481 for CommandClass
in profile
.commands
:
482 if Command
in CommandClass
.__bases
__:
483 command
= CommandClass(profile
, opts
)
485 command
= CommandClass(commands
)
486 commands
.append(command
)
487 for command
in commands
:
490 except FatalException
, e
:
491 print >>sys
.stderr
, "Error:", str(e
)
496 shutil
.rmtree(tempdir
)
498 if __name__
== "__main__":