[svn r639] Added creation of package gmyth-utils containing gmyth-cat application
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)
176 _stdin = open(self.args["input"])
177 size = int(os.path.getsize(self.args["input"]))
182 stdr, stdw = os.pipe()
184 if not self._run_mencoder(input=stdr):
187 stdout = self._check_opened_file(stdw, _stdin)
190 while self.proc and self.proc.poll() == None:
192 data_in = _stdin.read(4096)
194 os.write(stdw, data_in)
196 d = stdout.read(4096)
197 if self.args["sendback"]:
199 self.status = total_read * 100 / size
205 d = stdout.read(4096)
206 if self.args["sendback"] and d != "":
210 self.log.error("Problems handling data: %s" % e)
214 self.log.info("%s: Finished sending data to client" % repr(self))
215 if not self.args["sendback"]:
221 def _start(self, outfd):
222 if not self._run_mencoder(output=subprocess.PIPE):
226 while self.proc and self.proc.poll() == None:
227 d = self.proc.stdout.read(1024)
230 self.log.error("Problems handling data: %s" % e)
233 self.log.info("%s: Finished sending data to client" % repr(self))
237 def _start_myth(self, outfd):
238 # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
239 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
240 # myth://IP:PORT:type:file
241 host = self.args["gmyth-cat"][0]
242 port = self.args["gmyth-cat"][1]
243 kind = self.args["gmyth-cat"][2]
244 fchan = self.args["gmyth-cat"][3]
246 gmyth_cat = utils.which("gmyth-cat")
247 opts = [gmyth_cat, "-h", host, "-p", port, "-" + kind, fchan]
250 self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE, close_fds=True)
252 self.log.error("Error executing gmyth-cat: %s" % e)
255 if not self._run_mencoder(input=self.gmyth.stdout, output=subprocess.PIPE):
261 #n800 150000 and 1000
264 while self.proc and self.proc.poll() == None:
266 r, w, x = select([self.proc.stdout], [], [], 0)
267 if self.proc.stdout in r:
268 d = self.proc.stdout.read(4096)
270 # self.log.debug("Sending tv data: %d" % show_loading)
271 # if show_loading > 150000 and show:
272 # self.log.debug("Disabling loading video")
275 #elif show_loading % 10000 == 0 and show:
277 # d = open("loading.mpg")
278 # outfd.write(d.read())
279 # self.log.debug("Sendind loading video: %d" % show_loading)
284 #self.log.debug("SHOW_LOADING: %d" % show_loading)
287 self.log.error("Problems handling data: %s" % e)
293 def _run_mencoder(self, input=None, output=None):
295 self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
296 stdout=output, close_fds=True)
298 self.log.error("Error executing mencoder: %s" % e)
304 def start(self, outfd):
305 cmd = " ".join(self.mencoder_opts)
306 self.log.debug("Mencoder: %s" % cmd)
310 if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
311 ret = self._start(outfd)
313 elif self.args["type"] == "myth":
314 ret = self._start_myth(outfd)
317 ret = self._start_outfile(outfd)
327 os.kill(self.proc.pid, signal.SIGKILL)
340 os.kill(self.gmyth.pid, signal.SIGKILL)