gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
author renatofilho
Tue Aug 21 16:04:44 2007 +0100 (2007-08-21)
branchtrunk
changeset 815 7f290a3a34b1
parent 813 e3911c7373f7
child 825 32ec09590cf2
permissions -rw-r--r--
[svn r821] created debian packages;\nImplemented server deamon;\n
     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         except Exception, e:
    76             self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    77             outfd.write("Error: GMencoder: %s" % e)
    78             return False
    79 
    80         try:
    81 	    p = select.poll()
    82 	    #print outfd.fileno()
    83 	    p.register (outfd, select.POLLNVAL | select.POLLERR | select.POLLHUP | select.POLLIN )
    84             while (self.proc and self.proc.poll() == None):
    85                 r, w, x = select.select([self.proc.stdout], [], [], 1)
    86                 if self.proc.stdout in r:
    87                     progress = self.proc.stdout.readline()
    88                     if (progress.find ("PROGRESS") >= 0):
    89                         self.status = progress.split (":")[1]
    90 		    elif (progress.find ("Erro") >= 0):
    91 			return False
    92 
    93 		    ret = p.poll(0)
    94 		    if ret:
    95 		    	print "Lost Connection"
    96 		    	self.stop ()
    97 		    	return False
    98 
    99         except Exception, e:
   100             self.log.error(self.tid, "Problems handling data: %s" % e)
   101             return False
   102 
   103         self.status = 100;
   104 
   105         if outfile:
   106            outfd.write("OK   ")
   107 
   108         return True
   109     # start()
   110 
   111 
   112     def stop(self):
   113         if self.proc:
   114             self.log.info(self.tid, "Stopped GMencoder plugin")
   115             try:
   116                 os.kill(self.proc.pid, signal.SIGKILL)
   117                 self.proc.wait()
   118             except Exception, e:
   119                 pass
   120 
   121             self.proc = None
   122     # stop()
   123 
   124     def get_progress(self):
   125         return self.status
   126 
   127     def get_lenght(self):
   128         return -1
   129 
   130 # TranscoderGMencoder