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