A GUI YouTube Video and Audio Downloader in Python

It's a youtube video downloader using python tkinter

Introduction

Hello Python Programmers, hope everything is going well. Today I'm gonna show you an interesting project that I created using Python. It can be useful in our daily lives.

I hope the subject already gets your hand by seeing the image above. It's a YouTube Downloader using Python. You can download videos and audio both using this beautiful application.

The user interface is very simple and easy to use. I get help from the Tkinter library to manage the graphics in this YouTube downloader.

So before you start writing your code, let's see the project details and all the requirements the project needs.

👉Visit AlsoAn Image Viewer Application using Python Tkinter

The Project Details

The user interface of this YouTube Downloader may make you think that it is a real application. Nothing surprises here.

It's a youtube video downloader application in python
Image - YouTube Downloader Application

I used three Tkinter Frames to represent different widgets in this application. As you can see, the header section displays two images(The first one is the YouTube logo, and the second one is a download symbol) just for a better look.

Frame 3 shows different widgets related to downloading videos and audio from YouTube. There are two options to provide the URL of a video. First, you must copy the URL from YouTube. Next, either paste the copied URL directly('Ctrl+V') into the Entry box or you can press the "Paste URL" button to perform the same task.

The next task is to select the video resolution through a drop-down menu. You will get an option to choose only 'Audio' too from here. Now in the last or final step, select the location where the downloaded file will be saved through the Tkinter File Dialog.

Now you see a nice 'Download' button, press it to get your desired result. Watch the entire video to better understand how this Yt Downloader works.

Requirements

The project requires you to have these tools installed on your system. Use 'pip3' instead of 'pip' if you are running a Linux machine.

☛Install Tkinter: pip install tk

☛Install Pillow: pip install Pillow

☛Install pytube: pip install pytube

☛Install pyperclip: pip install pyperclip

Import the Modules

Create a separate folder for this project and declare a python file there with this name "Yt_Downloader.py". Now start writing your code by importing these modules.


import pyperclip
from tkinter import *
from threading import *
from tkinter import ttk
from pytube import YouTube
from PIL import ImageTk, Image
from tkinter import filedialog
from tkinter import messagebox, ttk

Declare a Python List

Declare a python list that contains the Download Quality(video resolutions and audio).


# Resolution Options(Audio Option is also available)
download_quality = ['144p', '360p', '720p', 'Audio Only']

Declare the 'Yt_Downloader' class

Now declare the 'Yt_Downloader' class.


class Yt_Downloader:

The __init__() function

It will create a GUI window for us. We set the window size, title, resizable option, etc. here.

I added one single comment line before each segment of the code. It will help you to understand the objectives of the program better.


def __init__(self, root):
# Setting the Tkinter main window
self.window = root
self.window.geometry("700x480")
self.window.title('YouTube Video and Audio Downloader')
self.window.resizable(width = False, height = False)

self.save_to_loc = ''

# Frame 1: For YouTube Logo
self.frame_1 = Frame(self.window, width=220, height=80)
self.frame_1.pack()
self.frame_1.place(x=20, y=20)

# Frame 2: For Download Logo
self.frame_2 = Frame(self.window, width=50, height=50)
self.frame_2.pack()
self.frame_2.place(x=235, y=40)

self.show_yt_logo()
self.show_dn_logo()

# About Button
about_btn = Button(self.window, text="About", \
font=("Kokila", 10, 'bold'), bg="dodger blue", \
fg="white", width=5, command=self.About_Window)
about_btn.place(x=600, y=30)

# Exit Button
exit_btn = Button(self.window, text="Exit", \
font=("Kokila", 10, 'bold'), bg="dodger blue", \
fg="white", width=5, command=self.Exit_Window)
exit_btn.place(x=600, y=70)

# Frame 3: For the Main Page Widgets
self.frame_3 = Frame(self.window,bg="white",\
width=700,height=480)
self.frame_3.place(x=0, y=130)

# Calling the Main_Page() Function
self.Main_Page()

The YouTube logo

This function will display the YouTube logo in the header section. It gives the application a pro look.

The image path(or location) is mentioned in the "Image.open()" function. The file must be present there. Don't worry, you will get the two image files used in the project from my GitHub page. The Download button is given below.

youtube logo

# This function displays the YouTube Logo
def show_yt_logo(self):
# Opening the YouTube logo image
image = Image.open('Images/YouTube_logo.png')
# Resizing the image
resized_image = image.resize((220, 80))

# Create an object of tkinter ImageTk
self.img_1 = ImageTk.PhotoImage(resized_image)

# Create a Label Widget to display the text or Image
label = Label(self.frame_1, image=self.img_1)
label.pack()

Show Download logo

This function does the same task as the previous. It will show this Download image there.

download symbol

# This Function displays the Download logo
def show_dn_logo(self):
# The image Path(Opening the image)
image = Image.open('Images/Download_Button.png')
resized_image = image.resize((50, 50))

# Create an object of tkinter ImageTk
self.img_2 = ImageTk.PhotoImage(resized_image)

# Create a Label Widget to display the text or Image
label = Label(self.frame_2, image=self.img_2)
label.pack()

The Main Page

The "Main_Page()" function will display all the widgets related to downloading videos and audio from YouTube. For examples, the Entry Box(for pasting the copied URL), 'Paste URL' Button, 'Save To' Button(for selecting the directory where the downloaded file will be stored), Status Label(for showing the current status among these options: 'Not Selected', 'Downloading...', or 'Download Completed'), and finally the 'Download' button.


# This function displays all the widgets in the Main Page
def Main_Page(self):
self.URL = Entry(self.frame_3, \
font=("Helvetica", 18), width=41)
self.URL.place(x=20,y=20)

# Paste URL Button
paste_btn = Button(self.frame_3, text="Paste URL", \
command=self.Paste_URL)
paste_btn.place(x=580,y=20)

# Resolution Label
resolution_lbl = Label(self.frame_3, \
text="Download Quality", \
font=("Times New Roman", 13, 'bold'))
resolution_lbl.place(x=150, y=70)

self.quality = StringVar()
# Combo Box for showing the available video resolution
# and Audio download options
self.quality_combobox = ttk.Combobox(self.frame_3, \
textvariable=self.quality, font=("times new roman",13))
self.quality_combobox['values'] = download_quality
self.quality_combobox.current(0)
self.quality_combobox.place(x=310,y=70)

# Save To Button: Where the downloaded file will be stored
save_to_btn = Button(self.frame_3, text="Save To", \
font=("Kokila", 10, 'bold'), bg="gold", width=6, \
command=self.Select_Directory)
save_to_btn.place(x=150, y=110)

# Tkinter Label sor showing the Save To location path
# on the window
self.loc_label = Label(self.frame_3, \
text=self.save_to_loc, font=("Helvetica", 12), \
fg='blue', bg='white')
self.loc_label.place(x=240, y=116)

status_lbl = Label(self.frame_3, text="Status", \
font=("Times New Roman", 13, 'bold'))
status_lbl.place(x=150, y=160)

# Status Label:
# Options: 'Not Slected(By Default), or 'Downloading...',
# or 'Download Completed''
self.status = Label(self.frame_3, text="Not Selected", \
font=("Helvetica", 12), bg="white", fg="red")
self.status.place(x=220, y=163)

download_btn = Button(self.frame_3, text="Download", \
font=("Kokila", 13, 'bold'), bg="red", fg="white", \
width=8, command=self.Threading)
download_btn.place(x=280, y=210)

Paste the Copied URL

This 'Paste_URL()' function pastes the copied URL from the clipboard in the Entry Box using the python pyperclip module.


# When the 'Paste URL' button is pressed, this function
# gets a call and paste the pre-copied text(URL) in the
# Tkinter Entry Box
def Paste_URL(self):
exact_URL = StringVar()
self.URL.config(textvariable=exact_URL)
exact_URL.set(str(pyperclip.paste()))

Choose the Save To location

The "Select_Directory()" function will let you select the directory where you want to save the file downloaded from YouTube.


# This function opens the Tkinter file dialog to
# let users select the save to location for the Yt video
def Select_Directory(self):
# Storing the 'saving location' for the result file
self.save_to_loc = filedialog.askdirectory(title = \
"Select a location")
self.loc_label.config(text=self.save_to_loc)

Create a different Thread and call the Download function

Let's create a different thread using the Thread module to perform the Download Operation from YouTube. It will help the application run smoothly without any lack.


# Creating a different thread to run the 'Download' function
def Threading(self):
# Killing a thread through "daemon=True" isn't a good idea
self.x = Thread(target=self.Download, daemon=True)
self.x.start()

def Download(self):
# If the user doesn't enter any URL, a warning messagebox
# will popped up
if self.URL.get() == '':
messagebox.showwarning('Warning!', \
"Please Enter a Valid URL")
else:
try:
yt = YouTube(self.URL.get())
# If the user selects 'Audio Only' option
# from the combo box(Download the Audio)
if self.quality_combobox.get() == 'Audio Only':
self.status.config(text="Downloading...")
audio = yt.streams.filter(type="audio").last()
audio.download(output_path=self.save_to_loc)
# If the user selects any video resolution from
# the combo box
else:
self.status.config(text="Downloading...")
video = yt.streams.filter(mime_type="video/mp4",\
res=self.quality_combobox.get(), progressive=True).first()
video.download(output_path=self.save_to_loc)

self.status.config(text="Download Completed")
except Exception as es:
messagebox.showerror("Error!", f"Error due to {es}")

The miscellaneous functions

We unknowingly forgot about those two buttons in the header section. Now is their time. Here, we'll declare two functions. When the 'About' and 'Exit' button is pressed these two("About_Window()", and "Exit_Window()") functions will get a call respectively.


# When the 'About' button is pressed, this function gets a call
def About_Window(self):
messagebox.showinfo("Yt Downloader 22.05",\
"Developed by Subhankar Rakshit\n~PySeek")

# This function closes the main window
def Exit_Window(self):
self.window.destroy()

The main function

Now declare the main function. Here, we will create a Tk(), and a 'Yt_Downloader' class object. The Tkinter event loop(mainloop) will start running from here.


# The main function
if __name__ == "__main__":
root = Tk()
# Creating a 'Yt_Downloader' class object
obj = Yt_Downloader(root)
root.mainloop()

Download the Project

Download the entire project file from my GitHub page(https://github.com/subhankar-rakshit) through the download button.

Important Note
If you get an error message like this: "HTTP Error 410: Gone" while running this project, follow the next step to resolve the issue.
Do upgrade the pytube library with the help of the following command.
python -m pip install --upgrade pytube

Summary

In this tutorial, we've done an interesting python project. We built a YouTube Video Downloader using Python. You can download Audio too from YouTube using this Downloader Application.

To get more lovely Tkinter Examples, visit the separate page created only for Python Projects. Some examples are given below.

👉 A PDF Editor using Python(Split, Merge, and Rotate)

👉Image to Pencil Sketch Converter in Python

👉An Advanced Alarm Clock using Python Tkinter

It can be used by anyone even if he/she is a non-programmer just because of its simple graphical interface. Do use this Yt Downloader personally and please share your opinion in the comment section. It would be a great inspiration for me also.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post