gmyth-stream/server/0.2/plugins/transcoders/mencoder.py
branchtrunk
changeset 595 5c5cff842d57
parent 585 a1783dab9ba6
child 605 4dd9bf602c18
     1.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py	Mon Apr 23 16:06:54 2007 +0100
     1.2 +++ b/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py	Wed Apr 25 15:53:19 2007 +0100
     1.3 @@ -2,10 +2,13 @@
     1.4  import shlex
     1.5  import signal
     1.6  import subprocess
     1.7 +import time
     1.8  
     1.9  import lib.utils as utils
    1.10  import lib.server as server
    1.11  
    1.12 +from select import select
    1.13 +
    1.14  __all__ = ("TranscoderMencoder",)
    1.15  
    1.16  class TranscoderMencoder(server.Transcoder):
    1.17 @@ -16,6 +19,9 @@
    1.18      args = {}
    1.19      proc = None
    1.20  
    1.21 +    # only works with avi container
    1.22 +    status = 0
    1.23 +
    1.24      def _setup_params(self):
    1.25          params_first = self.params_first
    1.26  
    1.27 @@ -25,6 +31,11 @@
    1.28          self.args["subtitle"] = params_first("subtitle", False)
    1.29          self.args["format"]   = params_first("format", "")
    1.30          self.args["outfile"]  = params_first("outfile", "-")
    1.31 +        self.args["sendback"] = params_first("sendback", True)
    1.32 +
    1.33 +        # handle sendback variable
    1.34 +        if self.args["sendback"] == "False":
    1.35 +            self.args["sendback"] = False
    1.36  
    1.37          # input_opt
    1.38          uri = params_first("uri", "file://-").split("://")
    1.39 @@ -84,7 +95,11 @@
    1.40  
    1.41      def _setup_mencoder_opts(self, args):
    1.42          args.append(self.mencoder_path)
    1.43 -        args.append(self.args["input"])
    1.44 +
    1.45 +        if self.args["outfile"] == "-" and self.args["type"]:
    1.46 +            args.append(self.args["input"])
    1.47 +        else:
    1.48 +            args.append("-")
    1.49  
    1.50          if self.args["language"]:
    1.51              self._arg_append(args, "-alang %s" % self.args["language"])
    1.52 @@ -99,7 +114,7 @@
    1.53  
    1.54          self._arg_append(args, "-really-quiet")
    1.55          self._arg_append(args, "-o %s" % self.args["outfile"])
    1.56 -        self._arg_append(args, "2> %s" % os.devnull)
    1.57 +        self._arg_append(args, "2>%s" % os.devnull)
    1.58      # _setup_args()
    1.59  
    1.60  
    1.61 @@ -113,6 +128,12 @@
    1.62  
    1.63          elif _type == "dvd":
    1.64              self.args["input"] = "dvd://".join(self.args["input"])
    1.65 +
    1.66 +        elif _type == "myth":
    1.67 +            # gmyth-cat -h 192.168.1.124 -p 6543 -c 111
    1.68 +            # gmyth-cat -h 192.168.1.124 -p 6543 -f file.nuv
    1.69 +            # myth://IP:PORT:type:file
    1.70 +            self.args["gmyth-cat"] = self.args["input"].split(":")
    1.71      # _setup_filename()
    1.72  
    1.73  
    1.74 @@ -129,16 +150,79 @@
    1.75      # __init__()
    1.76  
    1.77  
    1.78 -    def start(self, outfd):
    1.79 -        cmd = " ".join(self.mencoder_opts)
    1.80 -        self.log.debug("Mencoder: %s" % cmd)
    1.81 +    def _check_opened_file(self, stdw, _stdin):
    1.82 +        loop = True
    1.83 +        while loop:
    1.84 +            try:
    1.85 +                return open(self.args["outfile"])
    1.86 +            except:
    1.87 +                os.write(stdw, _stdin.read(1024))
    1.88 +    # _check_opened_file
    1.89 +
    1.90 +
    1.91 +    def _start_outfile(self, outfd):
    1.92 +        finished = False
    1.93 +
    1.94 +        # fix this (not necessary)
    1.95 +        outfd.write("OK")
    1.96 +
    1.97 +        # Configuring stdin
    1.98 +        _stdin = open(self.args["input"])
    1.99 +        size = int(os.path.getsize(self.args["input"]))
   1.100 +        self.status = 0
   1.101 +        total_read = 0
   1.102 +
   1.103 +        # Configuring pipes
   1.104 +        stdr, stdw = os.pipe()
   1.105  
   1.106          try:
   1.107 -            self.proc = subprocess.Popen(self.mencoder_opts, stdout=subprocess.PIPE, close_fds=True)
   1.108 +            self.proc = subprocess.Popen(self.mencoder_opts, stdin=stdr, close_fds=True)
   1.109          except Exception, e:
   1.110              self.log.error("Error executing mencoder: %s" % e)
   1.111              return False
   1.112  
   1.113 +        stdout = self._check_opened_file(stdw, _stdin)
   1.114 +
   1.115 +        try:
   1.116 +            while self.proc and self.proc.poll() == None:
   1.117 +                if not finished:
   1.118 +                    data_in = _stdin.read(4096)
   1.119 +                    if data_in != "":
   1.120 +                        os.write(stdw, data_in)
   1.121 +                        total_read += 4096
   1.122 +                        d = stdout.read(4096)
   1.123 +                        if self.args["sendback"]:
   1.124 +                            outfd.write(d)
   1.125 +                        self.status = total_read * 100 / size
   1.126 +                    else:
   1.127 +                        finished = True
   1.128 +                        os.close(stdw)
   1.129 +
   1.130 +                else:
   1.131 +                    d = stdout.read(4096)
   1.132 +                    if self.args["sendback"] and d != "":
   1.133 +                        outfd.write(d)
   1.134 +
   1.135 +        except Exception, e:
   1.136 +            self.log.error("Problems handling data: %s" % e)
   1.137 +            return False
   1.138 +
   1.139 +        self.log.info("%s: Finished sending data to client" % repr(self))
   1.140 +        if not self.args["sendback"]:
   1.141 +            outfd.write("DONE")
   1.142 +
   1.143 +        return True
   1.144 +    # _start_outfile()
   1.145 +
   1.146 +    def _start(self, outfd):
   1.147 +        try:
   1.148 +            self.proc = subprocess.Popen(self.mencoder_opts,
   1.149 +                                         stdout=subprocess.PIPE, close_fds=True)
   1.150 +        except Exception, e:
   1.151 +            self.log.error("Error executing mencoder: %s" % e)
   1.152 +            return False
   1.153 +
   1.154 +
   1.155          try:
   1.156              while self.proc and self.proc.poll() == None:
   1.157                  d = self.proc.stdout.read(1024)
   1.158 @@ -147,7 +231,19 @@
   1.159              self.log.error("Problems handling data: %s" % e)
   1.160              return False
   1.161  
   1.162 +        self.log.info("%s: Finished sending data to client" % repr(self))
   1.163          return True
   1.164 +    # _start()
   1.165 +
   1.166 +
   1.167 +    def start(self, outfd):
   1.168 +        cmd = " ".join(self.mencoder_opts)
   1.169 +        self.log.debug("Mencoder: %s" % cmd)
   1.170 +
   1.171 +        if self.args["outfile"] == "-":
   1.172 +            return self._start(outfd)
   1.173 +        else:
   1.174 +            return self._start_outfile(outfd)
   1.175      # start()
   1.176  
   1.177