]>
code.delx.au - transcoding/blob - encode.py
11 class MencoderCommand(object):
14 "xvid": "-xvidencopts",
15 "x264": "-x264encopts",
17 "mp3lame": "-lameopts",
20 def __init__(self
, profile
, opts
):
21 self
.profile
= profile
24 def insert_options(self
, cmd
):
25 def try_opt(opt
, var
):
29 if self
.opts
.deinterlace
:
30 cmd
+= ["-vf-add", "pp=ci"]
31 try_opt("-ss", self
.opts
.startpos
)
32 try_opt("-endpos", self
.opts
.endpos
)
33 try_opt("-dvd-device", self
.opts
.dvd
)
34 try_opt("-chapter", self
.opts
.chapter
)
35 try_opt("-aid", self
.opts
.audioid
)
36 try_opt("-sid", self
.opts
.subtitleid
)
37 try_opt("-vf-add", self
.opts
.vfilters
)
38 try_opt("-af", self
.opts
.afilters
)
40 def subst_values(self
, cmd
, vpass
):
42 "vbitrate": self
.opts
.vbitrate
,
43 "abitrate": self
.opts
.abitrate
,
44 "input": self
.opts
.input,
45 "output": self
.opts
.output
,
49 return [x
% subst
for x
in cmd
]
54 cmd
+= ["mencoder", "%(input)s", "-o", "/dev/null"]
55 self
.insert_options(cmd
)
56 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
57 cmd
+= ["-oac", "copy"]
58 cmd
= self
.subst_values(cmd
, vpass
=1)
64 cmd
+= ["mencoder", "%(input)s", "-o", "%(output)s"]
65 self
.insert_options(cmd
)
66 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
67 cmd
+= ["-oac", p
.acodec
, self
.codec2opts
[p
.acodec
], p
.aopts
]
68 if self
.opts
.episode_name
:
69 cmd
+= ["-info", "name='%s'" % self
.opts
.episode_name
]
70 cmd
+= self
.profile
.extra
71 cmd
= self
.subst_values(cmd
, vpass
=2)
74 class Profile(object):
75 def __init__(self
, CommandClass
, **kwargs
):
78 self
.CommandClass
= CommandClass
79 self
.__dict
__.update(kwargs
)
80 def __contains__(self
, keyname
):
81 return hasattr(self
, keyname
)
86 CommandClass
=MencoderCommand
,
88 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
90 aopts
="br=%(abitrate)d:mpeg=4:object=2",
95 CommandClass
=MencoderCommand
,
97 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
99 aopts
="abr:br=%(abitrate)d",
100 extra
=["-ffourcc", "DX50"],
105 CommandClass
=MencoderCommand
,
107 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
109 aopts
="br=%(abitrate)d:mpeg=4:object=2",
114 CommandClass
=MencoderCommand
,
116 vopts
="pass=%(vpass)d: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",
118 aopts
="br=%(abitrate)d:mpeg=4:object=2:raw",
119 extra
=["-of", "lavf", "-lavfopts", "format=mp4", "-channels", "2", "-srate", "48000"]
127 for profile_name
in profiles
.keys():
128 if sys
.argv
[0].find(profile_name
) >= 0:
131 profile_name
= "xvid"
133 parser
= optparse
.OptionParser(usage
="%prog [options] input output")
134 parser
.add_option("--dvd", action
="store", dest
="dvd")
135 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
136 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
137 parser
.add_option("--afilters", action
="store", dest
="afilters")
138 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int", default
=1000)
139 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int", default
=192)
140 parser
.add_option("--chapter", action
="store", dest
="chapter")
141 parser
.add_option("--startpos", action
="store", dest
="startpos")
142 parser
.add_option("--endpos", action
="store", dest
="endpos")
143 parser
.add_option("--audioid", action
="store", dest
="audioid")
144 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
145 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
146 parser
.add_option("--episode-name", action
="store", dest
="episode_name")
147 parser
.add_option("--dump", action
="store_true", dest
="dump")
149 opts
, (input, output
) = parser
.parse_args(sys
.argv
[1:])
154 if "://" not in input:
155 opts
.input = os
.path
.join(os
.getcwd(), input)
158 opts
.dvd
= os
.path
.join(os
.getcwd(), opts
.dvd
)
161 opts
.output
= os
.path
.join(os
.getcwd(), output
)
166 print "".join(map(commands
.mkarg
, args
))[1:]
168 return subprocess
.Popen(args
).wait()
173 profile
= profiles
[opts
.profile_name
]
175 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
178 tempdir
= tempfile
.mkdtemp()
181 cmd
= profile
.CommandClass(profile
, opts
)
182 if run(cmd
.pass1(), opts
.dump
) == 0 or opts
.dump
:
183 run(cmd
.pass2(), opts
.dump
)
186 shutil
.rmtree(tempdir
)
188 if __name__
== "__main__":