morphbr@565: import lib.utils as utils morphbr@565: import lib.server as server morphbr@565: import os morphbr@565: import signal morphbr@565: import subprocess morphbr@565: morphbr@565: __all__ = ("TranscoderMencoder",) morphbr@565: morphbr@565: class TranscoderMencoder(server.Transcoder): morphbr@565: mencoder_path = utils.which("mencoder") morphbr@565: def_mencoder_outfile = os.path.join(os.path.sep, "tmp", morphbr@565: "mencoder-fifo-%(uid)s-%(pid)s") morphbr@565: name = "mencoder" morphbr@565: priority = -1 morphbr@565: morphbr@565: def __init__(self, params): morphbr@565: server.Transcoder.__init__(self, params) morphbr@565: self.proc = None morphbr@565: self.args = None morphbr@565: morphbr@565: vars = {"uid": os.getuid(), "pid": os.getpid()} morphbr@565: mencoder_outfile_base = self.def_mencoder_outfile % vars morphbr@565: mencoder_outfile = mencoder_outfile_base morphbr@565: i = 0 morphbr@565: while os.path.exists(mencoder_outfile): morphbr@565: i += 1 morphbr@565: mencoder_outfile = mencoder_outfile_base + ".%s" % i morphbr@565: morphbr@565: self.mencoder_outfile = mencoder_outfile morphbr@565: os.mkfifo(self.mencoder_outfile) morphbr@565: morphbr@565: args = [self.mencoder_path, "-really-quiet", morphbr@565: "-o", self.mencoder_outfile] morphbr@565: morphbr@565: params_first = self.params_first morphbr@565: morphbr@565: type = params_first("type") morphbr@565: location = params_first("location") morphbr@565: args.append("%s://%s" % (type, location)) morphbr@565: morphbr@565: mux = params_first("mux", "avi") morphbr@565: args.extend(["-of", mux]) morphbr@565: morphbr@565: acodec = params_first("acodec", "mp3") morphbr@565: abitrate = params_first("abitrate", "128") morphbr@565: if acodec == "mp3lame": morphbr@565: args.extend(["-oac", "mp3lame", "-lameopts", morphbr@565: "cbr:br=%s" % abitrate]) morphbr@565: else: morphbr@565: args.extend(["-oac", "lavc", "-lavcopts", morphbr@565: "acodec=%s:abitrate=%s" % (acodec, abitrate)]) morphbr@565: morphbr@565: vcodec = params_first("vcodec", "mpeg4") morphbr@565: vbitrate = params_first("vbitrate", "400") morphbr@565: args.extend(["-ovc", "lavc", "-lavcopts", morphbr@565: "vcodec=%s:vbitrate=%s" % (vcodec, vbitrate)]) morphbr@565: morphbr@565: fps = params_first("fps", "24") morphbr@565: args.extend(["-ofps", fps]) morphbr@565: morphbr@565: width = params_first("width", "320") morphbr@565: height = params_first("height", "240") morphbr@565: args.extend(["-vf", "scale=%s:%s" % (width, height)]) morphbr@565: morphbr@565: self.args = args morphbr@565: # __init__() morphbr@565: morphbr@565: morphbr@565: def _unlink_fifo(self): morphbr@565: try: morphbr@565: os.unlink(self.mencoder_outfile) morphbr@565: except Exception, e: morphbr@565: pass morphbr@565: # _unlink_fifo() morphbr@565: morphbr@565: morphbr@565: def start(self, outfd): morphbr@565: cmd = " ".join(self.args) morphbr@565: self.log.info("Mencoder: %s" % cmd) morphbr@565: morphbr@565: try: morphbr@565: self.proc = subprocess.Popen(self.args, close_fds=True) morphbr@565: except Exception, e: morphbr@565: self.log.error("Error executing mencoder: %s" % cmd) morphbr@565: return False morphbr@565: morphbr@565: try: morphbr@565: fifo_read = open(self.mencoder_outfile) morphbr@565: except Exception, e: morphbr@565: self.log.error("Error opening fifo: %s" % cmd) morphbr@565: return False morphbr@565: morphbr@565: try: morphbr@565: while self.proc and self.proc.poll() == None: morphbr@565: d = fifo_read.read(1024) morphbr@565: outfd.write(d) morphbr@565: except Exception, e: morphbr@565: self.log.error("Problems handling data: %s" % e) morphbr@565: self._unlink_fifo() morphbr@565: return False morphbr@565: morphbr@565: self._unlink_fifo() morphbr@565: return True morphbr@565: # start() morphbr@565: morphbr@565: morphbr@565: def stop(self): morphbr@565: if self.proc: morphbr@565: try: morphbr@565: os.kill(self.proc.pid, signal.SIGTERM) morphbr@565: except OSError, e: morphbr@565: pass morphbr@565: morphbr@565: try: morphbr@565: self.proc.wait() morphbr@565: except Exception, e: morphbr@565: pass morphbr@565: morphbr@565: self.proc = None morphbr@565: morphbr@565: self._unlink_fifo() morphbr@565: # stop() morphbr@565: # TranscoderMencoder