morphbr@718: #!/usr/bin/env python morphbr@718: morphbr@718: __author__ = "Renato Filho" morphbr@718: __author_email__ = "renato.filho@indt.org.br" morphbr@718: __license__ = "GPL" morphbr@735: __version__ = "0.2" morphbr@718: morphbr@718: import os morphbr@735: import sys morphbr@718: import shlex morphbr@718: import signal morphbr@718: import subprocess morphbr@718: import time morphbr@718: morphbr@718: import select renatofilho@806: import fcntl morphbr@718: morphbr@718: import lib.utils as utils morphbr@718: import lib.server as server morphbr@718: import lib.transcoder as transcoder morphbr@718: morphbr@718: __all__ = ("TranscoderGMencoder",) morphbr@718: morphbr@718: class TranscoderGMencoder(transcoder.Transcoder): morphbr@718: gmencoder_path = utils.which("gmencoder") morphbr@718: name = "gmencoder" morphbr@718: priority = -1 morphbr@718: proc = None morphbr@718: morphbr@718: def __init__(self, params): renatofilho@800: self.status = 0 morphbr@718: transcoder.Transcoder.__init__(self, params) morphbr@718: self.opts = [] renatofilho@800: self.opts.append (self.gmencoder_path) renatofilho@800: self.opts.append ("-d") renatofilho@800: self._parser_params () renatofilho@800: morphbr@718: # __init__() morphbr@718: morphbr@718: def _insert_param (self, name, value): morphbr@718: if (value != ""): morphbr@718: self.opts.append(name) morphbr@718: self.opts.append(value) morphbr@718: morphbr@718: def _parser_params (self): morphbr@718: self._insert_param("-i", \ morphbr@719: "%s://%s" % (self.params_first("type", "file"), morphbr@719: self.params_first("uri", ""))) melunko@794: self._insert_param("--video-encode", self.params_first("ve", "ffenc_mpeg1video")) melunko@794: self._insert_param("--video-opts", "bitrate=600000,pass=512,quantizer=0.05,quant-type=1") morphbr@718: self._insert_param("--video-fps", self.params_first("fps", "")) morphbr@719: self._insert_param("--video-width", self.params_first("width", "320")) morphbr@719: self._insert_param("--video-height", self.params_first("height", "240")) morphbr@718: self._insert_param("--audio-rate", "32000") morphbr@718: self._insert_param("--audio-encode", self.params_first("ae", "")) morphbr@718: # _parse_params morphbr@718: morphbr@718: def start(self, outfd): renatofilho@803: outfile = self.params_first("outfile", "") renatofilho@803: renatofilho@803: if outfile != "": renatofilho@803: path = os.path.join(sys.path[0], ".transcoded", outfile) renatofilho@803: self._insert_param("-o", "file://%s" % path) renatofilho@803: else: renatofilho@803: self._insert_param ("-o", "fd://%d" % outfd.fileno()) renatofilho@803: self.opts.append ("-c") renatofilho@803: renatofilho@803: cmd = " ".join(self.opts) morphbr@719: self.log.info(self.tid, "GMencoder: %s" % cmd) morphbr@718: morphbr@718: try: morphbr@719: self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE, morphbr@719: stdout=subprocess.PIPE) morphbr@718: except Exception, e: morphbr@735: self.log.error(self.tid, "Error: executing GMencoder: %s" % e) morphbr@735: outfd.write("Error: GMencoder: %s" % e) morphbr@718: return False morphbr@718: morphbr@718: try: renatofilho@806: #p = select.poll() renatofilho@806: #print outfd.fileno() renatofilho@806: #p.register (connection, select.POLLNVAL | select.POLLERR | select.POLLHUP) morphbr@718: while (self.proc and self.proc.poll() == None): renatofilho@806: r, w, x = select.select([self.proc.stdout], [], [], 1) morphbr@718: if self.proc.stdout in r: morphbr@718: progress = self.proc.stdout.readline() morphbr@718: if (progress.find ("PROGRESS") >= 0): morphbr@718: self.status = progress.split (":")[1] renatofilho@806: elif (progress.find ("Erro") >= 0): renatofilho@806: return False renatofilho@806: """ renatofilho@806: try: renatofilho@806: print connection.getpeername() renatofilho@806: print connection.getsockname() renatofilho@806: except Esception, e: renatofilho@806: print "Lost Connection" renatofilho@806: self.stop () renatofilho@806: return False renatofilho@806: """ renatofilho@806: morphbr@718: except Exception, e: morphbr@718: self.log.error(self.tid, "Problems handling data: %s" % e) morphbr@718: return False morphbr@718: renatofilho@783: self.status = 100; renatofilho@803: renatofilho@803: if outfile: renatofilho@803: outfd.write("OK ") renatofilho@803: morphbr@718: return True morphbr@718: # start() morphbr@718: morphbr@718: morphbr@718: def stop(self): morphbr@718: if self.proc: morphbr@719: self.log.info(self.tid, "Stopped GMencoder plugin") morphbr@718: try: morphbr@719: os.kill(self.proc.pid, signal.SIGKILL) morphbr@718: self.proc.wait() morphbr@718: except Exception, e: morphbr@718: pass morphbr@718: morphbr@718: self.proc = None morphbr@718: # stop() morphbr@718: renatofilho@800: def get_progress(self): renatofilho@800: return self.status renatofilho@800: renatofilho@800: def get_lenght(self): renatofilho@800: return -1 renatofilho@800: morphbr@718: # TranscoderGMencoder