pytube urllib.error.HTTPError: HTTP Error 404: Not Found

pytube urllib.error.HTTPError: HTTP Error 404: Not Found

我尝试创建一个脚本来下载 Youtube 视频。有时它可以工作,但有时会出现 'HTTP Error 404' 错误。下面是代码:

/*** A001MainMenu.py ***/

from C001Youtube import C001_YouTube

vlink = input("Enter the link: ")
fer = "D:\ABC\Youtube"

video = C001_YouTube(vlink, fer, None)
C001_YouTube.DVideo(video, vlink, fer, None)

/*** C001YouTube.py ***/

from pytube import YouTube
from pytube import extract
import ffmpeg

class C001_YouTube:

    def __init__(self, video_link, folder, maxres):
        self.video_link = video_link
        self.folder = folder
        self.maxres = maxres

    def checkfilename(self, filename):
        deletechars = "\"" + '"' + "\" + "/:*?<>|"
        print(deletechars)
        for c in deletechars:
            filename = filename.replace(c, '')
        return filename

    def setfilename(self, video_link):
        filename = extract.video_id(video_link) + "_" + YouTube(video_link).title
        fname = self.checkfilename(filename)
        return fname

    def DVideo(self, video_link, folder, maxres=None):
        video_name = self.setfilename(video_link)
        print(video_name)
        if maxres is None:
            print("Video Started 2")
            video_file = YouTube(self.video_link).streams.order_by('resolution').desc().first().download()
            print(" Video Done 1")

        else:
            print("Video Started 3")
            video_file = YouTube(self.video_link).streams.filter(res=maxres).order_by('resolution').desc().first().download()
            print("Video Done 2")

        print("Audio Started")
        audio_file = YouTube(self.video_link).streams.filter(only_audio=True).order_by('abr').desc().first().download(filename_prefix="audio_")
        print("Audio Done")

        source_audio = ffmpeg.input(audio_file)
        source_video = ffmpeg.input(video_file)

        print("Concatenation Started")
        ffmpeg.concat(source_video, source_audio, v=1, a=1).output(f"{folder}\{video_name}.mp4").run()
        print("Concatenation Done")

        return None

下面是有效的示例视频 link:https://www.youtube.com/watch?v=bH7dRlbEDJY 以下是无效的示例视频 link(错误 404):https://www.youtube.com/watch?v=DjbhQJhXfs8

如果有人能帮助我理解为什么有时代码有效而有时无效,我将不胜感激。提前致谢。

这是 a known bug 与 pytube。

我 运行 上个月第一次遇到这个问题,但后来似乎已经解决,直到现在我又开始看到它了。您只需要等待下一次 pytube 更新,希望问题会得到解决。

您可以尝试通过 运行 此命令升级 pytube pip install pytube --upgrade 每当新更新发布时,这可能会解决问题。