我正在编写代码来使用yt_dlp.我的任务是确保如果连接到 URL 出现问题,则会调用错误处理程序。代码如下:
import yt_dlp
output = 'downloads/%(title)s.mp4'
ydl_opts = {
'outtmpl': output,
'quiet': True,
'ignoreerrors': False,
}
video_url = 'https://www.youtube.com/watch?v=a4fVPvDTYVQ'
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
print("Code:", ydl.download([video_url]))
except yt_dlp.utils.DownloadError:
print("Download Error")
except Exception as e:
print("Error:", e)
else:
print("Success!")
就我而言,YouTube 一般无法连接。控制台日志yt_dlp显示以下消息:WARNING: [youtube] Failed to download m3u8 information: HTTPSConnectionPool(host='manifest.googlevideo.com', port=443): Read timed out. (read timeout=20.0)。但是,该函数正常工作并最终进入块而不是错误处理程序else。
如何确保如果出现连接问题,函数ydl.download([video_url])会因错误而崩溃?
