]> jfr.im git - yt-dlp.git/commitdiff
[twitcasting] Websocket support (#399)
authorpukkandan <redacted>
Mon, 21 Jun 2021 17:23:55 +0000 (22:53 +0530)
committerpukkandan <redacted>
Mon, 21 Jun 2021 17:26:45 +0000 (22:56 +0530)
Closes #392
Authored by: nao20010128nao

yt_dlp/extractor/twitcasting.py

index 71ac9e725a5d856b88170402ea3d88ee2b4690e8..16584e940b85cf0e734506b9ea71dad735cb669b 100644 (file)
@@ -5,12 +5,14 @@
 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 @@ def _real_extract(self, url):
             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 @@ def _real_extract(self, url):
             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 {