7 import lib.utils as utils
8 import lib.server as server
10 from select import select
12 __all__ = ("TranscoderMencoder",)
14 class TranscoderMencoder(server.Transcoder):
15 """Transcoder class that implements a transcoder using Mencoder"""
16 mencoder_path = utils.which("mencoder")
23 # only works with avi container
26 def _setup_params(self):
27 params_first = self.params_first
30 self.args["local"] = params_first("local", False)
31 self.args["language"] = params_first("language", False)
32 self.args["subtitle"] = params_first("subtitle", False)
33 self.args["format"] = params_first("format", "")
34 self.args["outfile"] = params_first("outfile", "-")
35 self.args["sendback"] = params_first("sendback", True)
37 # handle sendback variable
38 if self.args["sendback"] == "False":
39 self.args["sendback"] = False
42 uri = params_first("uri", "file:-").split(":", 1)
43 self.args["type"] = uri[0]
44 self.args["input"] = uri[1]
47 self.args["acodec"] = params_first("acodec", "mp2")
48 self.args["abitrate"] = params_first("abitrate", 192)
49 self.args["volume"] = params_first("volume", 5)
52 self.args["mux"] = params_first("mux", "mpeg")
53 self.args["fps"] = params_first("fps", 25)
54 self.args["vcodec"] = params_first("vcodec", "mpeg1video")
55 self.args["vbitrate"] = params_first("vbitrate", 400)
56 self.args["width"] = params_first("width", 320)
57 self.args["height"] = params_first("height", 240)
61 def _setup_audio(self):
62 if self.args["acodec"] == "mp3lame":
63 audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
64 self.args["abitrate"], self.args["volume"])
66 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
67 self.args["acodec"], self.args["abitrate"])
73 def _setup_video(self):
74 video = " -of %s" % self.args["mux"]
75 video += " -ofps %s" % self.args["fps"]
77 vcodec = self.args["vcodec"]
78 if vcodec == "nuv" or vcodec == "xvid"\
79 or vcodec == "qtvideo" or vcodec == "copy":
80 video += " -ovc %s" % vcodec
82 video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
83 vcodec, self.args["vbitrate"])
85 video += " %s" % self.args["format"]
86 video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
92 def _arg_append(self, args, options):
93 for arg in shlex.split(options):
97 def _setup_mencoder_opts(self, args):
98 args.append(self.mencoder_path)
100 if self.args["outfile"] == "-" and self.args["type"]:
101 args.append(self.args["input"])
105 if self.args["language"]:
106 self._arg_append(args, "-alang %s" % self.args["language"])
108 if self.args["subtitle"]:
109 self._arg_append(args, "-slang %s" % self.args["subtitle"])
110 self._arg_append(args, "-subfps %s" % self.args["fps"])
112 self._arg_append(args, "-idx")
113 self._arg_append(args, "-cache 500")
114 self._arg_append(args, self._setup_audio())
115 self._arg_append(args, self._setup_video())
117 self._arg_append(args, "-really-quiet")
118 self._arg_append(args, "-o %s" % self.args["outfile"])
119 self._arg_append(args, "2>%s" % os.devnull)
123 def _setup_filename(self):
124 _type = self.args["type"]
127 if not os.path.exists(self.args["input"]):
129 "File requested does not exist: %s." % self.args["input"]
131 self.args["input"] = "file://%s" % self.args["input"]
134 self.args["input"] = "dvd://".join(self.args["input"])
136 elif _type == "myth":
137 # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
138 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
139 # myth://IP:PORT:type:file
140 self.args["gmyth-cat"] = self.args["input"].split(":")
141 self.args["input"] = "-"
145 def __init__(self, params):
146 server.Transcoder.__init__(self, params)
147 self.mencoder_opts = []
151 self._setup_filename()
152 self._setup_mencoder_opts(self.mencoder_opts)
158 def _check_opened_file(self, stdw, _stdin):
162 return open(self.args["outfile"])
164 os.write(stdw, _stdin.read(1024))
168 def _start_outfile(self, outfd):
171 # fix this (not necessary)
175 _stdin = open(self.args["input"])
176 size = int(os.path.getsize(self.args["input"]))
181 stdr, stdw = os.pipe()
183 if not self._run_mencoder(input=stdr):
186 stdout = self._check_opened_file(stdw, _stdin)
189 while self.proc and self.proc.poll() == None:
191 data_in = _stdin.read(4096)
193 os.write(stdw, data_in)
195 d = stdout.read(4096)
196 if self.args["sendback"]:
198 self.status = total_read * 100 / size
204 d = stdout.read(4096)
205 if self.args["sendback"] and d != "":
209 self.log.error("Problems handling data: %s" % e)
212 self.log.info("%s: Finished sending data to client" % repr(self))
213 if not self.args["sendback"]:
219 def _start(self, outfd):
220 if not self._run_mencoder(output=subprocess.PIPE):
224 while self.proc and self.proc.poll() == None:
225 d = self.proc.stdout.read(1024)
228 self.log.error("Problems handling data: %s" % e)
231 self.log.info("%s: Finished sending data to client" % repr(self))
235 def _start_myth(self, outfd):
236 # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
237 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
238 # myth://IP:PORT:type:file
239 host = self.args["gmyth-cat"][0]
240 port = self.args["gmyth-cat"][1]
241 kind = self.args["gmyth-cat"][2]
242 fchan = self.args["gmyth-cat"][3]
244 gmyth_cat = utils.which("gmyth-cat")
245 opts = [gmyth_cat, "-h", host, "-p", port, "-" + kind, fchan]
249 self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE, close_fds=True)
251 self.log.error("Error executing gmyth-cat: %s" % e)
254 if not self._run_mencoder(input=self.gmyth.stdout, output=subprocess.PIPE):
258 while self.proc and self.proc.poll() == None:
259 r, w, x = select([self.proc.stdout], [], [], 0)
260 if self.proc.stdout in r:
261 d = self.proc.stdout.read(4096)
264 self.log.error("Problems handling data: %s" % e)
270 def _run_mencoder(self, input=None, output=None):
272 self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
273 stdout=output, close_fds=True)
275 self.log.error("Error executing mencoder: %s" % e)
281 def start(self, outfd):
282 cmd = " ".join(self.mencoder_opts)
283 self.log.debug("Mencoder: %s" % cmd)
287 if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
288 ret = self._start(outfd)
290 elif self.args["type"] == "myth":
291 ret = self._start_myth(outfd)
294 ret = self._start_outfile(outfd)
304 os.kill(self.proc.pid, signal.SIGKILL)
317 os.kill(self.gmyth.pid, signal.SIGKILL)