How to fix error downloading video from youtube with pytube

What is pytube?

It is a module for Python that let you download a video with pytube.

In this page I will try to take see how to fix some issues that sometimes occur with this module.

Update: error 01.08.2021

1. uninstall pytube pip unistall pytube

2. Install it from here pip install git+https://github.com/Zeecka/pytube@fix_1060

Updated errors: 10/09/2019

The latest error was a

KeyError: ‘title’

Searching how to fix the error I made this, it’s rough but it works.

Go in the pytube dir, making this

Go in the directory site-packages of your Python installation (see the video if you do not know where it is) and go into the pytube folder. Open with an editor the streams.py file. Go to line 176, comment it:

        #title = self.player_config_args['title']

Go to line 177 and make it like this

        filename = safe_filename("title")

Save it and then test it running this code (you will be asked to input the address of the youtube video (right click on the video to have it) … dowload only no copyighted things).

from pytube import YouTube
url = input("Video url: > ")
yt = YouTube(url)
stream = yt.streams.first()
stream.download() # this will download in your current working Dir

It will work

Updated: 10.9.2019

Install pytube

Install it with pip in the cmd:

pip install pytube

Then open your editor write this code, save it as ytube.py, for example, and run it.

from pytube import YouTube
yt = YouTube("https://youtu.be/1ckBWihtEVc")
print(yt.title)
stream = yt.streams.first()
stream.download() # this will download in your current working Dir

That is all the code you need to download a video from youtube.

Download all the playlist

You can also dowload an entire playlilst

from pytube import Playlist
pl = Playlist("https://www.youtube.com/watch?v=Edpy1szoG80&list=PL153hDY-y1E00uQtCVCVC8xJ25TYX8yPU")
pl.download_all()

Errors

In case you have an error like:

pytube.exceptions.RegexMatchError: regex pattern (yt.akamaized.net/)\s*||\s*.?\sc\s*&&\sd.set([^,]+\s,\s*(?P[a-zA-Z0-9$]+)() had zero matches

continue reading below.

Error downloading (april 2019): how to fix it

Go in the extract.py file in lib/site-packages/pytube/ dir of your python installation and comment some lines of code (116…) about the t variable and you’re done.

Dowload a playlilst

Now the error should be fixed in version 9.5.1. If you want to download a full playlist:

from pytube import Playlist
pl = Playlist("https://www.youtube.com/watch?v=B0AIcf2v_34&list=PL_38rYkBIiiWPXx3C_aIATYnnbCTDByHy")
pl.download_all()
# or if you want to download in a specific directory
pl.download_all('.')

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.