gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author morphbr
Thu May 31 19:36:03 2007 +0100 (2007-05-31)
branchtrunk
changeset 728 6e54ca4a4e9a
parent 719 668ecfca1789
child 735 15a191556cd6
permissions -rw-r--r--
[svn r734] * GMyth-Streamer
- Close socket when transcoding to file (bug fix)
     1 #!/usr/bin/env python
     2 
     3 __author__ = "Renato Filho"
     4 __author_email__ = "renato.filho@indt.org.br"
     5 __license__ = "GPL"
     6 __version__ = "0.1"
     7 
     8 import os
     9 import shlex
    10 import signal
    11 import subprocess
    12 import time
    13 
    14 import select
    15 
    16 import lib.utils as utils
    17 import lib.server as server
    18 import lib.transcoder as transcoder
    19 
    20 __all__ = ("TranscoderGMencoder",)
    21 
    22 class TranscoderGMencoder(transcoder.Transcoder):
    23     gmencoder_path = utils.which("gmencoder")
    24     name = "gmencoder"
    25     priority = -1
    26     proc = None
    27 
    28     def __init__(self, params):
    29         transcoder.Transcoder.__init__(self, params)
    30         self.opts = []
    31     # __init__()
    32 
    33     def _insert_param (self, name, value):
    34         if (value != ""):
    35             self.opts.append(name)
    36             self.opts.append(value)
    37 
    38     def _parser_params (self):
    39         self._insert_param("-i", \
    40             "%s://%s" % (self.params_first("type", "file"),
    41                          self.params_first("uri", "")))
    42         self._insert_param("--video-encode", self.params_first("ve", ""))
    43         self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
    44         self._insert_param("--video-fps", self.params_first("fps", ""))
    45         self._insert_param("--video-width", self.params_first("width", "320"))
    46         self._insert_param("--video-height", self.params_first("height", "240"))
    47         self._insert_param("--audio-rate", "32000")
    48         self._insert_param("--audio-encode", self.params_first("ae", ""))
    49     # _parse_params
    50 
    51     def start(self, outfd):
    52         self.opts.append (self.gmencoder_path)
    53         self._parser_params ()
    54         self._insert_param ("-o", "fd://%d" % outfd.fileno())
    55 
    56         cmd = " ".join(self.opts)
    57         self.log.info(self.tid, "GMencoder: %s" % cmd)
    58 
    59         try:
    60             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
    61                                          stdout=subprocess.PIPE)
    62         except Exception, e:
    63             self.log.error(self.tid, "Error executing GMencoder: %s" % e)
    64             return False
    65 
    66         try:
    67             while (self.proc and self.proc.poll() == None):
    68                 r, w, x = select.select([self.proc.stdout], [], [], 0)
    69                 if self.proc.stdout in r:
    70                     progress = self.proc.stdout.readline()
    71                     self.log.debug(self.tid, "stdout %s" % progress)
    72                     if (progress.find ("PROGRESS") >= 0):
    73                         self.status = progress.split (":")[1]
    74         except Exception, e:
    75             self.log.error(self.tid, "Problems handling data: %s" % e)
    76             return False
    77 
    78         return True
    79     # start()
    80 
    81 
    82     def stop(self):
    83         if self.proc:
    84             self.log.info(self.tid, "Stopped GMencoder plugin")
    85             try:
    86                 self.proc.stdin.write("QUIT\n")
    87                 os.kill(self.proc.pid, signal.SIGKILL)
    88                 self.proc.wait()
    89             except Exception, e:
    90                 pass
    91 
    92             self.proc = None
    93     # stop()
    94 
    95 # TranscoderGMencoder