YouTube Video Downloader
Easy Script to Make Your Own Youtube Video Downloader with Python

The article aims to build a Script to make a Youtube video downloader with the help of Python Programming Language. Downloading Youtube videos is not a straightforward task for everyone, but through coding, one can fix this too. Python is a language known for its simplification of tasks. And justifying that only, a few lines of code is all you need.

A library named “Pytube” will be used in this project and downloading youtube videos will become a very easy task. Pytube is a lightweight and independent library, but it is not a native library, so one can install it simply using the pip method, which will be covered shortly.

What You Will Make?

A Quick Python Script that lets you download youtube videos for free.

What You Need?

Before you head on any further, make sure to tick the below checklist:

  • Any IDE to write the code (I have used Visual Studio Code)
  • Basic Python Knowledge to understand everything
  • Pytube Library
    • Installing pytube using pip is a simple task, just type in the following command in your terminal or the command prompt.
pip install pytube

Script to Make your own YouTube Video Downloader

As we have already installed pytube in our system, we can now use this library to finally code our mini-project.

Importing pytube

We will now import the YouTube module from the pytube library so that we can work in the direction of building our Youtube video downloader. So to do the said task, start the program by writing the following piece of code:

from pytube import YouTube

Letting the User Input the Link

In this step, we are taking the user’s input to add the link to the video that our user is interested in downloading. The input that the user enters is going to store in a variable named “linkInput”.

linkInput = input("Copy and Paste the link of YouTube video that you want to download:  ")

Creating an Object of the YouTube module

We simply pass on the linkInput we got from the previous step to the YouTube Class. This way we have created an object of the YouTube module. This will help us in getting all the details about the intended video like the title, time, description, etc. And it will all lead us to download our video.

yt = YouTube(linkInput)

Retrieving Details about the Video

Since we have created an object of the YouTube class, now we can use that to retrieve and print all the information related to our video. For this purpose, we have the following code that will print the title, description, thumbnail URL, number of views, and length of the video. There are more than just these parameters which can be used to retrieve information about the video using pytube. Here is complete detail on that.

I have used one of the videos available on our channel only.

#Step to Show details about the video
#Prints the title
print("Title: ",yt.title) 

#Prints the description
print("Description: ",yt.description)

#Prints the thumbnail_url
print("Thumbnail URL: ",yt.thumbnail_url)

#Prints the number of views
print("Number of views: ",yt.views)

#Prints the duration 
print("Length of video: ",yt.length)
#Output of the above code:
Title:  Automate WhatsApp with Python (Only 2 Lines of Code)
Description:  Hello Everyone!
So in this video,........Pywhatkit 
#I've not pasted the whole description output since it was too long
Thumbnail URL:  https://i.ytimg.com/vi/zHo62rpRGJ8/sddefault.jpg
Number of views:  114
Length of video:  216
Rating of video:  None

Selecting Streams

Now moving on to the second last step before we’ve our own Youtube video downloader, we have to understand one more technical thing before moving ahead. So as we all know a video is comprised of both, audio and video. So in a case, where we want to download a video as a whole, wherein it has both the audio and the video, then we will have to go for a PROGRESSIVE stream. Otherwise, we would have gone for the audio or video individually following the DASH stream pattern.

#printing all the available streams
print(yt.streams)
[<Stream: itag="17" mime_type="video/3gpp" res="144p" fps="8fps" vcodec="mp4v.20.3" acodec="mp4a.40.2" progressive="True" type="video">, 
<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="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.64001f" 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.4d401f" 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="139" mime_type="audio/mp4" abr="48kbps" acodec="mp4a.40.5" progressive="False" type="audio">, 
<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 that we know about all the available streams, we can now progress towards knowing about progressive and dash streams.

Progressive Stream: As mentioned earlier, a single file that has both audio and video is known to be a progressive stream. The main drawback of using this type of legacy stream is that it can download only up to 720p and below resolutions.

Dash Stream: It is comparatively a new type of data stream wherein both the audio and the video can be downloaded separately and later can be merged using another software. Dynamic Adaptive Streaming over HTTP (DASH) is known to provide a better user experience because it acts adaptively to the user’s viewing screen.

So if you want to have a better UX, then you can go for the adaptive streaming option. For that, you can filter to only adaptive streams like so:

yt.streams.filter(adaptive=True)

In case you want only progressive streams, then you can go like this:

yt.streams.filter(progressive=True)

Selecting the Highest Resolution Stream

This code will store the highest resolution possible in the highestRes variable.

highestRes = yt.streams.get_highest_resolution()

Downloading the Video

Now we can just download the video using the following code. By default, it will be downloaded to the current working directory.

highestRes.download()

But in case you have a preferred location to download your video, you can just pass on the location.

highestRes.download('location')

More parameters can be used while calling the download function. Here is a list of all.

Here’s the complete code for your reference:

from pytube import YouTube

#Ask for the link from user
linkInput = input("Copy and Paste the link of YouTube video that you want to download:  ")
yt = YouTube(linkInput)

#Showing details
print("Title: ",yt.title)
print("Description: ",yt.description)
print("Thumbnail URL: ",yt.thumbnail_url)
print("Number of views: ",yt.views)
print("Length of video: ",yt.length)
print("Rating of video: ",yt.rating)

#Getting the highest resolution
highestRes = yt.streams.get_highest_resolution()

#Starting download
print("Downloading...")
highestRes.download()
print("Your Download has been completed!")

View Post

So here we have come to an end, we have successfully made our Basic YouTube Video Downloader. There’s a lot more than you can add to this program. One can even create a GUI(Graphical User Interface) using the Tkinter library of Python. Make sure to share your way of working out to code a Youtube video downloader of your own.

Here is a link to our previous articles, we would highly appreciate it if you can take some effort and share them with the right audience. Find some more exciting Python Project Ideas Here:


Vaishali Rastogi

Hey There! I am Vaishali Rastogi and I am a tech-researcher. I've been doing writing for a good 4-5 years, so here I am now. Working on my own website to make people digitally aware of the updates in technology.

7 Comments

Hemraj · December 31, 2021 at 8:40 pm

Excellent!!

Miley · January 1, 2022 at 1:34 pm

Very Detailed post! Want to see the one with GUI though.

this hyperlink · March 10, 2022 at 5:45 pm

Hey! Someone in my Myspace group shared this website with us so I came to
give it a look. I’m definitely enjoying the information. I’m bookmarking and will
be tweeting this to my followers! Outstanding blog and fantastic design and style.

bit.ly · March 24, 2022 at 7:35 pm

It’s not my first time to pay a visit this web site, i am visiting this site dailly
and get nice information from here everyday.

Leave a Reply

Avatar placeholder

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

16 + 4 =