[svn r604] Generate coverage reports using lcov.
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")
22 # only works with avi container
25 def _setup_params(self):
26 params_first = self.params_first
29 self.args["local"] = params_first("local", False)
30 self.args["language"] = params_first("language", False)
31 self.args["subtitle"] = params_first("subtitle", False)
32 self.args["format"] = params_first("format", "")
33 self.args["outfile"] = params_first("outfile", "-")
34 self.args["sendback"] = params_first("sendback", True)
36 # handle sendback variable
37 if self.args["sendback"] == "False":
38 self.args["sendback"] = False
41 uri = params_first("uri", "file://-").split("://")
42 self.args["type"] = uri[0]
43 self.args["input"] = uri[1]
46 self.args["acodec"] = params_first("acodec", "mp2")
47 self.args["abitrate"] = params_first("abitrate", 192)
48 self.args["volume"] = params_first("volume", 5)
51 self.args["mux"] = params_first("mux", "mpeg")
52 self.args["fps"] = params_first("fps", 25)
53 self.args["vcodec"] = params_first("vcodec", "mpeg1video")
54 self.args["vbitrate"] = params_first("vbitrate", 400)
55 self.args["width"] = params_first("width", 320)
56 self.args["height"] = params_first("height", 240)
60 def _setup_audio(self):
61 if self.args["acodec"] == "mp3lame":
62 audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
63 self.args["abitrate"], self.args["volume"])
65 audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
66 self.args["acodec"], self.args["abitrate"])
72 def _setup_video(self):
73 video = " -of %s" % self.args["mux"]
74 video += " -ofps %s" % self.args["fps"]
76 vcodec = self.args["vcodec"]
77 if vcodec == "nuv" or vcodec == "xvid"\
78 or vcodec == "qtvideo" or vcodec == "copy":
79 video += " -ovc %s" % vcodec
81 video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
82 vcodec, self.args["vbitrate"])
84 video += " %s" % self.args["format"]
85 video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
91 def _arg_append(self, args, options):
92 for arg in shlex.split(options):
96 def _setup_mencoder_opts(self, args):
97 args.append(self.mencoder_path)
99 if self.args["outfile"] == "-" and self.args["type"]:
100 args.append(self.args["input"])
104 if self.args["language"]:
105 self._arg_append(args, "-alang %s" % self.args["language"])
107 if self.args["subtitle"]:
108 self._arg_append(args, "-slang %s" % self.args["subtitle"])
109 self._arg_append(args, "-subfps %s" % self.args["fps"])
111 self._arg_append(args, "-idx")
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"]
130 self.args["input"] = "dvd://".join(self.args["input"])
132 elif _type == "myth":
133 # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
134 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
135 # myth://IP:PORT:type:file
136 self.args["gmyth-cat"] = self.args["input"].split(":")
140 def __init__(self, params):
141 server.Transcoder.__init__(self, params)
142 self.mencoder_opts = []
146 self._setup_filename()
147 self._setup_mencoder_opts(self.mencoder_opts)
153 def _check_opened_file(self, stdw, _stdin):
157 return open(self.args["outfile"])
159 os.write(stdw, _stdin.read(1024))
163 def _start_outfile(self, outfd):
166 # fix this (not necessary)
170 _stdin = open(self.args["input"])
171 size = int(os.path.getsize(self.args["input"]))
176 stdr, stdw = os.pipe()
179 self.proc = subprocess.Popen(self.mencoder_opts, stdin=stdr, close_fds=True)
181 self.log.error("Error executing mencoder: %s" % e)
184 stdout = self._check_opened_file(stdw, _stdin)
187 while self.proc and self.proc.poll() == None:
189 data_in = _stdin.read(4096)
191 os.write(stdw, data_in)
193 d = stdout.read(4096)
194 if self.args["sendback"]:
196 self.status = total_read * 100 / size
202 d = stdout.read(4096)
203 if self.args["sendback"] and d != "":
207 self.log.error("Problems handling data: %s" % e)
210 self.log.info("%s: Finished sending data to client" % repr(self))
211 if not self.args["sendback"]:
217 def _start(self, outfd):
219 self.proc = subprocess.Popen(self.mencoder_opts,
220 stdout=subprocess.PIPE, close_fds=True)
222 self.log.error("Error executing mencoder: %s" % e)
227 while self.proc and self.proc.poll() == None:
228 d = self.proc.stdout.read(1024)
231 self.log.error("Problems handling data: %s" % e)
234 self.log.info("%s: Finished sending data to client" % repr(self))
239 def start(self, outfd):
240 cmd = " ".join(self.mencoder_opts)
241 self.log.debug("Mencoder: %s" % cmd)
243 if self.args["outfile"] == "-":
244 return self._start(outfd)
246 return self._start_outfile(outfd)
253 os.kill(self.proc.pid, signal.SIGTERM)