]> code.delx.au - transcoding/blob - encode.py
Formatting changes
[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 MencoderCommand(object):
12 codec2opts = {
13 "lavc": "-lavcopts",
14 "xvid": "-xvidencopts",
15 "x264": "-x264encopts",
16 "faac": "-faacopts",
17 "mp3lame": "-lameopts",
18 }
19
20 def __init__(self, profile, opts):
21 self.profile = profile
22 self.opts = opts
23
24 def insert_options(self, cmd):
25 def try_opt(opt, var):
26 if var is not None:
27 cmd.append(opt)
28 cmd.append(var)
29 if self.opts.deinterlace:
30 cmd += ["-vf-add", "pp=ci"]
31 try_opt("-ss", self.opts.startpos)
32 try_opt("-endpos", self.opts.endpos)
33 try_opt("-dvd-device", self.opts.dvd)
34 try_opt("-chapter", self.opts.chapter)
35 try_opt("-aid", self.opts.audioid)
36 try_opt("-sid", self.opts.subtitleid)
37 try_opt("-vf-add", self.opts.vfilters)
38 try_opt("-af", self.opts.afilters)
39
40 def subst_values(self, cmd, vpass):
41 subst = {
42 "vbitrate": self.opts.vbitrate,
43 "abitrate": self.opts.abitrate,
44 "input": self.opts.input,
45 "output": self.opts.output,
46 "vpass": vpass,
47 }
48
49 return [x % subst for x in cmd]
50
51 def pass1(self):
52 p = self.profile
53 cmd = []
54 cmd += ["mencoder", "%(input)s", "-o", "/dev/null"]
55 self.insert_options(cmd)
56 cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
57 cmd += ["-oac", "copy"]
58 cmd = self.subst_values(cmd, vpass=1)
59 return cmd
60
61 def pass2(self):
62 p = self.profile
63 cmd = []
64 cmd += ["mencoder", "%(input)s", "-o", "%(output)s"]
65 self.insert_options(cmd)
66 cmd += ["-ovc", p.vcodec, self.codec2opts[p.vcodec], p.vopts]
67 cmd += ["-oac", p.acodec, self.codec2opts[p.acodec], p.aopts]
68 if self.opts.episode_name:
69 cmd += ["-info", "name='%s'" % self.opts.episode_name]
70 cmd += self.profile.extra
71 cmd = self.subst_values(cmd, vpass=2)
72 return cmd
73
74 class Profile(object):
75 def __init__(self, CommandClass, **kwargs):
76 self.extra = []
77
78 self.CommandClass = CommandClass
79 self.__dict__.update(kwargs)
80 def __contains__(self, keyname):
81 return hasattr(self, keyname)
82
83 profiles = {
84 "qt7" :
85 Profile(
86 CommandClass=MencoderCommand,
87 vcodec="x264",
88 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
89 acodec="faac",
90 aopts="br=%(abitrate)d:mpeg=4:object=2",
91 ),
92
93 "xvid" :
94 Profile(
95 CommandClass=MencoderCommand,
96 vcodec="xvid",
97 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
98 acodec="mp3lame",
99 aopts="abr:br=%(abitrate)d",
100 extra=["-ffourcc", "DX50"],
101 ),
102
103 "ipodxvid" :
104 Profile(
105 CommandClass=MencoderCommand,
106 vcodec="xvid",
107 vopts="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
108 acodec="faac",
109 aopts="br=%(abitrate)d:mpeg=4:object=2",
110 ),
111
112 "ipod264" :
113 Profile(
114 CommandClass=MencoderCommand,
115 vcodec="x264",
116 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:global_header:turbo",
117 acodec="faac",
118 aopts="br=%(abitrate)d:mpeg=4:object=2:raw",
119 extra=["-of", "lavf", "-lavfopts", "format=mp4", "-channels", "2", "-srate", "48000"]
120 ),
121 }
122
123
124
125
126 def parse_args():
127 for profile_name in profiles.keys():
128 if sys.argv[0].find(profile_name) >= 0:
129 break
130 else:
131 profile_name = "xvid"
132
133 parser = optparse.OptionParser(usage="%prog [options] input output")
134 parser.add_option("--dvd", action="store", dest="dvd")
135 parser.add_option("--deinterlace", action="store_true", dest="deinterlace")
136 parser.add_option("--vfilters", action="store", dest="vfilters")
137 parser.add_option("--afilters", action="store", dest="afilters")
138 parser.add_option("--vbitrate", action="store", dest="vbitrate", type="int", default=1000)
139 parser.add_option("--abitrate", action="store", dest="abitrate", type="int", default=192)
140 parser.add_option("--chapter", action="store", dest="chapter")
141 parser.add_option("--startpos", action="store", dest="startpos")
142 parser.add_option("--endpos", action="store", dest="endpos")
143 parser.add_option("--audioid", action="store", dest="audioid")
144 parser.add_option("--subtitleid", action="store", dest="subtitleid")
145 parser.add_option("--profile", action="store", dest="profile_name", default=profile_name)
146 parser.add_option("--episode-name", action="store", dest="episode_name")
147 parser.add_option("--dump", action="store_true", dest="dump")
148 try:
149 opts, (input, output) = parser.parse_args(sys.argv[1:])
150 except Exception:
151 parser.print_usage()
152 sys.exit(1)
153
154 if "://" not in input:
155 opts.input = os.path.join(os.getcwd(), input)
156 else:
157 if opts.dvd:
158 opts.dvd = os.path.join(os.getcwd(), opts.dvd)
159 opts.input = input
160
161 opts.output = os.path.join(os.getcwd(), output)
162 return opts
163
164 def run(args, dump):
165 if dump:
166 print "".join(map(commands.mkarg, args))[1:]
167 else:
168 return subprocess.Popen(args).wait()
169
170 def main():
171 opts = parse_args()
172 try:
173 profile = profiles[opts.profile_name]
174 except KeyError:
175 print >>sys.stderr, "Profile '%s' not found!" % opts.profile_name
176 sys.exit(1)
177
178 tempdir = tempfile.mkdtemp()
179 try:
180 os.chdir(tempdir)
181 cmd = profile.CommandClass(profile, opts)
182 if run(cmd.pass1(), opts.dump) == 0 or opts.dump:
183 run(cmd.pass2(), opts.dump)
184 finally:
185 os.chdir("/")
186 shutil.rmtree(tempdir)
187
188 if __name__ == "__main__":
189 main()
190