morphbr@577
|
1 |
import os
|
morphbr@577
|
2 |
import shlex
|
morphbr@577
|
3 |
import signal
|
morphbr@577
|
4 |
import subprocess
|
morphbr@595
|
5 |
import time
|
morphbr@651
|
6 |
import fcntl
|
morphbr@577
|
7 |
|
morphbr@565
|
8 |
import lib.utils as utils
|
morphbr@565
|
9 |
import lib.server as server
|
morphbr@653
|
10 |
import plugins.transcoders.mencoder_lib.mythtv as mythtv
|
morphbr@565
|
11 |
|
morphbr@595
|
12 |
from select import select
|
morphbr@595
|
13 |
|
morphbr@565
|
14 |
__all__ = ("TranscoderMencoder",)
|
morphbr@565
|
15 |
|
morphbr@565
|
16 |
class TranscoderMencoder(server.Transcoder):
|
morphbr@585
|
17 |
"""Transcoder class that implements a transcoder using Mencoder"""
|
morphbr@565
|
18 |
mencoder_path = utils.which("mencoder")
|
morphbr@565
|
19 |
name = "mencoder"
|
morphbr@565
|
20 |
priority = -1
|
morphbr@577
|
21 |
args = {}
|
morphbr@577
|
22 |
proc = None
|
morphbr@605
|
23 |
gmyth = None
|
morphbr@565
|
24 |
|
morphbr@595
|
25 |
# only works with avi container
|
morphbr@595
|
26 |
status = 0
|
morphbr@595
|
27 |
|
morphbr@569
|
28 |
def _setup_params(self):
|
morphbr@569
|
29 |
params_first = self.params_first
|
morphbr@569
|
30 |
|
morphbr@569
|
31 |
# general_opts
|
morphbr@569
|
32 |
self.args["local"] = params_first("local", False)
|
morphbr@569
|
33 |
self.args["language"] = params_first("language", False)
|
morphbr@569
|
34 |
self.args["subtitle"] = params_first("subtitle", False)
|
morphbr@628
|
35 |
self.args["format"] = params_first("format", "mpeg1")
|
morphbr@569
|
36 |
self.args["outfile"] = params_first("outfile", "-")
|
morphbr@569
|
37 |
|
morphbr@569
|
38 |
# input_opt
|
morphbr@605
|
39 |
uri = params_first("uri", "file:-").split(":", 1)
|
morphbr@577
|
40 |
self.args["type"] = uri[0]
|
morphbr@577
|
41 |
self.args["input"] = uri[1]
|
morphbr@569
|
42 |
|
morphbr@569
|
43 |
# audio_opts
|
morphbr@569
|
44 |
self.args["acodec"] = params_first("acodec", "mp2")
|
morphbr@569
|
45 |
self.args["abitrate"] = params_first("abitrate", 192)
|
morphbr@569
|
46 |
self.args["volume"] = params_first("volume", 5)
|
morphbr@569
|
47 |
|
morphbr@569
|
48 |
# video_opts
|
morphbr@569
|
49 |
self.args["mux"] = params_first("mux", "mpeg")
|
morphbr@569
|
50 |
self.args["fps"] = params_first("fps", 25)
|
morphbr@569
|
51 |
self.args["vcodec"] = params_first("vcodec", "mpeg1video")
|
morphbr@569
|
52 |
self.args["vbitrate"] = params_first("vbitrate", 400)
|
morphbr@569
|
53 |
self.args["width"] = params_first("width", 320)
|
morphbr@569
|
54 |
self.args["height"] = params_first("height", 240)
|
morphbr@569
|
55 |
# _setup_params()
|
morphbr@569
|
56 |
|
morphbr@569
|
57 |
|
morphbr@569
|
58 |
def _setup_audio(self):
|
morphbr@569
|
59 |
if self.args["acodec"] == "mp3lame":
|
morphbr@569
|
60 |
audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
|
morphbr@569
|
61 |
self.args["abitrate"], self.args["volume"])
|
morphbr@569
|
62 |
else:
|
morphbr@569
|
63 |
audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
|
morphbr@569
|
64 |
self.args["acodec"], self.args["abitrate"])
|
morphbr@569
|
65 |
|
morphbr@569
|
66 |
return audio
|
morphbr@569
|
67 |
# _setup_audio()
|
morphbr@569
|
68 |
|
morphbr@569
|
69 |
|
morphbr@569
|
70 |
def _setup_video(self):
|
morphbr@577
|
71 |
video = " -of %s" % self.args["mux"]
|
morphbr@577
|
72 |
video += " -ofps %s" % self.args["fps"]
|
morphbr@569
|
73 |
|
morphbr@569
|
74 |
vcodec = self.args["vcodec"]
|
morphbr@569
|
75 |
if vcodec == "nuv" or vcodec == "xvid"\
|
morphbr@569
|
76 |
or vcodec == "qtvideo" or vcodec == "copy":
|
morphbr@569
|
77 |
video += " -ovc %s" % vcodec
|
morphbr@569
|
78 |
else:
|
morphbr@569
|
79 |
video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
|
morphbr@569
|
80 |
vcodec, self.args["vbitrate"])
|
morphbr@569
|
81 |
|
morphbr@628
|
82 |
if self.args["mux"] == "mpeg":
|
morphbr@628
|
83 |
video += " -mpegopts format=%s" % self.args["format"]
|
morphbr@569
|
84 |
video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
|
morphbr@569
|
85 |
|
morphbr@569
|
86 |
return video
|
morphbr@569
|
87 |
# _setup_video()
|
morphbr@569
|
88 |
|
morphbr@569
|
89 |
|
morphbr@569
|
90 |
def _arg_append(self, args, options):
|
morphbr@577
|
91 |
for arg in shlex.split(options):
|
morphbr@569
|
92 |
args.append(arg)
|
morphbr@569
|
93 |
# arg_append()
|
morphbr@569
|
94 |
|
morphbr@569
|
95 |
def _setup_mencoder_opts(self, args):
|
morphbr@569
|
96 |
args.append(self.mencoder_path)
|
morphbr@595
|
97 |
|
morphbr@595
|
98 |
if self.args["outfile"] == "-" and self.args["type"]:
|
morphbr@595
|
99 |
args.append(self.args["input"])
|
morphbr@595
|
100 |
else:
|
morphbr@595
|
101 |
args.append("-")
|
morphbr@569
|
102 |
|
morphbr@569
|
103 |
if self.args["language"]:
|
morphbr@569
|
104 |
self._arg_append(args, "-alang %s" % self.args["language"])
|
morphbr@569
|
105 |
|
morphbr@569
|
106 |
if self.args["subtitle"]:
|
morphbr@569
|
107 |
self._arg_append(args, "-slang %s" % self.args["subtitle"])
|
morphbr@569
|
108 |
self._arg_append(args, "-subfps %s" % self.args["fps"])
|
morphbr@569
|
109 |
|
morphbr@569
|
110 |
self._arg_append(args, "-idx")
|
morphbr@628
|
111 |
self._arg_append(args, "-cache 1024")
|
morphbr@577
|
112 |
self._arg_append(args, self._setup_audio())
|
morphbr@577
|
113 |
self._arg_append(args, self._setup_video())
|
morphbr@569
|
114 |
|
morphbr@569
|
115 |
self._arg_append(args, "-really-quiet")
|
morphbr@569
|
116 |
self._arg_append(args, "-o %s" % self.args["outfile"])
|
morphbr@595
|
117 |
self._arg_append(args, "2>%s" % os.devnull)
|
morphbr@569
|
118 |
# _setup_args()
|
morphbr@569
|
119 |
|
morphbr@569
|
120 |
|
morphbr@569
|
121 |
def _setup_filename(self):
|
morphbr@569
|
122 |
_type = self.args["type"]
|
morphbr@569
|
123 |
|
morphbr@569
|
124 |
if _type == "file":
|
morphbr@569
|
125 |
if not os.path.exists(self.args["input"]):
|
morphbr@577
|
126 |
raise IOError,\
|
morphbr@577
|
127 |
"File requested does not exist: %s." % self.args["input"]
|
morphbr@605
|
128 |
else:
|
morphbr@605
|
129 |
self.args["input"] = "file://%s" % self.args["input"]
|
morphbr@569
|
130 |
|
morphbr@569
|
131 |
elif _type == "dvd":
|
morphbr@569
|
132 |
self.args["input"] = "dvd://".join(self.args["input"])
|
morphbr@595
|
133 |
|
morphbr@595
|
134 |
elif _type == "myth":
|
morphbr@595
|
135 |
# gmyth-cat -h 192.168.1.124 -p 6543 -c 111
|
morphbr@595
|
136 |
# gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
|
morphbr@595
|
137 |
# myth://IP:PORT:type:file
|
morphbr@595
|
138 |
self.args["gmyth-cat"] = self.args["input"].split(":")
|
morphbr@605
|
139 |
self.args["input"] = "-"
|
morphbr@569
|
140 |
# _setup_filename()
|
morphbr@569
|
141 |
|
morphbr@569
|
142 |
|
morphbr@572
|
143 |
def __init__(self, params):
|
morphbr@565
|
144 |
server.Transcoder.__init__(self, params)
|
morphbr@577
|
145 |
self.mencoder_opts = []
|
morphbr@565
|
146 |
|
morphbr@569
|
147 |
try:
|
morphbr@569
|
148 |
self._setup_params()
|
morphbr@569
|
149 |
self._setup_filename()
|
morphbr@569
|
150 |
self._setup_mencoder_opts(self.mencoder_opts)
|
morphbr@569
|
151 |
except Exception, e:
|
morphbr@569
|
152 |
self.log.error(e)
|
morphbr@565
|
153 |
# __init__()
|
morphbr@565
|
154 |
|
morphbr@565
|
155 |
|
morphbr@595
|
156 |
def _check_opened_file(self, stdw, _stdin):
|
morphbr@595
|
157 |
loop = True
|
morphbr@595
|
158 |
while loop:
|
morphbr@595
|
159 |
try:
|
morphbr@595
|
160 |
return open(self.args["outfile"])
|
morphbr@595
|
161 |
except:
|
morphbr@595
|
162 |
os.write(stdw, _stdin.read(1024))
|
morphbr@595
|
163 |
# _check_opened_file
|
morphbr@595
|
164 |
|
morphbr@595
|
165 |
|
morphbr@595
|
166 |
def _start_outfile(self, outfd):
|
morphbr@595
|
167 |
finished = False
|
morphbr@595
|
168 |
|
morphbr@595
|
169 |
# fix this (not necessary)
|
morphbr@595
|
170 |
outfd.write("OK")
|
morphbr@595
|
171 |
|
morphbr@595
|
172 |
# Configuring stdin
|
morphbr@638
|
173 |
try:
|
morphbr@638
|
174 |
_stdin = open(self.args["input"])
|
morphbr@638
|
175 |
size = int(os.path.getsize(self.args["input"]))
|
morphbr@638
|
176 |
except Exception, e:
|
morphbr@638
|
177 |
self.log.error("Mencoder stdin setup error: %s" % e)
|
morphbr@638
|
178 |
return False
|
morphbr@638
|
179 |
|
morphbr@595
|
180 |
self.status = 0
|
morphbr@595
|
181 |
total_read = 0
|
morphbr@595
|
182 |
|
morphbr@595
|
183 |
# Configuring pipes
|
morphbr@595
|
184 |
stdr, stdw = os.pipe()
|
morphbr@569
|
185 |
|
morphbr@605
|
186 |
if not self._run_mencoder(input=stdr):
|
morphbr@565
|
187 |
return False
|
morphbr@565
|
188 |
|
morphbr@595
|
189 |
stdout = self._check_opened_file(stdw, _stdin)
|
morphbr@595
|
190 |
|
morphbr@595
|
191 |
try:
|
morphbr@595
|
192 |
while self.proc and self.proc.poll() == None:
|
morphbr@595
|
193 |
if not finished:
|
morphbr@595
|
194 |
data_in = _stdin.read(4096)
|
morphbr@595
|
195 |
if data_in != "":
|
morphbr@595
|
196 |
os.write(stdw, data_in)
|
morphbr@595
|
197 |
total_read += 4096
|
morphbr@595
|
198 |
d = stdout.read(4096)
|
morphbr@595
|
199 |
self.status = total_read * 100 / size
|
morphbr@595
|
200 |
else:
|
morphbr@595
|
201 |
finished = True
|
morphbr@595
|
202 |
os.close(stdw)
|
morphbr@595
|
203 |
|
morphbr@595
|
204 |
else:
|
morphbr@595
|
205 |
d = stdout.read(4096)
|
morphbr@595
|
206 |
|
morphbr@595
|
207 |
except Exception, e:
|
morphbr@595
|
208 |
self.log.error("Problems handling data: %s" % e)
|
morphbr@628
|
209 |
self.stop()
|
morphbr@595
|
210 |
return False
|
morphbr@595
|
211 |
|
morphbr@595
|
212 |
self.log.info("%s: Finished sending data to client" % repr(self))
|
morphbr@595
|
213 |
return True
|
morphbr@595
|
214 |
# _start_outfile()
|
morphbr@595
|
215 |
|
morphbr@595
|
216 |
def _start(self, outfd):
|
morphbr@651
|
217 |
# Play a file on disk or DVD
|
morphbr@605
|
218 |
if not self._run_mencoder(output=subprocess.PIPE):
|
morphbr@595
|
219 |
return False
|
morphbr@595
|
220 |
|
morphbr@565
|
221 |
try:
|
morphbr@565
|
222 |
while self.proc and self.proc.poll() == None:
|
morphbr@577
|
223 |
d = self.proc.stdout.read(1024)
|
morphbr@565
|
224 |
outfd.write(d)
|
morphbr@565
|
225 |
except Exception, e:
|
morphbr@565
|
226 |
self.log.error("Problems handling data: %s" % e)
|
morphbr@565
|
227 |
return False
|
morphbr@565
|
228 |
|
morphbr@595
|
229 |
self.log.info("%s: Finished sending data to client" % repr(self))
|
morphbr@565
|
230 |
return True
|
morphbr@595
|
231 |
# _start()
|
morphbr@595
|
232 |
|
morphbr@605
|
233 |
def _run_mencoder(self, input=None, output=None):
|
morphbr@605
|
234 |
try:
|
morphbr@605
|
235 |
self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
|
morphbr@605
|
236 |
stdout=output, close_fds=True)
|
morphbr@605
|
237 |
except Exception, e:
|
morphbr@605
|
238 |
self.log.error("Error executing mencoder: %s" % e)
|
morphbr@605
|
239 |
return False
|
morphbr@605
|
240 |
|
morphbr@605
|
241 |
return True
|
morphbr@605
|
242 |
# _run_mencoder()
|
morphbr@595
|
243 |
|
morphbr@595
|
244 |
def start(self, outfd):
|
morphbr@595
|
245 |
cmd = " ".join(self.mencoder_opts)
|
morphbr@595
|
246 |
self.log.debug("Mencoder: %s" % cmd)
|
morphbr@595
|
247 |
|
morphbr@605
|
248 |
ret = False
|
morphbr@605
|
249 |
|
morphbr@605
|
250 |
if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
|
morphbr@605
|
251 |
ret = self._start(outfd)
|
morphbr@605
|
252 |
|
morphbr@605
|
253 |
elif self.args["type"] == "myth":
|
morphbr@653
|
254 |
ret = mythtv.start_myth(self, outfd)
|
morphbr@605
|
255 |
|
morphbr@595
|
256 |
else:
|
morphbr@605
|
257 |
ret = self._start_outfile(outfd)
|
morphbr@605
|
258 |
|
morphbr@605
|
259 |
self.stop()
|
morphbr@605
|
260 |
return ret
|
morphbr@565
|
261 |
# start()
|
morphbr@565
|
262 |
|
morphbr@565
|
263 |
|
morphbr@565
|
264 |
def stop(self):
|
morphbr@565
|
265 |
if self.proc:
|
morphbr@565
|
266 |
try:
|
morphbr@605
|
267 |
os.kill(self.proc.pid, signal.SIGKILL)
|
morphbr@565
|
268 |
except OSError, e:
|
morphbr@565
|
269 |
pass
|
morphbr@565
|
270 |
|
morphbr@565
|
271 |
try:
|
morphbr@565
|
272 |
self.proc.wait()
|
morphbr@565
|
273 |
except Exception, e:
|
morphbr@565
|
274 |
pass
|
morphbr@565
|
275 |
|
morphbr@565
|
276 |
self.proc = None
|
morphbr@605
|
277 |
|
morphbr@605
|
278 |
if self.gmyth:
|
morphbr@605
|
279 |
try:
|
morphbr@605
|
280 |
os.kill(self.gmyth.pid, signal.SIGKILL)
|
morphbr@605
|
281 |
except OSError, e:
|
morphbr@605
|
282 |
pass
|
morphbr@605
|
283 |
|
morphbr@605
|
284 |
try:
|
morphbr@605
|
285 |
self.gmyth.wait()
|
morphbr@605
|
286 |
except Exception, e:
|
morphbr@605
|
287 |
pass
|
morphbr@605
|
288 |
|
morphbr@605
|
289 |
self.gmyth = None
|
morphbr@605
|
290 |
|
morphbr@569
|
291 |
# stop()
|
morphbr@565
|
292 |
|
morphbr@565
|
293 |
# TranscoderMencoder
|