# HG changeset patch # User renatofilho # Date 1187040540 -3600 # Node ID 2b1824e138b108df0d8b5d2475c5ea287838c2c1 # Parent 13f38117f52018b396f694793b110f8bca139a0b [svn r806] created a new method for transcode files diff -r 13f38117f520 -r 2b1824e138b1 gmyth-stream/server/0.3/gms.py --- a/gmyth-stream/server/0.3/gms.py Fri Aug 10 15:20:19 2007 +0100 +++ b/gmyth-stream/server/0.3/gms.py Mon Aug 13 22:29:00 2007 +0100 @@ -8,9 +8,11 @@ import sys import os +import mimetypes import logging as log from lib.server import serve_forever, load_plugins_transcoders +mimetypes.init() log_level = log.INFO for p in sys.argv[1:]: if p == "-v" or p == "--verbose": diff -r 13f38117f520 -r 2b1824e138b1 gmyth-stream/server/0.3/lib/request_handler.py --- a/gmyth-stream/server/0.3/lib/request_handler.py Fri Aug 10 15:20:19 2007 +0100 +++ b/gmyth-stream/server/0.3/lib/request_handler.py Mon Aug 13 22:29:00 2007 +0100 @@ -13,6 +13,7 @@ import threading import SocketServer import BaseHTTPServer +import mimetypes import lib.utils as utils import lib.file_handler as files @@ -64,6 +65,7 @@ "/get_log.do": self.serve_get_log, "/get_all_log.do": self.serve_get_all_log, "/stream.do": self.serve_stream, + "/transcode.do": self.serve_transcode, "/list.do": self.serve_list, "/get_file_info.do": self.serve_file_info, } @@ -353,6 +355,46 @@ # serve_file_info() def serve_stream(self, body): + filename = self.query.get("uri", None)[0] + + if not os.path.exists (filename): + self.send_error(404, "File not found") + return + + size = int(os.path.getsize(filename)) + self.send_response(200) + self.send_header("Content-Type", mimetypes.guess_type(filename)[0]) + self.send_header("Cache-Control","no-cache") + self.send_header("Content-Length", size) + #self.send_header("Connection", "close") + self.end_headers() + + media = open(filename) + data_in = " " + total_read = 0 + + test_tid = int(self.query.get("tid", "0")[0]) + if test_tid == 0 or test_tid not in self.tid_queue: + test_tid = self._get_new_id(self.server.last_tid) + + self.transcoders_log.insert(test_tid, "gms.Stream: %s" % filename) + + try: + while data_in != "": + data_in = media.read(4096) + total_read += 4096 + self.wfile.write(data_in) + status = utils.progress_bar(total_read, size, 50) + msg_status = "Status:%s:%s%%" % (test_tid, status) + self.transcoders_log._update_status(test_tid, msg_status) + self.transcoders_log._update_status(test_tid, "OK: Done") + + except Exception, e: + self.log.error("Stream error: %s" %e) + self.transcoders_log._update_status(test_tid, "Error: %s" % e) + # serve_stream() + + def serve_transcode(self, body): transcoder = self._get_transcoder() try: obj = transcoder(self.query) @@ -362,7 +404,7 @@ self.send_response(200) self.send_header("Content-Type", obj.get_mimetype()) - self.send_header("Cache-Control","no-cache") + self.send_header("Cache-Control","no-cache") if (obj.name == "gmencoder"): self.send_header("Transfer-Encoding", "chunked") @@ -371,12 +413,10 @@ self.end_headers() if body: - test_tid = int(self.query.get("tid", "0")[0]) if test_tid == 0 or test_tid not in self.tid_queue: test_tid = self._get_new_id(self.server.last_tid) - if self.query.get("transcoder", None): self.transcoders_log.insert(test_tid, "gms.%s" % obj.name) obj.tid = test_tid @@ -387,33 +427,6 @@ self.server.del_transcoders(self, obj) files.TranscodedFile("", self.query) - elif self.query.get("type", "")[0] == "file" and \ - self.query.get("uri", None): - - filename = self.query.get("uri", None)[0] - self.transcoders_log.insert(test_tid, "gms.%s" % filename) - - try: - media = open(filename) - data_in = " " - total_read = 0 - size = int(os.path.getsize(filename)) - - while data_in != "": - data_in = media.read(4096) - total_read += 4096 - self.wfile.write(data_in) - status = utils.progress_bar(total_read, size, 50) - msg_status = "Status:%s:%s%%" % (test_tid, status) - self.transcoders_log._update_status(test_tid, - msg_status) - - self.transcoders_log._update_status(test_tid, "OK: Done") - - except Exception, e: - self.log.error("Stream error: %s" %e) - self.transcoders_log._update_status(test_tid, - "Error: %s" % e) # serve_stream() diff -r 13f38117f520 -r 2b1824e138b1 gmyth-stream/server/0.3/lib/transcoder.py --- a/gmyth-stream/server/0.3/lib/transcoder.py Fri Aug 10 15:20:19 2007 +0100 +++ b/gmyth-stream/server/0.3/lib/transcoder.py Mon Aug 13 22:29:00 2007 +0100 @@ -20,7 +20,6 @@ self.params = params # __init__() - def params_first(self, key, default=None): if default is None: return self.params[key][0] @@ -31,26 +30,25 @@ return default # params_first() - def get_mimetype(self): - mux = self.params_first("mux", "mpg") - - if mux == "mpeg": - return "video/mpeg" - elif mux == "avi": - return "video/x-msvideo" - else: - return "application/octet-stream" + return "application/octet-stream" # get_mimetype() def start(self, outfile): - return True + pass # start() + def stop(self): + pass + # stop() - def stop(self): - return True - # stop() + def get_legth (self): + pass + # get_leght () + + def get_progress (self): + pass + # get_progress () def __str__(self): return '%s: %s( params=%s ) - Status: %s%%' % \ diff -r 13f38117f520 -r 2b1824e138b1 gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py --- a/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py Fri Aug 10 15:20:19 2007 +0100 +++ b/gmyth-stream/server/0.3/plugins/transcoders/gmencoder.py Mon Aug 13 22:29:00 2007 +0100 @@ -27,8 +27,24 @@ proc = None def __init__(self, params): + self.status = 0 transcoder.Transcoder.__init__(self, params) self.opts = [] + self.opts.append (self.gmencoder_path) + self.opts.append ("-d") + self._parser_params () + + outfile = self.params_first("outfile", "") + + if outfile != "": + path = os.path.join(sys.path[0], ".transcoded", outfile) + self._insert_param("-o", "file://%s" % path) + else: + self._insert_param ("-o", "fd://%d" % outfd.fileno()) + self.opts.append ("-c") + + cmd = " ".join(self.opts) + # __init__() def _insert_param (self, name, value): @@ -50,21 +66,6 @@ # _parse_params def start(self, outfd): - self.status = 0 - self.opts.append (self.gmencoder_path) - self.opts.append ("-d") - self._parser_params () - - outfile = self.params_first("outfile", "") - - if outfile != "": - path = os.path.join(sys.path[0], ".transcoded", outfile) - self._insert_param("-o", "file://%s" % path) - else: - self._insert_param ("-o", "fd://%d" % outfd.fileno()) - self.opts.append ("-c") - - cmd = " ".join(self.opts) self.log.info(self.tid, "GMencoder: %s" % cmd) try: @@ -75,23 +76,18 @@ outfd.write("Error: GMencoder: %s" % e) return False - if outfile: - outfd.write("OK ") - try: while (self.proc and self.proc.poll() == None): r, w, x = select.select([self.proc.stdout], [], [], 0) if self.proc.stdout in r: progress = self.proc.stdout.readline() if (progress.find ("PROGRESS") >= 0): - self.log.info (self.tid, "%s" % progress) self.status = progress.split (":")[1] except Exception, e: self.log.error(self.tid, "Problems handling data: %s" % e) return False self.status = 100; - self.log.info(self.tid, "OK: Done") return True # start() @@ -100,7 +96,6 @@ if self.proc: self.log.info(self.tid, "Stopped GMencoder plugin") try: - self.proc.stdin.write("QUIT\n") os.kill(self.proc.pid, signal.SIGKILL) self.proc.wait() except Exception, e: @@ -109,4 +104,10 @@ self.proc = None # stop() + def get_progress(self): + return self.status + + def get_lenght(self): + return -1 + # TranscoderGMencoder