]>
code.delx.au - transcoding/blob - encode.py
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
):
56 do_opt
= partial(append_cmd
, cmd
)
59 cmd
+= ["-vf-pre", "yadif"]
63 cmd
+= ["-sb", str(o
.skipkb
* 1024)]
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", o
.vfilters
)
74 do_opt("-af-add", o
.afilters
)
77 class Command(object):
78 def __init__(self
, profile
, opts
):
79 self
.profile
= profile
87 def check_command(self
, cmd
):
90 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
91 raise FatalException("Command '%s' is required" % cmd
)
93 def check_no_file(self
, path
):
94 if os
.path
.exists(path
):
95 raise FatalException("Output file '%s' exists." % path
)
97 def do_exec(self
, args
, wait
=True):
99 print " ".join(map(mkarg
, args
))
101 self
.__process
= subprocess
.Popen(args
)
107 if self
.__process
== None:
109 if self
.__process
.wait() != 0:
110 raise FatalException("Failure executing command: %s" % self
.__args
)
111 self
.__process
= None
114 class MP4Box(Command
):
116 self
.check_command("MP4Box")
117 self
.check_no_file(self
.opts
.output
+ ".mp4")
126 fps
= midentify(p
.video_tmp
, "ID_VIDEO_FPS")
138 class MKVMerge(Command
):
140 self
.check_command("mkvmerge")
141 self
.check_no_file(self
.opts
.output
+ ".mkv")
150 fps
= midentify(p
.video_tmp
, "ID_VIDEO_FPS")
154 "-o", o
.output
+ ".mkv",
155 "--default-duration", "0:%sfps"%fps
,
162 class MencoderFixRemux(Command
):
164 self
.check_command("mencoder")
165 self
.check_no_file("remux.avi")
168 self
.opts
= duplicate_opts(orig
)
169 orig
.input = "remux.avi"
170 orig
.dvd
= orig
.chapter
= orig
.startpos
= orig
.endpos
= None
177 "-oac", "copy", "-ovc", "copy",
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
)
192 class MPlayer(Command
):
194 self
.check_command("mplayer")
195 self
.check_no_file("video.y4m")
196 self
.check_no_file("audio.wav")
199 os
.mkfifo("video.y4m")
200 os
.mkfifo("audio.wav")
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
.mplayeropts
208 self
.do_exec(cmd
, wait
=False)
211 class MencoderCopyAC3(Command
):
213 self
.check_command("mplayer")
214 self
.check_no_file("audio.ac3")
215 self
.profile
.audio_tmp
= "audio.ac3"
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
)
229 self
.check_command("x264")
230 self
.profile
.video_tmp
= "video.h264"
235 cmd
+= ["x264", "--no-progress"]
237 cmd
+= ["-o", p
.video_tmp
]
239 self
.do_exec(cmd
, wait
=False)
244 self
.check_command("lame")
245 self
.profile
.audio_tmp
= "audio.mp3"
250 cmd
+= ["lame", "--quiet"]
254 self
.do_exec(cmd
, wait
=False)
259 self
.check_command("faac")
260 self
.profile
.audio_tmp
= "audio.aac"
266 cmd
+= ["-o", p
.audio_tmp
]
269 self
.do_exec(cmd
, wait
=False)
272 class Mencoder(Command
):
274 "xvid": "-xvidencopts",
275 "x264": "-x264encopts",
277 "mp3lame": "-lameopts",
284 self
.check_command("mencoder")
285 self
.check_no_file(o
.output
+ ".avi")
287 p
.video_tmp
= o
.output
+ ".avi"
288 p
.audio_tmp
= o
.output
+ ".avi"
295 cmd
+= ["mencoder", o
.input]
296 cmd
+= ["-noconfig", "all"]
297 insert_mplayer_options(cmd
, o
)
298 cmd
+= ["-vf-add", "harddup"]
299 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
300 cmd
+= ["-oac", p
.acodec
]
302 cmd
+= [self
.codec2opts
[p
.acodec
], p
.aopts
]
303 cmd
+= self
.profile
.mplayeropts
304 cmd
+= ["-o", self
.opts
.output
+ ".avi"]
309 class MencoderDemux(Command
):
322 self
.check_command("mencoder")
323 p
.audio_tmp
= "audio." + self
.codec2exts
[p
.acodec
]
324 p
.video_tmp
= "video." + self
.codec2exts
[p
.vcodec
]
325 self
.check_no_file(p
.audio_tmp
)
326 self
.check_no_file(p
.video_tmp
)
332 cmd
= ["mencoder", "-ovc", "copy", "-oac", "copy", o
.output
+ ".avi"]
333 cmd
+= ["-noconfig", "all", "-noskip", "-mc", "0"]
334 self
.do_exec(cmd
+ ["-of", "rawaudio", "-o", p
.audio_tmp
])
335 self
.do_exec(cmd
+ ["-of", "rawvideo", "-o", p
.video_tmp
])
336 self
.do_exec(["rm", "-f", o
.output
+ ".avi"])
340 class Profile(object):
341 def __init__(self
, commands
, **kwargs
):
342 self
.commands
= commands
343 self
.__dict
__.update(kwargs
)
345 def __contains__(self
, keyname
):
346 return hasattr(self
, keyname
)
349 def __init__(self
, commands
):
350 self
.commands
= commands
[:]
353 for command
in self
.commands
:
361 commands
=[MPlayer
, X264
, Lame
, Wait
, MKVMerge
],
363 x264opts
=["--preset", "veryslow", "--crf", "20"],
364 lameopts
=["--preset", "medium"],
369 commands
=[MPlayer
, X264
, Wait
, MencoderCopyAC3
, MKVMerge
],
370 mplayeropts
=["-nosound"],
371 x264opts
=["--preset", "veryslow", "--crf", "20"],
377 mplayeropts
=["-ffourcc", "DX50"],
379 vopts
="fixed_quant=2:vhq=4:autoaspect",
386 commands
=[MPlayer
, X264
, Faac
, Wait
, MP4Box
],
388 x264opts
=["--crf", "20", "--bframes", "1"],
389 faacopts
=["-q", "100", "--mpeg-vers", "4"],
394 commands
=[Mencoder
, MencoderDemux
, MP4Box
],
395 mplayeropts
=["-vf-add", "scale=640:-10"],
397 vopts
="bitrate=384:vhq=4:autoaspect:max_bframes=0",
399 aopts
="br=64:mpeg=4:object=2",
407 for x
, y
in mappings
.iteritems():
408 profiles
[x
] = profiles
[y
]
414 for profile_name
in profiles
.keys():
415 if sys
.argv
[0].find(profile_name
) >= 0:
418 profile_name
= "xvid/lame"
420 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
421 parser
.add_option("--dvd", action
="store", dest
="dvd")
422 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
423 parser
.add_option("--fixmux", action
="store_true", dest
="fixmux")
424 parser
.add_option("--mc", action
="store", dest
="mc", type="int")
425 parser
.add_option("--noskip", action
="store_true", dest
="noskip")
426 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
427 parser
.add_option("--afilters", action
="store", dest
="afilters")
428 parser
.add_option("--chapter", action
="store", dest
="chapter")
429 parser
.add_option("--ifps", action
="store", dest
="ifps")
430 parser
.add_option("--skipkb", action
="store", dest
="skipkb", type="int")
431 parser
.add_option("--startpos", action
="store", dest
="startpos")
432 parser
.add_option("--endpos", action
="store", dest
="endpos")
433 parser
.add_option("--audioid", action
="store", dest
="audioid")
434 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
435 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
436 parser
.add_option("--dump", action
="store_true", dest
="dump")
438 opts
, args
= parser
.parse_args(sys
.argv
[1:])
441 output
= os
.path
.splitext(os
.path
.basename(input))[0]
450 if "://" not in input:
451 opts
.input = os
.path
.abspath(input)
454 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
457 opts
.output
= os
.path
.abspath(output
)
468 profile
= profiles
[opts
.profile_name
]
470 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
473 # Run in a temp dir so that multiple instances can be run simultaneously
474 tempdir
= tempfile
.mkdtemp()
481 profile
.commands
.insert(0, MencoderFixRemux
)
482 for CommandClass
in profile
.commands
:
483 if Command
in CommandClass
.__bases
__:
484 command
= CommandClass(profile
, opts
)
486 command
= CommandClass(commands
)
487 commands
.append(command
)
488 for command
in commands
:
491 except FatalException
, e
:
492 print >>sys
.stderr
, "Error:", str(e
)
497 shutil
.rmtree(tempdir
)
499 if __name__
== "__main__":