]> code.delx.au - transcoding/blob - encode.py
Use linear blend deinterlacing, it looks much better
[transcoding] / encode.py
1 #!/usr/bin/env python
2
3 import commands
4 import optparse
5 import subprocess
6 import sys
7 import os
8 import tempfile
9 import shutil
10
11 class FatalException(Exception):
12 pass
13
14 class Command(object):
15 def __init__(self, profile, opts):
16 self.profile = profile
17 self.opts = opts
18
19 def print_install_message(self):
20 print >>sys.stderr, "Problem with command: %s", self.name
21 if self.package:
22 print >>sys.stderr, "Try running:\n# aptitude install %s", self.package
23
24 def check_command(self, cmd):
25 if self.opts.dump:
26 return
27 if subprocess.Popen(["which", cmd], stdout=open("/dev/null", "w")).wait() != 0:
28 raise FatalException("Command '%s' is required" % cmd)
29
30 def check_no_file(self, path):
31 if os.path.exists(path):
32 raise FatalException("Output file '%s' exists." % path)
33
34 def do_exec(self, args):
35 if self.opts.dump:
36 print "".join(map(commands.mkarg, args))[1:]
37 else:
38 if subprocess.Popen(args).wait() != 0:
39 raise FatalException("Failure executing command: %s" % args)
40
41
42 class MP4Box(Command):
43 codec2exts = {
44 "xvid": "m4v",
45 "x264": "h264",
46 "faac": "aac",
47 }
48
49 def check(self):
50 self.check_command("mencoder")
51 self.check_command("MP4Box")
52 self.check_no_file(self.opts.output + ".mp4")
53
54 def run(self):
55 p = self.profile
56 video = "video.%s" % self.codec2exts[p.vcodec]
57 audio = "audio.%s" % self.codec2exts[p.acodec]
58 input = self.opts.output + ".avi" # From Mencoder command
59 output = self.opts.output + ".mp4"
60 mencoder = ["mencoder", input, "-ovc", "copy", "-oac", "copy", "-of"]
61 self.do_exec(["rm", "-f", output])
62 self.do_exec(mencoder + ["rawvideo", "-o", video])
63 self.do_exec(mencoder + ["rawaudio", "-o", audio])
64 self.do_exec(["MP4Box", "-add", video, "-add", audio, output])
65 self.do_exec(["rm", "-f", video, audio, input])
66
67
68 class Mencoder(Command):
69 codec2opts = {
70 "lavc": "-lavcopts",
71 "xvid": "-xvidencopts",
72 "x264": "-x264encopts",
73 "faac": "-faacopts",
74 "mp3lame": "-lameopts",
75 }
76
77 def insert_options(self, cmd):
78 def try_opt(opt, var):
79 if var is not None:
80 cmd.append(opt)
81 cmd.append(var)
82 if self.opts.deinterlace:
83 cmd += ["-vf-add", "pp=lb"]
84 try_opt("-ss", self.opts.startpos)
85 try_opt("-endpos", self.opts.endpos)
86 try_opt("-dvd-device", self.opts.dvd)
87 try_opt("-chapter", self.opts.chapter)
88 try_opt("-aid", self.opts.audioid)
89 try_opt("-sid", self.opts.subtitleid)
90 try_opt("-vf-add", self.opts.vfilters)
91 try_opt("-af", self.opts.afilters)
92
93 def subst_values(self, cmd, vpass):
94 subst = {
95 "vbitrate": self.opts.vbitrate,
96 "abitrate": self.opts.abitrate,
97 "input": self.opts.input,
98 "output": self.opts.output + ".avi",
99 "vpass": vpass,
100 }
101
102 return [x % subst for x in cmd]
103
104 def pass1(self):
105 p = self.profile
106 cmd = []
107 cmd += ["mencoder", "%(input)s", "-o", "/dev/null"]
108 self.insert_options(cmd)
109 cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
110 cmd += ["-oac", "copy"]
111 cmd += self.profile.extra + self.profile.extra1
112 cmd = self.subst_values(cmd, vpass=1)
113 return cmd
114
115 def pass2(self):
116 p = self.profile
117 cmd = []
118 cmd += ["mencoder", "%(input)s", "-o", "%(output)s"]
119 self.insert_options(cmd)
120 cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
121 cmd += ["-oac", p.acodec, self.codec2opts[p.acodec], p.aopts]
122 if self.opts.episode_name:
123 cmd += ["-info", "name='%s'" % self.opts.episode_name]
124 cmd += self.profile.extra + self.profile.extra2
125 cmd = self.subst_values(cmd, vpass=2)
126 return cmd
127
128 def check(self):
129 self.check_command("mencoder")
130 self.check_no_file(self.opts.output + ".avi")
131
132 def run(self):
133 self.do_exec(self.pass1())
134 self.do_exec(self.pass2())
135
136
137
138 class Profile(object):
139 def __init__(self, commands, **kwargs):
140 self.default_opts = {
141 "vbitrate": 1000,
142 "abitrate": 192,
143 }
144 self.extra = []
145 self.extra1 = []
146 self.extra2 = []
147 self.commands = commands
148 self.__dict__.update(kwargs)
149
150 def __contains__(self, keyname):
151 return hasattr(self, keyname)
152
153
154 profiles = {
155 "qt7" :
156 Profile(
157 commands=[Mencoder, MP4Box],
158 vcodec="x264",
159 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
160 acodec="faac",
161 aopts="br=%(abitrate)d:mpeg=4:object=2",
162 ),
163
164 "xvid" :
165 Profile(
166 commands=[Mencoder],
167 vcodec="xvid",
168 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
169 acodec="mp3lame",
170 aopts="abr:br=%(abitrate)d",
171 extra2=["-ffourcc", "DX50"],
172 ),
173
174 "ipodxvid" :
175 Profile(
176 commands=[Mencoder, MP4Box],
177 vcodec="xvid",
178 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
179 acodec="faac",
180 aopts="br=%(abitrate)d:mpeg=4:object=2",
181 extra=["-vf-add", "scale=480:-10"],
182 ),
183
184 "ipodx264" :
185 Profile(
186 commands=[Mencoder, MP4Box],
187 vcodec="x264",
188 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",
189 acodec="faac",
190 aopts="br=%(abitrate)d:mpeg=4:object=2",
191 extra=["-vf-add", "scale=480:-10"],
192 extra2=["-channels", "2", "-srate", "48000"],
193 ),
194
195 "nokiax264" :
196 Profile(
197 commands=[Mencoder, MP4Box],
198 default_opts={
199 "vbitrate": 256,
200 "abitrate": 96,
201 },
202 vcodec="x264",
203 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto",
204 acodec="faac",
205 aopts="br=%(abitrate)d:mpeg=4:object=2",
206 extra=["-vf-add", "scale=320:-10"],
207 ),
208
209 "n97xvid" :
210 Profile(
211 commands=[Mencoder, MP4Box],
212 default_opts={
213 "vbitrate": 1000,
214 "abitrate": 96,
215 },
216 vcodec="xvid",
217 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
218 acodec="faac",
219 aopts="br=%(abitrate)d:mpeg=4:object=2",
220 extra=["-vf-add", "scale=640:-10"],
221 ),
222
223 "n97x264" :
224 Profile(
225 commands=[Mencoder, MP4Box],
226 default_opts={
227 "vbitrate": 1000,
228 "abitrate": 96,
229 },
230 vcodec="x264",
231 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",
232 acodec="faac",
233 aopts="br=%(abitrate)d:mpeg=4:object=2",
234 extra=["-vf-add", "scale=640:-10"],
235 ),
236 }
237
238
239
240
241 def parse_args():
242 for profile_name in profiles.keys():
243 if sys.argv[0].find(profile_name) >= 0:
244 break
245 else:
246 profile_name = "xvid"
247
248 parser = optparse.OptionParser(usage="%prog [options] input [output]")
249 parser.add_option("--dvd", action="store", dest="dvd")
250 parser.add_option("--deinterlace", action="store_true", dest="deinterlace")
251 parser.add_option("--vfilters", action="store", dest="vfilters")
252 parser.add_option("--afilters", action="store", dest="afilters")
253 parser.add_option("--vbitrate", action="store", dest="vbitrate", type="int")
254 parser.add_option("--abitrate", action="store", dest="abitrate", type="int")
255 parser.add_option("--chapter", action="store", dest="chapter")
256 parser.add_option("--startpos", action="store", dest="startpos")
257 parser.add_option("--endpos", action="store", dest="endpos")
258 parser.add_option("--audioid", action="store", dest="audioid")
259 parser.add_option("--subtitleid", action="store", dest="subtitleid")
260 parser.add_option("--profile", action="store", dest="profile_name", default=profile_name)
261 parser.add_option("--episode-name", action="store", dest="episode_name")
262 parser.add_option("--dump", action="store_true", dest="dump")
263 try:
264 opts, args = parser.parse_args(sys.argv[1:])
265 if len(args) == 1:
266 input = args[0]
267 output = os.path.splitext(os.path.basename(input))[0]
268 elif len(args) == 2:
269 input, output = args
270 else:
271 raise ValueError
272 except Exception:
273 parser.print_usage()
274 sys.exit(1)
275
276 if "://" not in input:
277 opts.input = os.path.abspath(input)
278 else:
279 if opts.dvd:
280 opts.dvd = os.path.abspath(opts.dvd)
281 opts.input = input
282
283 opts.output = os.path.abspath(output)
284
285 return opts
286
287 def main():
288 opts = parse_args()
289
290 # Find our profile
291 try:
292 profile = profiles[opts.profile_name]
293 except KeyError:
294 print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name
295 sys.exit(1)
296
297 # Pull in default option values from the profile
298 for key, value in profile.default_opts.iteritems():
299 if getattr(opts, key) is None:
300 setattr(opts, key, value)
301
302 # Run in a temp dir so that multiple instances can be run simultaneously
303 tempdir = tempfile.mkdtemp()
304 try:
305 os.chdir(tempdir)
306
307 try:
308 commands = []
309 for CommandClass in profile.commands:
310 command = CommandClass(profile, opts)
311 commands.append(command)
312 command.check()
313 for command in commands:
314 command.run()
315
316 except FatalException, e:
317 print >>sys.stderr, "Error:", e.message
318 sys.exit(1)
319
320 finally:
321 os.chdir("/")
322 shutil.rmtree(tempdir)
323
324 if __name__ == "__main__":
325 main()
326