]>
code.delx.au - transcoding/blob - encode.py
3 import commands
, optparse
, subprocess
, sys
, os
, tempfile
, shutil
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 if self
.opts
.deinterlace
:
24 cmd
+= ["-vf-add", "pp=ci"]
25 tryOpt("-ss", self
.opts
.startpos
)
26 tryOpt("-endpos", self
.opts
.endpos
)
27 tryOpt("-dvd-device", self
.opts
.dvd
)
28 tryOpt("-chapter", self
.opts
.chapter
)
29 tryOpt("-aid", self
.opts
.audioid
)
30 tryOpt("-sid", self
.opts
.subtitleid
)
31 tryOpt("-vf-add", self
.opts
.vfilters
)
32 tryOpt("-af", self
.opts
.afilters
)
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 if self
.opts
.episode_name
:
62 cmd
+= ["-info", "name='%s'" % self
.opts
.episode_name
]
63 cmd
+= self
.profile
.extra
64 cmd
= self
.substValues(cmd
)
67 class Profile(object):
68 def __init__(self
, CommandClass
, **kwargs
):
71 self
.CommandClass
= CommandClass
72 self
.__dict
__.update(kwargs
)
73 def __contains__(self
, keyname
):
74 return hasattr(self
, keyname
)
79 CommandClass
=MencoderCommand
,
81 vopts
="bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
83 aopts
="br=%(abitrate)d:mpeg=4:object=2",
88 CommandClass
=MencoderCommand
,
90 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect",
92 aopts
="abr:br=%(abitrate)d",
93 extra
=["-ffourcc", "DX50"],
97 CommandClass
=MencoderCommand
,
99 vopts
="bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
101 aopts
="br=%(abitrate)d:mpeg=4:object=2",
105 CommandClass
=MencoderCommand
,
107 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",
109 aopts
="br=%(abitrate)d:mpeg=4:object=2:raw",
110 extra
=['-of', 'lavf', '-lavfopts', 'format=mp4', '-channels', '2', '-srate', '48000']
118 for profile_name
in profiles
.keys():
119 if sys
.argv
[0].find(profile_name
) >= 0:
122 profile_name
= "xvid"
124 parser
= optparse
.OptionParser(usage
="%prog [options] input output")
125 parser
.add_option("--dvd", action
="store", dest
="dvd")
126 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
127 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
128 parser
.add_option("--afilters", action
="store", dest
="afilters")
129 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int", default
=1000)
130 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int", default
=192)
131 parser
.add_option("--chapter", action
="store", dest
="chapter")
132 parser
.add_option("--startpos", action
="store", dest
="startpos")
133 parser
.add_option("--endpos", action
="store", dest
="endpos")
134 parser
.add_option("--audioid", action
="store", dest
="audioid")
135 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
136 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
137 parser
.add_option("--episode-name", action
="store", dest
="episode_name")
138 parser
.add_option("--dump", action
="store_true", dest
="dump")
140 opts
, (input, output
) = parser
.parse_args(sys
.argv
[1:])
145 if '://' not in input:
146 opts
.input = os
.path
.join(os
.getcwd(), input)
149 opts
.dvd
= os
.path
.join(os
.getcwd(), opts
.dvd
)
152 opts
.output
= os
.path
.join(os
.getcwd(), output
)
157 print "".join(map(commands
.mkarg
, args
))[1:]
159 return subprocess
.Popen(args
).wait()
164 profile
= profiles
[opts
.profile_name
]
166 print >>sys
.stderr
, "Profile '%s' not found!" % profile_name
169 tempdir
= tempfile
.mkdtemp()
172 cmd
= profile
.CommandClass(profile
, opts
)
173 if run(cmd
.pass1(), opts
.dump
) == 0 or opts
.dump
:
174 run(cmd
.pass2(), opts
.dump
)
177 shutil
.rmtree(tempdir
)
179 if __name__
== "__main__":