Download YouTube Videos Using Python

Posted
In a few simple steps, I'll show you how to download YouTube videos using a Python script.
Share
Download YouTube Videos Using Python by MattyAlanEstock

I'm tired of using sketchy third-party websites to snag a YouTube video for watching offline. So I figured, there's probably a cool way to do this with programming. Well kind viewer, there is! If you were ever interested in downloading your favorite YouTuber's videos, here's a fun way to do it. All this Python script needs is a YouTube video URL and you're set.

If you're familiar with writing Python, this mini-tutorial uses a library called pytube. You can install pytube for Python by running this command:

python -m pip install pytube

With pytube installed, now you can save this code to a .py file and run it:

# https://pytube.io/en/latest/user/quickstart.html

from pytube import YouTube

your_video_url = 'https://www.youtube.com/watch?v=uM4NvQidc1s'

yt = YouTube(your_video_url)

yt.streams.get_highest_resolution().download()

This very simplified demonstration downloads the highest quality video with audio available (typically 720p). Take a look at the pytube documentation if you want higher quality, or are looking to download audio & video separately. Pytube is pretty neat!