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