gmyth-stream/server/plugins/transcoders/gmencoder.py
branchtrunk
changeset 833 7d4d75351461
parent 825 32ec09590cf2
child 837 6b3c7c0b32ad
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/server/plugins/transcoders/gmencoder.py	Wed Aug 29 15:33:31 2007 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +__author__ = "Renato Filho"
     1.7 +__author_email__ = "renato.filho@indt.org.br"
     1.8 +__license__ = "GPL"
     1.9 +__version__ = "0.2"
    1.10 +
    1.11 +import os
    1.12 +import sys
    1.13 +import shlex
    1.14 +import signal
    1.15 +import subprocess
    1.16 +import time
    1.17 +
    1.18 +import select
    1.19 +import fcntl
    1.20 +
    1.21 +import lib.utils as utils
    1.22 +import lib.server as server
    1.23 +import lib.transcoder as transcoder
    1.24 +
    1.25 +__all__ = ("TranscoderGMencoder",)
    1.26 +
    1.27 +class TranscoderGMencoder(transcoder.Transcoder):
    1.28 +    gmencoder_path = utils.which("gmencoder")
    1.29 +    name = "gmencoder"
    1.30 +    priority = -1
    1.31 +    proc = None
    1.32 +
    1.33 +    def __init__(self, params):
    1.34 +        self.status = 0
    1.35 +        transcoder.Transcoder.__init__(self, params)
    1.36 +        self.opts = []
    1.37 +        self.opts.append (self.gmencoder_path)
    1.38 +        self.opts.append ("-d")
    1.39 +        self._parser_params ()
    1.40 +
    1.41 +    # __init__()
    1.42 +
    1.43 +    def _insert_param (self, name, value):
    1.44 +        if (value != ""):
    1.45 +            self.opts.append(name)
    1.46 +            self.opts.append(value)
    1.47 +
    1.48 +    def _parser_params (self):
    1.49 +        self._insert_param("-i", \
    1.50 +            "%s://%s" % (self.params_first("type", "file"),
    1.51 +                         self.params_first("uri", "")))
    1.52 +        self._insert_param("--video-encode", self.params_first("ve", "ffenc_mpeg1video"))
    1.53 +        self._insert_param("--video-opts", "bitrate=300000,pass=512,quantizer=0.01,quant-type=1")
    1.54 +        #self._insert_param("--video-fps", self.params_first("fps", ""))
    1.55 +        self._insert_param("--video-fps", self.params_first("fps", "10"))
    1.56 +        self._insert_param("--video-width", self.params_first("width", "320"))
    1.57 +        self._insert_param("--video-height", self.params_first("height", "240"))
    1.58 +        self._insert_param("--audio-rate", "32000")
    1.59 +        self._insert_param("--audio-encode", self.params_first("ae", ""))
    1.60 +    # _parse_params
    1.61 +
    1.62 +    def start(self, outfd):
    1.63 +        outfile = self.params_first("outfile", "")
    1.64 +
    1.65 +        if outfile != "":
    1.66 +            path = os.path.join(utils.config.get_transcoded_location(), outfile)
    1.67 +            self._insert_param("-o", "file://%s" % path)
    1.68 +        else:
    1.69 +            self._insert_param ("-o", "fd://%d" % outfd.fileno())
    1.70 +            self.opts.append ("-c")
    1.71 +
    1.72 +        cmd = " ".join(self.opts)
    1.73 +        self.log.info(self.tid, "GMencoder: %s" % cmd)
    1.74 +
    1.75 +        try:
    1.76 +            self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE,
    1.77 +                                         stdout=subprocess.PIPE)
    1.78 +
    1.79 +	    if outfile:
    1.80 +           	outfd.write("OK   ")
    1.81 +
    1.82 +        except Exception, e:
    1.83 +            self.log.error(self.tid, "Error: executing GMencoder: %s" % e)
    1.84 +            outfd.write("Error: GMencoder: %s" % e)
    1.85 +            return False
    1.86 +
    1.87 +        try:
    1.88 +	    if not outfile:
    1.89 +	    	p = select.poll()
    1.90 +	    	p.register (outfd, select.POLLNVAL | select.POLLERR | select.POLLHUP | select.POLLIN )
    1.91 +
    1.92 +            while (self.proc and self.proc.poll() == None):
    1.93 +                r, w, x = select.select([self.proc.stdout], [], [], 1)
    1.94 +                if self.proc.stdout in r:
    1.95 +                    progress = self.proc.stdout.readline()
    1.96 +                    if (progress.find ("PROGRESS") >= 0):
    1.97 +                        self.status = progress.split (":")[1]
    1.98 +		    elif (progress.find ("Erro") >= 0):
    1.99 +			return False
   1.100 +
   1.101 +		    if not outfile:
   1.102 +			    ret = p.poll(0)
   1.103 +			    if ret:
   1.104 +			    	self.log.info(self.tid, "Lost connection")
   1.105 +		    		self.stop ()
   1.106 +			    	return False
   1.107 +
   1.108 +        except Exception, e:
   1.109 +            self.log.error(self.tid, "Problems handling data: %s" % e)
   1.110 +            return False
   1.111 +
   1.112 +        self.status = 100;
   1.113 +
   1.114 +
   1.115 +        return True
   1.116 +    # start()
   1.117 +
   1.118 +
   1.119 +    def stop(self):
   1.120 +        if self.proc:
   1.121 +            self.log.info(self.tid, "Stopped GMencoder plugin")
   1.122 +            try:
   1.123 +                os.kill(self.proc.pid, signal.SIGKILL)
   1.124 +                self.proc.wait()
   1.125 +            except Exception, e:
   1.126 +                pass
   1.127 +
   1.128 +            self.proc = None
   1.129 +    # stop()
   1.130 +
   1.131 +    def get_progress(self):
   1.132 +        return self.status
   1.133 +
   1.134 +    def get_lenght(self):
   1.135 +        return -1
   1.136 +
   1.137 +# TranscoderGMencoder