(python) Youtube-dl 连接错误处理
(python) Youtube-dl Connection Error Handling
我是 运行 在 python
中使用 youtube-dl 下载视频的脚本
def dl_videos():
while True:
try:
while True:
ydl_opts = {
'ignoreerrors': 'True',
'download_archive': 'archive',
'format': 'bestaudio/best',
'outtmpl': 'mp3downloads/%(playlist_title)s/%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '193',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
with open ('PlaylistOnly', 'r') as r:
d = r.readlines()
for line in d:
ydl.download([line])
time.sleep(24.0 * 60.0 * 60.0)
except(FileNotFoundError):
time.sleep(5)
continue
dl_videos()
但是,我希望此脚本能够处理连接中断。所以当我在程序中间切断连接时,它会因这个错误而彻底崩溃:
[0;31mERROR:[0m Unable to download webpage: <urlopen error [Errno -3] Temporary failure in name resolution> (caused by URLError(gaierror(-3, 'Temporary failure in name resolution')))
注意:如果在线连接丢失,只会在 youtube-dl 过程的某个点出现错误
我希望程序稍等片刻,然后重试该模块,但我完全不确定如何处理此错误。 Idk 如果这是我可以处理异常的特定错误类型。任何帮助表示赞赏
-编辑-
(解决方案)
def dl_videos():
while True:
try:
while True:
ydl_opts = {
'ignoreerrors': 'True',
'download_archive': 'archive',
'format': 'bestaudio/best',
'outtmpl': 'mp3downloads/%(playlist_title)s/%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '193',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
with open ('PlaylistOnly', 'r') as r:
d = r.readlines()
for line in d:
ydl.download([line])
#Checks if there's a connection to youtube.com, if there's none it loops back before the "freeze" which my dumb a didnt realize was just the next time.sleep function
if assets.connect() == False:
time.sleep(10)
continue
time.sleep(24.0 * 60.0 * 60.0)
except(FileNotFoundError):
time.sleep(5)
continue
dl_videos()
尝试更新 youtube-dl
每当我遇到 运行 奇怪的连接问题时,都会通过更新来解决。
另见
https://github.com/ytdl-org/youtube-dl/issues/618
https://github.com/ytdl-org/youtube-dl/issues/7586
此外,如果您尝试同时下载多个 link,不妨尝试将其分成更小的块。是不是每次都出问题的link?有没有下载成功?
/// 编辑
经过您的编辑,我更好地理解了您的问题。
对于基本的尝试和排除解决方案,您可以
for line in d:
try:
ydl.download([line])
except:
print(f"error with {line}")
我是 运行 在 python
中使用 youtube-dl 下载视频的脚本def dl_videos():
while True:
try:
while True:
ydl_opts = {
'ignoreerrors': 'True',
'download_archive': 'archive',
'format': 'bestaudio/best',
'outtmpl': 'mp3downloads/%(playlist_title)s/%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '193',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
with open ('PlaylistOnly', 'r') as r:
d = r.readlines()
for line in d:
ydl.download([line])
time.sleep(24.0 * 60.0 * 60.0)
except(FileNotFoundError):
time.sleep(5)
continue
dl_videos()
但是,我希望此脚本能够处理连接中断。所以当我在程序中间切断连接时,它会因这个错误而彻底崩溃:
[0;31mERROR:[0m Unable to download webpage: <urlopen error [Errno -3] Temporary failure in name resolution> (caused by URLError(gaierror(-3, 'Temporary failure in name resolution')))
注意:如果在线连接丢失,只会在 youtube-dl 过程的某个点出现错误
我希望程序稍等片刻,然后重试该模块,但我完全不确定如何处理此错误。 Idk 如果这是我可以处理异常的特定错误类型。任何帮助表示赞赏
-编辑- (解决方案)
def dl_videos():
while True:
try:
while True:
ydl_opts = {
'ignoreerrors': 'True',
'download_archive': 'archive',
'format': 'bestaudio/best',
'outtmpl': 'mp3downloads/%(playlist_title)s/%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '193',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
with open ('PlaylistOnly', 'r') as r:
d = r.readlines()
for line in d:
ydl.download([line])
#Checks if there's a connection to youtube.com, if there's none it loops back before the "freeze" which my dumb a didnt realize was just the next time.sleep function
if assets.connect() == False:
time.sleep(10)
continue
time.sleep(24.0 * 60.0 * 60.0)
except(FileNotFoundError):
time.sleep(5)
continue
dl_videos()
尝试更新 youtube-dl
每当我遇到 运行 奇怪的连接问题时,都会通过更新来解决。
另见
https://github.com/ytdl-org/youtube-dl/issues/618
https://github.com/ytdl-org/youtube-dl/issues/7586
此外,如果您尝试同时下载多个 link,不妨尝试将其分成更小的块。是不是每次都出问题的link?有没有下载成功?
/// 编辑
经过您的编辑,我更好地理解了您的问题。 对于基本的尝试和排除解决方案,您可以
for line in d:
try:
ydl.download([line])
except:
print(f"error with {line}")