Search code examples
pythonpytube

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


I am trying to download the audio of a video using pytube, but I keep getting an error that I don't understand.

My code is:

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

The error that I keep getting is:

/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

Can someone explain to me what I am doing wrong, please?


Solution

  • the keyerror:'cipher'issue was fixed on the pytube repository a long time ago. You just need to reinstall pytube from the repository link

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