From 972a2d51ad70dfe395799c5bf6566c36fe84efe8 Mon Sep 17 00:00:00 2001 From: Mozi <29089388+pzhlkj6612@users.noreply.github.com> Date: Tue, 12 Mar 2024 05:06:28 +0000 Subject: [PATCH] [ie/niconico] Apply suggestions: info_dict, protocols and downloaders - Use "downloader_options" to pass options used by the downloader. - Combine the two downloaders into one. - Don't inherit from "HlsFD". Co-authored-by: pukkandan --- yt_dlp/downloader/__init__.py | 3 +-- yt_dlp/downloader/niconico.py | 37 +++++++++++++---------------------- yt_dlp/extractor/niconico.py | 8 +++++--- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/yt_dlp/downloader/__init__.py b/yt_dlp/downloader/__init__.py index 444c34857..9f6385058 100644 --- a/yt_dlp/downloader/__init__.py +++ b/yt_dlp/downloader/__init__.py @@ -30,7 +30,7 @@ from .hls import HlsFD from .http import HttpFD from .ism import IsmFD from .mhtml import MhtmlFD -from .niconico import NiconicoDmcFD, NiconicoLiveFD, NiconicoLiveTimeshiftFD +from .niconico import NiconicoDmcFD, NiconicoLiveFD from .rtmp import RtmpFD from .rtsp import RtspFD from .websocket import WebSocketFragmentFD @@ -51,7 +51,6 @@ PROTOCOL_MAP = { 'mhtml': MhtmlFD, 'niconico_dmc': NiconicoDmcFD, 'm3u8_niconico_live': NiconicoLiveFD, - 'm3u8_niconico_live_timeshift': NiconicoLiveTimeshiftFD, 'fc2_live': FC2LiveFD, 'websocket_frag': WebSocketFragmentFD, 'youtube_live_chat': YoutubeLiveChatFD, diff --git a/yt_dlp/downloader/niconico.py b/yt_dlp/downloader/niconico.py index f50e89ac2..ea00a9a8e 100644 --- a/yt_dlp/downloader/niconico.py +++ b/yt_dlp/downloader/niconico.py @@ -7,7 +7,7 @@ import time from . import get_suitable_downloader from .common import FileDownloader from .external import FFmpegFD -from ..downloader.hls import HlsFD +from ..downloader.fragment import FragmentFD from ..networking import Request from ..networking.exceptions import network_exceptions from ..utils import ( @@ -67,7 +67,10 @@ class NiconicoDmcFD(FileDownloader): return success -class NiconicoLiveBaseFD(FileDownloader): +class NiconicoLiveFD(FragmentFD): + """ Downloads niconico live/timeshift VOD """ + + _PER_FRAGMENT_DOWNLOAD_RATIO = 0.1 _WEBSOCKET_RECONNECT_DELAY = 10 @contextlib.contextmanager @@ -75,8 +78,8 @@ class NiconicoLiveBaseFD(FileDownloader): """ Hold a WebSocket object and release it when leaving """ video_id = info_dict['id'] - live_latency = info_dict['live_latency'] - self.ws = info_dict['__ws'] + live_latency = info_dict['downloader_options']['live_latency'] + self.ws = info_dict['downloader_options']['ws'] self.m3u8_lock = threading.Event() self.m3u8_url = info_dict['manifest_url'] @@ -167,27 +170,15 @@ class NiconicoLiveBaseFD(FileDownloader): self.m3u8_lock.wait() return self.m3u8_url - -class NiconicoLiveFD(NiconicoLiveBaseFD): - """ Downloads niconico live without being stopped """ - - def real_download(self, filename, info_dict): - with self._ws_context(info_dict): - new_info_dict = info_dict.copy() - new_info_dict.update({ - 'protocol': 'm3u8', - }) - - return FFmpegFD(self.ydl, self.params or {}).download(filename, new_info_dict) - - -class NiconicoLiveTimeshiftFD(NiconicoLiveBaseFD, HlsFD): - """ Downloads niconico live timeshift VOD """ - - _PER_FRAGMENT_DOWNLOAD_RATIO = 0.1 - def real_download(self, filename, info_dict): with self._ws_context(info_dict) as ws_context: + # live + if info_dict.get('is_live'): + info_dict = info_dict.copy() + info_dict['protocol'] = 'm3u8' + return FFmpegFD(self.ydl, self.params or {}).download(filename, info_dict) + + # timeshift VOD from ..extractor.niconico import NiconicoIE ie = NiconicoIE(self.ydl) diff --git a/yt_dlp/extractor/niconico.py b/yt_dlp/extractor/niconico.py index 805b40193..cba25b532 100644 --- a/yt_dlp/extractor/niconico.py +++ b/yt_dlp/extractor/niconico.py @@ -1002,7 +1002,7 @@ class NiconicoLiveIE(InfoExtractor): for fmt, q in zip(formats, reversed(qualities[1:])): fmt.update({ 'format_id': q, - 'protocol': 'm3u8_niconico_live' if is_live else 'm3u8_niconico_live_timeshift', + 'protocol': 'm3u8_niconico_live', }) yield fmt @@ -1075,7 +1075,9 @@ class NiconicoLiveIE(InfoExtractor): 'live_status': live_status, 'thumbnails': thumbnails, 'formats': [*self._yield_formats(ws, video_id, latency, live_status == 'is_live')] if ws else None, - 'live_latency': latency, 'http_headers': headers, - '__ws': ws, + 'downloader_options': { + 'live_latency': latency, + 'ws': ws, + }, }