如何隐藏来自 youtube-dl / yt-dlp 的错误消息?

How to hide error message from youtube-dl / yt-dlp?

我正在使用 yt-dlp(在 Python 中)从 Twitch 视频中提取信息。

如果我尝试从不存在的视频或私人视频中提取信息,我会遇到异常,这是预期的行为。 但是如果我设置为“安静”模式并且如果我捕获了潜在的异常,我仍然会记录错误。 这是代码:

import yt_dlp as youtube_dl
from yt_dlp.utils import DownloadError


url = "https://www.twitch.tv/videos/1410795876"

options = {
    "quiet": True,
    "format": "bestaudio/worst",
}

with youtube_dl.YoutubeDL(options) as ydl:
    try:
        info = ydl.extract_info(url, download=False)
    except DownloadError:
        print("An exception has been caught")

使用此脚本时,输出如下:

ERROR: [twitch:vod] 1410795876: Failed to download m3u8 information: HTTP Error 403: Forbidden (caused by <HTTPError 403: 'Forbidden'>); please report this issue on  https://github.com/yt-dlp/yt-dlp , filling out the "Broken site" issue template properly. Confirm you are on the latest version using -U (caused by <HTTPError 403: 'Forbidden'>); please report this issue on  https://github.com/yt-dlp/yt-dlp , filling out the "Broken site" issue template properly. Confirm you are on the latest version using -U
An exception has been caught

有没有办法隐藏这个错误日志?如果没有,有没有办法使用 yt-dlp 检查视频是否可以访问,这样我就不会在视频不可用时调用 extract_info? 谢谢。

我想隐藏所有日志记录错误的最简单方法是实现您自己的日志处理程序。考虑对您的代码进行以下更改,我相信您会从这里弄清楚如何实现您想要的:

import yt_dlp as youtube_dl
from yt_dlp.utils import DownloadError


url = "https://www.twitch.tv/videos/1410795876"

class loggerOutputs:
    def error(msg):
        print("Captured Error: "+msg)
    def warning(msg):
        print("Captured Warning: "+msg)
    def debug(msg):
        print("Captured Log: "+msg)


options = {
    "quiet": True,
    "format": "bestaudio/worst",
    "logger": loggerOutputs,
}

with youtube_dl.YoutubeDL(options) as ydl:
    try:
        info = ydl.extract_info(url, download=False)
    except DownloadError:
        print("An exception has been caught")
 

在以下示例中,他们通过实施“FakeLogger”来禁用日志记录:https://github.com/ytdl-org/youtube-dl/blob/master/test/test_http.py