我正在使用yt(在Python中)从to视频中提取信息。
如果我试图从不存在的视频或私人视频中提取信息,我就会得到一个例外,这就是预期的行为。但是,如果我设置为“静默”模式,如果我捕捉到潜在的异常,并且仍然会记录错误。以下是代码:
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检查视频是否是可访问的,这样如果视频不是,我就不会调用extract_info
?谢谢。
发布于 2022-03-02 18:12:22
我想隐藏所有日志错误的最简单的方法是实现您自己的日志处理程序。考虑对代码的以下更改,我相信您将从这里了解如何实现您想要的内容:
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禁用日志记录
https://stackoverflow.com/questions/71326109
复制相似问题