1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/plugins/media/ffmpeg.py Tue Apr 03 19:42:17 2007 +0100
1.3 @@ -0,0 +1,91 @@
1.4 +import os
1.5 +import sys
1.6 +import lib
1.7 +import time
1.8 +import socket
1.9 +import ConfigParser
1.10 +
1.11 +class Media:
1.12 +
1.13 + def __init__(self, config):
1.14 +
1.15 + self.config = config
1.16 + self.socket = None
1.17 + self.child_pid = None
1.18 +
1.19 + def setup(self, filename, mux, vcodec, vbitrate,\
1.20 + fps, acodec, abitrate, width, height, port):
1.21 +
1.22 + self.filename = filename
1.23 + self.mux = mux
1.24 + self.vcodec = vcodec
1.25 + self.vbitrate = int(vbitrate)
1.26 + self.fps = int(fps)
1.27 + self.acodec = acodec
1.28 + self.abitrate = int(abitrate)
1.29 + self.width = int(width)
1.30 + self.height = int(height)
1.31 +
1.32 + self.port = int(port)
1.33 +
1.34 + # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000
1.35 + self.path = self.config.get("FFmpeg", "path")
1.36 + self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % (
1.37 + self.filename, self.mux, self.vcodec, self.vbitrate,\
1.38 + self.fps, self.acodec, self.abitrate, self.width, self.height)
1.39 +
1.40 + if (self.socket != None):
1.41 + del(self.socket)
1.42 +
1.43 + self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
1.44 + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1.45 + self.socket.bind( ('', self.port) )
1.46 + self.socket.settimeout(10)
1.47 + self.socket.listen(1)
1.48 +
1.49 + def play(self):
1.50 +
1.51 + lib.log("Starting FFmpeg: %s" % self.path)
1.52 +
1.53 + # exec FFmpeg and get stdout
1.54 + child_stdin, child_stdout = os.popen2(self.path)
1.55 + child_stdin.close()
1.56 +
1.57 + self.child_pid = os.fork()
1.58 + if (self.child_pid == 0):
1.59 + #child
1.60 +
1.61 + conn,addr= self.socket.accept()
1.62 + lib.log("Sending Data to client: %s" % addr[0])
1.63 + data = child_stdout.read(1024)
1.64 + conn.settimeout(5)
1.65 + retry = 0
1.66 +
1.67 + while( data != "" and retry < 5):
1.68 + try:
1.69 + conn.send(data)
1.70 + except socket.error:
1.71 + lib.log("Socket error (maybe timeout ?)")
1.72 + retry = retry + 1
1.73 +
1.74 + data = child_stdout.read(1024)
1.75 +
1.76 + if (retry < 5):
1.77 + lib.log("Finished sending Data to client: %s" % addr[0])
1.78 + else:
1.79 + lib.log("Client timed out")
1.80 +
1.81 + child_stdout.close()
1.82 + #conn.close()
1.83 + #sys.exit()
1.84 +
1.85 +
1.86 + def stop(self):
1.87 +
1.88 + if (self.socket != None):
1.89 + lib.log("Closing socket")
1.90 + self.socket.close()
1.91 +
1.92 + lib.log("Trying to stop FFmpeg process")
1.93 + if (self.child_pid != None):
1.94 + os.kill(self.child_pid, 9)