10 def __init__(self, config):
16 def setup(self, filename, mux, vcodec, vbitrate,\
17 fps, acodec, abitrate, width, height, port):
19 self.filename = filename
22 self.vbitrate = int(vbitrate)
25 self.abitrate = int(abitrate)
26 self.width = int(width)
27 self.height = int(height)
31 # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000
32 self.path = self.config.get("FFmpeg", "path")
33 self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % (
34 self.filename, self.mux, self.vcodec, self.vbitrate,\
35 self.fps, self.acodec, self.abitrate, self.width, self.height)
37 if (self.socket != None):
40 self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
41 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
42 self.socket.bind( ('', self.port) )
43 self.socket.settimeout(10)
48 lib.log("Starting FFmpeg: %s" % self.path)
50 # exec FFmpeg and get stdout
51 child_stdin, child_stdout = os.popen2(self.path)
54 self.child_pid = os.fork()
55 if (self.child_pid == 0):
58 conn,addr= self.socket.accept()
59 lib.log("Sending Data to client: %s" % addr[0])
60 data = child_stdout.read(1024)
64 while( data != "" and retry < 5):
68 lib.log("Socket error (maybe timeout ?)")
71 data = child_stdout.read(1024)
74 lib.log("Finished sending Data to client: %s" % addr[0])
76 lib.log("Client timed out")
85 if (self.socket != None):
86 lib.log("Closing socket")
89 lib.log("Trying to stop FFmpeg process")
90 if (self.child_pid != None):
91 os.kill(self.child_pid, 9)