From 3f55f3348788cbcbe2b47dfe0971b5e3ea5858af Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Thu, 26 May 2016 21:46:37 +0200 Subject: kodi plugin --- mediabrowser/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++++-- mediabrowser/ffmpeg.py | 2 +- 2 files changed, 41 insertions(+), 3 deletions(-) (limited to 'mediabrowser') diff --git a/mediabrowser/__init__.py b/mediabrowser/__init__.py index 28241b4..5faed84 100644 --- a/mediabrowser/__init__.py +++ b/mediabrowser/__init__.py @@ -6,7 +6,7 @@ from datetime import datetime from functools import partial from flask import Blueprint, render_template, abort, \ - url_for, Response, request + url_for, Response, request, jsonify from . import ffmpeg @@ -37,11 +37,14 @@ class cached(object): return wrapped_func + class cached_stream(object): """decorator to apply SavingIoWrapper""" + def __init__(self, cache, keyfunc): self.cache = cache self.keyfunc = keyfunc + def __call__(self, func): def wrapped_func(*args, **kwargs): key = self.keyfunc(*args, **kwargs) @@ -58,6 +61,7 @@ class cached_stream(object): class SavingIoWrapper(io.RawIOBase): """Wraps a read-only io stream and buffers all read-ed data. on close() that data is written to the specified cache""" + def __init__(self, stream, key, cache): self.stream = stream self.key = key @@ -165,7 +169,7 @@ def build(root_directory, cache): new_t_prev_duration, new_t = ffmpeg.find_next_keyframe(ospath, ss + t, t / 2) new_t -= new_ss # minus one frame - new_t -= new_t_prev_duration + # new_t -= new_t_prev_duration process = ffmpeg.stream(ospath, new_ss, new_t) return Response(process.stdout, mimetype='video/MP2T') @@ -277,4 +281,38 @@ def build(root_directory, cache): except FileNotFoundError: abort(404) + @blueprint.route('/json/', defaults={'path': ''}) + @blueprint.route('/json/') + def json(path): + def gather_fileinfo(path, ospath, filename): + osfilepath = os.path.join(ospath, filename) + if os.path.isdir(osfilepath) and not filename.startswith('.'): + return {'type': 'directory', + 'name': filename, + 'path': url_for('mediabrowser.json', + path=os.path.join(path, filename))} + else: + if not get_video_mime_type(osfilepath): + return None + else: + return {'type': 'file', + 'name': filename, + 'download': url_for('mediabrowser.download', + path=os.path.join(path, filename)), + 'poster': url_for('mediabrowser.poster', + path=os.path.join(path, filename)), + 'trailer': url_for('mediabrowser.thumbnail_video', + path=os.path.join(path, filename)), + 'm3u8': url_for('mediabrowser.m3u8', + path=os.path.join(path, filename))} + + try: + path = os.path.normpath(path) + ospath = os.path.join(root_directory, path) + files = list( + map(partial(gather_fileinfo, path, ospath), os.listdir(ospath))) + return jsonify({'files': files}) + except FileNotFoundError: + abort(404) + return blueprint diff --git a/mediabrowser/ffmpeg.py b/mediabrowser/ffmpeg.py index d4ad699..dfdc619 100644 --- a/mediabrowser/ffmpeg.py +++ b/mediabrowser/ffmpeg.py @@ -41,7 +41,7 @@ def stream(ospath, ss, t): shlex.split("-c:a aac -strict experimental -ac 2 -b:a 64k" " -c:v libx264 -pix_fmt yuv420p -profile:v high -level 4.0 -preset ultrafast -trellis 0" " -crf 31 -vf scale=w=trunc(oh*a/2)*2:h=480" - " -f mpegts" + " -shortest -f mpegts" " -output_ts_offset {output_ts_offset:.6f} -t {t:.6f} pipe:%d.ts".format(**locals())), stdout=PIPE) return cutter -- cgit v1.2.1