当我尝试使用 pytube 仅下载 Youtube 视频的音频时,为什么总是出现错误?

Why do I keep getting an error when I try to download only the audio of a Youtube video using pytube?

我正在尝试使用 pytube 下载视频的音频,但我一直收到我不明白的错误。

我的代码是:

from pytube import YouTube
lista_de_videos = ["https://www.youtube.com/watch?v=iYYRH4apXDo"]

for video in lista_de_videos:
    yt = YouTube(video)
    yt.streams.get_audio_only().download('/Users/applemacosx/Downloads')

我不断收到的错误是:

/Users/applemacosx/PycharmProjects/pythonProject6/venv/bin/python /Users/applemacosx/PycharmProjects/pythonProject6/download.py
Traceback (most recent call last):
  File "/Users/applemacosx/PycharmProjects/pythonProject6/venv/lib/python3.8/site-packages/pytube/extract.py", line 288, in apply_descrambler
    stream_data[key] = [
  File "/Users/applemacosx/PycharmProjects/pythonProject6/venv/lib/python3.8/site-packages/pytube/extract.py", line 290, in <listcomp>
    "url": format_item["url"],
KeyError: 'url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/applemacosx/PycharmProjects/pythonProject6/download.py", line 84, in <module>
    yt = YouTube(video)
  File "/Users/applemacosx/PycharmProjects/pythonProject6/venv/lib/python3.8/site-packages/pytube/__main__.py", line 92, in __init__
    self.descramble()
  File "/Users/applemacosx/PycharmProjects/pythonProject6/venv/lib/python3.8/site-packages/pytube/__main__.py", line 132, in descramble
    apply_descrambler(self.player_config_args, fmt)
  File "/Users/applemacosx/PycharmProjects/pythonProject6/venv/lib/python3.8/site-packages/pytube/extract.py", line 300, in apply_descrambler
    cipher_url = [
  File "/Users/applemacosx/PycharmProjects/pythonProject6/venv/lib/python3.8/site-packages/pytube/extract.py", line 301, in <listcomp>
    parse_qs(formats[i]["cipher"]) for i, data in enumerate(formats)
KeyError: 'cipher'

Process finished with exit code 1

有人可以向我解释我做错了什么吗?

这样试试:

from pytube import YouTube
yt=YouTube(link)
t=yt.streams.filter(only_audio=True).all()
t[0].download(/path)

参考:- Download audio from YouTube using pytube

您要做的第一件事是:

pip3 uninstall pytube
`pip3 install pytube3`

你也可以试试pytubex

找遍了这个。这是我发现的唯一有解决方案的线程。这个问题似乎是因为pytube3文件夹中的两个文件,即extract.py和ciphers.py。您需要更改其中的一些代码。访问 https://github.com/nficano/pytube/issues/641

keyerror:'cipher'问题很久以前就在 pytube 存储库中修复了。您只需要从存储库重新安装 pytube link

pip uninstall pytube
pip install git+https://github.com/nficano/pytube