N Kaushik

How to download YouTube videos using Python

April 11, 2021

How to download YouTube videos using Python:

pytube is a popular library for downloading YouTube videos. It lightweight and dependency-free library for downloading YouTube videos.

You can use it with python-2 or python-3 and it can be downloaded easily using pip.

For Python-2:

pip install pytube

For python-3

pip install pytube3

How to download a YouTube video:

Downloading a video needs only a few lines of code:

from pytube import YouTube

url = "VIDEO_URL"

youtube = YouTube(url)
video = youtube.streams.get_highest_resolution()
video.download()

Replace VIDEO_URL with the url of the video, and it will download the highest resolution version for that video.

It downloads the video in the same folder. You can also pass the folder path as an argument to the download() method.

You can check the (library page)[https://github.com/pytube/pytube] to learn more on it.

Using Command Line Interface:

pytube also provides CLI for downloading YouTube videos. You can use the below command to download videos with highest quality:

pytube VIDEO_URL

Subscribe to my Newsletter