1 min read

How to Download YouTube Videos Using Python in 2022

The easiest way to download a YouTube video using python in 2022 is to use the pytybe python library.
How to Download YouTube Videos Using Python in 2022
Photo by Szabo Viktor / Unsplash

As of writing this post on 2022 the easiest way to download a YouTube video using python is to use the pytybe library. The source code can be found on Github and the official documentation on the library's website pytube.io

There are a various ways to use the library but we'll focus on the two most common ones:

  1. Using a python script
  2. Using the command line (CLI)

Installation

python -m pip install pytube

Using a python script

First we have to import the library. Next, we initialise the YouTube class by passing the video_url as an argument to the class. The last line filters through the multiple available streams and downloads the first one.

This is the easiest setup to get started. After that, we can examine the available streams that YouTube gives us. For more advance usage please refer to the library's documentation.

from pytube import YouTube
video_url = 'https://www.youtube.com/watch?v=fbVlD-0Z6Uk'
yt = YouTube(video_url)
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()

Using the command line

Using the command line (CLI) it's very easy.

Tip: If you are using a virtual environment make sure that your environment is active for the command to work.

pytube https://www.youtube.com/watch?v=fbVlD-0Z6Uk

The end

Congratulations! You made it to the end. It was a simple one :)

At this point you should have a working installation of pytube and you should be able to download any youtube video you want. Please check Youtube's terms and conditions before doing so.

If you like the content here please consider subscribing.

If you have any comments or feedback feel free to reach me @costapiy