pytube urllib.error.HTTPError:HTTP 错误 404:未找到

时间:2021-06-15 17:09:13

标签: python pytube

我尝试创建一个脚本来下载 Youtube 视频。有时它可以工作,但有时会出现“HTTP 错误 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

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

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

1 个答案:

答案 0 :(得分:0)

这是带有 pytube 的 forking their audio before they join the conference using <Start><Stream>

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

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