1 import lib.utils as utils
2 import lib.server as server
7 __all__ = ("TranscoderMencoder",)
9 class TranscoderMencoder(server.Transcoder):
10 mencoder_path = utils.which("mencoder")
14 def _setup_params(self):
15 params_first = self.params_first
18 self.args["local"] = params_first("local", False)
19 self.args["language"] = params_first("language", False)
20 self.args["subtitle"] = params_first("subtitle", False)
21 self.args["format"] = params_first("format", "")
22 self.args["outfile"] = params_first("outfile", "-")
25 self.args["type"] = params_first("type", "file")
26 self.args["input"] = params_first("input", "-")
29 self.args["acodec"] = params_first("acodec", "mp2")
30 self.args["abitrate"] = params_first("abitrate", 192)
31 self.args["volume"] = params_first("volume", 5)
34 self.args["mux"] = params_first("mux", "mpeg")
35 self.args["fps"] = params_first("fps", 25)
36 self.args["vcodec"] = params_first("vcodec", "mpeg1video")
37 self.args["vbitrate"] = params_first("vbitrate", 400)
38 self.args["width"] = params_first("width", 320)
39 self.args["height"] = params_first("height", 240)
43 def _setup_audio(self):
44 if self.args["acodec"] == "mp3lame":
45 audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
46 self.args["abitrate"], self.args["volume"])
48 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
49 self.args["acodec"], self.args["abitrate"])
55 def _setup_video(self):
56 video = " -of %s" % self.mux
57 video += " -ofps %s" % self.fps
59 vcodec = self.args["vcodec"]
60 if vcodec == "nuv" or vcodec == "xvid"\
61 or vcodec == "qtvideo" or vcodec == "copy":
62 video += " -ovc %s" % vcodec
64 video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
65 vcodec, self.args["vbitrate"])
67 video += " %s" % self.args["format"]
68 video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
74 def _arg_append(self, args, options):
75 for arg in shlex.split(options)
79 def _setup_mencoder_opts(self, args):
80 args.append(self.mencoder_path)
83 if self.args["language"]:
84 self._arg_append(args, "-alang %s" % self.args["language"])
86 if self.args["subtitle"]:
87 self._arg_append(args, "-slang %s" % self.args["subtitle"])
88 self._arg_append(args, "-subfps %s" % self.args["fps"])
90 self._arg_append(args, "-idx")
91 self._arg_append(args, self._setup_audio)
92 self._arg_append(args, self._setup_video)
94 self._arg_append(args, "-really-quiet")
95 self._arg_append(args, "-o %s" % self.args["outfile"])
96 self._arg_append(args, "2> %s" % os.devnull)
100 def _setup_filename(self):
101 _type = self.args["type"]
104 if not os.path.exists(self.args["input"]):
105 raise IOError, "File requested does not exist."
108 self.args["input"] = "dvd://".join(self.args["input"])
110 elif _type == "myth":
111 self.args["input"] = "myth://".join(self.args["input"])
112 self.gst_pipe = os.pipe()
116 def __init__(self, params, request):
117 server.Transcoder.__init__(self, params)
120 self.mencoder_opts = None
124 self._setup_filename()
125 self._setup_mencoder_opts(self.mencoder_opts)
134 def start(self, outfd):
135 cmd = " ".join(self.args)
136 self.log.info("Mencoder: %s" % cmd)
138 if self.args["type"] == "file":
139 _input = open(self.args["input"])
142 self.proc = subprocess.Popen(self.args, close_fds=True)
144 self.log.error("Error executing mencoder: %s" % cmd)
148 fifo_read = open(self.mencoder_outfile)
150 self.log.error("Error opening fifo: %s" % cmd)
154 while self.proc and self.proc.poll() == None:
155 d = fifo_read.read(1024)
158 self.log.error("Problems handling data: %s" % e)
168 os.kill(self.proc.pid, signal.SIGTERM)