gmyth-stream/server/0.2/plugins/transcoders/gmencoder.py
author renatofilho
Thu Aug 16 14:46:11 2007 +0100 (2007-08-16)
branchtrunk
changeset 807 add4025ca678
parent 672 4e623a4274a1
permissions -rw-r--r--
[svn r813] created a timeout function for invalid channels
     1 import os
     2 import shlex
     3 import signal
     4 import subprocess
     5 import time
     6 
     7 import select
     8 
     9 import lib.utils as utils
    10 import lib.server as server
    11 
    12 __all__ = ("TranscoderGMencoder",)
    13 
    14 class TranscoderGMencoder(server.Transcoder):
    15     gmencoder_path = utils.which("gmencoder")
    16     name = "gmencoder"
    17     priority = -1
    18     proc = None
    19 
    20     def __init__(self, params):
    21         server.Transcoder.__init__(self, params)
    22         self.opts = []
    23     # __init__()
    24 
    25     def _insert_param (self, name, value):
    26         if (value != ""):
    27             self.opts.append(name)
    28             self.opts.append(value)
    29 
    30     def _parser_params (self):
    31         self._insert_param("-i", \
    32             "%s://%s" % (self.params_first("type", "file"), self.params_first("uri", "")))
    33         self._insert_param("--video-encode", self.params_first("ve", ""))
    34         self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
    35         self._insert_param("--video-fps", self.params_first("fps", ""))
    36         self._insert_param("--video-width", self.params_first("width", ""))
    37         self._insert_param("--video-height", self.params_first("height", ""))
    38         self._insert_param("--audio-rate", "32000")
    39         self._insert_param("--audio-encode", self.params_first("ae", ""))
    40     # _parse_params
    41 
    42     def start(self, outfd):
    43         self.opts.append (self.gmencoder_path)
    44         self._parser_params ()
    45         self._insert_param ("-o", "fd://%d" % outfd.fileno())
    46 
    47         cmd = " ".join(self.opts)
    48         self.log.info ("GMencoder: %s", cmd)
    49 
    50         try:
    51             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    52         except Exception, e:
    53             self.log.error("Error executing GMencoder: %s" % e)
    54             return False
    55 
    56         try:
    57 			while (self.proc and self.proc.poll() == None):
    58 				r, w, x = select.select([self.proc.stdout], [], [], 0)
    59 				if self.proc.stdout in r:
    60 					progress = self.proc.stdout.readline()
    61 					self.log.info ("stdout %s" % progress)
    62 					if (progress.find ("PROGRESS") >= 0):
    63 						self.status = progress.split (":")[1]
    64 					#if (progress.find ("DONE") >= 0):
    65 					#	break
    66 			self.log.info ("Process exit")
    67         except Exception, e:
    68             self.log.error("Problems handling data: %s" % e)
    69             return False
    70 
    71         return True
    72     # start()
    73 
    74 
    75     def stop(self):
    76         if self.proc:
    77             self.log.info ("STOPED GMencoder plugin")
    78             try:
    79                 self.proc.stdin.write ("QUIT\n")
    80             except Exception, e:
    81                 pass
    82 
    83             try:
    84                 self.proc.wait()
    85             except Exception, e:
    86                 pass
    87 
    88             self.proc = None
    89     # stop()
    90 
    91 # TranscoderGMencoder