为什么我的 YouTube 视频下载器只下载了一些视频,而对于其他视频,它显示 URL 和 cipher 之类的键盘错误?

Why my YouTube video downloader only downloads some videos and for other videos it shows keyerror like URL and cipher?

我正在尝试使用 Python pytube3 制作 YouTube 视频下载器,但它无法下载所有视频。有些视频很容易下载,但有些视频不会下载,而不是下载它显示错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 297, in apply_descrambler
    for format_item in formats
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 297, in <listcomp>
    for format_item in formats
KeyError: 'url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\tarun\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/tarun/PycharmProjects/YTDownloader/YTD.py", line 15, in video_download
    my_video = YouTube(input_user)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\__main__.py", line 92, in __init__
    self.descramble()
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\__main__.py", line 132, in descramble
    apply_descrambler(self.player_config_args, fmt)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 301, in apply_descrambler
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
  File "C:\Users\tarun\PycharmProjects\YTDownloader\venv\lib\site-packages\pytube\extract.py", line 301, in <listcomp>
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
KeyError: 'cipher'

这是 pytube3 的问题,我相信到目前为止他们还没有提交修复。 Here is the link to the issue on github

这是来自 pytube 的文件 extract.py 中的一个错误。

  1. 转到安装包的位置。如果你不知道在哪里,运行命令

    pip show pytube3
    

    它会给你这样的东西:

我们可以看到Location: c:\users\tiago\anaconda3\lib\site-packages.

  1. 转到该位置,打开文件夹 pytube 和文件 extract.py

  1. 在文件中,行号。 306 或 301,你会发现 parse_qs(formats[i]["cipher"])。如果是,则将 "cipher" 更改为 "signatureCipher"(确保 'C' 为大写)。

    所以,您最初会

     cipher_url = [
                     parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
                 ]
    

    但应该是

     cipher_url = [
                     parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
                 ]
    

  1. 运行 下面的脚本可以正常运行

     # -*- coding: utf-8 -*-
     """
     Created on Mon Jun 15 12:21:49 2020
    
     @author: tiago
     """
     from pytube import YouTube
    
     video_url = "https://youtu.be/gp5tziO5lXg" # YouTube video URL
     youtube = YouTube(video_url)
     video = youtube.streams.first()
     video.download("C:/Users/tiago/Desktop/videos/") # Path where to store the video
    

然后您会在该文件夹中看到下载的视频

如果您遇到 Keyerror:"cipher" 错误,请转到打开 pytube 的位置 extract.py,然后在第 301 行,您会得到这个

cipher_url = [
                parse_qs(formats[i]["Cipher"]) for i, data in enumerate(formats)
            ]

现在将此行编辑为

cipher_url = [
                parse_qs(formats[i]["signatureCipher"]) for i, data in enumerate(formats)
            ]

保存更改并....大功告成。 现在尝试下载视频,您现在不会收到任何错误。

  1. 只需转到 pytube\extract.py(在 pytube 库中)文件。 文件的路径将是(Windows):C:\ProgramData\Anaconda3\lib\site-packages\pytube\extract.py

  2. 打开 extract.py 文件并搜索行:

    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)

  3. 现在将“cipher”替换为“signatureCipher”。

  4. 保存。

  5. 现在再次运行你的代码

在 pytube 库中,有一个 extract.py 文件 在此文件中将密码更改为 signaturecipher