gmyth-stream/plugins/media/mencoder.py
author morphbr
Fri Mar 30 23:07:54 2007 +0100 (2007-03-30)
branchtrunk
changeset 471 2eef40885322
child 473 b94790c04fd5
permissions -rw-r--r--
[svn r476] * GMyth-Stream:
- Bug fixes
- Improved error handling
- Created Mencoder plugin
     1 import os
     2 import sys
     3 import lib
     4 import time
     5 import socket
     6 import ConfigParser
     7 
     8 class Media:
     9 
    10     def __init__(self, config):
    11 
    12         self.config = config
    13         self.socket = None
    14         self.child_pid = None
    15 
    16     def setup(self, filename, mux, vcodec, vbitrate,\
    17               fps, acodec, abitrate, width, height, port):
    18 
    19         self.filename = filename
    20         self.mux = mux
    21         self.vcodec = vcodec
    22         self.vbitrate = int(vbitrate)
    23         self.fps = int(fps)
    24         self.acodec = acodec
    25         self.abitrate = int(abitrate)
    26         self.width = int(width)
    27         self.height = int(height)
    28 
    29         self.port = int(port)
    30         self.fifo = self.config.get("Mencoder", "fifo_path")
    31 
    32         try:
    33             os.mkfifo(self.fifo)
    34         except:
    35             lib.log("Fifo already exists")
    36 
    37         # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240 5000
    38         self.path = self.config.get("Mencoder", "path")
    39         self.path += " %s -mf fps=%d -of %s -oac %s -lameopts cbr:br=%d -ovc lavc"\
    40                      " -lavcopts vcodec=%s:vbitrate=%d -vf scale=%d:%d"\
    41                      " -o %s 1> /dev/null 2> /dev/null" % (
    42             self.filename, self.fps, self.mux, self.acodec, self.abitrate,
    43             self.vcodec, self.vbitrate, self.width, self.height, self.fifo)
    44 
    45 
    46         if (self.socket != None):
    47             del(self.socket)
    48 
    49         self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    50         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    51         self.socket.bind( ('', self.port) )
    52         self.socket.settimeout(10)
    53         self.socket.listen(1)
    54 
    55     def play(self):
    56 
    57         lib.log("Starting Mencoder: %s" % self.path )
    58         # exec Mencoder
    59         pin, pout = os.popen2(self.path)
    60         pin.close()
    61 
    62         fifo = open(self.fifo)
    63 
    64         self.child_pid = os.fork()
    65         if (self.child_pid == 0):
    66             conn,addr= self.socket.accept()
    67             lib.log("Sending Data to client: %s" % addr[0])
    68 
    69             data = fifo.read(4096)
    70             conn.settimeout(5)
    71             retry = 0
    72 
    73             while( data != "" and retry < 5):
    74                 try:
    75                     conn.send(data)
    76                 except socket.error:
    77                     lib.log("Socket error (maybe timeout ?)")
    78                     retry += 1
    79 
    80                 data = fifo.read(4096)
    81 
    82             if (retry < 5):
    83                 lib.log("Finished sending Data to client: %s" % addr[0])
    84             else:
    85                 lib.log("Client timed out")
    86 
    87         pout.close()
    88 
    89     def stop(self):
    90 
    91         if (self.socket != None):
    92             lib.log("Closing socket")
    93             self.socket.close()
    94 
    95             lib.log("Trying to stop Mencoder process")
    96             if (self.child_pid != None):
    97                 os.kill(self.child_pid, 9)