]>
code.delx.au - transcoding/blob - encode.py
3 import commands
, optparse
, subprocess
, sys
5 class MencoderCommand(object):
8 "xvid": "-xvidencopts",
9 "x264": "-x264encopts",
11 "mp3lame": "-lameopts",
14 def __init__(self
, profile
, opts
):
15 self
.profile
= profile
18 def insertOptions(self
, cmd
):
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("-vf", self
.opts
.vfilters
)
29 tryOpt("-af", self
.opts
.afilters
)
31 def substValues(self
, cmd
):
33 "vbitrate": self
.opts
.vbitrate
,
34 "abitrate": self
.opts
.abitrate
,
35 "input": self
.opts
.input,
36 "output": self
.opts
.output
,
39 return [x
% subst
for x
in cmd
]
44 cmd
+= ["mencoder", "%(input)s", "-o", "/dev/null"]
45 self
.insertOptions(cmd
)
46 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], "pass=1:"+p
.vopts
]
47 cmd
+= ["-oac", "copy"]
48 cmd
= self
.substValues(cmd
)
54 cmd
+= ["mencoder", "%(input)s", "-o", "%(output)s"]
55 self
.insertOptions(cmd
)
56 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], "pass=2:"+p
.vopts
]
57 cmd
+= ["-oac", p
.acodec
, self
.codec2opts
[p
.acodec
], p
.aopts
]
58 cmd
+= self
.profile
.extra
59 cmd
= self
.substValues(cmd
)
62 class Profile(object):
63 def __init__(self
, CommandClass
, **kwargs
):
64 self
.CommandClass
= CommandClass
65 self
.__dict
__.update(kwargs
)
70 CommandClass
=MencoderCommand
,
72 vopts
="bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
74 aopts
="br=%(abitrate)d:mpeg=4:object=2",
79 CommandClass
=MencoderCommand
,
81 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect",
83 aopts
="abr:br=%(abitrate)d",
84 extra
=["-ffourcc", "DX50"],
92 for profile_name
in profiles
.keys():
93 if sys
.argv
[0].find(profile_name
) >= 0:
98 parser
= optparse
.OptionParser(usage
="%prog [options] input output")
99 parser
.add_option("--dvd", action
="store", dest
="dvd")
100 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
101 parser
.add_option("--afilters", action
="store", dest
="afilters")
102 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int", default
=1000)
103 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int", default
=192)
104 parser
.add_option("--chapter", action
="store", dest
="chapter")
105 parser
.add_option("--startpos", action
="store", dest
="startpos")
106 parser
.add_option("--endpos", action
="store", dest
="endpos")
107 parser
.add_option("--audioid", action
="store", dest
="audioid")
108 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
109 parser
.add_option("--dump", action
="store_true", dest
="dump")
111 opts
, (input, output
) = parser
.parse_args(sys
.argv
[1:])
122 print "".join(map(commands
.mkarg
, args
))[1:]
124 return subprocess
.Popen(args
).wait()
129 profile
= profiles
[opts
.profile_name
]
131 print >>sys
.stderr
, "Profile '%s' not found!" % profile_name
134 cmd
= profile
.CommandClass(profile
, opts
)
135 if run(cmd
.pass1(), opts
.dump
) == 0 or opts
.dump
:
136 run(cmd
.pass2(), opts
.dump
)
138 if __name__
== "__main__":