¿Cómo puedo descargar videos de youtube usando Python?

Nov 02 2020

escribo un sencillo programa de descarga de youtube

este es el código:

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

cuando ejecuto este código, recibo este 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'

Respuestas

2 SachinRajput Nov 02 2020 at 11:12

Pruebe esto, funcionó para mí. También podría resolver su problema.

pip uninstall pytube
pip uninstall pytube3
python -m pip install git+https://github.com/nficano/pytube
2 Ragnar Nov 02 2020 at 13:01

Esto es lo que he usado recientemente.

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)