1.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Wed Apr 25 15:53:19 2007 +0100
1.2 +++ b/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Tue May 01 17:22:20 2007 +0100
1.3 @@ -18,6 +18,7 @@
1.4 priority = -1
1.5 args = {}
1.6 proc = None
1.7 + gmyth = None
1.8
1.9 # only works with avi container
1.10 status = 0
1.11 @@ -38,7 +39,7 @@
1.12 self.args["sendback"] = False
1.13
1.14 # input_opt
1.15 - uri = params_first("uri", "file://-").split("://")
1.16 + uri = params_first("uri", "file:-").split(":", 1)
1.17 self.args["type"] = uri[0]
1.18 self.args["input"] = uri[1]
1.19
1.20 @@ -109,6 +110,7 @@
1.21 self._arg_append(args, "-subfps %s" % self.args["fps"])
1.22
1.23 self._arg_append(args, "-idx")
1.24 + self._arg_append(args, "-cache 500")
1.25 self._arg_append(args, self._setup_audio())
1.26 self._arg_append(args, self._setup_video())
1.27
1.28 @@ -125,6 +127,8 @@
1.29 if not os.path.exists(self.args["input"]):
1.30 raise IOError,\
1.31 "File requested does not exist: %s." % self.args["input"]
1.32 + else:
1.33 + self.args["input"] = "file://%s" % self.args["input"]
1.34
1.35 elif _type == "dvd":
1.36 self.args["input"] = "dvd://".join(self.args["input"])
1.37 @@ -134,6 +138,7 @@
1.38 # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
1.39 # myth://IP:PORT:type:file
1.40 self.args["gmyth-cat"] = self.args["input"].split(":")
1.41 + self.args["input"] = "-"
1.42 # _setup_filename()
1.43
1.44
1.45 @@ -175,10 +180,7 @@
1.46 # Configuring pipes
1.47 stdr, stdw = os.pipe()
1.48
1.49 - try:
1.50 - self.proc = subprocess.Popen(self.mencoder_opts, stdin=stdr, close_fds=True)
1.51 - except Exception, e:
1.52 - self.log.error("Error executing mencoder: %s" % e)
1.53 + if not self._run_mencoder(input=stdr):
1.54 return False
1.55
1.56 stdout = self._check_opened_file(stdw, _stdin)
1.57 @@ -215,14 +217,9 @@
1.58 # _start_outfile()
1.59
1.60 def _start(self, outfd):
1.61 - try:
1.62 - self.proc = subprocess.Popen(self.mencoder_opts,
1.63 - stdout=subprocess.PIPE, close_fds=True)
1.64 - except Exception, e:
1.65 - self.log.error("Error executing mencoder: %s" % e)
1.66 + if not self._run_mencoder(output=subprocess.PIPE):
1.67 return False
1.68
1.69 -
1.70 try:
1.71 while self.proc and self.proc.poll() == None:
1.72 d = self.proc.stdout.read(1024)
1.73 @@ -235,22 +232,76 @@
1.74 return True
1.75 # _start()
1.76
1.77 + def _start_myth(self, outfd):
1.78 + # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
1.79 + # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
1.80 + # myth://IP:PORT:type:file
1.81 + host = self.args["gmyth-cat"][0]
1.82 + port = self.args["gmyth-cat"][1]
1.83 + kind = self.args["gmyth-cat"][2]
1.84 + fchan = self.args["gmyth-cat"][3]
1.85 +
1.86 + gmyth_cat = utils.which("gmyth-cat")
1.87 + opts = [gmyth_cat, "-h", host, "-p", port, "-" + kind, fchan]
1.88 + gr, gw = os.pipe()
1.89 +
1.90 + try:
1.91 + self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE, close_fds=True)
1.92 + except Exception, e:
1.93 + self.log.error("Error executing gmyth-cat: %s" % e)
1.94 + return False
1.95 +
1.96 + if not self._run_mencoder(input=self.gmyth.stdout, output=subprocess.PIPE):
1.97 + return False
1.98 +
1.99 + try:
1.100 + while self.proc and self.proc.poll() == None:
1.101 + r, w, x = select([self.proc.stdout], [], [], 0)
1.102 + if self.proc.stdout in r:
1.103 + d = self.proc.stdout.read(4096)
1.104 + outfd.write(d)
1.105 + except Exception, e:
1.106 + self.log.error("Problems handling data: %s" % e)
1.107 + return False
1.108 +
1.109 + return True
1.110 + # _start_myth()
1.111 +
1.112 + def _run_mencoder(self, input=None, output=None):
1.113 + try:
1.114 + self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
1.115 + stdout=output, close_fds=True)
1.116 + except Exception, e:
1.117 + self.log.error("Error executing mencoder: %s" % e)
1.118 + return False
1.119 +
1.120 + return True
1.121 + # _run_mencoder()
1.122
1.123 def start(self, outfd):
1.124 cmd = " ".join(self.mencoder_opts)
1.125 self.log.debug("Mencoder: %s" % cmd)
1.126
1.127 - if self.args["outfile"] == "-":
1.128 - return self._start(outfd)
1.129 + ret = False
1.130 +
1.131 + if self.args["outfile"] == "-" and self.args["type"] in ["file", "dvd"]:
1.132 + ret = self._start(outfd)
1.133 +
1.134 + elif self.args["type"] == "myth":
1.135 + ret = self._start_myth(outfd)
1.136 +
1.137 else:
1.138 - return self._start_outfile(outfd)
1.139 + ret = self._start_outfile(outfd)
1.140 +
1.141 + self.stop()
1.142 + return ret
1.143 # start()
1.144
1.145
1.146 def stop(self):
1.147 if self.proc:
1.148 try:
1.149 - os.kill(self.proc.pid, signal.SIGTERM)
1.150 + os.kill(self.proc.pid, signal.SIGKILL)
1.151 except OSError, e:
1.152 pass
1.153
1.154 @@ -260,6 +311,20 @@
1.155 pass
1.156
1.157 self.proc = None
1.158 +
1.159 + if self.gmyth:
1.160 + try:
1.161 + os.kill(self.gmyth.pid, signal.SIGKILL)
1.162 + except OSError, e:
1.163 + pass
1.164 +
1.165 + try:
1.166 + self.gmyth.wait()
1.167 + except Exception, e:
1.168 + pass
1.169 +
1.170 + self.gmyth = None
1.171 +
1.172 # stop()
1.173
1.174 # TranscoderMencoder