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 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): morphbr@718: transcoder.Transcoder.__init__(self, params) morphbr@718: self.opts = [] 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", ""))) morphbr@718: self._insert_param("--video-encode", self.params_first("ve", "")) morphbr@718: self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5") 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): morphbr@718: self.opts.append (self.gmencoder_path) morphbr@718: self._parser_params () morphbr@735: morphbr@735: outfile = self.params_first("outfile", None) morphbr@735: morphbr@735: if outfile: morphbr@735: path = os.path.join(sys.path[0], ".transcoded", outfile) morphbr@735: self._insert_param("-o", "file://%s" % path) morphbr@735: else: morphbr@735: self._insert_param ("-o", "fd://%d" % outfd.fileno()) morphbr@718: morphbr@718: 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@735: if outfile: morphbr@735: outfd.write("OK ") morphbr@735: morphbr@718: try: morphbr@718: while (self.proc and self.proc.poll() == None): morphbr@718: r, w, x = select.select([self.proc.stdout], [], [], 0) morphbr@718: if self.proc.stdout in r: morphbr@718: progress = self.proc.stdout.readline() morphbr@719: self.log.debug(self.tid, "stdout %s" % progress) morphbr@718: if (progress.find ("PROGRESS") >= 0): morphbr@718: self.status = progress.split (":")[1] morphbr@718: except Exception, e: morphbr@718: self.log.error(self.tid, "Problems handling data: %s" % e) morphbr@718: return False morphbr@718: morphbr@735: self.log.info(self.tid, "OK: Done") 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: self.proc.stdin.write("QUIT\n") 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: morphbr@718: # TranscoderGMencoder