]>
code.delx.au - transcoding/blob - encode.py
ee036136c4005f298901a03043d9a740a0fbd22f
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
)
31 if self
.opts
.deinterlace
:
32 cmd
+= ["-vf-add", "pp=ci"]
34 def substValues(self
, cmd
):
36 "vbitrate": self
.opts
.vbitrate
,
37 "abitrate": self
.opts
.abitrate
,
38 "input": self
.opts
.input,
39 "output": self
.opts
.output
,
42 return [x
% subst
for x
in 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
)
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 cmd
+= self
.profile
.extra
62 cmd
= self
.substValues(cmd
)
65 class Profile(object):
66 def __init__(self
, CommandClass
, **kwargs
):
67 self
.CommandClass
= CommandClass
68 self
.__dict
__.update(kwargs
)
73 CommandClass
=MencoderCommand
,
75 vopts
="bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
77 aopts
="br=%(abitrate)d:mpeg=4:object=2",
82 CommandClass
=MencoderCommand
,
84 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect",
86 aopts
="abr:br=%(abitrate)d",
87 extra
=["-ffourcc", "DX50"],
91 CommandClass
=MencoderCommand
,
93 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
95 aopts
="br=%(abitrate)d:mpeg=4:object=2",
103 for profile_name
in profiles
.keys():
104 if sys
.argv
[0].find(profile_name
) >= 0:
107 profile_name
= "xvid"
109 parser
= optparse
.OptionParser(usage
="%prog [options] input output")
110 parser
.add_option("--dvd", action
="store", dest
="dvd")
111 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
112 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
113 parser
.add_option("--afilters", action
="store", dest
="afilters")
114 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int", default
=1000)
115 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int", default
=192)
116 parser
.add_option("--chapter", action
="store", dest
="chapter")
117 parser
.add_option("--startpos", action
="store", dest
="startpos")
118 parser
.add_option("--endpos", action
="store", dest
="endpos")
119 parser
.add_option("--audioid", action
="store", dest
="audioid")
120 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
121 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
122 parser
.add_option("--dump", action
="store_true", dest
="dump")
124 opts
, (input, output
) = parser
.parse_args(sys
.argv
[1:])
135 print "".join(map(commands
.mkarg
, args
))[1:]
137 return subprocess
.Popen(args
).wait()
142 profile
= profiles
[opts
.profile_name
]
144 print >>sys
.stderr
, "Profile '%s' not found!" % profile_name
147 cmd
= profile
.CommandClass(profile
, opts
)
148 if run(cmd
.pass1(), opts
.dump
) == 0 or opts
.dump
:
149 run(cmd
.pass2(), opts
.dump
)
151 if __name__
== "__main__":