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