UPDATE 01-14-2025
YouTube changed something that breaks pytube. However, pytubefix by JuanBindez appears to have solved this issue. I've updated this post to reflect the code changes. Enjoy!


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 pytubefix. You can install pytubefix for Python by running this command:

python -m pip install pytubefix

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

# https://pytubefix.readthedocs.io/en/latest/
from pytubefix 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 pytubefix documentation if you want higher quality, or are looking to download audio & video separately. Pytube is pretty neat!