2022-04-11 16:32:57 -06:00
|
|
|
from ..utils import NO_DEFAULT, determine_protocol
|
2021-02-08 09:46:01 -07:00
|
|
|
|
|
|
|
|
2021-07-31 04:53:54 -06:00
|
|
|
def get_suitable_downloader(info_dict, params={}, default=NO_DEFAULT, protocol=None, to_stdout=False):
|
2021-07-31 04:51:01 -06:00
|
|
|
info_dict['protocol'] = determine_protocol(info_dict)
|
2021-02-08 09:46:01 -07:00
|
|
|
info_copy = info_dict.copy()
|
2021-07-31 04:53:54 -06:00
|
|
|
info_copy['to_stdout'] = to_stdout
|
2021-10-12 05:20:04 -06:00
|
|
|
|
2021-12-19 23:06:46 -07:00
|
|
|
protocols = (protocol or info_copy['protocol']).split('+')
|
|
|
|
downloaders = [_get_suitable_downloader(info_copy, proto, params, default) for proto in protocols]
|
|
|
|
|
2021-10-12 05:20:04 -06:00
|
|
|
if set(downloaders) == {FFmpegFD} and FFmpegFD.can_merge_formats(info_copy, params):
|
|
|
|
return FFmpegFD
|
2021-12-19 23:06:46 -07:00
|
|
|
elif (set(downloaders) == {DashSegmentsFD}
|
|
|
|
and not (to_stdout and len(protocols) > 1)
|
|
|
|
and set(protocols) == {'http_dash_segments_generator'}):
|
|
|
|
return DashSegmentsFD
|
2021-10-12 05:20:04 -06:00
|
|
|
elif len(downloaders) == 1:
|
|
|
|
return downloaders[0]
|
|
|
|
return None
|
2021-02-08 09:46:01 -07:00
|
|
|
|
|
|
|
|
2021-07-31 04:51:01 -06:00
|
|
|
# Some of these require get_suitable_downloader
|
2013-09-23 09:59:27 -06:00
|
|
|
from .common import FileDownloader
|
2021-02-08 09:46:01 -07:00
|
|
|
from .dash import DashSegmentsFD
|
2022-04-11 16:32:57 -06:00
|
|
|
from .external import FFmpegFD, get_external_downloader
|
2015-01-23 17:38:48 -07:00
|
|
|
from .f4m import F4mFD
|
2022-02-24 19:16:23 -07:00
|
|
|
from .fc2 import FC2LiveFD
|
2013-09-23 09:59:27 -06:00
|
|
|
from .hls import HlsFD
|
|
|
|
from .http import HttpFD
|
2016-10-19 09:22:40 -06:00
|
|
|
from .ism import IsmFD
|
2021-05-23 10:34:49 -06:00
|
|
|
from .mhtml import MhtmlFD
|
2023-05-29 03:35:10 -06:00
|
|
|
from .niconico import NiconicoDmcFD, NiconicoLiveFD
|
2022-04-11 16:32:57 -06:00
|
|
|
from .rtmp import RtmpFD
|
|
|
|
from .rtsp import RtspFD
|
2021-06-21 11:23:17 -06:00
|
|
|
from .websocket import WebSocketFragmentFD
|
2021-06-22 18:12:39 -06:00
|
|
|
from .youtube_live_chat import YoutubeLiveChatFD
|
2013-09-23 09:59:27 -06:00
|
|
|
|
2015-01-23 15:50:31 -07:00
|
|
|
PROTOCOL_MAP = {
|
|
|
|
'rtmp': RtmpFD,
|
2021-11-28 14:22:52 -07:00
|
|
|
'rtmpe': RtmpFD,
|
2021-05-02 08:13:37 -06:00
|
|
|
'rtmp_ffmpeg': FFmpegFD,
|
2016-02-19 11:29:24 -07:00
|
|
|
'm3u8_native': HlsFD,
|
|
|
|
'm3u8': FFmpegFD,
|
2016-03-13 08:24:02 -06:00
|
|
|
'mms': RtspFD,
|
|
|
|
'rtsp': RtspFD,
|
2015-01-23 15:50:31 -07:00
|
|
|
'f4m': F4mFD,
|
2015-06-04 08:27:29 -06:00
|
|
|
'http_dash_segments': DashSegmentsFD,
|
2021-12-19 23:06:46 -07:00
|
|
|
'http_dash_segments_generator': DashSegmentsFD,
|
2016-10-19 09:22:40 -06:00
|
|
|
'ism': IsmFD,
|
2021-05-23 10:34:49 -06:00
|
|
|
'mhtml': MhtmlFD,
|
2021-02-09 23:45:20 -07:00
|
|
|
'niconico_dmc': NiconicoDmcFD,
|
2023-05-29 03:35:10 -06:00
|
|
|
'niconico_live': NiconicoLiveFD,
|
2022-02-24 19:16:23 -07:00
|
|
|
'fc2_live': FC2LiveFD,
|
2021-06-21 11:23:17 -06:00
|
|
|
'websocket_frag': WebSocketFragmentFD,
|
2021-06-22 18:12:39 -06:00
|
|
|
'youtube_live_chat': YoutubeLiveChatFD,
|
|
|
|
'youtube_live_chat_replay': YoutubeLiveChatFD,
|
2015-01-23 15:50:31 -07:00
|
|
|
}
|
2014-01-25 04:02:43 -07:00
|
|
|
|
2015-01-23 15:50:31 -07:00
|
|
|
|
2021-04-10 09:08:33 -06:00
|
|
|
def shorten_protocol_name(proto, simplify=False):
|
|
|
|
short_protocol_names = {
|
2022-06-27 23:10:54 -06:00
|
|
|
'm3u8_native': 'm3u8',
|
|
|
|
'm3u8': 'm3u8F',
|
|
|
|
'rtmp_ffmpeg': 'rtmpF',
|
2021-04-10 09:08:33 -06:00
|
|
|
'http_dash_segments': 'dash',
|
2022-06-27 23:10:54 -06:00
|
|
|
'http_dash_segments_generator': 'dashG',
|
2021-04-10 09:08:33 -06:00
|
|
|
'niconico_dmc': 'dmc',
|
2021-06-21 11:23:17 -06:00
|
|
|
'websocket_frag': 'WSfrag',
|
2021-04-10 09:08:33 -06:00
|
|
|
}
|
|
|
|
if simplify:
|
|
|
|
short_protocol_names.update({
|
|
|
|
'https': 'http',
|
|
|
|
'ftps': 'ftp',
|
2022-06-27 23:10:54 -06:00
|
|
|
'm3u8': 'm3u8', # Reverse above m3u8 mapping
|
2021-04-10 09:08:33 -06:00
|
|
|
'm3u8_native': 'm3u8',
|
2021-12-19 23:06:46 -07:00
|
|
|
'http_dash_segments_generator': 'dash',
|
2021-05-02 08:13:37 -06:00
|
|
|
'rtmp_ffmpeg': 'rtmp',
|
2021-04-10 09:08:33 -06:00
|
|
|
'm3u8_frag_urls': 'm3u8',
|
|
|
|
'dash_frag_urls': 'dash',
|
|
|
|
})
|
|
|
|
return short_protocol_names.get(proto, proto)
|
|
|
|
|
|
|
|
|
2021-10-12 05:20:04 -06:00
|
|
|
def _get_suitable_downloader(info_dict, protocol, params, default):
|
2013-09-23 09:59:27 -06:00
|
|
|
"""Get the downloader class that can handle the info dict."""
|
2021-07-31 04:51:01 -06:00
|
|
|
if default is NO_DEFAULT:
|
|
|
|
default = HttpFD
|
2015-01-23 15:50:31 -07:00
|
|
|
|
2022-06-06 14:13:50 -06:00
|
|
|
if (info_dict.get('section_start') or info_dict.get('section_end')) and FFmpegFD.can_download(info_dict):
|
|
|
|
return FFmpegFD
|
2016-02-19 11:29:24 -07:00
|
|
|
|
2021-10-12 05:20:04 -06:00
|
|
|
info_dict['protocol'] = protocol
|
2021-04-10 09:08:33 -06:00
|
|
|
downloaders = params.get('external_downloader')
|
|
|
|
external_downloader = (
|
2022-05-26 17:06:23 -06:00
|
|
|
downloaders if isinstance(downloaders, str) or downloaders is None
|
2021-04-10 09:08:33 -06:00
|
|
|
else downloaders.get(shorten_protocol_name(protocol, True), downloaders.get('default')))
|
|
|
|
|
2021-07-31 04:53:54 -06:00
|
|
|
if external_downloader is None:
|
|
|
|
if info_dict['to_stdout'] and FFmpegFD.can_merge_formats(info_dict, params):
|
|
|
|
return FFmpegFD
|
|
|
|
elif external_downloader.lower() != 'native':
|
2015-01-23 17:38:48 -07:00
|
|
|
ed = get_external_downloader(external_downloader)
|
2021-02-27 04:22:27 -07:00
|
|
|
if ed.can_download(info_dict, external_downloader):
|
2015-01-23 17:38:48 -07:00
|
|
|
return ed
|
|
|
|
|
2021-08-23 18:12:45 -06:00
|
|
|
if protocol == 'http_dash_segments':
|
2021-08-23 18:51:33 -06:00
|
|
|
if info_dict.get('is_live') and (external_downloader or '').lower() != 'native':
|
2021-08-23 18:12:45 -06:00
|
|
|
return FFmpegFD
|
|
|
|
|
2021-04-13 22:27:48 -06:00
|
|
|
if protocol in ('m3u8', 'm3u8_native'):
|
2021-02-08 09:46:01 -07:00
|
|
|
if info_dict.get('is_live'):
|
|
|
|
return FFmpegFD
|
2021-07-31 04:53:54 -06:00
|
|
|
elif (external_downloader or '').lower() == 'native':
|
2021-04-10 09:08:33 -06:00
|
|
|
return HlsFD
|
2022-02-10 23:39:03 -07:00
|
|
|
elif protocol == 'm3u8_native' and get_suitable_downloader(
|
2021-07-31 04:53:54 -06:00
|
|
|
info_dict, params, None, protocol='m3u8_frag_urls', to_stdout=info_dict['to_stdout']):
|
2021-02-08 09:46:01 -07:00
|
|
|
return HlsFD
|
|
|
|
elif params.get('hls_prefer_native') is True:
|
|
|
|
return HlsFD
|
|
|
|
elif params.get('hls_prefer_native') is False:
|
|
|
|
return FFmpegFD
|
2016-04-21 11:02:17 -06:00
|
|
|
|
2021-02-08 09:46:01 -07:00
|
|
|
return PROTOCOL_MAP.get(protocol, default)
|
2015-01-23 15:50:31 -07:00
|
|
|
|
2014-11-23 14:25:12 -07:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'FileDownloader',
|
2021-04-10 09:08:33 -06:00
|
|
|
'get_suitable_downloader',
|
|
|
|
'shorten_protocol_name',
|
2014-11-23 14:25:12 -07:00
|
|
|
]
|