]>
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-add", "yadif"]
61 cmd
+= ["-vf-add", "pullup,softskip"]
65 cmd
+= ["-sb", str(o
.skipkb
* 1024)]
68 do_opt("-fps", o
.ifps
)
69 do_opt("-ss", o
.startpos
)
70 do_opt("-endpos", o
.endpos
)
71 do_opt("-dvd-device", o
.dvd
)
72 do_opt("-chapter", o
.chapter
)
73 do_opt("-aid", o
.audioid
)
74 do_opt("-sid", o
.subtitleid
)
75 do_opt("-vf-add", o
.vfilters
)
76 do_opt("-af-add", o
.afilters
)
79 class Command(object):
80 def __init__(self
, profile
, opts
):
81 self
.profile
= profile
89 def check_command(self
, cmd
):
92 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
93 raise FatalException("Command '%s' is required" % cmd
)
95 def check_no_file(self
, path
):
96 if os
.path
.exists(path
):
97 raise FatalException("Output file '%s' exists." % path
)
99 def do_exec(self
, args
, wait
=True):
101 print " ".join(map(mkarg
, args
))
103 self
.__process
= subprocess
.Popen(args
)
109 if self
.__process
== None:
111 if self
.__process
.wait() != 0:
112 raise FatalException("Failure executing command: %s" % self
.__args
)
113 self
.__process
= None
116 class MP4Box(Command
):
118 self
.check_command("MP4Box")
119 self
.check_no_file(self
.opts
.output
+ ".mp4")
128 fps
= midentify(p
.video_tmp
, "ID_VIDEO_FPS")
140 class MKVMerge(Command
):
142 self
.check_command("mkvmerge")
143 self
.check_no_file(self
.opts
.output
+ ".mkv")
152 fps
= midentify(p
.video_tmp
, "ID_VIDEO_FPS")
156 "-o", o
.output
+ ".mkv",
157 "--default-duration", "0:%sfps"%fps
,
164 class MencoderFixRemux(Command
):
166 self
.check_command("mencoder")
167 self
.check_no_file("remux.avi")
170 self
.opts
= duplicate_opts(orig
)
171 orig
.input = "remux.avi"
172 orig
.dvd
= orig
.chapter
= orig
.startpos
= orig
.endpos
= None
179 "-oac", "copy", "-ovc", "copy",
183 do_opt
= partial(append_cmd
, cmd
)
184 do_opt("-dvd-device", o
.dvd
)
185 do_opt("-chapter", o
.chapter
)
186 do_opt("-ss", o
.startpos
)
187 do_opt("-endpos", o
.endpos
)
194 class MPlayer(Command
):
196 self
.check_command("mplayer")
197 self
.check_no_file("video.y4m")
198 self
.check_no_file("audio.wav")
201 os
.mkfifo("video.y4m")
202 os
.mkfifo("audio.wav")
204 cmd
+= ["mplayer", self
.opts
.input]
205 cmd
+= ["-benchmark", "-noconsolecontrols", "-noconfig", "all"]
206 cmd
+= ["-vo", "yuv4mpeg:file=video.y4m"]
207 cmd
+= ["-ao", "pcm:waveheader:file=audio.wav"]
208 insert_mplayer_options(cmd
, self
.opts
)
209 cmd
+= self
.profile
.extra
210 self
.do_exec(cmd
, wait
=False)
215 self
.check_command("x264")
216 self
.profile
.video_tmp
= "video.h264"
221 cmd
+= ["x264", "--no-progress"]
223 cmd
+= ["-o", p
.video_tmp
]
225 self
.do_exec(cmd
, wait
=False)
230 self
.check_command("lame")
231 self
.profile
.audio_tmp
= "audio.mp3"
236 cmd
+= ["lame", "--quiet"]
240 self
.do_exec(cmd
, wait
=False)
245 self
.check_command("faac")
246 self
.profile
.audio_tmp
= "audio.aac"
252 cmd
+= ["-o", p
.audio_tmp
]
255 self
.do_exec(cmd
, wait
=False)
258 class Mencoder(Command
):
260 "xvid": "-xvidencopts",
261 "x264": "-x264encopts",
263 "mp3lame": "-lameopts",
270 self
.check_command("mencoder")
271 self
.check_no_file(o
.output
+ ".avi")
273 p
.video_tmp
= o
.output
+ ".avi"
274 p
.audio_tmp
= o
.output
+ ".avi"
276 if o
.deinterlace
and o
.detelecine
:
277 raise FatalException("Cannot use --detelecine with --deinterlace")
284 cmd
+= ["mencoder", o
.input]
285 insert_mplayer_options(cmd
, o
)
286 cmd
+= ["-vf-add", "harddup"]
287 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
288 cmd
+= ["-oac", p
.acodec
]
290 cmd
+= [self
.codec2opts
[p
.acodec
], p
.aopts
]
291 cmd
+= self
.profile
.extra
292 cmd
+= ["-o", self
.opts
.output
+ ".avi"]
297 class MencoderDemux(Command
):
310 self
.check_command("mencoder")
311 p
.audio_tmp
= "audio." + self
.codec2exts
[p
.acodec
]
312 p
.video_tmp
= "video." + self
.codec2exts
[p
.vcodec
]
313 self
.check_no_file(p
.audio_tmp
)
314 self
.check_no_file(p
.video_tmp
)
320 cmd
= ["mencoder", "-ovc", "copy", "-oac", "copy", o
.output
+ ".avi"]
321 self
.do_exec(cmd
+ ["-of", "rawaudio", "-o", p
.audio_tmp
])
322 self
.do_exec(cmd
+ ["-of", "rawvideo", "-o", p
.video_tmp
])
323 self
.do_exec(["rm", "-f", o
.output
+ ".avi"])
327 class Profile(object):
328 def __init__(self
, commands
, **kwargs
):
330 self
.commands
= commands
331 self
.__dict
__.update(kwargs
)
333 def __contains__(self
, keyname
):
334 return hasattr(self
, keyname
)
337 def __init__(self
, commands
):
338 self
.commands
= commands
[:]
341 for command
in self
.commands
:
349 commands
=[MPlayer
, X264
, Lame
, Wait
, MKVMerge
],
350 x264opts
=["--preset", "veryslow", "--crf", "19"],
351 lameopts
=["--preset", "extreme"],
358 vopts
="fixed_quant=2:vhq=4:autoaspect",
365 commands
=[MPlayer
, X264
, Faac
, Wait
, MP4Box
],
366 x264opts
=["--crf", "19", "--bframes", "1"],
367 faacopts
=["-q", "100", "--mpeg-vers", "4"],
372 commands
=[Mencoder
, MencoderDemux
, MP4Box
],
374 vopts
="bitrate=256:vhq=4:autoaspect:max_bframes=0",
376 aopts
="br=64:mpeg=4:object=2",
377 extra
=["-vf-add", "scale=640:-10"],
385 for profile_name
in profiles
.keys():
386 if sys
.argv
[0].find(profile_name
) >= 0:
389 profile_name
= "xvid"
391 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
392 parser
.add_option("--dvd", action
="store", dest
="dvd")
393 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
394 parser
.add_option("--detelecine", action
="store_true", dest
="detelecine")
395 parser
.add_option("--fixmux", action
="store_true", dest
="fixmux")
396 parser
.add_option("--mc", action
="store", dest
="mc", type="int")
397 parser
.add_option("--noskip", action
="store_true", dest
="noskip")
398 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
399 parser
.add_option("--afilters", action
="store", dest
="afilters")
400 parser
.add_option("--chapter", action
="store", dest
="chapter")
401 parser
.add_option("--ifps", action
="store", dest
="ifps")
402 parser
.add_option("--skipkb", action
="store", dest
="skipkb", type="int")
403 parser
.add_option("--startpos", action
="store", dest
="startpos")
404 parser
.add_option("--endpos", action
="store", dest
="endpos")
405 parser
.add_option("--audioid", action
="store", dest
="audioid")
406 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
407 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
408 parser
.add_option("--dump", action
="store_true", dest
="dump")
410 opts
, args
= parser
.parse_args(sys
.argv
[1:])
413 output
= os
.path
.splitext(os
.path
.basename(input))[0]
422 if "://" not in input:
423 opts
.input = os
.path
.abspath(input)
426 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
429 opts
.output
= os
.path
.abspath(output
)
440 profile
= profiles
[opts
.profile_name
]
442 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
445 # Run in a temp dir so that multiple instances can be run simultaneously
446 tempdir
= tempfile
.mkdtemp()
453 profile
.commands
.insert(0, MencoderFixRemux
)
454 for CommandClass
in profile
.commands
:
455 if Command
in CommandClass
.__bases
__:
456 command
= CommandClass(profile
, opts
)
458 command
= CommandClass(commands
)
459 commands
.append(command
)
460 for command
in commands
:
463 except FatalException
, e
:
464 print >>sys
.stderr
, "Error:", str(e
)
469 shutil
.rmtree(tempdir
)
471 if __name__
== "__main__":