【问题标题】:(python) Youtube-dl Connection Error Handling(python) Youtube-dl 连接错误处理
【发布时间】:2020-05-27 16:45:28
【问题描述】:

我正在运行一个脚本以在 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 进程的某个时间点出现错误

我希望程序稍等片刻,然后重试该模块,但我完全不确定如何处理此错误。如果这是一个特定的错误类型,我可以处理异常。任何帮助表示赞赏

-编辑- (解决方案)

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()

【问题讨论】:

  • 建议:不用d = r.readlines() for line in d: ydl.download([line]),可以ydl.download(r.readlines())

标签: python python-3.x error-handling youtube-dl


【解决方案1】:

尝试更新 youtube-dl

每当我遇到奇怪的连接问题时,都会通过更新解决。

另外,请参阅
https://github.com/ytdl-org/youtube-dl/issues/618
https://github.com/ytdl-org/youtube-dl/issues/7586

另外,如果您尝试同时下载多个链接,不妨尝试将其分成更小的块。每次都出现问题的链接是否相同?有的下载成功了吗?

/// 编辑

既然您已编辑,我更能理解您的问题。 对于基本的尝试和解决方案,您可以

for line in d:
    try:
        ydl.download([line])
    except:
        print(f"error with {line}")

【讨论】:

  • 哦,我不是说问题是连接随机中断,我切断了互联网来测试脚本,这样我就可以编写它来处理任何实际的连接中断。但是,当它确实下降时,程序会收到此错误。我正在努力解决这个问题。我会尝试在帖子中更好地澄清
  • 对不起,前几次测试似乎有效,但我错了。如果在下载播放列表的过程中断开连接,则会出现错误,但如果在下载播放列表之间断开连接,则根本没有错误。
  • 我刚刚注意到在您的代码的第 3 行有一个 try 语句,但没有对应的 except。也许修复它,然后在代码的不同点添加更多的 try except 子句来尝试捕获错误。另外,您可以导入时间,然后 time.sleep(60) 等待一分钟,例如。
  • 哦,那是我的错,我有一个例外,我忘了在这里添加。对其进行了编辑。但无论如何,我终于意识到问题出在哪里。我误解了我的 time.sleep 功能“冻结”了一天......但无论如何我设法通过在 time.sleep 之前添加一个带有网络检查模块的 if 和 continue 循环来解决这个问题,并且atm 是固定的。但无论如何,我感谢您抽出时间提供帮助!
猜你喜欢
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多