๐ Let's Get Started
In this article, we will build a YouTube video downloader application in Python making use of the PyTube Library.
To create the graphical user interface (GUI) we'll make use of the Tkinter Library; while for handling video downloads we'll rely on PyTube, which is a lightweight Python library for downloading YouTube content.
๐ Explanation
Now, let's break down the code and understand each line step-by-step:
๐ Step 1
Install the libraries we are about to use.
Make sure you have python and pip installed on your PC to build this project.
Paste these lines to your terminal opened in the specific directory you want to write code for this project.
pip install tkinter
pip install pytube
๐ Step 2
# importing tkinter
from tkinter import *
# importing pytube module
from pytube import YouTube
# initializing tkinter
root = Tk()
# setting the geometry of the GUI
root.geometry("400x350")
# setting the title of the GUI
root.title("Youtube Video Downloader Application")
Tkinter and Pytube are imported.
The GUI window is activated with dimensions(width - 400px | height - 350px) and a title("Youtube Video Downloader Application").
๐ Step 3
# defining download function
def download():
# using try and except to execute the program without errors
try:
myVar.set("Downloading...")
root.update()
YouTube(link.get()).streams.first().download()
link.set("Video downloaded successfully")
except Exception as e:
myVar.set("Mistake")
root.update()
link.set("Enter correct link")
A function called "download" is defined to manage the download process.
Inside the try-except block, it sets the status message and updates the GUI accordingly as "Downloading...".
Then utilizes Pytube to download the video from the inputted YouTube link and adjusts the status message to "Video downloaded successfully".
If an exception occurs it updates the status to indicate an error - "Mistake" and prompts the user to enter the correct link.
๐ Step 4
# created the Label widget to welcome user
Label(root, text="Welcome to youtube\nDownloader Application", font="Consolas 15 bold").pack()
A label is created with the message - "Welcome to youtube\nDownloader Application" for the user.
Here "\n" means move to the next line.
The font "Consolas", font size "15" and font weight "bold" are used for the styling of the text.
๐ Step 5
# declaring StringVar type variable
myVar = StringVar()
# setting the default text to myVar
myVar.set("Enter the link below")
# created the Entry widget to ask the user to enter the URL
Entry(root, textvariable=myVar, width=40).pack(pady=10)
A variable called "myVar" of type StringVar is created.
Set as the default text(Entry), for user prompts. ("Enter the link below")
Here "pady" is the padding on the y-axis to enhance the visual layout.
๐ Step 6
# declaring StringVar type variable
link = StringVar()
# created the Entry widget to get the link
Entry(root, textvariable=link, width=40).pack(pady=10)
Another variable called "link" of type StringVar is created.
An entry link widget is created for users to input their desired YouTube link.
Here too, "pady" is the padding on the y-axis.
๐ Step 7
# created and called the download function to download video
Button(root, text="Download Video", command=download).pack()
# running the mainloop
root.mainloop()
A button labelled "Download Video" is created, with its associated command set as the "download" function that will be executed when clicked.
By mainloop() the GUI enters into its event loop enabling users to interact with the application.
The program will keep running until the user decides to close the graphical user interface (GUI) window.
๐ Wrapping Up
Congratulations!
You've successfully built a user-friendly YouTube Video Downloader using Tkinter for the GUI and Pytube for downloading YouTube videos in Python.
I hope you found this article helpful and thanks for reading it till the end. โค๏ธ
Connect With Me: linktree
Happy Coding! ๐
Thanks for 13458! ๐ค
Top comments (1)
Nice Application