[svn r535] Fixed to the older GMythFileTransfer revision (no GObject inheritance).
12 from subprocess import *
16 def __init__(self, config):
27 self.mencoder_pid = None
28 self.mplayer_pid = None
32 def setup_opts(self, options):
37 self.mplayer = lib.which("mplayer")
39 elif opt.find("language=") >= 0:
41 lan = opt.split("=")[1]
45 log.error("Bad language option: %s" % e)
47 elif opt.find("subtitle=") >= 0:
49 sub = opt.split("=")[1]
53 log.error("Bad subtitle option: %s" % e)
55 elif opt.find("format=") >= 0:
57 self.mpegopts = opt.split("=")[1]
59 log.error("Bad format option: %s" % e)
63 def run_mplayer(self):
66 if self.kind == "dvd":
69 self.mplayer_pid = Popen([self.mplayer, self.filename, "1> %s" % os.devnull,\
70 "2> %s" % os.devnull], stdout=PIPE, close_fds=True)
74 def setup_mencoder(self):
75 self.path = self.config.get("Mencoder", "path")
76 mp = Popen([self.path], stdout=PIPE, close_fds=True)
78 version = mp.stdout.read().split("MEncoder ")[1].split(" (C)")[0].split("-")[-1]
80 if version > "4.1.1": self.mencoder_old = False
81 else: self.mencoder_old = True
83 os.kill(mp.pid, signal.SIGKILL)
84 log.info("Mencoder version: %s" % version)
88 self.fifo = self.config.get("Mencoder", "fifo_path")
91 log.info("Fifo: %s" % e)
97 def setup_audio(self):
99 if self.acodec == "mp3lame":
100 return "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
102 return "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\
103 self.acodec, self.abitrate)
108 def setup_video(self):
112 video += " -of %s" % self.mux
113 video += " -ofps %s" % self.fps
115 if self.vcodec == "nuv" or self.vcodec == "xvid"\
116 or self.vcodec == "qtvideo" or self.vcodec == "copy":
117 video += " -ovc %s" % self.vcodec
119 video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
120 self.vcodec, self.vbitrate)
122 if self.mux == "mpeg" and self.mpegopts is not None:
123 video += " -mpegopts format=%s" % self.mpegopts
125 video += " -vf scale=%s:%s" % (self.width, self.height)
132 def arg_append(self, args, options):
133 l = shlex.split(options)
139 def setup_args(self, args):
141 args.append(self.path)
142 args.append(self.filename)
144 if self.language != None:
145 self.arg_append(args, "-alang %s" % self.language)
147 if self.subtitle != None:
148 self.arg_append(args, "-slang %s" % self.subtitle)
149 self.arg_append(args, "-subfps %s" % self.fps)
151 self.arg_append(args, "-idx")
152 self.arg_append(args, self.audio_opts)
153 self.arg_append(args, self.video_opts)
155 self.arg_append(args, "-really-quiet")
156 self.arg_append(args, "-o %s" % self.fifo)
157 self.arg_append(args, "2> %s" % os.devnull)
161 def setup_filename(self, filename):
163 self.kind, self.filename = filename.split("://")
165 return (False, "Wrong filename protocol")
167 if self.kind == "file":
168 if not os.path.exists(self.filename):
169 msg = "File requested does not exist. SETUP failed."
173 elif self.kind == "dvd":
174 self.filename = "dvd://" + filename
180 def setup_socket(self):
181 if self.socket != None:
184 self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
185 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
188 self.socket.bind( ('', self.port) )
189 self.socket.listen(1)
191 log.error("Could not create socket: %s" % e)
199 MENCODER SETUP DESCRIPTION
200 ===========================
202 -> mux, vcodecs and acodecs
203 |-> mencoder (-of | -ovc | -oac) help
205 -> if used mpeg as mux:
206 |-> to setup format: format=%s as an option at the end
211 # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240
212 # file:///tmp/dvb.mpg mpeg mpeg1video 400 25 mp2 192 320 240 format=mpeg1
213 # dvd://4 mpeg mpeg1video 400 25 mp3lame 192 400 240 language=en local
214 # file:///tmp/mpg/bad_day.mpg avi mpeg4 400 25 mp3 192 320 240
216 def setup(self, filename, mux, vcodec, vbitrate,\
217 fps, acodec, abitrate, width, height, port, options):
221 self.vbitrate = vbitrate
224 self.abitrate = abitrate
227 self.port = int(port)
229 self.setup_mencoder()
231 ret_val = self.setup_filename(filename)
235 self.setup_opts(options)
236 self.audio_opts = self.setup_audio()
237 self.video_opts = self.setup_video()
238 self.setup_args(self.args)
240 ret_val = self.setup_socket()
250 log.info("Starting Mencoder: %s" % self.args )
253 self.mencoder_pid = Popen(self.args, stdout=PIPE, close_fds=True)
255 msg = "Could not init Mencoder: %s" % e
259 if self.mencoder_old: self.pout = open(self.fifo)
260 else: self.pout = self.mencoder_pid.stdout
262 self.child_pid = os.fork()
264 if self.child_pid == 0:
265 conn, addr = self.socket.accept()
266 log.info("Sending Data to client: %s" % addr[0])
268 data = self.pout.read(4096)
273 while data != "" and retry < 5:
276 r, w, x = select([conn], [], [], 0)
278 back = conn.recv(1024)
279 if back == "OK" and self.mplayer and not self.mplayer_pid:
282 except socket.error, e:
283 log.error("Socket error: %s" % e)
286 data = self.pout.read(4096)
289 log.info("Finished sending Data to client: %s" % addr[0])
291 log.error("Client timed out, retried more than %s times" % retry)
293 os.kill(self.mencoder_pid.pid, signal.SIGKILL)
304 if self.mencoder_pid:
305 os.kill(self.mencoder_pid.pid, signal.SIGKILL)
306 self.mencoder_pid = None
309 os.kill(self.mplayer_pid.pid, signal.SIGKILL)
310 self.mplayer_pid = None
312 if self.socket != None:
315 if self.child_pid != None:
316 os.kill(self.child_pid, signal.SIGKILL)
319 log.error("Stop error: %s" % e)