0

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?

Rafael Pinheiro
  • 393
  • 4
  • 16

2 Answers2

1

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
0

Try it this way:

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

Reference:- Download audio from YouTube using pytube

First thing you have to do is:

pip3 uninstall pytube
`pip3 install pytube3`

You can try pytubex as well

After looking all over for this. This is the only thread I found which has solutions. This problem seems to be because of two files in the pytube3 folder, namely, extract.py and ciphers.py. You need to change some code in them. Visit https://github.com/nficano/pytube/issues/641

Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
  • @RafaelPinheiro Updated Answer – Abhishek Rai Oct 27 '20 at 07:55
  • Thanks Abrar. Looks like the problem I am having is similar to other users. I think it has to do with some changes in Youtube. The solutions.I am seeing in github are far from my programming skills, so I think I will wait to see if there is a pytube update in the near future with this problem solved. Thnx for trying to help! https://github.com/nficano/pytube/issues/777 – Rafael Pinheiro Oct 29 '20 at 16:21