With more and more people turning to YouTube for entertainment and educational content, it has become increasingly important to have an efficient and reliable way to download videos from the platform.

Write For US

Fortunately, with the help of Python, it’s possible to create your own YouTube downloader with just a few simple steps.

This step-by-step guide will show you how to build a program that can quickly and easily download videos from YouTube and save them to your device for later viewing.

By learning the fundamentals of Python programming, you can create a powerful tool that will help you get the most out of YouTube. So, let’s get started and build your own YouTube downloader!

Downloading and Importing the libraries

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. We need to import the library into our program to use 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.

Accepting Input from User

Our next step would be to ask the user to provide us with the link to the youtube video 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 let us download it.

Revealing various Information about the Video

Now we have the link, and we have passed it to 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.

#Output obtained on running the code
Title:  Dark Season 3 | Official Trailer | Netflix
Number of views:  2019403
Length of video:  147 seconds
Ratings:  4.9721289

When we run this code, we will 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 of your choice.

#Output obtained on running the code
Title:  Dark Season 3 | Official Trailer | Netflix
Number of views:  2019403
Length of video:  147 seconds
Ratings:  4.9721289

So, as you can see above, we have printed various details about the show.

Looking at Streams Available

YouTube offers various quality options for viewing. Pytube also allows us to download all available streams while downloading.

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 streams
print(yt.streams)

On running the above code, we get all the available streams for that video. Below is the output generated:

[<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">,
 <Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">, 
 <Stream: itag="313" mime_type="video/webm" res="2160p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="271" mime_type="video/webm" res="1440p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028" progressive="False" type="video">, 
 <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f" progressive="False" type="video">,
 <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
 <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
 <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015" progressive="False" type="video">,
 <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c" progressive="False" type="video">,
 <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">,
 <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus" progressive="False" type="audio">,
 <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus" progressive="False" type="audio">,
 <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus" progressive="False" type="audio">]

Now, you may see 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:

[<Stream: itag=”140″ mime_type=”audio/mp4″ abr=”128kbps” acodec=”mp4a.40.2″ progressive=”False” type=”audio”>,
<Stream: itag=”249″ mime_type=”audio/webm” abr=”50kbps” acodec=”opus” progressive=”False” type=”audio”>,
<Stream: itag=”250″ mime_type=”audio/webm” abr=”70kbps” acodec=”opus” progressive=”False” type=”audio”>,
<Stream: itag=”251″ mime_type=”audio/webm” abr=”160kbps” acodec=”opus” progressive=”False” type=”audio”>]
<gwmw style="display:none;">

Audio Only Streams

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:

[<Stream: itag="313" mime_type="video/webm" res="2160p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="271" mime_type="video/webm" res="1440p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028" progressive="False" type="video">,
 <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f" progressive="False" type="video">,
 <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
 <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">,
 <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015" progressive="False" type="video">,
 <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9" progressive="False" type="video">,
 <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c" progressive="False" type="video">,
 <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9" progressive="False" type="video">]

Video Only Streams

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 with built-in audio.

But, for 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 use 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:

[<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">,
 <Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">]

Progressive Streams Available

To get the highest resolution progressive stream available, we can write the code below:

ys = yt.streams.get_highest_resolution()

This will create and 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 the 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:

from pytube import YouTube
#ask for the link from user
link = input("Enter the link of YouTube video you want to download:  ")
yt = YouTube(link)
#Showing details
print("Title: ",yt.title)
print("Number of views: ",yt.views)
print("Length of video: ",yt.length)
print("Rating of video: ",yt.rating)
#Getting the highest resolution possible
ys = yt.streams.get_highest_resolution()
#Starting download
print("Downloading...")
ys.download()
print("Download completed!!")

Complete Code for YouTube Downloader

Do visit my GitHub Repository for more details and updates. We encourage you to try something new with this code, then we’d love to hear your thoughts and experiences. It would be great to hear what you learned and what more you built. All the best, everyone!

This is a very simple version of what we built. We can also create a user-friendly application or website based on the same concept. It is also possible to add support for different websites by using their APIs. These feature-rich video downloaders use similar concepts and allow users to easily download videos

YTSaver, a feature-rich application that lets you download videos and playlists in different sizes and formats from tons of websites, is one of my favorites. You can also convert videos between formats and it is much faster than other downloaders.

Note: The above paragraph contains an affiliate link.

It is also possible to convert videos from one format to another using Python libraries like this one. If you are interested, you can also try that. Thank you so much for reading!

Conclusion

There is no doubt that YouTube has become an essential part of our daily lives. Whether you’re looking for entertainment or educational content, you’re likely to find it on the platform.

But with so much content available, it can be difficult to find what you’re looking for. That’s why it is so useful to have an efficient way to download videos from the platform.

With the help of Python, it’s possible to build a program that can efficiently download videos and save them to your device. Now that you’ve learned how to build your own YouTube downloader, you can get the most out of the platform.

Write For US
Author

An online tech blog or portal is a website that provides news, information, and analysis about technology and the tech industry. It can cover a wide range of topics, including Big Data & Analytics, blockchain, AI & ML, The mobile app economy, digital commerce and payments, AR & VR, Big Data, low code development, Gaming and microservices, enterprise software and cultural impact of technology.

loader