]>
code.delx.au - transcoding/blob - encode.py
bac7ce3f19afe275fe51ce7db2533c1207956461
11 class FatalException(Exception):
15 if re
.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg
):
28 class Command(object):
29 def __init__(self
, profile
, opts
):
30 self
.profile
= profile
33 def print_install_message(self
):
34 print >>sys
.stderr
, "Problem with command: %s", self
.name
36 print >>sys
.stderr
, "Try running:\n# aptitude install %s", self
.package
38 def check_command(self
, cmd
):
41 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
42 raise FatalException("Command '%s' is required" % cmd
)
44 def check_no_file(self
, path
):
45 if os
.path
.exists(path
):
46 raise FatalException("Output file '%s' exists." % path
)
48 def do_exec(self
, args
):
50 print " ".join(map(mkarg
, args
))
52 if subprocess
.Popen(args
).wait() != 0:
53 raise FatalException("Failure executing command: %s" % args
)
56 class MP4Box(Command
):
64 self
.check_command("mencoder")
65 self
.check_command("MP4Box")
66 self
.check_no_file(self
.opts
.output
+ ".mp4")
70 video
= "video.%s" % self
.codec2exts
[p
.vcodec
]
71 audio
= "audio.%s" % self
.codec2exts
[p
.acodec
]
72 input = self
.opts
.output
+ ".avi" # From Mencoder command
73 output
= self
.opts
.output
+ ".mp4"
74 mencoder
= ["mencoder", input, "-ovc", "copy", "-oac", "copy", "-of"]
75 self
.do_exec(["rm", "-f", output
])
76 self
.do_exec(mencoder
+ ["rawvideo", "-o", video
])
77 self
.do_exec(mencoder
+ ["rawaudio", "-o", audio
])
78 self
.do_exec(["MP4Box", "-add", video
, "-add", audio
, output
])
79 self
.do_exec(["rm", "-f", video
, audio
, input])
83 class MKVMerge(Command
):
85 self
.check_command("mkvmerge")
86 self
.check_no_file(self
.opts
.output
+ ".mkv")
89 input = self
.opts
.output
+ ".avi" # From Mencoder command
90 output
= self
.opts
.output
+ ".mkv"
91 self
.do_exec(["mkvmerge", "-o", output
, input])
92 self
.do_exec(["rm", "-f", input])
96 class Mencoder(Command
):
99 "xvid": "-xvidencopts",
100 "x264": "-x264encopts",
102 "mp3lame": "-lameopts",
105 def insert_options(self
, cmd
):
106 def try_opt(opt
, var
):
110 if self
.opts
.deinterlace
:
111 cmd
+= ["-vf-add", "pp=lb"]
112 if self
.opts
.detelecine
:
113 self
.opts
.ofps
= "24000/1001"
114 cmd
+= ["-vf-add", "pullup,softskip"]
115 try_opt("-fps", self
.opts
.ifps
)
116 try_opt("-ofps", self
.opts
.ofps
)
117 try_opt("-ss", self
.opts
.startpos
)
118 try_opt("-endpos", self
.opts
.endpos
)
119 try_opt("-dvd-device", self
.opts
.dvd
)
120 try_opt("-chapter", self
.opts
.chapter
)
121 try_opt("-aid", self
.opts
.audioid
)
122 try_opt("-sid", self
.opts
.subtitleid
)
123 try_opt("-vf-add", self
.opts
.vfilters
)
124 try_opt("-af", self
.opts
.afilters
)
126 def subst_values(self
, cmd
, vpass
):
128 "vbitrate": self
.opts
.vbitrate
,
129 "abitrate": self
.opts
.abitrate
,
130 "input": self
.opts
.input,
131 "output": self
.opts
.output
+ ".avi",
135 return [x
% subst
for x
in cmd
]
140 cmd
+= ["mencoder", "%(input)s", "-o", "/dev/null"]
141 self
.insert_options(cmd
)
142 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
143 cmd
+= ["-oac", "copy"]
144 cmd
+= self
.profile
.extra1
+ self
.profile
.extra
145 cmd
= self
.subst_values(cmd
, vpass
=1)
151 cmd
+= ["mencoder", "%(input)s", "-o", "%(output)s"]
152 self
.insert_options(cmd
)
153 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
154 cmd
+= ["-oac", p
.acodec
, self
.codec2opts
[p
.acodec
], p
.aopts
]
155 if self
.opts
.episode_name
:
156 cmd
+= ["-info", "name='%s'" % self
.opts
.episode_name
]
157 cmd
+= self
.profile
.extra2
+ self
.profile
.extra
158 cmd
= self
.subst_values(cmd
, vpass
=2)
162 self
.check_command("mencoder")
163 self
.check_no_file(self
.opts
.output
+ ".avi")
166 self
.do_exec(self
.pass1())
167 self
.do_exec(self
.pass2())
171 class Profile(object):
172 def __init__(self
, commands
, **kwargs
):
173 self
.default_opts
= {
180 self
.commands
= commands
181 self
.__dict
__.update(kwargs
)
183 def __contains__(self
, keyname
):
184 return hasattr(self
, keyname
)
190 commands
=[Mencoder
, MP4Box
],
192 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
194 aopts
="br=%(abitrate)d:mpeg=4:object=2",
195 extra
=["-vf-add", "harddup"],
200 commands
=[Mencoder
, MKVMerge
],
202 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:subq=6:frameref=6:me=umh:partitions=all:bframes=4:b_adapt:qcomp=0.7:keyint=250",
204 aopts
="abr:br=%(abitrate)d",
205 extra
=["-vf-add", "harddup"],
212 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
214 aopts
="abr:br=%(abitrate)d",
215 extra2
=["-ffourcc", "DX50"],
220 commands
=[Mencoder
, MP4Box
],
222 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
224 aopts
="br=%(abitrate)d:mpeg=4:object=2",
225 extra
=["-vf-add", "scale=480:-10,harddup"],
230 commands
=[Mencoder
, MP4Box
],
232 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",
234 aopts
="br=%(abitrate)d:mpeg=4:object=2",
235 extra
=["-vf-add", "scale=480:-10,harddup"],
236 extra2
=["-channels", "2", "-srate", "48000"],
241 commands
=[Mencoder
, MP4Box
],
247 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto",
249 aopts
="br=%(abitrate)d:mpeg=4:object=2",
250 extra
=["-vf-add", "scale=320:-10,harddup"],
255 commands
=[Mencoder
, MP4Box
],
261 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
263 aopts
="br=%(abitrate)d:mpeg=4:object=2",
264 extra
=["-vf-add", "scale=640:-10,harddup"],
269 commands
=[Mencoder
, MP4Box
],
275 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vbv_maxrate=2000:vbv_bufsize=2000:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto:level_idc=20",
277 aopts
="br=%(abitrate)d:mpeg=4:object=2",
278 extra
=["-vf-add", "scale=640:-10,harddup"],
286 for profile_name
in profiles
.keys():
287 if sys
.argv
[0].find(profile_name
) >= 0:
290 profile_name
= "xvid"
292 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
293 parser
.add_option("--dvd", action
="store", dest
="dvd")
294 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
295 parser
.add_option("--detelecine", action
="store_true", dest
="detelecine")
296 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
297 parser
.add_option("--afilters", action
="store", dest
="afilters")
298 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int")
299 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int")
300 parser
.add_option("--chapter", action
="store", dest
="chapter")
301 parser
.add_option("--ifps", action
="store", dest
="ifps")
302 parser
.add_option("--ofps", action
="store", dest
="ofps")
303 parser
.add_option("--startpos", action
="store", dest
="startpos")
304 parser
.add_option("--endpos", action
="store", dest
="endpos")
305 parser
.add_option("--audioid", action
="store", dest
="audioid")
306 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
307 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
308 parser
.add_option("--episode-name", action
="store", dest
="episode_name")
309 parser
.add_option("--dump", action
="store_true", dest
="dump")
311 opts
, args
= parser
.parse_args(sys
.argv
[1:])
314 output
= os
.path
.splitext(os
.path
.basename(input))[0]
323 if "://" not in input:
324 opts
.input = os
.path
.abspath(input)
327 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
330 opts
.output
= os
.path
.abspath(output
)
339 profile
= profiles
[opts
.profile_name
]
341 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
344 # Pull in default option values from the profile
345 for key
, value
in profile
.default_opts
.iteritems():
346 if getattr(opts
, key
) is None:
347 setattr(opts
, key
, value
)
349 # Run in a temp dir so that multiple instances can be run simultaneously
350 tempdir
= tempfile
.mkdtemp()
356 for CommandClass
in profile
.commands
:
357 command
= CommandClass(profile
, opts
)
358 commands
.append(command
)
360 for command
in commands
:
363 except FatalException
, e
:
364 print >>sys
.stderr
, "Error:", str(e
)
369 shutil
.rmtree(tempdir
)
371 if __name__
== "__main__":