8 import lib.utils as utils
9 import lib.server as server
10 import plugins.transcoders.mencoder_lib.mythtv as mythtv
12 from select import select
14 __all__ = ("TranscoderMencoder",)
16 class TranscoderMencoder(server.Transcoder):
17 """Transcoder class that implements a transcoder using Mencoder"""
18 mencoder_path = utils.which("mencoder")
25 # only works with avi container
28 def _setup_params(self):
29 params_first = self.params_first
32 self.args["local"] = params_first("local", False)
33 self.args["language"] = params_first("language", False)
34 self.args["subtitle"] = params_first("subtitle", False)
35 self.args["format"] = params_first("format", "mpeg1")
36 self.args["outfile"] = params_first("outfile", "-")
39 uri = params_first("uri", "file:-").split(":", 1)
40 self.args["type"] = uri[0]
41 self.args["input"] = uri[1]
44 self.args["acodec"] = params_first("acodec", "mp2")
45 self.args["abitrate"] = params_first("abitrate", 192)
46 self.args["volume"] = params_first("volume", 5)
49 self.args["mux"] = params_first("mux", "mpeg")
50 self.args["fps"] = params_first("fps", 25)
51 self.args["vcodec"] = params_first("vcodec", "mpeg1video")
52 self.args["vbitrate"] = params_first("vbitrate", 400)
53 self.args["width"] = params_first("width", 320)
54 self.args["height"] = params_first("height", 240)
58 def _setup_audio(self):
59 if self.args["acodec"] == "mp3lame":
60 audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
61 self.args["abitrate"], self.args["volume"])
63 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
64 self.args["acodec"], self.args["abitrate"])
70 def _setup_video(self):
71 video = " -of %s" % self.args["mux"]
72 video += " -ofps %s" % self.args["fps"]
74 vcodec = self.args["vcodec"]
75 if vcodec == "nuv" or vcodec == "xvid"\
76 or vcodec == "qtvideo" or vcodec == "copy":
77 video += " -ovc %s" % vcodec
79 video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
80 vcodec, self.args["vbitrate"])
82 if self.args["mux"] == "mpeg":
83 video += " -mpegopts format=%s" % self.args["format"]
84 video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
90 def _arg_append(self, args, options):
91 for arg in shlex.split(options):
95 def _setup_mencoder_opts(self, args):
96 args.append(self.mencoder_path)
98 if self.args["outfile"] == "-" and self.args["type"]:
99 args.append(self.args["input"])
103 if self.args["language"]:
104 self._arg_append(args, "-alang %s" % self.args["language"])
106 if self.args["subtitle"]:
107 self._arg_append(args, "-slang %s" % self.args["subtitle"])
108 self._arg_append(args, "-subfps %s" % self.args["fps"])
110 self._arg_append(args, "-idx")
111 self._arg_append(args, "-cache 1024")
112 self._arg_append(args, self._setup_audio())
113 self._arg_append(args, self._setup_video())
115 self._arg_append(args, "-really-quiet")
116 self._arg_append(args, "-o %s" % self.args["outfile"])
117 self._arg_append(args, "2>%s" % os.devnull)
121 def _setup_filename(self):
122 _type = self.args["type"]
125 if not os.path.exists(self.args["input"]):
127 "File requested does not exist: %s." % self.args["input"]
129 self.args["input"] = "file://%s" % self.args["input"]
132 self.args["input"] = "dvd://".join(self.args["input"])
134 elif _type == "myth":
135 # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
136 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
137 # myth://IP:PORT:type:file
138 self.args["gmyth-cat"] = self.args["input"].split(":")
139 self.args["input"] = "-"
143 def __init__(self, params):
144 server.Transcoder.__init__(self, params)
145 self.mencoder_opts = []
149 self._setup_filename()
150 self._setup_mencoder_opts(self.mencoder_opts)
156 def _check_opened_file(self, stdw, _stdin):
160 return open(self.args["outfile"])
162 os.write(stdw, _stdin.read(1024))
166 def _start_outfile(self, outfd):
169 # fix this (not necessary)
174 _stdin = open(self.args["input"])
175 size = int(os.path.getsize(self.args["input"]))
177 self.log.error("Mencoder stdin setup error: %s" % e)
184 stdr, stdw = os.pipe()
186 if not self._run_mencoder(input=stdr):
189 stdout = self._check_opened_file(stdw, _stdin)
192 while self.proc and self.proc.poll() == None:
194 data_in = _stdin.read(4096)
196 os.write(stdw, data_in)
198 d = stdout.read(4096)
199 self.status = total_read * 100 / size
205 d = stdout.read(4096)
208 self.log.error("Problems handling data: %s" % e)
212 self.log.info("%s: Finished sending data to client" % repr(self))
216 def _start(self, outfd):
217 # Play a file on disk or DVD
218 if not self._run_mencoder(output=subprocess.PIPE):
222 while self.proc and self.proc.poll() == None:
223 d = self.proc.stdout.read(1024)
226 self.log.error("Problems handling data: %s" % e)
229 self.log.info("%s: Finished sending data to client" % repr(self))
233 def _run_mencoder(self, input=None, output=None):
235 self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
236 stdout=output, close_fds=True)
238 self.log.error("Error executing mencoder: %s" % e)
244 def start(self, outfd):
245 cmd = " ".join(self.mencoder_opts)
246 self.log.debug("Mencoder: %s" % cmd)
250 if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
251 ret = self._start(outfd)
253 elif self.args["type"] == "myth":
254 ret = mythtv.start_myth(self, outfd)
257 ret = self._start_outfile(outfd)
267 os.kill(self.proc.pid, signal.SIGKILL)
280 os.kill(self.gmyth.pid, signal.SIGKILL)