gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author renatofilho
Thu Aug 16 15:48:57 2007 +0100 (2007-08-16)
branchtrunk
changeset 809 ec0a31edf362
parent 806 d35b50f4d77e
child 813 e3911c7373f7
permissions -rw-r--r--
[svn r815] added sockmonitor on gmencoder plugin
     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 (outfd, select.POLLNVAL | select.POLLERR | select.POLLHUP | select.POLLIN )
    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 		    ret = p.poll(0)
    93 		    if ret:
    94 		    	print "Lost Connection"
    95 		    	self.stop ()
    96 		    	return False
    97 
    98         except Exception, e:
    99             self.log.error(self.tid, "Problems handling data: %s" % e)
   100             return False
   101 
   102         self.status = 100;
   103 
   104         if outfile:
   105            outfd.write("OK   ")
   106 
   107         return True
   108     # start()
   109 
   110 
   111     def stop(self):
   112         if self.proc:
   113             self.log.info(self.tid, "Stopped GMencoder plugin")
   114             try:
   115                 os.kill(self.proc.pid, signal.SIGKILL)
   116                 self.proc.wait()
   117             except Exception, e:
   118                 pass
   119 
   120             self.proc = None
   121     # stop()
   122 
   123     def get_progress(self):
   124         return self.status
   125 
   126     def get_lenght(self):
   127         return -1
   128 
   129 # TranscoderGMencoder