1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/plugins/media/ffmpeg.py Wed Mar 28 21:26:42 2007 +0100
1.3 @@ -0,0 +1,72 @@
1.4 +import os
1.5 +import sys
1.6 +import time
1.7 +import socket
1.8 +import ConfigParser
1.9 +
1.10 +class Media:
1.11 +
1.12 + def __init__(self, config):
1.13 +
1.14 + self.config = config
1.15 +
1.16 + def setup(self, filename, mux, vcodec, vbitrate,\
1.17 + fps, acodec, abitrate, width, height, port):
1.18 +
1.19 + self.filename = filename
1.20 + self.mux = mux
1.21 + self.vcodec = vcodec
1.22 + self.vbitrate = int(vbitrate)
1.23 + self.fps = int(fps)
1.24 + self.acodec = acodec
1.25 + self.abitrate = int(abitrate)
1.26 + self.width = int(width)
1.27 + self.height = int(height)
1.28 +
1.29 + self.port = int(port)
1.30 +
1.31 + # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000
1.32 + self.path = self.config.get("FFmpeg", "path")
1.33 + self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % (
1.34 + self.filename, self.mux, self.vcodec, self.vbitrate,\
1.35 + self.fps, self.acodec, self.abitrate, self.width, self.height)
1.36 +
1.37 + self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
1.38 + self.socket.bind( ('', self.port) )
1.39 +
1.40 + def play(self):
1.41 +
1.42 + self.socket.listen(1)
1.43 +
1.44 + print "Starting FFmpeg: %s" % self.path
1.45 +
1.46 + # exec FFmpeg and get stdout
1.47 + child_stdin, child_stdout = os.popen2(self.path)
1.48 + child_stdin.close()
1.49 +
1.50 + self.child_pid = os.fork()
1.51 + if (self.child_pid == 0):
1.52 + #child
1.53 +
1.54 + conn,addr= self.socket.accept()
1.55 + print "--> Sending Data..."
1.56 + data = child_stdout.read(1024)
1.57 +
1.58 + while( data != ""):
1.59 + try:
1.60 + conn.send(data)
1.61 + except socket.error:
1.62 + break
1.63 + data = child_stdout.read(1024)
1.64 +
1.65 + print "--> Finished sending data..."
1.66 + conn.close()
1.67 +
1.68 +
1.69 + def stop(self):
1.70 +
1.71 + print "--> Trying to stop FFmpeg process..."
1.72 + self.socket.close()
1.73 + os.kill(self.child_pid, 9)
1.74 +
1.75 +