From ef36a3f64884926f2f9cd4dc050fb716a858397e Mon Sep 17 00:00:00 2001 From: girst Date: Tue, 17 Aug 2021 15:43:29 +0200 Subject: [PATCH] split stream_map.adaptive into *_video and *_audio --- app/common/common.py | 4 +++- app/youtube/__init__.py | 21 +++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/common/common.py b/app/common/common.py index 27568ea..59bb4e4 100644 --- a/app/common/common.py +++ b/app/common/common.py @@ -278,7 +278,9 @@ def get_video_info(video_id, sts=0, algo="", _embed=True): adaptive[i]['url'] = unscramble(cipher, algo) stream_map = { - 'adaptive': adaptive, 'muxed': formats, + 'adaptive_video': [a for a in adaptive if a['mimeType'].startswith('video/')], + 'adaptive_audio': [a for a in adaptive if a['mimeType'].startswith('audio/')], + 'muxed': formats, 'hlsManifestUrl': metadata['streamingData'].get('hlsManifestUrl'), } diff --git a/app/youtube/__init__.py b/app/youtube/__init__.py index 90b2036..b499e34 100644 --- a/app/youtube/__init__.py +++ b/app/youtube/__init__.py @@ -98,12 +98,10 @@ def watch(): videoplayback = url_for('proxy.videoplayback') query = urlparse(video_url).query video_url = f"{videoplayback}?{query}" - for s in stream_map['adaptive']: - query = urlparse(s['url']).query - s['url'] = f"{videoplayback}?{query}" - for s in stream_map['muxed']: - query = urlparse(s['url']).query - s['url'] = f"{videoplayback}?{query}" + for t in ('adaptive_video', 'adaptive_audio', 'muxed'): + for s in stream_map[t]: + query = urlparse(s['url']).query + s['url'] = f"{videoplayback}?{query}" error = None # if the proxy is enabled, we can also play livestreams: @@ -143,12 +141,11 @@ def watch(): if error and not stream_map: msg = errdetails if error=='player' else f"{error.upper()}: {errdetails}" return msg, 400, plaintextheaders # TODO: nicer - stream = next(iter(sorted([ - stream for stream in stream_map['adaptive'] - if stream['mimeType'].startswith('audio/')], - key=lambda stream: ('opus' not in stream['mimeType'], - stream['bitrate']), reverse=True) - ),{}).get('url') + stream = next(iter(sorted( + stream_map['adaptive_audio'], + key=lambda e: ('opus' not in e['mimeType'], e['bitrate']), + reverse=True + )),{}).get('url') return redirect(stream) else: if error and not metadata: # e.g. malformed, private/deleted video, ... -- 2.39.3