[svn r806] created a new method for transcode files trunk
authorrenatofilho
Mon Aug 13 22:29:00 2007 +0100 (2007-08-13)
branchtrunk
changeset 8002b1824e138b1
parent 799 13f38117f520
child 801 e51af4d0caf5
[svn r806] created a new method for transcode files
gmyth-stream/server/0.3/gms.py
gmyth-stream/server/0.3/lib/request_handler.py
gmyth-stream/server/0.3/lib/transcoder.py
gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py
     1.1 --- a/gmyth-stream/server/0.3/gms.py	Fri Aug 10 15:20:19 2007 +0100
     1.2 +++ b/gmyth-stream/server/0.3/gms.py	Mon Aug 13 22:29:00 2007 +0100
     1.3 @@ -8,9 +8,11 @@
     1.4  
     1.5  import sys
     1.6  import os
     1.7 +import mimetypes
     1.8  import logging as log
     1.9  from lib.server import serve_forever, load_plugins_transcoders
    1.10  
    1.11 +mimetypes.init()
    1.12  log_level = log.INFO
    1.13  for p in sys.argv[1:]:
    1.14      if p == "-v" or p == "--verbose":
     2.1 --- a/gmyth-stream/server/0.3/lib/request_handler.py	Fri Aug 10 15:20:19 2007 +0100
     2.2 +++ b/gmyth-stream/server/0.3/lib/request_handler.py	Mon Aug 13 22:29:00 2007 +0100
     2.3 @@ -13,6 +13,7 @@
     2.4  import threading
     2.5  import SocketServer
     2.6  import BaseHTTPServer
     2.7 +import mimetypes
     2.8  
     2.9  import lib.utils as utils
    2.10  import lib.file_handler as files
    2.11 @@ -64,6 +65,7 @@
    2.12              "/get_log.do": self.serve_get_log,
    2.13              "/get_all_log.do": self.serve_get_all_log,
    2.14              "/stream.do": self.serve_stream,
    2.15 +            "/transcode.do": self.serve_transcode,
    2.16              "/list.do": self.serve_list,
    2.17              "/get_file_info.do": self.serve_file_info,
    2.18              }
    2.19 @@ -353,6 +355,46 @@
    2.20      # serve_file_info()
    2.21  
    2.22      def serve_stream(self, body):
    2.23 +        filename = self.query.get("uri", None)[0]
    2.24 +
    2.25 +        if not os.path.exists (filename):
    2.26 +            self.send_error(404, "File not found")
    2.27 +            return
    2.28 +
    2.29 +        size = int(os.path.getsize(filename))
    2.30 +        self.send_response(200)
    2.31 +        self.send_header("Content-Type", mimetypes.guess_type(filename)[0])
    2.32 +        self.send_header("Cache-Control","no-cache")
    2.33 +        self.send_header("Content-Length", size)
    2.34 +        #self.send_header("Connection", "close")
    2.35 +        self.end_headers()
    2.36 +
    2.37 +        media = open(filename)
    2.38 +        data_in = " "
    2.39 +        total_read = 0
    2.40 +
    2.41 +        test_tid = int(self.query.get("tid", "0")[0])
    2.42 +        if test_tid == 0 or test_tid not in self.tid_queue:
    2.43 +            test_tid = self._get_new_id(self.server.last_tid)
    2.44 +
    2.45 +        self.transcoders_log.insert(test_tid, "gms.Stream: %s" % filename)
    2.46 +
    2.47 +        try:
    2.48 +            while data_in != "":
    2.49 +                data_in = media.read(4096)
    2.50 +                total_read += 4096
    2.51 +                self.wfile.write(data_in)
    2.52 +                status = utils.progress_bar(total_read, size, 50)
    2.53 +                msg_status = "Status:%s:%s%%" % (test_tid, status)
    2.54 +                self.transcoders_log._update_status(test_tid, msg_status)
    2.55 +                self.transcoders_log._update_status(test_tid, "OK: Done")
    2.56 +
    2.57 +        except Exception, e:
    2.58 +            self.log.error("Stream error: %s" %e)
    2.59 +            self.transcoders_log._update_status(test_tid, "Error: %s" % e)
    2.60 +    # serve_stream()
    2.61 +
    2.62 +    def serve_transcode(self, body):
    2.63          transcoder = self._get_transcoder()
    2.64          try:
    2.65              obj = transcoder(self.query)
    2.66 @@ -362,7 +404,7 @@
    2.67  
    2.68          self.send_response(200)
    2.69          self.send_header("Content-Type", obj.get_mimetype())
    2.70 -	self.send_header("Cache-Control","no-cache")
    2.71 +        self.send_header("Cache-Control","no-cache")
    2.72  
    2.73          if (obj.name == "gmencoder"):
    2.74              self.send_header("Transfer-Encoding", "chunked")
    2.75 @@ -371,12 +413,10 @@
    2.76          self.end_headers()
    2.77  
    2.78          if body:
    2.79 -
    2.80              test_tid = int(self.query.get("tid", "0")[0])
    2.81              if test_tid == 0 or test_tid not in self.tid_queue:
    2.82                  test_tid = self._get_new_id(self.server.last_tid)
    2.83  
    2.84 -
    2.85              if self.query.get("transcoder", None):
    2.86                  self.transcoders_log.insert(test_tid, "gms.%s" % obj.name)
    2.87                  obj.tid = test_tid
    2.88 @@ -387,33 +427,6 @@
    2.89                  self.server.del_transcoders(self, obj)
    2.90                  files.TranscodedFile("", self.query)
    2.91  
    2.92 -            elif self.query.get("type", "")[0] == "file" and \
    2.93 -                     self.query.get("uri", None):
    2.94 -
    2.95 -                filename = self.query.get("uri", None)[0]
    2.96 -                self.transcoders_log.insert(test_tid, "gms.%s" % filename)
    2.97 -
    2.98 -                try:
    2.99 -                    media = open(filename)
   2.100 -                    data_in = " "
   2.101 -                    total_read = 0
   2.102 -                    size = int(os.path.getsize(filename))
   2.103 -
   2.104 -                    while data_in != "":
   2.105 -                        data_in = media.read(4096)
   2.106 -                        total_read += 4096
   2.107 -                        self.wfile.write(data_in)
   2.108 -                        status = utils.progress_bar(total_read, size, 50)
   2.109 -                        msg_status = "Status:%s:%s%%" % (test_tid, status)
   2.110 -                        self.transcoders_log._update_status(test_tid,
   2.111 -                                                            msg_status)
   2.112 -
   2.113 -                    self.transcoders_log._update_status(test_tid, "OK: Done")
   2.114 -
   2.115 -                except Exception, e:
   2.116 -                    self.log.error("Stream error: %s" %e)
   2.117 -                    self.transcoders_log._update_status(test_tid,
   2.118 -                                                        "Error: %s" % e)
   2.119      # serve_stream()
   2.120  
   2.121  
     3.1 --- a/gmyth-stream/server/0.3/lib/transcoder.py	Fri Aug 10 15:20:19 2007 +0100
     3.2 +++ b/gmyth-stream/server/0.3/lib/transcoder.py	Mon Aug 13 22:29:00 2007 +0100
     3.3 @@ -20,7 +20,6 @@
     3.4          self.params = params
     3.5      # __init__()
     3.6  
     3.7 -
     3.8      def params_first(self, key, default=None):
     3.9          if default is None:
    3.10              return self.params[key][0]
    3.11 @@ -31,26 +30,25 @@
    3.12                  return default
    3.13      # params_first()
    3.14  
    3.15 -
    3.16      def get_mimetype(self):
    3.17 -        mux = self.params_first("mux", "mpg")
    3.18 -
    3.19 -        if mux == "mpeg":
    3.20 -            return "video/mpeg"
    3.21 -        elif mux == "avi":
    3.22 -            return "video/x-msvideo"
    3.23 -        else:
    3.24 -            return "application/octet-stream"
    3.25 +        return "application/octet-stream"
    3.26      # get_mimetype()
    3.27  
    3.28      def start(self, outfile):
    3.29 -        return True
    3.30 +        pass
    3.31      # start()
    3.32  
    3.33 +    def stop(self):
    3.34 +        pass
    3.35 +    # stop()
    3.36  
    3.37 -    def stop(self):
    3.38 -        return True
    3.39 -    # stop()
    3.40 +    def get_legth (self):
    3.41 +        pass
    3.42 +    # get_leght ()
    3.43 +
    3.44 +    def get_progress (self):
    3.45 +        pass
    3.46 +    # get_progress ()
    3.47  
    3.48      def __str__(self):
    3.49          return '%s: %s( params=%s ) - Status: %s%%' % \
     4.1 --- a/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py	Fri Aug 10 15:20:19 2007 +0100
     4.2 +++ b/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py	Mon Aug 13 22:29:00 2007 +0100
     4.3 @@ -27,8 +27,24 @@
     4.4      proc = None
     4.5  
     4.6      def __init__(self, params):
     4.7 +        self.status = 0
     4.8          transcoder.Transcoder.__init__(self, params)
     4.9          self.opts = []
    4.10 +        self.opts.append (self.gmencoder_path)
    4.11 +        self.opts.append ("-d")
    4.12 +        self._parser_params ()
    4.13 +
    4.14 +        outfile = self.params_first("outfile", "")
    4.15 +
    4.16 +        if outfile != "":
    4.17 +            path = os.path.join(sys.path[0], ".transcoded", outfile)
    4.18 +            self._insert_param("-o", "file://%s" % path)
    4.19 +        else:
    4.20 +            self._insert_param ("-o", "fd://%d" % outfd.fileno())
    4.21 +            self.opts.append ("-c")
    4.22 +
    4.23 +        cmd = " ".join(self.opts)
    4.24 +
    4.25      # __init__()
    4.26  
    4.27      def _insert_param (self, name, value):
    4.28 @@ -50,21 +66,6 @@
    4.29      # _parse_params
    4.30  
    4.31      def start(self, outfd):
    4.32 -        self.status = 0
    4.33 -        self.opts.append (self.gmencoder_path)
    4.34 -        self.opts.append ("-d") 
    4.35 -        self._parser_params ()
    4.36 -
    4.37 -        outfile = self.params_first("outfile", "")
    4.38 -
    4.39 -        if outfile != "":
    4.40 -            path = os.path.join(sys.path[0], ".transcoded", outfile)
    4.41 -            self._insert_param("-o", "file://%s" % path)
    4.42 -        else:
    4.43 -            self._insert_param ("-o", "fd://%d" % outfd.fileno())
    4.44 -            self.opts.append ("-c")
    4.45 -
    4.46 -        cmd = " ".join(self.opts)
    4.47          self.log.info(self.tid, "GMencoder: %s" % cmd)
    4.48  
    4.49          try:
    4.50 @@ -75,23 +76,18 @@
    4.51              outfd.write("Error: GMencoder: %s" % e)
    4.52              return False
    4.53  
    4.54 -        if outfile:
    4.55 -            outfd.write("OK   ")
    4.56 -
    4.57          try:
    4.58              while (self.proc and self.proc.poll() == None):
    4.59                  r, w, x = select.select([self.proc.stdout], [], [], 0)
    4.60                  if self.proc.stdout in r:
    4.61                      progress = self.proc.stdout.readline()
    4.62                      if (progress.find ("PROGRESS") >= 0):
    4.63 -                        self.log.info (self.tid, "%s" % progress)
    4.64                          self.status = progress.split (":")[1]
    4.65          except Exception, e:
    4.66              self.log.error(self.tid, "Problems handling data: %s" % e)
    4.67              return False
    4.68  
    4.69          self.status = 100;
    4.70 -        self.log.info(self.tid, "OK: Done")
    4.71          return True
    4.72      # start()
    4.73  
    4.74 @@ -100,7 +96,6 @@
    4.75          if self.proc:
    4.76              self.log.info(self.tid, "Stopped GMencoder plugin")
    4.77              try:
    4.78 -                self.proc.stdin.write("QUIT\n")
    4.79                  os.kill(self.proc.pid, signal.SIGKILL)
    4.80                  self.proc.wait()
    4.81              except Exception, e:
    4.82 @@ -109,4 +104,10 @@
    4.83              self.proc = None
    4.84      # stop()
    4.85  
    4.86 +    def get_progress(self):
    4.87 +        return self.status
    4.88 +
    4.89 +    def get_lenght(self):
    4.90 +        return -1
    4.91 +
    4.92  # TranscoderGMencoder