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