1

i write a simple youtube downloader program

this is the code:

YouTube("https://youtu.be/7nXt6oggOlE").streams.first().download()

when i execute this code,i got this error:

File "<stdin>", line 1, in <module>
  File "C:\Users\ajaym\AppData\Local\Programs\Python\Python37\lib\site-packages\pytube\__main__.py", line 91, in __init__
    self.prefetch()
  File "C:\Users\ajaym\AppData\Local\Programs\Python\Python37\lib\site-packages\pytube\__main__.py", line 183, in prefetch
    self.js_url = extract.js_url(self.watch_html)
  File "C:\Users\ajaym\AppData\Local\Programs\Python\Python37\lib\site-packages\pytube\extract.py", line 143, in js_url
    base_js = get_ytplayer_config(html)["assets"]["js"]
KeyError: 'assets'
  • Does this answer your question? [Pytube only works periodically (KeyError: 'assets')](https://stackoverflow.com/questions/64492922/pytube-only-works-periodically-keyerror-assets) – ewong Nov 02 '20 at 03:55

2 Answers2

2

Try this it worked for me.It might resolve your issue as well.

pip uninstall pytube
pip uninstall pytube3
python -m pip install git+https://github.com/nficano/pytube
Sachin Rajput
  • 329
  • 2
  • 13
2

This is what I have used recently

from pytube import YouTube
import os

link = "https://www.youtube.com/watch?v=wl8X-kV-gmU&ab_channel=MixHound"
path = 'C:\\YouTube_download_path'

def downloadYouTube(videourl, path):

    yt = YouTube(videourl)
    yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
    if not os.path.exists(path):
        os.makedirs(path)
    yt.download(path)

downloadYouTube(link, path)
Lawhatre
  • 652
  • 2
  • 16