]>
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(
30 ["mplayer", "-frames", "0", "-identify", source
],
31 stdout
=subprocess
.PIPE
,
32 stderr
=subprocess
.PIPE
,
34 for line
in process
.stdout
:
36 key
, value
= line
.split("=")
44 class Command(object):
53 def __init__(self
, profile
, opts
):
54 self
.profile
= profile
56 self
.audio_tmp
= "audio." + self
.codec2exts
[profile
.acodec
]
57 self
.video_tmp
= "video." + self
.codec2exts
[profile
.vcodec
]
59 def print_install_message(self
):
60 print >>sys
.stderr
, "Problem with command: %s", self
.name
62 print >>sys
.stderr
, "Try running:\n# aptitude install %s", self
.package
64 def check_command(self
, cmd
):
67 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
68 raise FatalException("Command '%s' is required" % cmd
)
70 def check_no_file(self
, path
):
71 if os
.path
.exists(path
):
72 raise FatalException("Output file '%s' exists." % path
)
74 def do_exec(self
, args
):
76 print " ".join(map(mkarg
, args
))
78 if subprocess
.Popen(args
).wait() != 0:
79 raise FatalException("Failure executing command: %s" % args
)
82 class MP4Box(Command
):
84 self
.check_command("MP4Box")
85 self
.check_no_file(self
.opts
.output
+ ".mp4")
91 fps
= midentify(self
.video_tmp
, "ID_VIDEO_FPS")
93 output
= self
.opts
.output
+ ".mp4"
97 "-add", self
.video_tmp
,
98 "-add", self
.audio_tmp
,
104 class MKVMerge(Command
):
106 self
.check_command("mkvmerge")
107 self
.check_no_file(self
.opts
.output
+ ".mkv")
113 fps
= midentify(self
.video_tmp
, "ID_VIDEO_FPS")
117 "-o", self
.opts
.output
+ ".mkv",
118 "--default-duration", "0:%sfps"%fps
,
125 class MencoderMux(Command
):
127 self
.check_command("mencoder")
128 self
.check_no_file(self
.opts
.output
+ ".avi")
133 "-o", self
.opts
.output
+ ".avi",
134 "-oac", "copy", "-ovc", "copy",
135 "-noskip", "-mc", "0",
136 "-audiofile", self
.audio_tmp
,
142 class Mencoder(Command
):
144 "xvid": "-xvidencopts",
145 "x264": "-x264encopts",
147 "mp3lame": "-lameopts",
150 def insert_options(self
, cmd
):
151 def try_opt(opt
, var
):
155 if self
.opts
.deinterlace
:
156 cmd
+= ["-vf-add", "pp=lb"]
157 if self
.opts
.detelecine
:
158 self
.opts
.ofps
= "24000/1001"
159 cmd
+= ["-vf-add", "pullup,softskip"]
160 try_opt("-fps", self
.opts
.ifps
)
161 try_opt("-ofps", self
.opts
.ofps
)
162 try_opt("-ss", self
.opts
.startpos
)
163 try_opt("-endpos", self
.opts
.endpos
)
164 try_opt("-dvd-device", self
.opts
.dvd
)
165 try_opt("-chapter", self
.opts
.chapter
)
166 try_opt("-aid", self
.opts
.audioid
)
167 try_opt("-sid", self
.opts
.subtitleid
)
168 try_opt("-vf-add", self
.opts
.vfilters
)
169 try_opt("-af-add", self
.opts
.afilters
)
170 cmd
+= ["-vf-add", "harddup"]
172 def subst_values(self
, cmd
, vpass
):
174 "vbitrate": self
.opts
.vbitrate
,
175 "abitrate": self
.opts
.abitrate
,
179 return [x
% subst
for x
in cmd
]
185 if self
.opts
.copyac3
:
191 cmd
+= ["mencoder", self
.opts
.input]
192 self
.insert_options(cmd
)
193 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
194 cmd
+= ["-oac", acodec
]
196 cmd
+= [self
.codec2opts
[p
.acodec
], p
.aopts
]
197 cmd
+= self
.profile
.extra1
+ self
.profile
.extra
198 cmd
= self
.subst_values(cmd
, vpass
=n
)
205 cmd
+= ["-o", self
.audio_tmp
, "-of", "rawaudio"]
210 cmd
+= ["-o", self
.video_tmp
, "-of", "rawvideo"]
214 self
.check_command("mencoder")
215 self
.check_no_file(self
.audio_tmp
)
216 self
.check_no_file(self
.video_tmp
)
219 self
.do_exec(self
.pass1())
220 self
.do_exec(self
.pass2())
224 class Profile(object):
225 def __init__(self
, commands
, **kwargs
):
226 self
.default_opts
= {
233 self
.commands
= commands
234 self
.__dict
__.update(kwargs
)
236 def __contains__(self
, keyname
):
237 return hasattr(self
, keyname
)
243 commands
=[Mencoder
, MKVMerge
],
245 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:subq=6:frameref=6:me=umh:partitions=all:bframes=4:b_adapt:qcomp=0.7:keyint=250",
247 aopts
="abr:br=%(abitrate)d",
252 commands
=[Mencoder
, MencoderMux
],
254 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
256 aopts
="abr:br=%(abitrate)d",
261 commands
=[Mencoder
, MP4Box
],
263 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
265 aopts
="br=%(abitrate)d:mpeg=4:object=2",
270 commands
=[Mencoder
, MP4Box
],
272 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
274 aopts
="br=%(abitrate)d:mpeg=4:object=2",
275 extra
=["-vf-add", "scale=480:-10"],
280 commands
=[Mencoder
, MP4Box
],
282 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",
284 aopts
="br=%(abitrate)d:mpeg=4:object=2",
285 extra
=["-vf-add", "scale=480:-10"],
286 extra2
=["-channels", "2", "-srate", "48000"],
291 commands
=[Mencoder
, MP4Box
],
297 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
299 aopts
="br=%(abitrate)d:mpeg=4:object=2",
300 extra
=["-vf-add", "scale=640:-10"],
308 for profile_name
in profiles
.keys():
309 if sys
.argv
[0].find(profile_name
) >= 0:
312 profile_name
= "xvid"
314 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
315 parser
.add_option("--dvd", action
="store", dest
="dvd")
316 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
317 parser
.add_option("--detelecine", action
="store_true", dest
="detelecine")
318 parser
.add_option("--copyac3", action
="store_true", dest
="copyac3")
319 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
320 parser
.add_option("--afilters", action
="store", dest
="afilters")
321 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int")
322 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int")
323 parser
.add_option("--chapter", action
="store", dest
="chapter")
324 parser
.add_option("--ifps", action
="store", dest
="ifps")
325 parser
.add_option("--ofps", action
="store", dest
="ofps")
326 parser
.add_option("--startpos", action
="store", dest
="startpos")
327 parser
.add_option("--endpos", action
="store", dest
="endpos")
328 parser
.add_option("--audioid", action
="store", dest
="audioid")
329 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
330 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
331 parser
.add_option("--dump", action
="store_true", dest
="dump")
333 opts
, args
= parser
.parse_args(sys
.argv
[1:])
336 output
= os
.path
.splitext(os
.path
.basename(input))[0]
345 if "://" not in input:
346 opts
.input = os
.path
.abspath(input)
349 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
352 opts
.output
= os
.path
.abspath(output
)
363 profile
= profiles
[opts
.profile_name
]
365 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
368 # Pull in default option values from the profile
369 for key
, value
in profile
.default_opts
.iteritems():
370 if getattr(opts
, key
) is None:
371 setattr(opts
, key
, value
)
373 # Run in a temp dir so that multiple instances can be run simultaneously
374 tempdir
= tempfile
.mkdtemp()
380 for CommandClass
in profile
.commands
:
381 command
= CommandClass(profile
, opts
)
382 commands
.append(command
)
384 for command
in commands
:
387 except FatalException
, e
:
388 print >>sys
.stderr
, "Error:", str(e
)
393 shutil
.rmtree(tempdir
)
395 if __name__
== "__main__":