Create YouTube Downloader with Python

FREE Online Courses: Click for Success, Learn for Free - Start Now!

We all watch youtube regularly for multiple purposes like entertainment, gaining knowledge, etc. Have you ever wanted to download any of those videos onto your device? If yes, we are here with a solution that you can build yourself to download a youtube video. In this project, we will build a youtube video downloader using Python.

Youtube Video Downloader Project in Python

In this project, we will be using the tkinter and pytube modules to build the youtube video downloader. We use the tkinter module to build GUI that can be used as an interface to take inputs like the youtube link, the location where the video needs to be downloaded, and the resolution.

And we use the pytube module to connect to the internet and download the respective video using the URL.

Project Prerequisites

It is advised for the user to have prior knowledge of Python and the tkinter module. One should also install the tkinter and pytube modules. You can use the below commands to do so.

pip install tkinter
pip install pytube3

Download code for the Python Youtube Downloader Project

Please download the code for the Python Youtube Video Downloader project using the link: YouTube Downloader Python Project

Project File Structure

Here are the steps to build the project:

  1. Import the required modules
  2. Build the main window
  3. Write the function to find the resolutions
  4. Write the function for reset
  5. Write the function to download video

1. Import the required modules

The first step is to import the tkinter and pytube modules

from pytube import YouTube
from tkinter import *
from tkinter import messagebox as mb

Code explanation:

a. The YouTube class in the pytube module helps in downloading youtube videos of resolution, and other properties of our choice

b. Using tkinter we build the GUI to take inputs of link, resolution, and path to download the video from the user

2. Build the main window

In this, we create the main window with two entry widgets. One to take the youtube link and the other is the address where the video needs to be installed. And two buttons, Find Resolution and Reset.

Find Resolution lets the user check the available resolutions and download the required one. And Reset clears the text entered by the user.

# Creating the main window
wn = Tk()
wn.title("Python Youtube Downloader by ProjectGurukul")
wn.geometry('700x300')
wn.config(bg='honeydew2')

res = StringVar()

# Heading label
Label(wn, text="Youtube Video Downloader by ProjectGurukul", font=('Courier', 15), fg='grey19').place(x=100,y=15)

#Getting youtube link
Label(wn, text='Enter the Youtube link:', font=("Courier", 13)).place(relx=0.05, rely=0.3)

yt_link = StringVar(wn)
link_entry = Entry(wn, width=50, textvariable=yt_link)
link_entry.place(relx=0.5, rely=0.3)

#Getting destination location
Label(wn, text='Enter the save location:', font=("Courier", 13)).place(relx=0.05, rely=0.5)

destination = StringVar(wn)
dir_entry = Entry(wn, width=50, textvariable=destination)
dir_entry.place(relx=0.5, rely=0.5)

#Buttons
resolutionBtn = Button(wn, text='Find Resolution', font=7, fg='grey19',
command=getResolution).place(relx=0.3, rely=0.75)

resetBtn= Button(wn, text='Reset', font=7, fg='grey19',
command=reset).place(relx=0.6, rely=0.75)

wn.mainloop()

Code explanation:

a. geometry(): It sets the length and width of the screen.
b. configure(): It sets the background color.
c. title(): It shows the title on the top of the window.
d. Label(): It helps in showing information or text.
e. place(): This positions the widgets in a specified location based on coordinates or relative to the parent component.
f. Entry(): This widget helps in taking input from the user
g. Button(): This creates a button with mentioned color, text, etc. and the command parameter represents the function that is executed on pressing the button.
h. mainloop(): This makes sure the screen runs in a loop till it is manually closed.

3. Write the function to find the resolutions

This function runs when the user presses the Find Resolution button. It gets the url from the user input and then creates a stream object for that video. Using this, we find the available resolutions.

Then based on the availability, we enable or disable the radio buttons. On choosing one of the resolutions, the user can download the video.

def getResolution():

url= yt_link.get() #getting link from the user
resolutions = set() #set that holds the resolutions available

try:
yt = YouTube(url) #creating the object that stores information about the video

for stream in yt.streams.filter(type="video"): # Only look for video streams
resolutions.add(stream.resolution) #Adding all resolutions available

resolutions=list(resolutions) #list containing all resolutions

except Exception as e:
mb.showerror('Error',e)
return

#creating a new window
wn = Tk()
wn.title("Youtube Video Downloader by ProjectGurukul")
wn.geometry('700x500')
wn.config(bg='honeydew2')

# Heading label
Label(wn, text="Youtube Video Downloader by ProjectGurukul", font=('Courier', 15), fg='grey19').place(x=100,y=15)

Label(wn, text="The available resolutions are checkable. \n Please choose one of the resolutions and click on the download button",
font=('Courier', 10), fg='grey19').place(x=20,y=50)

#Creating a radio button for each resolution
R1 = Radiobutton(wn, text='144p', variable=res, value='144p')
R1.place(x=100,y=100)
R1.deselect()
R2=Radiobutton(wn, text='240p', variable=res, value='240p')
R2.place(x=100,y=130)
R2.deselect()
R3=Radiobutton(wn, text='360p', variable=res, value='360p')
R3.place(x=100,y=160)
R3.deselect()
R4=Radiobutton(wn, text='480p', variable=res, value='480p')
R4.place(x=100,y=190)
R4.deselect()
R5=Radiobutton(wn, text='720p', variable=res, value='720p')
R5.place(x=100,y=220)
R5.deselect()
R6=Radiobutton(wn, text='1080p', variable=res, value='1080p')
R6.place(x=100,y=250)
R6.deselect()
R7=Radiobutton(wn, text='2016p', variable=res, value='2016p')
R7.place(x=100,y=280)
R7.deselect()

#disabling those radio buttons that are not available in the video
if('144p' not in resolutions):
R1.config(state = DISABLED)
elif('240p' not in resolutions):
R2.config(state = DISABLED)
elif('360p' not in resolutions):
R3.config(state = DISABLED)
elif('480p' not in resolutions):
R4.config(state = DISABLED)
elif('720p' not in resolutions):
R5.config(state = DISABLED)
elif('1080p' not in resolutions):
R6.config(state = DISABLED)
elif('2016p' not in resolutions):
R7.config(state = DISABLED)


downloadBtn = Button(wn, text='Download', font=7, fg='grey19',
command=downloadVideo).place(x=100,y=350)

quitBtn = Button(wn, text='Quit', font=7, fg='grey19',
command=wn.destroy).place(x=350,y=350)

Code explanation:

a. get(): This gets the text in the respective widget
b. geometry(): It sets the length and width of the screen.
c. configure(): It sets the background color
d. title(): It shows the title on the top of the window.
e. Label(): It helps in showing information or text
f. place(): This positions the widgets in a specified location based on coordinates or relative to the parent component.
g. Radiobutton(): These help in choosing one of the given options
h. config(state=DISABLED): This helps in disabling, making the button unselectable.
i. Button(): This creates a button with mentioned color, text, etc. and the command parameter represents the function that is executed on pressing the button.

4. Write the function for reset

This function clears the entries where the user has entered input.

def reset():
yt_link.set('')
destination.set('')

Code explanation:

a. set(): Sets the text inside the brackets to the respective widget

5. Write the function to download the video

This function runs when the user presses the ‘Download’ button. Using this function, we download the video of required resolution at the location of our choice.

def downloadVideo():

resol=res.get() #getting resolution based on the radio button selected
path = destination.get() #getting the location where the video needs to be downloaded

try: #downloading the video of selected resolution
ys=yt.streams.filter(resolution=resol)
ys.first().download(path)
except Exception as e:
mb.showerror('Error',e)

Code explanation:

a. get(): This gets the text in the respective widget
b. streams.filter(): It selects only those video streams that satisfy the condition mentioned in the brackets. Here, the resolution should be as mentioned.
c. first().download(): Downloads the first video stream after filtering based on resolution.

Output of the Python YouTube Downloader project

python youtube downloader output

Summary

Congratulations! You have successfully built the Youtube video downloader in Python. In this project, you could learn to use the tkinter and pytube modules. Hope you enjoyed developing this project. Hope to see you again in another project.

1 Response

  1. Ritish Bhardwaj says:

    after giving save location in youtube project it shows the (yt is not define) please let me know how to fix this

Leave a Reply

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