gmyth-stream/server/plugins/transcoders/gmencoder.py
author morphbr
Thu Aug 30 12:51:31 2007 +0100 (2007-08-30)
branchtrunk
changeset 835 36720270b368
parent 825 32ec09590cf2
child 837 6b3c7c0b32ad
permissions -rw-r--r--
[svn r841] - Fixed livetv buffer at 30kb during tests
     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             self.opts.append ("-c")
    68 
    69         cmd = " ".join(self.opts)
    70         self.log.info(self.tid, "GMencoder: %s" % cmd)
    71 
    72         try:
    73             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
    74                                          stdout=subprocess.PIPE)
    75 
    76 	    if outfile:
    77            	outfd.write("OK   ")
    78 
    79         except Exception, e:
    80             self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    81             outfd.write("Error: GMencoder: %s" % e)
    82             return False
    83 
    84         try:
    85 	    if not outfile:
    86 	    	p = select.poll()
    87 	    	p.register (outfd, select.POLLNVAL | select.POLLERR | select.POLLHUP | select.POLLIN )
    88 
    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 			return False
    97 
    98 		    if not outfile:
    99 			    ret = p.poll(0)
   100 			    if ret:
   101 			    	self.log.info(self.tid, "Lost connection")
   102 		    		self.stop ()
   103 			    	return False
   104 
   105         except Exception, e:
   106             self.log.error(self.tid, "Problems handling data: %s" % e)
   107             return False
   108 
   109         self.status = 100;
   110 
   111 
   112         return True
   113     # start()
   114 
   115 
   116     def stop(self):
   117         if self.proc:
   118             self.log.info(self.tid, "Stopped GMencoder plugin")
   119             try:
   120                 os.kill(self.proc.pid, signal.SIGKILL)
   121                 self.proc.wait()
   122             except Exception, e:
   123                 pass
   124 
   125             self.proc = None
   126     # stop()
   127 
   128     def get_progress(self):
   129         return self.status
   130 
   131     def get_lenght(self):
   132         return -1
   133 
   134 # TranscoderGMencoder