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")) renatofilho@813: self._insert_param("--video-opts", "bitrate=300000,pass=512,quantizer=0.01,quant-type=1") renatofilho@813: #self._insert_param("--video-fps", self.params_first("fps", "")) renatofilho@813: self._insert_param("--video-fps", self.params_first("fps", "10")) 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@815: path = os.path.join(utils.config.get_transcoded_location(), 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: 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) melunko@825: melunko@825: if outfile: melunko@825: outfd.write("OK ") melunko@825: 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: melunko@825: if not outfile: melunko@825: p = select.poll() morphbr@837: p.register (outfd, select.POLLNVAL | select.POLLERR | morphbr@837: select.POLLHUP) morphbr@837: tries = 0 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): morphbr@837: self.log.error(self.tid, "Detected problem @ gmencoder:" morphbr@837: " %s" % progress) morphbr@837: if tries < 50: morphbr@837: tries += 1 morphbr@837: else: morphbr@837: return False renatofilho@809: melunko@825: if not outfile: melunko@825: ret = p.poll(0) melunko@825: if ret: morphbr@837: self.log.info(self.tid, "* Lost connection *") melunko@825: self.stop () melunko@825: return False 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: 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