]>
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 if self
.opts
.episode_name
:
60 cmd
+= ["-info", "name='%s'" % self
.opts
.episode_name
]
61 cmd
+= self
.profile
.extra
62 cmd
= self
.substValues(cmd
)
65 class Profile(object):
66 def __init__(self
, CommandClass
, **kwargs
):
69 self
.CommandClass
= CommandClass
70 self
.__dict
__.update(kwargs
)
71 def __contains__(self
, keyname
):
72 return hasattr(self
, keyname
)
77 CommandClass
=MencoderCommand
,
79 vopts
="bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
81 aopts
="br=%(abitrate)d:mpeg=4:object=2",
86 CommandClass
=MencoderCommand
,
88 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect",
90 aopts
="abr:br=%(abitrate)d",
91 extra
=["-ffourcc", "DX50"],
95 CommandClass
=MencoderCommand
,
97 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
99 aopts
="br=%(abitrate)d:mpeg=4:object=2",
103 CommandClass
=MencoderCommand
,
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",
107 aopts
="br=%(abitrate)d:mpeg=4:object=2:raw",
108 extra
=['-of', 'lavf', '-lavfopts', 'format=mp4', '-channels', '2', '-srate', '48000']
116 for profile_name
in profiles
.keys():
117 if sys
.argv
[0].find(profile_name
) >= 0:
120 profile_name
= "xvid"
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")
137 opts
, (input, output
) = parser
.parse_args(sys
.argv
[1:])
148 print "".join(map(commands
.mkarg
, args
))[1:]
150 return subprocess
.Popen(args
).wait()
155 profile
= profiles
[opts
.profile_name
]
157 print >>sys
.stderr
, "Profile '%s' not found!" % profile_name
160 cmd
= profile
.CommandClass(profile
, opts
)
161 if run(cmd
.pass1(), opts
.dump
) == 0 or opts
.dump
:
162 run(cmd
.pass2(), opts
.dump
)
164 if __name__
== "__main__":