diff -r 2521ec986005 -r 6d00c497a403 gmyth-stream/plugins/media/mencoder.py --- a/gmyth-stream/plugins/media/mencoder.py Sun Apr 01 03:11:01 2007 +0100 +++ b/gmyth-stream/plugins/media/mencoder.py Tue Apr 03 14:56:51 2007 +0100 @@ -2,53 +2,90 @@ import sys import lib import time +import signal import socket import ConfigParser +from select import * +from subprocess import * + class Media: def __init__(self, config): self.config = config + self.language = "en" self.socket = None self.child_pid = None self.mplayer = None + self.mencoder_pid = None + self.mplayer_pid = None + signal.signal(signal.SIGABRT, self.kill_handler) + + def kill_handler(self, sig, frame): + try: + os.kill(self.mplayer_pid.pid + 1, signal.SIGKILL) + sys.exit(0) + except: + lib.log("Problems closing child") def set_args(self, options): for opt in options: + if (opt == "file" or opt == "dvd"): + if (self.acodec == "mp3lame"): + audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate + else: + audio = "-oac lavc -lavcopts acodec=%s abitrate=%s" % (\ + self.acodec, self.abitrate) + if (opt == "file"): self.kind = "file" - self.args += " %s -mf fps=%d -of %s -oac %s -lameopts vol=5 cbr:br=%d"\ - " -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\ + self.args += " %s -mf fps=%s -of %s %s"\ + " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\ " -o %s 1> /dev/null 2> /dev/null" % ( - self.filename, self.fps, self.mux, self.acodec, self.abitrate, - self.vcodec, self.vbitrate, self.width, self.height, self.fifo) + self.filename, self.fps, self.mux, audio, self.vcodec, + self.vbitrate, self.width, self.height, self.fifo) elif (opt == "dvd"): self.kind = "dvd" - self.args += " dvd://%s -vf scale=%d:%d -oac %s -lameopts vol=5 cbr:br=%d"\ - " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -o %s"\ - " -mf fps=%d 1> /dev/null 2> /dev/null" % ( - self.filename, self.width, self.height, self.acodec, self.abitrate, + self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\ + " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -o %s"\ + " -ofps %s 1> /dev/null 2> /dev/null" % ( + self.filename, self.language, self.width, self.height, audio, self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps) elif (opt == "local"): self.mplayer = os.popen("which mplayer").read().strip() + elif (opt.find("language=") >= 0): + try: + self.language = opt.split("=")[1] + except: + lib.log("Bad language option") + + + def run_mplayer(self): + msg = "%s 1>/dev/null 2>/dev/null" % self.filename + if (self.kind == "dvd"): + msg = "dvd://" + msg + + self.mplayer += " " + msg + self.mplayer_pid = Popen(self.mplayer, shell=True) + def setup(self, filename, mux, vcodec, vbitrate,\ fps, acodec, abitrate, width, height, port, options): self.filename = filename self.mux = mux self.vcodec = vcodec - self.vbitrate = int(vbitrate) - self.fps = int(fps) + self.vbitrate = vbitrate + self.fps = fps self.acodec = acodec - self.abitrate = int(abitrate) - self.width = int(width) - self.height = int(height) + self.abitrate = abitrate + self.width = width + self.height = height self.port = int(port) self.fifo = self.config.get("Mencoder", "fifo_path") @@ -63,9 +100,7 @@ return msg # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000 - #mencoder dvd://2 -vf scale=640:480 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4 self.path = self.config.get("Mencoder", "path") - self.path += self.args if (self.socket != None): del(self.socket) @@ -85,10 +120,9 @@ except: lib.log("Fifo already exists") - lib.log("Starting Mencoder: %s" % self.path ) + lib.log("Starting Mencoder: %s %s" % (self.path, self.args) ) # exec Mencoder - pin, self.pout = os.popen2(self.path) - pin.close() + self.mencoder_pid = Popen(self.path + self.args, shell=True) fifo = open(self.fifo) @@ -102,17 +136,15 @@ conn.settimeout(5) retry = 0 - if (self.mplayer): - msg = "%s 1>/dev/null 2>/dev/null" % self.filename - if (self.kind == "dvd"): - msg = "dvd://" + msg - - self.mplayer += " " + msg - mplayer_in, self.mplayer_out = os.popen2(self.mplayer) - while( data != "" and retry < 5): try: conn.send(data) + r, w, x = select([conn], [], [], 0) + if conn in r: + back = conn.recv(1024) + if (back == "OK" and self.mplayer and not self.mplayer_pid): + self.run_mplayer() + except socket.error: lib.log("Socket error (maybe timeout ?)") retry += 1 @@ -124,20 +156,20 @@ else: lib.log("Client timed out") + sys.exit(0) + def stop(self): try: - self.pout.close() - self.mplayer_out.close() + os.kill(self.mencoder_pid.pid + 1, signal.SIGKILL) + self.mplayer = None except: lib.log("Trying to stop before playing...") - self.mplayer = None - if (self.socket != None): lib.log("Closing socket") self.socket.close() lib.log("Trying to stop Mencoder process") if (self.child_pid != None): - os.kill(self.child_pid, 9) + os.kill(self.child_pid, signal.SIGABRT)