morphbr@466: import os morphbr@466: import sys morphbr@466: import time morphbr@466: import socket morphbr@466: import ConfigParser morphbr@466: morphbr@466: class Media: morphbr@466: morphbr@466: def __init__(self, config): morphbr@466: morphbr@466: self.config = config morphbr@466: morphbr@466: def setup(self, filename, mux, vcodec, vbitrate,\ morphbr@466: fps, acodec, abitrate, width, height, port): morphbr@466: morphbr@466: self.filename = filename morphbr@466: self.mux = mux morphbr@466: self.vcodec = vcodec morphbr@466: self.vbitrate = int(vbitrate) morphbr@466: self.fps = int(fps) morphbr@466: self.acodec = acodec morphbr@466: self.abitrate = int(abitrate) morphbr@466: self.width = int(width) morphbr@466: self.height = int(height) morphbr@466: morphbr@466: self.port = int(port) morphbr@466: morphbr@466: # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000 morphbr@466: self.path = self.config.get("FFmpeg", "path") morphbr@466: self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % ( morphbr@466: self.filename, self.mux, self.vcodec, self.vbitrate,\ morphbr@466: self.fps, self.acodec, self.abitrate, self.width, self.height) morphbr@466: morphbr@466: self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) morphbr@466: self.socket.bind( ('', self.port) ) morphbr@466: morphbr@466: def play(self): morphbr@466: morphbr@466: self.socket.listen(1) morphbr@466: morphbr@466: print "Starting FFmpeg: %s" % self.path morphbr@466: morphbr@466: # exec FFmpeg and get stdout morphbr@466: child_stdin, child_stdout = os.popen2(self.path) morphbr@466: child_stdin.close() morphbr@466: morphbr@466: self.child_pid = os.fork() morphbr@466: if (self.child_pid == 0): morphbr@466: #child morphbr@466: morphbr@466: conn,addr= self.socket.accept() morphbr@466: print "--> Sending Data..." morphbr@466: data = child_stdout.read(1024) morphbr@466: morphbr@466: while( data != ""): morphbr@466: try: morphbr@466: conn.send(data) morphbr@466: except socket.error: morphbr@466: break morphbr@466: data = child_stdout.read(1024) morphbr@466: morphbr@466: print "--> Finished sending data..." morphbr@466: conn.close() morphbr@466: morphbr@466: morphbr@466: def stop(self): morphbr@466: morphbr@466: print "--> Trying to stop FFmpeg process..." morphbr@466: self.socket.close() morphbr@466: os.kill(self.child_pid, 9) morphbr@466: morphbr@466: