From e6779b9400fd49a57d0222abbd854119ec1689da Mon Sep 17 00:00:00 2001 From: pukkandan Date: Mon, 21 Jun 2021 22:53:55 +0530 Subject: [PATCH] [twitcasting] Websocket support (#399) Closes #392 Authored by: nao20010128nao --- yt_dlp/extractor/twitcasting.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/yt_dlp/extractor/twitcasting.py b/yt_dlp/extractor/twitcasting.py index 71ac9e725..16584e940 100644 --- a/yt_dlp/extractor/twitcasting.py +++ b/yt_dlp/extractor/twitcasting.py @@ -5,12 +5,14 @@ import itertools import re from .common import InfoExtractor +from ..downloader.websocket import has_websockets from ..utils import ( clean_html, float_or_none, get_element_by_class, get_element_by_id, parse_duration, + qualities, str_to_int, try_get, unified_timestamp, @@ -89,9 +91,24 @@ class TwitCastingIE(InfoExtractor): video_js_data = video_js_data[0] m3u8_url = try_get(video_js_data, lambda x: x['source']['url']) + stream_server_data = self._download_json( + 'https://twitcasting.tv/streamserver.php?target=%s&mode=client' % uploader_id, video_id, + 'Downloading live info', fatal=False) + is_live = 'data-status="online"' in webpage + formats = [] if is_live and not m3u8_url: m3u8_url = 'https://twitcasting.tv/%s/metastream.m3u8' % uploader_id + if is_live and has_websockets and stream_server_data: + qq = qualities(['base', 'mobilesource', 'main']) + for mode, ws_url in stream_server_data['llfmp4']['streams'].items(): + formats.append({ + 'url': ws_url, + 'format_id': 'ws-%s' % mode, + 'ext': 'mp4', + 'quality': qq(mode), + 'protocol': 'websocket_frag', # TwitCasting simply sends moof atom directly over WS + }) thumbnail = video_js_data.get('thumbnailUrl') or self._og_search_thumbnail(webpage) description = clean_html(get_element_by_id( @@ -106,10 +123,9 @@ class TwitCastingIE(InfoExtractor): r'data-toggle="true"[^>]+datetime="([^"]+)"', webpage, 'datetime', None)) - formats = None if m3u8_url: - formats = self._extract_m3u8_formats( - m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', live=is_live) + formats.extend(self._extract_m3u8_formats( + m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', live=is_live)) self._sort_formats(formats) return {