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", "mpeg1")
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 if self.args["mux"] == "mpeg":
86 video += " -mpegopts format=%s" % self.args["format"]
87 video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
93 def _arg_append(self, args, options):
94 for arg in shlex.split(options):
98 def _setup_mencoder_opts(self, args):
99 args.append(self.mencoder_path)
101 if self.args["outfile"] == "-" and self.args["type"]:
102 args.append(self.args["input"])
106 if self.args["language"]:
107 self._arg_append(args, "-alang %s" % self.args["language"])
109 if self.args["subtitle"]:
110 self._arg_append(args, "-slang %s" % self.args["subtitle"])
111 self._arg_append(args, "-subfps %s" % self.args["fps"])
113 self._arg_append(args, "-idx")
114 self._arg_append(args, "-cache 1024")
115 self._arg_append(args, self._setup_audio())
116 self._arg_append(args, self._setup_video())
118 self._arg_append(args, "-really-quiet")
119 self._arg_append(args, "-o %s" % self.args["outfile"])
120 self._arg_append(args, "2>%s" % os.devnull)
124 def _setup_filename(self):
125 _type = self.args["type"]
128 if not os.path.exists(self.args["input"]):
130 "File requested does not exist: %s." % self.args["input"]
132 self.args["input"] = "file://%s" % self.args["input"]
135 self.args["input"] = "dvd://".join(self.args["input"])
137 elif _type == "myth":
138 # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
139 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
140 # myth://IP:PORT:type:file
141 self.args["gmyth-cat"] = self.args["input"].split(":")
142 self.args["input"] = "-"
146 def __init__(self, params):
147 server.Transcoder.__init__(self, params)
148 self.mencoder_opts = []
152 self._setup_filename()
153 self._setup_mencoder_opts(self.mencoder_opts)
159 def _check_opened_file(self, stdw, _stdin):
163 return open(self.args["outfile"])
165 os.write(stdw, _stdin.read(1024))
169 def _start_outfile(self, outfd):
172 # fix this (not necessary)
177 _stdin = open(self.args["input"])
178 size = int(os.path.getsize(self.args["input"]))
180 self.log.error("Mencoder stdin setup error: %s" % e)
187 stdr, stdw = os.pipe()
189 if not self._run_mencoder(input=stdr):
192 stdout = self._check_opened_file(stdw, _stdin)
195 while self.proc and self.proc.poll() == None:
197 data_in = _stdin.read(4096)
199 os.write(stdw, data_in)
201 d = stdout.read(4096)
202 if self.args["sendback"]:
204 self.status = total_read * 100 / size
210 d = stdout.read(4096)
211 if self.args["sendback"] and d != "":
215 self.log.error("Problems handling data: %s" % e)
219 self.log.info("%s: Finished sending data to client" % repr(self))
220 if not self.args["sendback"]:
226 def _start(self, outfd):
227 if not self._run_mencoder(output=subprocess.PIPE):
231 while self.proc and self.proc.poll() == None:
232 d = self.proc.stdout.read(1024)
235 self.log.error("Problems handling data: %s" % e)
238 self.log.info("%s: Finished sending data to client" % repr(self))
242 def _start_myth(self, outfd):
243 # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
244 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
245 # myth://IP:PORT:type:file
246 host = self.args["gmyth-cat"][0]
247 port = self.args["gmyth-cat"][1]
248 kind = self.args["gmyth-cat"][2]
249 fchan = self.args["gmyth-cat"][3]
251 gmyth_cat = utils.which("gmyth-cat")
252 opts = [gmyth_cat, "-h", host, "-p", port, "-" + kind, fchan]
255 self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE, close_fds=True)
257 self.log.error("Error executing gmyth-cat: %s" % e)
260 if not self._run_mencoder(input=self.gmyth.stdout, output=subprocess.PIPE):
266 #n800 150000 and 1000
269 while self.proc and self.proc.poll() == None:
271 r, w, x = select([self.proc.stdout], [], [], 0)
272 if self.proc.stdout in r:
273 d = self.proc.stdout.read(4096)
275 # self.log.debug("Sending tv data: %d" % show_loading)
276 # if show_loading > 150000 and show:
277 # self.log.debug("Disabling loading video")
280 #elif show_loading % 10000 == 0 and show:
282 # d = open("loading.mpg")
283 # outfd.write(d.read())
284 # self.log.debug("Sendind loading video: %d" % show_loading)
289 #self.log.debug("SHOW_LOADING: %d" % show_loading)
292 self.log.error("Problems handling data: %s" % e)
298 def _run_mencoder(self, input=None, output=None):
300 self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
301 stdout=output, close_fds=True)
303 self.log.error("Error executing mencoder: %s" % e)
309 def start(self, outfd):
310 cmd = " ".join(self.mencoder_opts)
311 self.log.debug("Mencoder: %s" % cmd)
315 if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
316 ret = self._start(outfd)
318 elif self.args["type"] == "myth":
319 ret = self._start_myth(outfd)
322 ret = self._start_outfile(outfd)
332 os.kill(self.proc.pid, signal.SIGKILL)
345 os.kill(self.gmyth.pid, signal.SIGKILL)