]>
code.delx.au - transcoding/blob - encode.py
11e8ebfd12f873ab4f42cd50d68f0205098ad4ef
11 class FatalException(Exception):
15 if re
.match("^[a-zA-Z0-9\-\\.,/@_:=]*$", arg
):
28 class Command(object):
29 def __init__(self
, profile
, opts
):
30 self
.profile
= profile
33 def print_install_message(self
):
34 print >>sys
.stderr
, "Problem with command: %s", self
.name
36 print >>sys
.stderr
, "Try running:\n# aptitude install %s", self
.package
38 def check_command(self
, cmd
):
41 if subprocess
.Popen(["which", cmd
], stdout
=open("/dev/null", "w")).wait() != 0:
42 raise FatalException("Command '%s' is required" % cmd
)
44 def check_no_file(self
, path
):
45 if os
.path
.exists(path
):
46 raise FatalException("Output file '%s' exists." % path
)
48 def do_exec(self
, args
):
50 print " ".join(map(mkarg
, args
))
52 if subprocess
.Popen(args
).wait() != 0:
53 raise FatalException("Failure executing command: %s" % args
)
56 class MP4Box(Command
):
64 self
.check_command("mencoder")
65 self
.check_command("MP4Box")
66 self
.check_no_file(self
.opts
.output
+ ".mp4")
70 video
= "video.%s" % self
.codec2exts
[p
.vcodec
]
71 audio
= "audio.%s" % self
.codec2exts
[p
.acodec
]
72 input = self
.opts
.output
+ ".avi" # From Mencoder command
73 output
= self
.opts
.output
+ ".mp4"
74 mencoder
= ["mencoder", input, "-ovc", "copy", "-oac", "copy", "-of"]
75 self
.do_exec(["rm", "-f", output
])
76 self
.do_exec(mencoder
+ ["rawvideo", "-o", video
])
77 self
.do_exec(mencoder
+ ["rawaudio", "-o", audio
])
78 self
.do_exec(["MP4Box", "-add", video
, "-add", audio
, output
])
79 self
.do_exec(["rm", "-f", video
, audio
, input])
83 class MKVMerge(Command
):
85 self
.check_command("mkvmerge")
86 self
.check_no_file(self
.opts
.output
+ ".mkv")
89 input = self
.opts
.output
+ ".avi" # From Mencoder command
90 output
= self
.opts
.output
+ ".mkv"
91 self
.do_exec(["mkvmerge", "-o", output
, input])
92 self
.do_exec(["rm", "-f", input])
96 class Mencoder(Command
):
99 "xvid": "-xvidencopts",
100 "x264": "-x264encopts",
102 "mp3lame": "-lameopts",
105 def insert_options(self
, cmd
):
106 def try_opt(opt
, var
):
110 if self
.opts
.deinterlace
:
111 cmd
+= ["-vf-add", "pp=lb"]
112 try_opt("-ss", self
.opts
.startpos
)
113 try_opt("-endpos", self
.opts
.endpos
)
114 try_opt("-dvd-device", self
.opts
.dvd
)
115 try_opt("-chapter", self
.opts
.chapter
)
116 try_opt("-aid", self
.opts
.audioid
)
117 try_opt("-sid", self
.opts
.subtitleid
)
118 try_opt("-vf-add", self
.opts
.vfilters
)
119 try_opt("-af", self
.opts
.afilters
)
121 def subst_values(self
, cmd
, vpass
):
123 "vbitrate": self
.opts
.vbitrate
,
124 "abitrate": self
.opts
.abitrate
,
125 "input": self
.opts
.input,
126 "output": self
.opts
.output
+ ".avi",
130 return [x
% subst
for x
in cmd
]
135 cmd
+= ["mencoder", "%(input)s", "-o", "/dev/null"]
136 self
.insert_options(cmd
)
137 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
138 cmd
+= ["-oac", "copy"]
139 cmd
+= self
.profile
.extra
+ self
.profile
.extra1
140 cmd
= self
.subst_values(cmd
, vpass
=1)
146 cmd
+= ["mencoder", "%(input)s", "-o", "%(output)s"]
147 self
.insert_options(cmd
)
148 cmd
+= ["-ovc", p
.vcodec
, self
.codec2opts
[p
.vcodec
], p
.vopts
]
149 cmd
+= ["-oac", p
.acodec
, self
.codec2opts
[p
.acodec
], p
.aopts
]
150 if self
.opts
.episode_name
:
151 cmd
+= ["-info", "name='%s'" % self
.opts
.episode_name
]
152 cmd
+= self
.profile
.extra
+ self
.profile
.extra2
153 cmd
= self
.subst_values(cmd
, vpass
=2)
157 self
.check_command("mencoder")
158 self
.check_no_file(self
.opts
.output
+ ".avi")
161 self
.do_exec(self
.pass1())
162 self
.do_exec(self
.pass2())
166 class Profile(object):
167 def __init__(self
, commands
, **kwargs
):
168 self
.default_opts
= {
175 self
.commands
= commands
176 self
.__dict
__.update(kwargs
)
178 def __contains__(self
, keyname
):
179 return hasattr(self
, keyname
)
185 commands
=[Mencoder
, MP4Box
],
187 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:me=umh:partitions=all:trellis=1:subq=7:bframes=1:direct_pred=auto",
189 aopts
="br=%(abitrate)d:mpeg=4:object=2",
194 commands
=[Mencoder
, MKVMerge
],
196 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:subq=6:frameref=6:me=umh:partitions=all:bframes=4:b_adapt:qcomp=0.7:keyint=250",
198 aopts
="abr:br=%(abitrate)d",
205 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect",
207 aopts
="abr:br=%(abitrate)d",
208 extra2
=["-ffourcc", "DX50"],
213 commands
=[Mencoder
, MP4Box
],
215 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
217 aopts
="br=%(abitrate)d:mpeg=4:object=2",
218 extra
=["-vf-add", "scale=480:-10"],
223 commands
=[Mencoder
, MP4Box
],
225 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:turbo",
227 aopts
="br=%(abitrate)d:mpeg=4:object=2",
228 extra
=["-vf-add", "scale=480:-10"],
229 extra2
=["-channels", "2", "-srate", "48000"],
234 commands
=[Mencoder
, MP4Box
],
240 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto",
242 aopts
="br=%(abitrate)d:mpeg=4:object=2",
243 extra
=["-vf-add", "scale=320:-10"],
248 commands
=[Mencoder
, MP4Box
],
254 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vhq=4:autoaspect:max_bframes=0",
256 aopts
="br=%(abitrate)d:mpeg=4:object=2",
257 extra
=["-vf-add", "scale=640:-10"],
262 commands
=[Mencoder
, MP4Box
],
268 vopts
="pass=%(vpass)d:bitrate=%(vbitrate)d:vbv_maxrate=2000:vbv_bufsize=2000:nocabac:me=umh:partitions=all:trellis=1:subq=7:bframes=0:direct_pred=auto:level_idc=20",
270 aopts
="br=%(abitrate)d:mpeg=4:object=2",
271 extra
=["-vf-add", "scale=640:-10"],
279 for profile_name
in profiles
.keys():
280 if sys
.argv
[0].find(profile_name
) >= 0:
283 profile_name
= "xvid"
285 parser
= optparse
.OptionParser(usage
="%prog [options] input [output]")
286 parser
.add_option("--dvd", action
="store", dest
="dvd")
287 parser
.add_option("--deinterlace", action
="store_true", dest
="deinterlace")
288 parser
.add_option("--vfilters", action
="store", dest
="vfilters")
289 parser
.add_option("--afilters", action
="store", dest
="afilters")
290 parser
.add_option("--vbitrate", action
="store", dest
="vbitrate", type="int")
291 parser
.add_option("--abitrate", action
="store", dest
="abitrate", type="int")
292 parser
.add_option("--chapter", action
="store", dest
="chapter")
293 parser
.add_option("--startpos", action
="store", dest
="startpos")
294 parser
.add_option("--endpos", action
="store", dest
="endpos")
295 parser
.add_option("--audioid", action
="store", dest
="audioid")
296 parser
.add_option("--subtitleid", action
="store", dest
="subtitleid")
297 parser
.add_option("--profile", action
="store", dest
="profile_name", default
=profile_name
)
298 parser
.add_option("--episode-name", action
="store", dest
="episode_name")
299 parser
.add_option("--dump", action
="store_true", dest
="dump")
301 opts
, args
= parser
.parse_args(sys
.argv
[1:])
304 output
= os
.path
.splitext(os
.path
.basename(input))[0]
313 if "://" not in input:
314 opts
.input = os
.path
.abspath(input)
317 opts
.dvd
= os
.path
.abspath(opts
.dvd
)
320 opts
.output
= os
.path
.abspath(output
)
329 profile
= profiles
[opts
.profile_name
]
331 print >>sys
.stderr
, "Profile '%s' not found!" % opts
.profile_name
334 # Pull in default option values from the profile
335 for key
, value
in profile
.default_opts
.iteritems():
336 if getattr(opts
, key
) is None:
337 setattr(opts
, key
, value
)
339 # Run in a temp dir so that multiple instances can be run simultaneously
340 tempdir
= tempfile
.mkdtemp()
346 for CommandClass
in profile
.commands
:
347 command
= CommandClass(profile
, opts
)
348 commands
.append(command
)
350 for command
in commands
:
353 except FatalException
, e
:
354 print >>sys
.stderr
, "Error:", e
.message
359 shutil
.rmtree(tempdir
)
361 if __name__
== "__main__":