YouTube Downloader with Python | Towards Data Science

Hello readers! Today, we will be building a YouTube downloader inPython3 using the PyTube3 library. The original pytube library no longer works and so we need to use the pytube3 library which only works with Python3 and not with Python2.
We will see various things we can do with our Youtube Downloader and the various functionalities it offers to us. So, let’s do it step by step.
First things first, before doing anything else, you need to download the pytube3 library in your system. To do this we will be using python3.
Type in the following command in the CLI to download and install pytube3 in your system.
pip install pytube3
This command will download and install pytube3 in your system.
Now we can start building our YouTube Downloader. Now we need to import the library in our program for using its functionalities.
So, we start our program with the following command:
from pytube import YouTube
You will notice that while we downloaded and installed pytube3 in our system but we are here importing pytube in the code.
To clear up the confusion, pytube3 is also imported by writing pytube only. We do not import it by writing it as pytube3.
Our next step would be to ask the user to provide us with the link to the youtube video which we need to download. The user will then provide us with the link to the video he intends to download.
link = input(“Enter the link: “)yt = YouTube(link)
So, we have accepted input from the user and passed on the link to our YouTube class. It will help us reveal all the information about the video and also will let us download it.
Now, we have the link, we have passed it into the YouTube class. Now, we can play with the link and reveal all sorts of information about the video like its title, number of views, ratings, description, length of the video and various other things.
#Title of videoprint(“Title: “,yt.title)#Number of views of videoprint(“Number of views: “,yt.views)#Length of the videoprint(“Length of video: “,yt.length,”seconds”)#Description of videoprint("Description: ",yt.description)#Ratingprint("Ratings: ",yt.rating)
Now, when we will run this code, we will get to see various details about the video whose link we have fed into the program. Also, there are many more such operations that can be performed which you can find in the official documentation of pytube3.
So, for output purposes, we are not printing descriptions (it’s large), so we are printing the rest four things.
We have used the link for the official trailer of Dark Season 3 here. You can use any link as per your choice.
So, as you can see above, we have printed various details about the show.
You must have seen there are various qualities available for viewing on youtube. So, while downloading using pytube, we also get the options for all streams available.
pytube offers a very easy way to see all the available streams for the link user has provided. So, let’s run the code to view all the streams available for that particular video.
#printing all the available streamsprint(yt.streams)
On running the above code, we get all the available streams for that video. Below is the output generated:
Now, you may be seeing both video and audio streams available. So, you can also filter out only audio or video streams. You can also filter out streams based on the file format. We can also filter out progressive and Dash streams (will talk about them in a second).
So, let’s filter out audio-only streams. To do so, we need to write code as such:
print(yt.streams.filter(only_audio=True))
The output we will get is as such:
Now, let’s filter out video-only streams. It will show us only the streams which contain video but no audio. So, it will also filter out all the progressive streams. For that, we will write as such:
print(yt.streams.filter(only_video=True))
The output we will get is as such:
Now, let’s talk about Progressive v/s Dash streams. YouTube uses Dash streams for higher-quality rendering.
Progressive streams are limited to 720p and contain both audio and video codec files while Dash streams have higher quality but only have video codecs.
So, if we want to download a progressive stream, we will get a ready to play video which also has built-in audio.
But, for the higher quality, we should use Dash streams for video and should also download an audio stream and then later merge them using any mixing tool.
So, for this article, we will be using progressive stream download to get ready to play videos. You are free to choose your quality of download and choice of stream.
So, let’s filter out progressive streams first. The code below will do it for us:
print(yt.streams.filter(progressive=True))
It will list the progressive streams available for download to us. It will have limited options but it does the work for us. The output will look like this:
To get the highest resolution progressive stream available, we can just write the code below:
ys = yt.streams.get_highest_resolution()
This will create store the highest resolution stream in the ys variable.
We can also choose any stream with the help of its itag.
ys = yt.streams.get_by_itag('22')
So, now we have stored our preferred stream in a variable. Now, let’s download it to our system.
ys.download()
The above code will download our preferred stream and save it in the current working directory.
We can also specify the location in our system where we want to download the youtube video. We can do so by specifying the absolute path in between the braces of download.
The code below helps you to download it in your preferred location.
ys.download('location')
That’s all! Congrats, you have just built your first simple YouTube downloader using Python. Use it for testing and educational purposes only. Do not misuse this knowledge.
Here’s the complete code for downloading a youtube video using the highest quality progressive streams available:
Do visit my Github Repository for more details and updates. I encourage you all to try something new out of this code and then please do share your thoughts and experience in the comments. I would love to hear what you learned and what more you built. All the best everyone!
See, what we built here is a very simple version. We can also try to convert the same concept into an application or a website that would perform the same functions in a user-friendly way. You can also add support for different websites by using their respective APIs. There are many such feature-rich video downloaders out there that use similar concepts and gives the user an option to download videos easily.
One of my favourites is YTSaver which is a feature-rich application to download videos or playlists from tons of websites in different resolutions and formats in one click. It also allows you to convert videos from one format to another and is much faster than some other downloaders out there.
Note: The above paragraph contains an affiliate link.
Actually, you can also use Python libraries like this one to convert videos from one format to another. You can also try to do that if you are interested. Thank you very much for reading!
More stories to read after finishing this one are:-