gmyth-stream/server/plugins/transcoders/gmencoder.py
author renatofilho
Mon Oct 29 20:13:33 2007 +0000 (2007-10-29)
branchtrunk
changeset 878 016b3410871d
parent 832 daa61fffb811
child 883 e8a82fc5f075
permissions -rw-r--r--
[svn r884] close service after disconnect called
     1 #!/usr/bin/env python
     2 
     3 __author__ = "Renato Filho"
     4 __author_email__ = "renato.filho@indt.org.br"
     5 __license__ = "GPL"
     6 __version__ = "0.2"
     7 
     8 import os
     9 import sys
    10 import shlex
    11 import signal
    12 import subprocess
    13 import time
    14 
    15 import select
    16 import fcntl
    17 
    18 import lib.utils as utils
    19 import lib.server as server
    20 import lib.transcoder as transcoder
    21 
    22 __all__ = ("TranscoderGMencoder",)
    23 
    24 class TranscoderGMencoder(transcoder.Transcoder):
    25     gmencoder_path = utils.which("gmencoder")
    26     name = "gmencoder"
    27     priority = -1
    28     proc = None
    29 
    30     def __init__(self, params):
    31         self.status = 0
    32         transcoder.Transcoder.__init__(self, params)
    33         self.opts = []
    34         self.opts.append (self.gmencoder_path)
    35         self.opts.append ("-d")
    36         self._parser_params ()
    37 
    38     # __init__()
    39 
    40     def _insert_param (self, name, value):
    41         if (value != ""):
    42             self.opts.append(name)
    43             self.opts.append(value)
    44 
    45     def _parser_params (self):
    46         self._insert_param("-i", \
    47             "%s://%s" % (self.params_first("type", "file"),
    48                          self.params_first("uri", "")))
    49         self._insert_param("--video-encode", self.params_first("ve", "ffenc_mpeg1video"))
    50         self._insert_param("--video-opts", "bitrate=300000,pass=512,quantizer=0.01,quant-type=1")
    51         #self._insert_param("--video-fps", self.params_first("fps", ""))
    52         self._insert_param("--video-fps", self.params_first("fps", "10"))
    53         self._insert_param("--video-width", self.params_first("width", "320"))
    54         self._insert_param("--video-height", self.params_first("height", "240"))
    55         self._insert_param("--audio-rate", "32000")
    56         self._insert_param("--audio-encode", self.params_first("ae", ""))
    57     # _parse_params
    58 
    59     def start(self, outfd):
    60         outfile = self.params_first("outfile", "")
    61 
    62         if outfile != "":
    63             path = os.path.join(utils.config.get_transcoded_location(), outfile)
    64             self._insert_param("-o", "file://%s" % path)
    65         else:
    66             self._insert_param ("-o", "fd://%d" % outfd.fileno())
    67 
    68         cmd = " ".join(self.opts)
    69         self.log.info(self.tid, "GMencoder: %s" % cmd)
    70 
    71         try:
    72             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
    73                                          stdout=subprocess.PIPE)
    74 
    75 	    if outfile:
    76            	outfd.write("OK   ")
    77 
    78         except Exception, e:
    79             self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    80             outfd.write("Error: GMencoder: %s" % e)
    81             return False
    82 
    83         try:
    84 	    if not outfile:
    85 	    	p = select.poll()
    86 	    	p.register (outfd, select.POLLNVAL | select.POLLERR |
    87                             select.POLLHUP)
    88             tries = 0
    89             while (self.proc and self.proc.poll() == None):
    90                 r, w, x = select.select([self.proc.stdout], [], [], 1)
    91                 if self.proc.stdout in r:
    92                     progress = self.proc.stdout.readline()
    93                     if (progress.find ("PROGRESS") >= 0):
    94                         self.status = progress.split (":")[1]
    95 		    elif (progress.find ("Erro") >= 0):
    96                         self.log.error(self.tid, "Detected problem @ gmencoder:"
    97                                        " %s" % progress)
    98                         if tries < 50:
    99                             tries += 1
   100                         else:
   101                             return False
   102 
   103 		    if not outfile:
   104 			    ret = p.poll(0)
   105 			    if ret:
   106 			    	self.log.info(self.tid, "* Lost connection *")
   107 		    		self.stop ()
   108 			    	return False
   109 
   110         except Exception, e:
   111             self.log.error(self.tid, "Problems handling data: %s" % e)
   112             return False
   113 
   114         self.status = 100;
   115 
   116 
   117         return True
   118     # start()
   119 
   120 
   121     def stop(self):
   122         if self.proc:
   123             self.log.info(self.tid, "Stopped GMencoder plugin")
   124             try:
   125                 os.kill(self.proc.pid, signal.SIGKILL)
   126                 self.proc.wait()
   127             except Exception, e:
   128                 pass
   129 
   130             self.proc = None
   131     # stop()
   132 
   133     def get_progress(self):
   134         return self.status
   135 
   136     def get_lenght(self):
   137         return -1
   138 
   139 # TranscoderGMencoder