1.1 --- a/gmyth-stream/plugins/media/mencoder.py Sun Apr 01 03:11:01 2007 +0100
1.2 +++ b/gmyth-stream/plugins/media/mencoder.py Mon Apr 02 18:00:39 2007 +0100
1.3 @@ -2,53 +2,90 @@
1.4 import sys
1.5 import lib
1.6 import time
1.7 +import signal
1.8 import socket
1.9 import ConfigParser
1.10
1.11 +from select import *
1.12 +from subprocess import *
1.13 +
1.14 class Media:
1.15
1.16 def __init__(self, config):
1.17
1.18 self.config = config
1.19 + self.language = "en"
1.20 self.socket = None
1.21 self.child_pid = None
1.22 self.mplayer = None
1.23 + self.mencoder_pid = None
1.24 + self.mplayer_pid = None
1.25 + signal.signal(signal.SIGABRT, self.kill_handler)
1.26 +
1.27 + def kill_handler(self, sig, frame):
1.28 + try:
1.29 + os.kill(self.mplayer_pid.pid + 1, signal.SIGKILL)
1.30 + sys.exit(0)
1.31 + except:
1.32 + lib.log("Problems closing child")
1.33
1.34 def set_args(self, options):
1.35
1.36 for opt in options:
1.37
1.38 + if (opt == "file" or opt == "dvd"):
1.39 + if (self.acodec == "mp3lame"):
1.40 + audio = "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
1.41 + else:
1.42 + audio = "-oac lavc -lavcopts acodec=%s abitrate=%s" % (\
1.43 + self.acodec, self.abitrate)
1.44 +
1.45 if (opt == "file"):
1.46 self.kind = "file"
1.47 - self.args += " %s -mf fps=%d -of %s -oac %s -lameopts vol=5 cbr:br=%d"\
1.48 - " -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
1.49 + self.args += " %s -mf fps=%s -of %s %s"\
1.50 + " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -vf scale=%s:%s"\
1.51 " -o %s 1> /dev/null 2> /dev/null" % (
1.52 - self.filename, self.fps, self.mux, self.acodec, self.abitrate,
1.53 - self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
1.54 + self.filename, self.fps, self.mux, audio, self.vcodec,
1.55 + self.vbitrate, self.width, self.height, self.fifo)
1.56
1.57 elif (opt == "dvd"):
1.58 self.kind = "dvd"
1.59 - self.args += " dvd://%s -vf scale=%d:%d -oac %s -lameopts vol=5 cbr:br=%d"\
1.60 - " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%d -o %s"\
1.61 - " -mf fps=%d 1> /dev/null 2> /dev/null" % (
1.62 - self.filename, self.width, self.height, self.acodec, self.abitrate,
1.63 + self.args += " dvd://%s -alang %s -vf scale=%s:%s %s"\
1.64 + " -of %s -ovc lavc -lavcopts vcodec=%s:vbitrate=%s -o %s"\
1.65 + " -ofps %s 1> /dev/null 2> /dev/null" % (
1.66 + self.filename, self.language, self.width, self.height, audio,
1.67 self.mux, self.vcodec, self.vbitrate, self.fifo, self.fps)
1.68
1.69 elif (opt == "local"):
1.70 self.mplayer = os.popen("which mplayer").read().strip()
1.71
1.72 + elif (opt.find("language=") >= 0):
1.73 + try:
1.74 + self.language = opt.split("=")[1]
1.75 + except:
1.76 + lib.log("Bad language option")
1.77 +
1.78 +
1.79 + def run_mplayer(self):
1.80 + msg = "%s 1>/dev/null 2>/dev/null" % self.filename
1.81 + if (self.kind == "dvd"):
1.82 + msg = "dvd://" + msg
1.83 +
1.84 + self.mplayer += " " + msg
1.85 + self.mplayer_pid = Popen(self.mplayer, shell=True)
1.86 +
1.87 def setup(self, filename, mux, vcodec, vbitrate,\
1.88 fps, acodec, abitrate, width, height, port, options):
1.89
1.90 self.filename = filename
1.91 self.mux = mux
1.92 self.vcodec = vcodec
1.93 - self.vbitrate = int(vbitrate)
1.94 - self.fps = int(fps)
1.95 + self.vbitrate = vbitrate
1.96 + self.fps = fps
1.97 self.acodec = acodec
1.98 - self.abitrate = int(abitrate)
1.99 - self.width = int(width)
1.100 - self.height = int(height)
1.101 + self.abitrate = abitrate
1.102 + self.width = width
1.103 + self.height = height
1.104
1.105 self.port = int(port)
1.106 self.fifo = self.config.get("Mencoder", "fifo_path")
1.107 @@ -63,9 +100,7 @@
1.108 return msg
1.109
1.110 # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
1.111 - #mencoder dvd://2 -vf scale=640:480 -o title2.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4
1.112 self.path = self.config.get("Mencoder", "path")
1.113 - self.path += self.args
1.114
1.115 if (self.socket != None):
1.116 del(self.socket)
1.117 @@ -85,10 +120,9 @@
1.118 except:
1.119 lib.log("Fifo already exists")
1.120
1.121 - lib.log("Starting Mencoder: %s" % self.path )
1.122 + lib.log("Starting Mencoder: %s %s" % (self.path, self.args) )
1.123 # exec Mencoder
1.124 - pin, self.pout = os.popen2(self.path)
1.125 - pin.close()
1.126 + self.mencoder_pid = Popen(self.path + self.args, shell=True)
1.127
1.128 fifo = open(self.fifo)
1.129
1.130 @@ -102,17 +136,15 @@
1.131 conn.settimeout(5)
1.132 retry = 0
1.133
1.134 - if (self.mplayer):
1.135 - msg = "%s 1>/dev/null 2>/dev/null" % self.filename
1.136 - if (self.kind == "dvd"):
1.137 - msg = "dvd://" + msg
1.138 -
1.139 - self.mplayer += " " + msg
1.140 - mplayer_in, self.mplayer_out = os.popen2(self.mplayer)
1.141 -
1.142 while( data != "" and retry < 5):
1.143 try:
1.144 conn.send(data)
1.145 + r, w, x = select([conn], [], [], 0)
1.146 + if conn in r:
1.147 + back = conn.recv(1024)
1.148 + if (back == "OK" and self.mplayer and not self.mplayer_pid):
1.149 + self.run_mplayer()
1.150 +
1.151 except socket.error:
1.152 lib.log("Socket error (maybe timeout ?)")
1.153 retry += 1
1.154 @@ -124,20 +156,20 @@
1.155 else:
1.156 lib.log("Client timed out")
1.157
1.158 + sys.exit(0)
1.159 +
1.160
1.161 def stop(self):
1.162 try:
1.163 - self.pout.close()
1.164 - self.mplayer_out.close()
1.165 + os.kill(self.mencoder_pid.pid + 1, signal.SIGKILL)
1.166 + self.mplayer = None
1.167 except:
1.168 lib.log("Trying to stop before playing...")
1.169
1.170 - self.mplayer = None
1.171 -
1.172 if (self.socket != None):
1.173 lib.log("Closing socket")
1.174 self.socket.close()
1.175
1.176 lib.log("Trying to stop Mencoder process")
1.177 if (self.child_pid != None):
1.178 - os.kill(self.child_pid, 9)
1.179 + os.kill(self.child_pid, signal.SIGABRT)