Hi Readers ! Today I am going to show you all how to download youtube videos using python.As we all know that YouTube is the most popular platform for viewing and sharing videos with others.

However,sometimes you feel like to download a video so that you can view it later and most importantly offline. So I thought to share a tutorial with you all regarding this.

As the title implies we are going to build this small program using Python and using an amazing python package called pytube. So lets first start by understanding about the pytube library.

About Pytube

pytube is a lightweight library written in Python. It has no third party dependencies and aims to be highly reliable.

pytube also makes pipelining easy, allowing you to specify callback functions for different download events, such as on progress or on complete.

  • Support for Both Progressive & DASH Streams
  • Support for downloading complete playlist
  • Easily Register on_download_progress & on_download_complete callbacks
  • Command-line Interfaced Included
  • Caption Track Support
  • Outputs Caption Tracks to .srt format (SubRip Subtitle)
  • Ability to Capture Thumbnail URL.
  • Extensively Documented Source Code
  • No Third-Party Dependencies

For further explanation about the library you can view its repo. Well,Enough of the theories and now we deal with the real business of downloading a youtube video using pytube library.Please follow the steps as below :

Step 1:

Download and install pytube using pip :

$ pip install pytube

Step 2:

from pytube import YouTube

video_url = input('Enter the youtube video url : ')
file_size = 0

def complete_func(stream, file_handle):
    print('Video downloaded successfully !')

def show_progress_bar(stream,chunk,file_handle,bytes_remaining):
    global file_size
    if file_size == 0:
        file_size = bytes_remaining
    else:
        r = float(file_size - bytes_remaining)
        print(str(round(r/float(file_size)*100,0))+ '% of video downloaded')

yt = YouTube(video_url)
yt.register_on_progress_callback(show_progress_bar)
yt.register_on_complete_callback(complete_func)
yt.streams.filter(progressive=True).first().download()

Explanation

We start by importing the pytube library and then take the youtube video url that the user wants to download as an input.We also define the initial file_size which obviously will be 0.

Now we use two most powerful functionalities of pytube that are event hooks that we can use to display the download progress to our users.Here we define two hooks namely complete_func and show_progress_bar.

Fair enough till now right ! Next we start the main business by creating an instance of Youtube class and then registering our event hooks created earlier to this instance.

Now the last line needs some explanation.In last line we have actually chained three tasks in one line.

  • Filtering the progressive stream videos from the list of all the video types available
  • Actually yt.streams returns a list of videos available for downloading.To check the complete list you can run this line yt.streams.all().This line will list down all the available options for that particular video.So here we choose the first element from the list using the first() method.
  • At last we use the download() function to start downloading the video.

That’s it folks ! I hope I was clear enough with what I was trying to explain here.However in case you need any further explanation please comment below.



Leave a Reply

Your email address will not be published. Required fields are marked *