6 import lib.utils as utils
7 import lib.server as server
9 __all__ = ("TranscoderMencoder",)
11 class TranscoderMencoder(server.Transcoder):
12 mencoder_path = utils.which("mencoder")
18 def _setup_params(self):
19 params_first = self.params_first
22 self.args["local"] = params_first("local", False)
23 self.args["language"] = params_first("language", False)
24 self.args["subtitle"] = params_first("subtitle", False)
25 self.args["format"] = params_first("format", "")
26 self.args["outfile"] = params_first("outfile", "-")
29 uri = params_first("uri", "file://-").split("://")
30 self.args["type"] = uri[0]
31 self.args["input"] = uri[1]
34 self.args["acodec"] = params_first("acodec", "mp2")
35 self.args["abitrate"] = params_first("abitrate", 192)
36 self.args["volume"] = params_first("volume", 5)
39 self.args["mux"] = params_first("mux", "mpeg")
40 self.args["fps"] = params_first("fps", 25)
41 self.args["vcodec"] = params_first("vcodec", "mpeg1video")
42 self.args["vbitrate"] = params_first("vbitrate", 400)
43 self.args["width"] = params_first("width", 320)
44 self.args["height"] = params_first("height", 240)
48 def _setup_audio(self):
49 if self.args["acodec"] == "mp3lame":
50 audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
51 self.args["abitrate"], self.args["volume"])
53 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
54 self.args["acodec"], self.args["abitrate"])
60 def _setup_video(self):
61 video = " -of %s" % self.args["mux"]
62 video += " -ofps %s" % self.args["fps"]
64 vcodec = self.args["vcodec"]
65 if vcodec == "nuv" or vcodec == "xvid"\
66 or vcodec == "qtvideo" or vcodec == "copy":
67 video += " -ovc %s" % vcodec
69 video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
70 vcodec, self.args["vbitrate"])
72 video += " %s" % self.args["format"]
73 video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
79 def _arg_append(self, args, options):
80 for arg in shlex.split(options):
84 def _setup_mencoder_opts(self, args):
85 args.append(self.mencoder_path)
86 args.append(self.args["input"])
88 if self.args["language"]:
89 self._arg_append(args, "-alang %s" % self.args["language"])
91 if self.args["subtitle"]:
92 self._arg_append(args, "-slang %s" % self.args["subtitle"])
93 self._arg_append(args, "-subfps %s" % self.args["fps"])
95 self._arg_append(args, "-idx")
96 self._arg_append(args, self._setup_audio())
97 self._arg_append(args, self._setup_video())
99 self._arg_append(args, "-really-quiet")
100 self._arg_append(args, "-o %s" % self.args["outfile"])
101 self._arg_append(args, "2> %s" % os.devnull)
105 def _setup_filename(self):
106 _type = self.args["type"]
109 if not os.path.exists(self.args["input"]):
111 "File requested does not exist: %s." % self.args["input"]
114 self.args["input"] = "dvd://".join(self.args["input"])
118 def __init__(self, params):
119 server.Transcoder.__init__(self, params)
120 self.mencoder_opts = []
124 self._setup_filename()
125 self._setup_mencoder_opts(self.mencoder_opts)
131 def start(self, outfd):
132 cmd = " ".join(self.mencoder_opts)
133 self.log.debug("Mencoder: %s" % cmd)
136 self.proc = subprocess.Popen(self.mencoder_opts, stdout=subprocess.PIPE, close_fds=True)
138 self.log.error("Error executing mencoder: %s" % e)
142 while self.proc and self.proc.poll() == None:
143 d = self.proc.stdout.read(1024)
146 self.log.error("Problems handling data: %s" % e)
156 os.kill(self.proc.pid, signal.SIGTERM)