]>
code.delx.au - transcoding/blob - encode.py
3 import commands
, optparse
, subprocess
, sys
, os
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("-sid", self
.opts
.subtitleid
)
29 tryOpt("-vf", self
.opts
.vfilters
)
30 tryOpt("-af", self
.opts
.afilters
)
32 def substValues(self
, cmd
):
34 "vbitrate": self
.opts
.vbitrate
,
35 "abitrate": self
.opts
.abitrate
,
36 "input": self
.opts
.input,
37 "output": self
.opts
.output
,
40 return [x
% subst
for x
in 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
)
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 cmd
+= self
.profile
.extra
60 cmd
= self
.substValues(cmd
)
63 class Profile(object):
64 def __init__(self
, CommandClass
, **kwargs
):
65 self
.CommandClass
= CommandClass
66 self
.__dict
__.update(kwargs
)
71 CommandClass
=MencoderCommand
,
73 vopts
="bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
75 aopts
="br=%(abitrate)d:mpeg=4:object=2",
80 CommandClass
=MencoderCommand
,
82 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect",
84 aopts
="abr:br=%(abitrate)d",
85 extra
=["-ffourcc", "DX50"],
89 CommandClass
=MencoderCommand
,
91 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
93 aopts
="br=%(abitrate)d:mpeg=4:object=2",
101 for profile_name
in profiles
.keys():
102 if sys
.argv
[0].find(profile_name
) >= 0:
105 profile_name
= "xvid"
107 parser
= optparse
.OptionParser(usage
="%prog [options] input output")
108 parser
.add_option("--dvd", action
="store", dest
="dvd")
109 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
110 parser
.add_option("--afilters", action
="store", dest
="afilters")
111 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int", default
=1000)
112 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int", default
=192)
113 parser
.add_option("--chapter", action
="store", dest
="chapter")
114 parser
.add_option("--startpos", action
="store", dest
="startpos")
115 parser
.add_option("--endpos", action
="store", dest
="endpos")
116 parser
.add_option("--audioid", action
="store", dest
="audioid")
117 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
118 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
119 parser
.add_option("--dump", action
="store_true", dest
="dump")
121 opts
, (input, output
) = parser
.parse_args(sys
.argv
[1:])
132 print "".join(map(commands
.mkarg
, args
))[1:]
134 return subprocess
.Popen(args
).wait()
139 profile
= profiles
[opts
.profile_name
]
141 print >>sys
.stderr
, "Profile '%s' not found!" % profile_name
144 cmd
= profile
.CommandClass(profile
, opts
)
145 if run(cmd
.pass1(), opts
.dump
) == 0 or opts
.dump
:
146 run(cmd
.pass2(), opts
.dump
)
148 if __name__
== "__main__":