]>
code.delx.au - transcoding/blob - encode.py
12 class FatalException(Exception):
16 quoted_arg
= commands
.mkarg(arg
)
17 arg
= quoted_arg
[2:-1]
18 if len(shlex
.split(arg
)) == 1:
23 class Command(object):
24 def __init__(self
, profile
, opts
):
25 self
.profile
= profile
28 def print_install_message(self
):
29 print >>sys
.stderr
, "Problem with command: %s", self
.name
31 print >>sys
.stderr
, "Try running:\n# aptitude install %s", self
.package
33 def check_command(self
, cmd
):
36 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
37 raise FatalException("Command '%s' is required" % cmd
)
39 def check_no_file(self
, path
):
40 if os
.path
.exists(path
):
41 raise FatalException("Output file '%s' exists." % path
)
43 def do_exec(self
, args
):
45 print " ".join(map(mkarg
, args
))
47 if subprocess
.Popen(args
).wait() != 0:
48 raise FatalException("Failure executing command: %s" % args
)
51 class MP4Box(Command
):
59 self
.check_command("mencoder")
60 self
.check_command("MP4Box")
61 self
.check_no_file(self
.opts
.output
+ ".mp4")
65 video
= "video.%s" % self
.codec2exts
[p
.vcodec
]
66 audio
= "audio.%s" % self
.codec2exts
[p
.acodec
]
67 input = self
.opts
.output
+ ".avi" # From Mencoder command
68 output
= self
.opts
.output
+ ".mp4"
69 mencoder
= ["mencoder", input, "-ovc", "copy", "-oac", "copy", "-of"]
70 self
.do_exec(["rm", "-f", output
])
71 self
.do_exec(mencoder
+ ["rawvideo", "-o", video
])
72 self
.do_exec(mencoder
+ ["rawaudio", "-o", audio
])
73 self
.do_exec(["MP4Box", "-add", video
, "-add", audio
, output
])
74 self
.do_exec(["rm", "-f", video
, audio
, input])
78 class MKVMerge(Command
):
80 self
.check_command("mkvmerge")
81 self
.check_no_file(self
.opts
.output
+ ".mkv")
84 input = self
.opts
.output
+ ".avi" # From Mencoder command
85 output
= self
.opts
.output
+ ".mkv"
86 self
.do_exec(["mkvmerge", "-o", output
, input])
87 self
.do_exec(["rm", "-f", input])
91 class Mencoder(Command
):
94 "xvid": "-xvidencopts",
95 "x264": "-x264encopts",
97 "mp3lame": "-lameopts",
100 def insert_options(self
, cmd
):
101 def try_opt(opt
, var
):
105 if self
.opts
.deinterlace
:
106 cmd
+= ["-vf-add", "pp=lb"]
107 try_opt("-ss", self
.opts
.startpos
)
108 try_opt("-endpos", self
.opts
.endpos
)
109 try_opt("-dvd-device", self
.opts
.dvd
)
110 try_opt("-chapter", self
.opts
.chapter
)
111 try_opt("-aid", self
.opts
.audioid
)
112 try_opt("-sid", self
.opts
.subtitleid
)
113 try_opt("-vf-add", self
.opts
.vfilters
)
114 try_opt("-af", self
.opts
.afilters
)
116 def subst_values(self
, cmd
, vpass
):
118 "vbitrate": self
.opts
.vbitrate
,
119 "abitrate": self
.opts
.abitrate
,
120 "input": self
.opts
.input,
121 "output": self
.opts
.output
+ ".avi",
125 return [x
% subst
for x
in cmd
]
130 cmd
+= ["mencoder", "%(input)s", "-o", "/dev/null"]
131 self
.insert_options(cmd
)
132 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
133 cmd
+= ["-oac", "copy"]
134 cmd
+= self
.profile
.extra
+ self
.profile
.extra1
135 cmd
= self
.subst_values(cmd
, vpass
=1)
141 cmd
+= ["mencoder", "%(input)s", "-o", "%(output)s"]
142 self
.insert_options(cmd
)
143 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
144 cmd
+= ["-oac", p
.acodec
, self
.codec2opts
[p
.acodec
], p
.aopts
]
145 if self
.opts
.episode_name
:
146 cmd
+= ["-info", "name='%s'" % self
.opts
.episode_name
]
147 cmd
+= self
.profile
.extra
+ self
.profile
.extra2
148 cmd
= self
.subst_values(cmd
, vpass
=2)
152 self
.check_command("mencoder")
153 self
.check_no_file(self
.opts
.output
+ ".avi")
156 self
.do_exec(self
.pass1())
157 self
.do_exec(self
.pass2())
161 class Profile(object):
162 def __init__(self
, commands
, **kwargs
):
163 self
.default_opts
= {
170 self
.commands
= commands
171 self
.__dict
__.update(kwargs
)
173 def __contains__(self
, keyname
):
174 return hasattr(self
, keyname
)
180 commands
=[Mencoder
, MP4Box
],
182 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
184 aopts
="br=%(abitrate)d:mpeg=4:object=2",
189 commands
=[Mencoder
, MKVMerge
],
191 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:subq=6:frameref=6:me=umh:partitions=all:bframes=4:b_adapt:qcomp=0.7:keyint=250:threads=2",
193 aopts
="abr:br=%(abitrate)d",
200 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
202 aopts
="abr:br=%(abitrate)d",
203 extra2
=["-ffourcc", "DX50"],
208 commands
=[Mencoder
, MP4Box
],
210 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
212 aopts
="br=%(abitrate)d:mpeg=4:object=2",
213 extra
=["-vf-add", "scale=480:-10"],
218 commands
=[Mencoder
, MP4Box
],
220 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",
222 aopts
="br=%(abitrate)d:mpeg=4:object=2",
223 extra
=["-vf-add", "scale=480:-10"],
224 extra2
=["-channels", "2", "-srate", "48000"],
229 commands
=[Mencoder
, MP4Box
],
235 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto",
237 aopts
="br=%(abitrate)d:mpeg=4:object=2",
238 extra
=["-vf-add", "scale=320:-10"],
243 commands
=[Mencoder
, MP4Box
],
249 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
251 aopts
="br=%(abitrate)d:mpeg=4:object=2",
252 extra
=["-vf-add", "scale=640:-10"],
257 commands
=[Mencoder
, MP4Box
],
263 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",
265 aopts
="br=%(abitrate)d:mpeg=4:object=2",
266 extra
=["-vf-add", "scale=640:-10"],
274 for profile_name
in profiles
.keys():
275 if sys
.argv
[0].find(profile_name
) >= 0:
278 profile_name
= "xvid"
280 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
281 parser
.add_option("--dvd", action
="store", dest
="dvd")
282 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
283 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
284 parser
.add_option("--afilters", action
="store", dest
="afilters")
285 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int")
286 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int")
287 parser
.add_option("--chapter", action
="store", dest
="chapter")
288 parser
.add_option("--startpos", action
="store", dest
="startpos")
289 parser
.add_option("--endpos", action
="store", dest
="endpos")
290 parser
.add_option("--audioid", action
="store", dest
="audioid")
291 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
292 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
293 parser
.add_option("--episode-name", action
="store", dest
="episode_name")
294 parser
.add_option("--dump", action
="store_true", dest
="dump")
296 opts
, args
= parser
.parse_args(sys
.argv
[1:])
299 output
= os
.path
.splitext(os
.path
.basename(input))[0]
308 if "://" not in input:
309 opts
.input = os
.path
.abspath(input)
312 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
315 opts
.output
= os
.path
.abspath(output
)
324 profile
= profiles
[opts
.profile_name
]
326 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
329 # Pull in default option values from the profile
330 for key
, value
in profile
.default_opts
.iteritems():
331 if getattr(opts
, key
) is None:
332 setattr(opts
, key
, value
)
334 # Run in a temp dir so that multiple instances can be run simultaneously
335 tempdir
= tempfile
.mkdtemp()
341 for CommandClass
in profile
.commands
:
342 command
= CommandClass(profile
, opts
)
343 commands
.append(command
)
345 for command
in commands
:
348 except FatalException
, e
:
349 print >>sys
.stderr
, "Error:", e
.message
354 shutil
.rmtree(tempdir
)
356 if __name__
== "__main__":