gmyth-stream/plugins/media/ffmpeg.py
author renatofilho
Wed Mar 28 21:26:42 2007 +0100 (2007-03-28)
branchtrunk
changeset 468 f4c6061f6f39
child 470 57833200a415
permissions -rw-r--r--
[svn r473] memory leek fix
     1 import os
     2 import sys
     3 import time
     4 import socket
     5 import ConfigParser
     6 
     7 class Media:
     8 
     9     def __init__(self, config):
    10 
    11         self.config = config
    12 
    13     def setup(self, filename, mux, vcodec, vbitrate,\
    14               fps, acodec, abitrate, width, height, port):
    15 
    16         self.filename = filename
    17         self.mux = mux
    18         self.vcodec = vcodec
    19         self.vbitrate = int(vbitrate)
    20         self.fps = int(fps)
    21         self.acodec = acodec
    22         self.abitrate = int(abitrate)
    23         self.width = int(width)
    24         self.height = int(height)
    25 
    26         self.port = int(port)
    27 
    28         # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000
    29         self.path = self.config.get("FFmpeg", "path")
    30         self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % (
    31             self.filename, self.mux, self.vcodec, self.vbitrate,\
    32             self.fps, self.acodec, self.abitrate, self.width, self.height)
    33 
    34         self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    35         self.socket.bind( ('', self.port) )
    36 
    37     def play(self):
    38 
    39         self.socket.listen(1)
    40 
    41         print "Starting FFmpeg: %s" % self.path
    42 
    43         # exec FFmpeg and get stdout
    44         child_stdin, child_stdout = os.popen2(self.path)
    45         child_stdin.close()
    46 
    47         self.child_pid = os.fork()
    48         if (self.child_pid == 0):
    49             #child
    50 
    51             conn,addr= self.socket.accept()
    52             print "--> Sending Data..."
    53             data = child_stdout.read(1024)
    54 
    55             while( data != ""):
    56                 try:
    57                     conn.send(data)
    58                 except socket.error:
    59                     break
    60                 data = child_stdout.read(1024)
    61 
    62             print "--> Finished sending data..."
    63             conn.close()
    64 
    65 
    66     def stop(self):
    67 
    68         print "--> Trying to stop FFmpeg process..."
    69         self.socket.close()
    70         os.kill(self.child_pid, 9)
    71 
    72