gmyth-stream/server/plugins/transcoders/gmencoder.py
author renatofilho
Mon Jan 28 19:39:14 2008 +0000 (2008-01-28)
branchtrunk
changeset 894 20de88fec6f4
parent 883 e8a82fc5f075
child 905 d2d226b5a4bd
permissions -rw-r--r--
[svn r900] removed default options
     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", ""))
    50         self._insert_param("--video-opts", self.params_first ("video-opts", ""))
    51         self._insert_param("--video-fps", self.params_first("fps", ""))
    52         self._insert_param("--video-width", self.params_first("width", ""))
    53         self._insert_param("--video-height", self.params_first("height", ""))
    54         self._insert_param("--audio-encode", self.params_first("ae", ""))
    55         self._insert_param("--audio-opts", self.params_first ("audio-opts", ""))
    56         self._insert_param("--mux-element", self.params_first("mux", ""))
    57         self._insert_param("--audio-rate", self.params_first("audio-rate", ""))
    58 
    59 
    60     # _parse_params
    61 
    62     def start(self, outfd):
    63         outfile = self.params_first("outfile", "")
    64 
    65         if outfile != "":
    66             path = os.path.join(utils.config.get_transcoded_location(), outfile)
    67             self._insert_param("-o", "file://%s" % path)
    68         else:
    69             self._insert_param ("-o", "fd://%d" % outfd.fileno())
    70 
    71         cmd = " ".join(self.opts)
    72         self.log.info(self.tid, "GMencoder: %s" % cmd)
    73 
    74         try:
    75             self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
    76                                          stdout=subprocess.PIPE)
    77 
    78             if outfile:
    79                outfd.write("OK   ")
    80 
    81         except Exception, e:
    82             self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    83             outfd.write("Error: GMencoder: %s" % e)
    84             return False
    85 
    86         try:
    87 	    if not outfile:
    88 	    	p = select.poll()
    89 	    	p.register (outfd, select.POLLNVAL | select.POLLERR |
    90                             select.POLLHUP)
    91             tries = 0
    92             while (self.proc and self.proc.poll() == None):
    93                 r, w, x = select.select([self.proc.stdout], [], [], 1)
    94                 if self.proc.stdout in r:
    95                     progress = self.proc.stdout.readline()
    96                     if (progress.find ("PROGRESS") >= 0):
    97                         self.status = progress.split (":")[1]
    98 		    elif (progress.find ("Erro") >= 0):
    99                         self.log.error(self.tid, "Detected problem @ gmencoder:"
   100                                        " %s" % progress)
   101                         if tries < 50:
   102                             tries += 1
   103                         else:
   104                             return False
   105 
   106 		    if not outfile:
   107 			    ret = p.poll(0)
   108 			    if ret:
   109 			    	self.log.info(self.tid, "* Lost connection *")
   110 		    		self.stop ()
   111 			    	return False
   112 
   113         except Exception, e:
   114             self.log.error(self.tid, "Problems handling data: %s" % e)
   115             return False
   116 
   117         self.status = 100;
   118 
   119 
   120         return True
   121     # start()
   122 
   123 
   124     def stop(self):
   125         if self.proc:
   126             self.log.info(self.tid, "Stopped GMencoder plugin")
   127             try:
   128                 os.kill(self.proc.pid, signal.SIGKILL)
   129                 self.proc.wait()
   130             except Exception, e:
   131                 pass
   132 
   133             self.proc = None
   134     # stop()
   135 
   136     def get_progress(self):
   137         return self.status
   138 
   139     def get_lenght(self):
   140         return -1
   141 
   142 # TranscoderGMencoder