1HᗩᑕK's 2 BIG AD Space for rent! Click here for more details.

 $20 for x1 ad slot with over a Million page views per month! Only x7 slots are available - for booking contact below.

@TheJoker or email at [email protected]

How To Build A YouTube Downloader With Python

Learn to build a simple working YouTube downloader using Python with the help of PyTube3

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 various functionalities it offers to us. So, let’s do it step by step.

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. 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 confusions, pytube3 is also imported by writing pytube only. We do not import it by writing as pytube3.

Accepting Input from User

Our next step would be to ask the user to provide us with the link of the youtube video which we need to download. User will then provide us with the link of 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.

Revealing various Information about the Video

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 video
print(“Title: “,yt.title)

#Number of views of video
print(“Number of views: “,yt.views)

#Length of the video
print(“Length of video: “,yt.length,”seconds”)

#Description of video
print("Description: ",yt.description)

#Rating
print("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 purpose, we are not printing description (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.

#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

Output showing various details of the video

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

Looking at Streams Available

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 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">]

Available Streams for the Video

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:

[<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">]

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 it contains both audio and video codec files while Dash streams have higher quality but only have video codecs.

So, if we want to download progressive stream, we will get a ready to play the 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:

[<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 just write the code below:

ys = yt.streams.get_highest_resolution()

This will create store the highest resolution stream in ys variable.

We can also choose any stream by 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 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. 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 learnt and what more you built. All the best everyone!

Source: Github & towardsdatascience

ENJOY & HAPPY LEARNING! :heart:

20 Likes

OR you could always use youtube-dl
xD

2 Likes

nice thing to try out with our own customization , GUI etc…

3 Likes

Instead of all this hustle,just install IDM. And you download any video in any website.

Friendly Websites

https://igg-games.com/ https://pcgamestorrents.com/ https://pirateiro.com/ ettvdl.com https://dodi-repacks.site/ https://crackingpatching.com/ https://glodls.to/ https://prostylex.org/ https://haxnode.com/ https://freedownloadae.com/ https://www.novahax.com/ https://www.sadeempc.com/ freecoursesonline.me ftuapps.dev