def send_file(self, handler, path, query): mime = 'video/x-tivo-mpeg' tsn = handler.headers.getheader('tsn', '') tivo_name = config.tivo_names.get(tsn, tsn) is_tivo_file = (path[-5:].lower() == '.tivo') if 'Format' in query: mime = query['Format'][0] needs_tivodecode = (is_tivo_file and mime == 'video/mpeg') compatible = (not needs_tivodecode and transcode.tivo_compatible(path, tsn, mime)[0]) try: # "bytes=XXX-" offset = int(handler.headers.getheader('Range')[6:-1]) except: offset = 0 if needs_tivodecode: valid = bool(config.get_bin('tivodecode') and config.get_server('tivo_mak')) else: valid = True if valid and offset: valid = ((compatible and offset < os.stat(path).st_size) or (not compatible and transcode.is_resumable(path, offset))) #faking = (mime in ['video/x-tivo-mpeg-ts', 'video/x-tivo-mpeg'] and faking = (mime == 'video/x-tivo-mpeg' and not (is_tivo_file and compatible)) fname = unicode(path, 'utf-8') thead = '' if faking: thead = self.tivo_header(tsn, path, mime) if compatible: size = os.stat(fname).st_size + len(thead) handler.send_response(200) handler.send_header('Content-Length', size - offset) handler.send_header('Content-Range', 'bytes %d-%d/%d' % (offset, size - offset - 1, size)) else: handler.send_response(206) handler.send_header('Transfer-Encoding', 'chunked') handler.send_header('Content-Type', mime) handler.end_headers() logger.info('[%s] Start sending "%s" to %s' % (time.strftime('%d/%b/%Y %H:%M:%S'), fname, tivo_name)) start = time.time() count = 0 if valid: if compatible: if faking and not offset: handler.wfile.write(thead) logger.debug('"%s" is tivo compatible' % fname) f = open(fname, 'rb') try: if mime == 'video/mp4': count = qtfaststart.process(f, handler.wfile, offset) else: if offset: offset -= len(thead) f.seek(offset) while True: block = f.read(512 * 1024) if not block: break handler.wfile.write(block) count += len(block) except Exception, msg: logger.info(msg) f.close() else: logger.debug('"%s" is not tivo compatible' % fname) if offset: count = transcode.resume_transfer(path, handler.wfile, offset) else: count = transcode.transcode(False, path, handler.wfile, tsn, mime, thead)
def send_file(self, handler, path, query): mime = 'video/x-tivo-mpeg' tsn = handler.headers.getheader('tsn', '') try: assert (tsn) tivo_name = config.tivos[tsn].get('name', tsn) except: tivo_name = handler.address_string() is_tivo_file = (path[-5:].lower() == '.tivo') if 'Format' in query: mime = query['Format'][0] needs_tivodecode = (is_tivo_file and mime == 'video/mpeg') compatible = (not needs_tivodecode and transcode.tivo_compatible(path, tsn, mime)[0]) try: # "bytes=XXX-" offset = int(handler.headers.getheader('Range')[6:-1]) except: offset = 0 if needs_tivodecode: valid = bool( config.get_bin('tivodecode') and config.get_server('tivo_mak')) else: valid = True if valid and offset: valid = ((compatible and offset < os.path.getsize(path)) or (not compatible and transcode.is_resumable(path, offset))) #faking = (mime in ['video/x-tivo-mpeg-ts', 'video/x-tivo-mpeg'] and faking = (mime == 'video/x-tivo-mpeg' and not (is_tivo_file and compatible)) fname = unicode(path, 'utf-8') thead = '' if faking: thead = self.tivo_header(tsn, path, mime) if compatible: size = os.path.getsize(fname) + len(thead) handler.send_response(200) handler.send_header('Content-Length', size - offset) handler.send_header( 'Content-Range', 'bytes %d-%d/%d' % (offset, size - offset - 1, size)) else: handler.send_response(206) handler.send_header('Transfer-Encoding', 'chunked') handler.send_header('Content-Type', mime) handler.end_headers() logger.info('[%s] Start sending "%s" to %s' % (time.strftime('%d/%b/%Y %H:%M:%S'), fname, tivo_name)) start = time.time() count = 0 if valid: if compatible: if faking and not offset: handler.wfile.write(thead) logger.debug('"%s" is tivo compatible' % fname) f = open(fname, 'rb') try: if mime == 'video/mp4': count = qtfaststart.process(f, handler.wfile, offset) else: if offset: offset -= len(thead) f.seek(offset) while True: block = f.read(512 * 1024) if not block: break handler.wfile.write(block) count += len(block) except Exception, msg: logger.info(msg) f.close() else: logger.debug('"%s" is not tivo compatible' % fname) if offset: count = transcode.resume_transfer(path, handler.wfile, offset) else: count = transcode.transcode(False, path, handler.wfile, tsn, mime, thead)
except Exception, msg: status[tivo_name][path]['error'] = str(msg) if tivolibre: tivolibre.kill() tivolibre.wait() logger.info(msg) f.close() else: status[tivo_name][path]['transcoding'] = True logger.debug('"%s" is not tivo compatible' % fname) if offset: count = transcode.resume_transfer(path, handler.wfile, offset, status[tivo_name][path]) else: count = transcode.transcode(False, path, handler.wfile, status[tivo_name][path], is_tivo_file, tsn, mime, thead) end_time = time.time() elapsed = end_time - status[tivo_name][path]['start'] rate = count * 8.0 / elapsed status[tivo_name][path]['active'] = False status[tivo_name][path]['end'] = end_time status[tivo_name][path]['rate'] = rate logger.info('[%s] Done sending "%s" to %s, %d bytes, %.2f Mb/s' %
def send_file(self, handler, path, query): mime = "video/x-tivo-mpeg" tsn = handler.headers.getheader("tsn", "") tivo_name = config.tivo_names.get(tsn, tsn) is_tivo_file = path[-5:].lower() == ".tivo" if "Format" in query: mime = query["Format"][0] needs_tivodecode = is_tivo_file and mime == "video/mpeg" compatible = not needs_tivodecode and transcode.tivo_compatible(path, tsn, mime)[0] try: # "bytes=XXX-" offset = int(handler.headers.getheader("Range")[6:-1]) except: offset = 0 if needs_tivodecode: valid = bool(config.get_bin("tivodecode") and config.get_server("tivo_mak")) else: valid = True if valid and offset: valid = (compatible and offset < os.stat(path).st_size) or ( not compatible and transcode.is_resumable(path, offset) ) # faking = (mime in ['video/x-tivo-mpeg-ts', 'video/x-tivo-mpeg'] and faking = mime == "video/x-tivo-mpeg" and not (is_tivo_file and compatible) fname = unicode(path, "utf-8") thead = "" if faking: thead = self.tivo_header(tsn, path, mime) if compatible: size = os.stat(fname).st_size + len(thead) handler.send_response(200) handler.send_header("Content-Length", size - offset) handler.send_header("Content-Range", "bytes %d-%d/%d" % (offset, size - offset - 1, size)) else: handler.send_response(206) handler.send_header("Transfer-Encoding", "chunked") handler.send_header("Content-Type", mime) handler.send_header("Connection", "close") handler.end_headers() logger.info('[%s] Start sending "%s" to %s' % (time.strftime("%d/%b/%Y %H:%M:%S"), fname, tivo_name)) start = time.time() count = 0 if valid: if compatible: if faking and not offset: handler.wfile.write(thead) logger.debug('"%s" is tivo compatible' % fname) f = open(fname, "rb") try: if mime == "video/mp4": count = qtfaststart.process(f, handler.wfile, offset) else: if offset: offset -= len(thead) f.seek(offset) while True: block = f.read(512 * 1024) if not block: break handler.wfile.write(block) count += len(block) except Exception, msg: logger.info(msg) f.close() else: logger.debug('"%s" is not tivo compatible' % fname) if offset: count = transcode.resume_transfer(path, handler.wfile, offset) else: count = transcode.transcode(False, path, handler.wfile, tsn, mime, thead)