gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author leo_sobral
Thu Aug 16 15:24:01 2007 +0100 (2007-08-16)
branchtrunk
changeset 808 215c45290ce3
parent 803 b1d0964060b3
child 809 ec0a31edf362
permissions -rw-r--r--
[svn r814] Added x error handling to playbin maemo
     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=600000,pass=512,quantizer=0.05,quant-type=1")
    51         self._insert_param("--video-fps", self.params_first("fps", ""))
    52         self._insert_param("--video-width", self.params_first("width", "320"))
    53         self._insert_param("--video-height", self.params_first("height", "240"))
    54         self._insert_param("--audio-rate", "32000")
    55         self._insert_param("--audio-encode", self.params_first("ae", ""))
    56     # _parse_params
    57 
    58     def start(self, outfd):
    59         outfile = self.params_first("outfile", "")
    60 
    61         if outfile != "":
    62             path = os.path.join(sys.path[0], ".transcoded", outfile)
    63             self._insert_param("-o", "file://%s" % path)
    64         else:
    65             self._insert_param ("-o", "fd://%d" % outfd.fileno())
    66             self.opts.append ("-c")
    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         except Exception, e:
    75             self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    76             outfd.write("Error: GMencoder: %s" % e)
    77             return False
    78 
    79         try:
    80 	    #p = select.poll()
    81 	    #print outfd.fileno()
    82 	    #p.register (connection, select.POLLNVAL | select.POLLERR | select.POLLHUP)
    83             while (self.proc and self.proc.poll() == None):
    84                 r, w, x = select.select([self.proc.stdout], [], [], 1)
    85                 if self.proc.stdout in r:
    86                     progress = self.proc.stdout.readline()
    87                     if (progress.find ("PROGRESS") >= 0):
    88                         self.status = progress.split (":")[1]
    89 		    elif (progress.find ("Erro") >= 0):
    90 			return False
    91 		"""
    92 		try:
    93 		    print connection.getpeername()
    94 		    print connection.getsockname()
    95 		except Esception, e:
    96 		    print "Lost Connection"
    97 		    self.stop ()
    98 		    return False
    99 		"""
   100 
   101         except Exception, e:
   102             self.log.error(self.tid, "Problems handling data: %s" % e)
   103             return False
   104 
   105         self.status = 100;
   106 
   107         if outfile:
   108            outfd.write("OK   ")
   109 
   110         return True
   111     # start()
   112 
   113 
   114     def stop(self):
   115         if self.proc:
   116             self.log.info(self.tid, "Stopped GMencoder plugin")
   117             try:
   118                 os.kill(self.proc.pid, signal.SIGKILL)
   119                 self.proc.wait()
   120             except Exception, e:
   121                 pass
   122 
   123             self.proc = None
   124     # stop()
   125 
   126     def get_progress(self):
   127         return self.status
   128 
   129     def get_lenght(self):
   130         return -1
   131 
   132 # TranscoderGMencoder