[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 <pukkandan.ytdlp@gmail.com>
This commit is contained in:
Mozi 2024-03-12 05:06:28 +00:00
parent 7398a7cb2f
commit 972a2d51ad
3 changed files with 20 additions and 28 deletions

View File

@ -30,7 +30,7 @@ from .hls import HlsFD
from .http import HttpFD from .http import HttpFD
from .ism import IsmFD from .ism import IsmFD
from .mhtml import MhtmlFD from .mhtml import MhtmlFD
from .niconico import NiconicoDmcFD, NiconicoLiveFD, NiconicoLiveTimeshiftFD from .niconico import NiconicoDmcFD, NiconicoLiveFD
from .rtmp import RtmpFD from .rtmp import RtmpFD
from .rtsp import RtspFD from .rtsp import RtspFD
from .websocket import WebSocketFragmentFD from .websocket import WebSocketFragmentFD
@ -51,7 +51,6 @@ PROTOCOL_MAP = {
'mhtml': MhtmlFD, 'mhtml': MhtmlFD,
'niconico_dmc': NiconicoDmcFD, 'niconico_dmc': NiconicoDmcFD,
'm3u8_niconico_live': NiconicoLiveFD, 'm3u8_niconico_live': NiconicoLiveFD,
'm3u8_niconico_live_timeshift': NiconicoLiveTimeshiftFD,
'fc2_live': FC2LiveFD, 'fc2_live': FC2LiveFD,
'websocket_frag': WebSocketFragmentFD, 'websocket_frag': WebSocketFragmentFD,
'youtube_live_chat': YoutubeLiveChatFD, 'youtube_live_chat': YoutubeLiveChatFD,

View File

@ -7,7 +7,7 @@ import time
from . import get_suitable_downloader from . import get_suitable_downloader
from .common import FileDownloader from .common import FileDownloader
from .external import FFmpegFD from .external import FFmpegFD
from ..downloader.hls import HlsFD from ..downloader.fragment import FragmentFD
from ..networking import Request from ..networking import Request
from ..networking.exceptions import network_exceptions from ..networking.exceptions import network_exceptions
from ..utils import ( from ..utils import (
@ -67,7 +67,10 @@ class NiconicoDmcFD(FileDownloader):
return success return success
class NiconicoLiveBaseFD(FileDownloader): class NiconicoLiveFD(FragmentFD):
""" Downloads niconico live/timeshift VOD """
_PER_FRAGMENT_DOWNLOAD_RATIO = 0.1
_WEBSOCKET_RECONNECT_DELAY = 10 _WEBSOCKET_RECONNECT_DELAY = 10
@contextlib.contextmanager @contextlib.contextmanager
@ -75,8 +78,8 @@ class NiconicoLiveBaseFD(FileDownloader):
""" Hold a WebSocket object and release it when leaving """ """ Hold a WebSocket object and release it when leaving """
video_id = info_dict['id'] video_id = info_dict['id']
live_latency = info_dict['live_latency'] live_latency = info_dict['downloader_options']['live_latency']
self.ws = info_dict['__ws'] self.ws = info_dict['downloader_options']['ws']
self.m3u8_lock = threading.Event() self.m3u8_lock = threading.Event()
self.m3u8_url = info_dict['manifest_url'] self.m3u8_url = info_dict['manifest_url']
@ -167,27 +170,15 @@ class NiconicoLiveBaseFD(FileDownloader):
self.m3u8_lock.wait() self.m3u8_lock.wait()
return self.m3u8_url 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): def real_download(self, filename, info_dict):
with self._ws_context(info_dict) as ws_context: 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 from ..extractor.niconico import NiconicoIE
ie = NiconicoIE(self.ydl) ie = NiconicoIE(self.ydl)

View File

@ -1002,7 +1002,7 @@ class NiconicoLiveIE(InfoExtractor):
for fmt, q in zip(formats, reversed(qualities[1:])): for fmt, q in zip(formats, reversed(qualities[1:])):
fmt.update({ fmt.update({
'format_id': q, 'format_id': q,
'protocol': 'm3u8_niconico_live' if is_live else 'm3u8_niconico_live_timeshift', 'protocol': 'm3u8_niconico_live',
}) })
yield fmt yield fmt
@ -1075,7 +1075,9 @@ class NiconicoLiveIE(InfoExtractor):
'live_status': live_status, 'live_status': live_status,
'thumbnails': thumbnails, 'thumbnails': thumbnails,
'formats': [*self._yield_formats(ws, video_id, latency, live_status == 'is_live')] if ws else None, 'formats': [*self._yield_formats(ws, video_id, latency, live_status == 'is_live')] if ws else None,
'live_latency': latency,
'http_headers': headers, 'http_headers': headers,
'__ws': ws, 'downloader_options': {
'live_latency': latency,
'ws': ws,
},
} }