Download a video from Youtube

Download a video from Youtube with Python is very simple, just a couple of lines of code and a module called pytube to install.

Install pytube

Go in the cmd or powershell (in windows) and install pytube like this:

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.

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.

You can also get the subtitles from a video

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.