1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Wed Apr 18 15:59:10 2007 +0100
1.3 @@ -0,0 +1,123 @@
1.4 +import lib.utils as utils
1.5 +import lib.server as server
1.6 +import os
1.7 +import signal
1.8 +import subprocess
1.9 +
1.10 +__all__ = ("TranscoderMencoder",)
1.11 +
1.12 +class TranscoderMencoder(server.Transcoder):
1.13 + mencoder_path = utils.which("mencoder")
1.14 + def_mencoder_outfile = os.path.join(os.path.sep, "tmp",
1.15 + "mencoder-fifo-%(uid)s-%(pid)s")
1.16 + name = "mencoder"
1.17 + priority = -1
1.18 +
1.19 + def __init__(self, params):
1.20 + server.Transcoder.__init__(self, params)
1.21 + self.proc = None
1.22 + self.args = None
1.23 +
1.24 + vars = {"uid": os.getuid(), "pid": os.getpid()}
1.25 + mencoder_outfile_base = self.def_mencoder_outfile % vars
1.26 + mencoder_outfile = mencoder_outfile_base
1.27 + i = 0
1.28 + while os.path.exists(mencoder_outfile):
1.29 + i += 1
1.30 + mencoder_outfile = mencoder_outfile_base + ".%s" % i
1.31 +
1.32 + self.mencoder_outfile = mencoder_outfile
1.33 + os.mkfifo(self.mencoder_outfile)
1.34 +
1.35 + args = [self.mencoder_path, "-really-quiet",
1.36 + "-o", self.mencoder_outfile]
1.37 +
1.38 + params_first = self.params_first
1.39 +
1.40 + type = params_first("type")
1.41 + location = params_first("location")
1.42 + args.append("%s://%s" % (type, location))
1.43 +
1.44 + mux = params_first("mux", "avi")
1.45 + args.extend(["-of", mux])
1.46 +
1.47 + acodec = params_first("acodec", "mp3")
1.48 + abitrate = params_first("abitrate", "128")
1.49 + if acodec == "mp3lame":
1.50 + args.extend(["-oac", "mp3lame", "-lameopts",
1.51 + "cbr:br=%s" % abitrate])
1.52 + else:
1.53 + args.extend(["-oac", "lavc", "-lavcopts",
1.54 + "acodec=%s:abitrate=%s" % (acodec, abitrate)])
1.55 +
1.56 + vcodec = params_first("vcodec", "mpeg4")
1.57 + vbitrate = params_first("vbitrate", "400")
1.58 + args.extend(["-ovc", "lavc", "-lavcopts",
1.59 + "vcodec=%s:vbitrate=%s" % (vcodec, vbitrate)])
1.60 +
1.61 + fps = params_first("fps", "24")
1.62 + args.extend(["-ofps", fps])
1.63 +
1.64 + width = params_first("width", "320")
1.65 + height = params_first("height", "240")
1.66 + args.extend(["-vf", "scale=%s:%s" % (width, height)])
1.67 +
1.68 + self.args = args
1.69 + # __init__()
1.70 +
1.71 +
1.72 + def _unlink_fifo(self):
1.73 + try:
1.74 + os.unlink(self.mencoder_outfile)
1.75 + except Exception, e:
1.76 + pass
1.77 + # _unlink_fifo()
1.78 +
1.79 +
1.80 + def start(self, outfd):
1.81 + cmd = " ".join(self.args)
1.82 + self.log.info("Mencoder: %s" % cmd)
1.83 +
1.84 + try:
1.85 + self.proc = subprocess.Popen(self.args, close_fds=True)
1.86 + except Exception, e:
1.87 + self.log.error("Error executing mencoder: %s" % cmd)
1.88 + return False
1.89 +
1.90 + try:
1.91 + fifo_read = open(self.mencoder_outfile)
1.92 + except Exception, e:
1.93 + self.log.error("Error opening fifo: %s" % cmd)
1.94 + return False
1.95 +
1.96 + try:
1.97 + while self.proc and self.proc.poll() == None:
1.98 + d = fifo_read.read(1024)
1.99 + outfd.write(d)
1.100 + except Exception, e:
1.101 + self.log.error("Problems handling data: %s" % e)
1.102 + self._unlink_fifo()
1.103 + return False
1.104 +
1.105 + self._unlink_fifo()
1.106 + return True
1.107 + # start()
1.108 +
1.109 +
1.110 + def stop(self):
1.111 + if self.proc:
1.112 + try:
1.113 + os.kill(self.proc.pid, signal.SIGTERM)
1.114 + except OSError, e:
1.115 + pass
1.116 +
1.117 + try:
1.118 + self.proc.wait()
1.119 + except Exception, e:
1.120 + pass
1.121 +
1.122 + self.proc = None
1.123 +
1.124 + self._unlink_fifo()
1.125 + # stop()
1.126 +# TranscoderMencoder