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