Python Extras

YouTube Downloader With Python & Pytube

In this Python project, we will build a GUI-based YouTube Video Downloader using the Tkinter and pytube modules of Python. It is a beginner-level project, and you will get to apply some cool libraries in real-life. Let’s get started!

About YouTube Video Downloader:

Aren’t there a lot of YouTube videos you wished you could’ve watched without the internet on your laptops or desktops?

Well, here’s your solution. In this project, we will create a YouTube video downloader where you can download the video straight from YouTube to your laptop.

About the YouTube Video Downloader project in Python:

The objective of this is to create a GUI based YouTube Video Downloader. To build this, you will need a little understanding of Tkinter and pytube. This project is very good and it solves a problem pertinent to most.

Project Prerequisites:

To build this Python YouTube Video Downloader project, we will need the following libraries:

1. Tkinter – To create the GUI
2. pytube.YouTube – To download the videos

Since the pytube library does not come pre-installed with Python, you will have to run the following command to install it:

python -m pip install pytube

Python wrapper is used because some computers do not allow the pip command alone.

Download YouTube Downloader Python Project

Please download the source code of python youtube downloader: YouTube Downloader Python Code

Project File Structure:

Here are the steps you will need to execute to build this Python YouTube Video Downloader Project:

1. Importing all the necessary imports
2. Creating the download and reset buttons functions
3. Initializing the window and placing all its components

Let’s take a closer look at these steps:

1. Importing all the necessary modules:

# Importing all the necessary modules for Python YouTube Video Downloader project
from tkinter import *
from tkinter import messagebox as mb
from pytube import YouTube

2. Creating the download and reset buttons functions:

# Defining the downloader function for YouTube Video Downloader project using Python
def downloader(link, directory, filename):
    yt_link = link.get()
    save_path = directory.get()
    aftersave_filename = filename.get()

        yt = YouTube(yt_link)
        video = yt.streams.first(), aftersave_filename)
        mb.showerror('Error', 'Connection Error! You are offline!')

def reset(l_strvar, d_strvar, fn_strvar):


  • We have created the download() function to make use of the YouTube class to download the YouTube videos.
    • We will give the function 3 parameters, all of which should be Entry objects controlled by StringVar objects. Then we will initially get the text from the Entry objects and assign it to variables.
    • Then we will use the try … except statements to make sure that when we initialize the YouTube class, the user has an active internet to download the videos, or else we will print an error message.
  • In the reset() function, we will set the 3 StringVar objects that are provided as arguments, that control the 3 Entry objects, as blank strings so the values in Entry objects also become blank.

3. Initializing the window and placing all its components:

# Initializing the window
root = Tk()
root.title('TechVidvan Youtube Video Downloader')
root.resizable(0, 0)

# Heading label
Label(root, text='TechVidvan Youtube Video Downloader', font=("Comic Sans MS", 15), bg='Coral').place(relx=0.25, rely=0.0)

# Creating the main window
Label(root, text='Enter the Youtube link:', font=("Times New Roman", 13), bg='Coral').place(relx=0.05, rely=0.2)

link_strvar = StringVar(root)
link_entry = Entry(root, width=50, textvariable=link_strvar), rely=0.2)

Label(root, text='Enter the save location:', font=("Times New Roman", 13), bg='Coral').place(relx=0.05, rely=0.4)

dir_strvar = StringVar(root)
dir_entry = Entry(root, width=50, textvariable=dir_strvar), rely=0.4)

Label(root, text='Enter the filename:', font=("Times New Roman", 13), bg='Coral').place(relx=0.05, rely=0.6)

filename_strvar = StringVar(root)
filename_entry = Entry(root, width=50, textvariable=filename_strvar), rely=0.6)

# Creating the buttons
download_btn = Button(root, text='Download', font=7, bg='Aquamarine',
                      command=lambda: downloader(link_entry, dir_entry, filename_entry)).place(relx=0.3, rely=0.75)

reset_btn = Button(root, text='Reset', font=7, bg='Aquamarine',
                   command=lambda: reset(link_strvar, dir_strvar, filename_strvar)).place(relx=0.5, rely=0.75)

# Finalizing the window of Python YouTube Video Downloader project


  • Now, we will use the Tk() class to initialize the GUI window.
    • The .title() method is used to set a title to the window.
    • The .geometry() method is used to specify the initial geometry of the window.
    • The .resizable() method, which takes truthy and falsy values as arguments, is used to allow/deny the permission to resize the window. It’s positional arguments are taken in the form (width, height).
    • The .configure() method is used to define the other attributes of the Tk class, such as bg which sets the background color to the window.
    • The .update() and .mainloop() methods are used to put the window in a loop so that the window does not close as soon as it opens.
  • The Label class is used to add static text to the window. Its parameters and methods are:
    • The master parameter, the positional argument root in this case, is the parent widget it is associated with.
    • The text parameter is the text that will be displayed on the widget.
    • The font parameter is the font family and size and text effects of the text in the widget.
  • The Button class is used to add a button to the window that runs a function when pressed. Its parameters and methods are:
    • The command parameter defines the function that will run when the button is pressed. You will not need to use the lambda keyword if the function to run does not need any arguments.
  • The Entry class is used to add an input field to the window, which allows the user to input data. Its parameters and methods are:
    • The width parameter is used to mention the number of characters that will fit in the widget.
    • The .get() method is used to get the text inputted by the user in the Entry widget. It takes no arguments.
  • The StringVar class is used to manipulate text in Label and Entry widgets. Its methods are:
    • The .set() method is used to set a value to the StringVar object and the widget it manipulates. The only argument it takes is the text that the value has to be set to.
  • The .place() method, which is one of the 3 Tkinter geometry manager methods, positions a widget such that it is placed on a Cartesian Plane.
    • The x, y parameters denote the horizontal and vertical offsets of the widget.
    • The relx, rely parameters are the horizontal and vertical offsets of the widget in terms of a floating number between 0.0 and 1.0
    • The anchor parameter defines the corner or side of the window that will be considered as the origin of the cartesian plane that is our window.

Leave a Reply

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

Back to top button