How to Download YouTube Videos Using Python with Pytube Library?
In this section, we will learn how to download youtube video using python with module pytube.
Prerequisite
Pytube is a library in the python programming language. Pytube is a lightweight, dependency-free Python library for downloading YouTube Videos. Pytube can help to download YouTube videos.
Table of Contents
Required Module
In this tutorial, we are using the Pytube Library of Python Programming Language. You can download it using PIP in python.
pip install pytube
Approach
- Import the pytube module and pytube.cli module.
- Create and initialize an object of YouTube() e.g, src = YouTube(url,on_progress_callback=on_progress).
- Use pytube.cli to get the indication as progess bar(amount of video downloaded).
- Create a video stream e.g. video = video.streams[0].
- Pytube object provides the download() function to download the video. e.g., video.download();
- To set the specific location of download, you need to pass the locaton in download() function. e.g. video.download(“C:\\Downloads”);
Pytube program implementation given below.
Program 1: Download YouTube video using Python with Pytube library
The following program shows the simplest method to download the YouTube videos using Python.
# Import Pytube module to use API
from pytube import YouTube
video_url = 'https://youtu.be/mBJMkFNRVek'
#create an object of YouTube() and pass the URL of YouTube Videos
video = YouTube(video_url)
print("Video Downloading......")
# prepare the streams
video = video.streams[0].download("C:\\Downloads");
print("Video Download Completed.")
Output
Video Downloading......
Video Download Completed.
Program 2: Download YouTube video using Pytube with progress bar.
The following program can able to download the YouTube Videos with a progress bar. It uses pytube.cli to show the progress bar as output.
"""
Created on Tue May 18 20:01:47 2021
@AIM: How to downlaod YouTube Vidoes using Python?
@author: www.codenaive.com
"""
# Import Pytube module to use API
from pytube import YouTube
# import pytube.cli to show progress bar
from pytube.cli import on_progress
video_url = 'https://youtu.be/mBJMkFNRVek'
#create an object of YouTube() and pass the URL of YouTube Videos along with On_progress to show progress bar
video = YouTube(video_url, on_progress_callback=on_progress)
# prepare the streams
video = video.streams[0]
# downloa the video
video = video.download("C:\\Downloads");
Output
The progress bar output of the downloading video shown below:

Leave a Reply