如何将允许的服务列表传递给 youtube-dl?

How to pass a list of allowed services to youtube-dl?

我希望 youtube-dl 只能从允许的服务列表中下载内容。喜欢 ['youtube','twitch'].

还有什么方法可以设置自定义错误消息吗?

您可以对 youtube_dl.extractor._ALL_CLASSES 进行 monkeypatch,因此 youtube_dl 不会知道除您指定的任何其他提取器。

import youtube_dl

def get_list_of_extractors(extractor_modules):
    extractors = []
    for module in extractor_modules:
        list_of_extractors = [
            getattr(module, name) for name in dir(module) 
            if name.endswith('IE') and name != 'GenericIE' and name.find("Base") == -1
        ]
        extractors = extractors + list_of_extractors
    return extractors

list_of_extractors = get_list_of_extractors([youtube_dl.extractor.youtube, youtube_dl.extractor.tiktok])
youtube_dl.extractor._ALL_CLASSES = list_of_extractors

ydl = youtube_dl.YoutubeDL({})

ydl.extract_info(
    'https://www.youtube.com/watch?v=BaW_jenozKc',
    download=False # We do not need to download
)

# causes youtube_dl.utils.DownloadError, because instagram extractor is not in the list
ydl.extract_info(
    'https://instagram.com/p/aye83DjauH',
    download=False # We do not need to download
)