Basic media playlist generation, missing special cases

pull/5/head
Mike Lang 6 years ago
parent 9e115f8a42
commit a1fa60828d

@ -1,3 +1,5 @@
import os
import urllib
def generate_master(playlists): def generate_master(playlists):
@ -14,3 +16,20 @@ def generate_master(playlists):
url, url,
] ]
return "\n".join(lines) + '\n' return "\n".join(lines) + '\n'
def generate_media(segments, base_url):
"""Generate a media playlist from a list of segments as returned by common.get_best_segments().
Segments are specified as hour/name.ts relative to base_url.
"""
lines = [
"#EXTM3U",
"#EXT-X-TARGETDURATION:6",
]
for segment in segments:
# TODO handle missing bits, stream endings, other stuff
if segment is not None:
path = '/'.join(segment.path.split('/')[-2:])
lines.append("#EXTINF:{:.3f},live".format(segment.duration.total_seconds()))
lines.append(urllib.quote(os.path.join(base_url, path)))
return "\n".join(lines) + '\n'

@ -6,6 +6,10 @@ import os
from flask import Flask, url_for, request, abort from flask import Flask, url_for, request, abort
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
import dateutil.parser
from common import get_best_segments
import generate_hls import generate_hls
@ -90,8 +94,19 @@ def generate_master_playlist(stream):
@app.route('/playlist/<stream>/<variant>.m3u8') @app.route('/playlist/<stream>/<variant>.m3u8')
def generate_media_playlist(stream, variant): def generate_media_playlist(stream, variant):
# TODO # path traversal / hidden folders
return "Not Implemented", 501 if stream.startswith('.'):
return "Stream may not start with period", 403
if variant.startswith('.'):
return "Variant may not start with period", 403
#TODO handle no start/end
#TODO error handling of args
# TODO lots of other stuff
start = dateutil.parser.parse(request.args['start'])
end = dateutil.parser.parse(request.args['end'])
hours_path = os.path.join(app.static_folder, stream, variant)
segments = get_best_segments(hours_path, start, end)
return generate_hls.generate_media(segments, os.path.join(app.static_url_path, stream, variant))
def main(host='0.0.0.0', port=8000, base_dir='.'): def main(host='0.0.0.0', port=8000, base_dir='.'):

Loading…
Cancel
Save