Home » Tutorials » How to Build a YouTube Video Downloader using Tkinter in Python

How to Build a YouTube Video Downloader using Tkinter in Python

Have you ever wanted to download your favorite YouTube videos and decide exactly where they’re saved on your computer? In today’s guide, we’re taking a step-by-step journey to create a tool for just that!

We are going to create a graphical user interface (GUI) using Python that allows the user to download videos from YouTube and choose where to save them. So, let’s get started!

Table of Contents

Necessary Libraries

For this code to function properly, make sure to install these libraries using the terminal or your command prompt:

$ pip install tk
$ pip install pytube

Learn also: How to Make a YouTube Video Downloader with Flask in Python

Imports

from tkinter import *
from tkinter import filedialog
from pytube import YouTube

We start by importing the tkinter library, which allows us to create a graphical user interface (GUI). From tkinter we import filedialog module that is used to interact with the file system; In this case, it will allow us to choose a destination for our download. Lastly, we import the pytube library to download videos from YouTube.

Creating the Main Window

# Create main window
root = Tk()
root.title("Youtube Downloader - The Pycodes")
root.geometry("400x200")
root.resizable(False, False)

Next, we create the main window, give it a title, define its geometry and background color, and disable its resizing.

YouTube Video Downloader Function

# Download Function
def download_video():
   url = entry_url.get()
   save_path = entry_path.get()
   print(f"URL: {url}")
   print(f"Save Path: {save_path}")

   try:
       yt = YouTube(url)
       stream = yt.streams.filter(progressive=True, file_extension="mp4").order_by('resolution').desc().first()
       stream.download(save_path)
       status_label.config(text="Download complete")
   except Exception as e:
       status_label.config(text=f"Error: {str(e)}")

In this section, we create a function that extracts the YouTube video URL together with the save_path. In other words, where the user wishes to save his download, then it attempts to download the video, if the video download is successful the status label displays “Download complete“. Otherwise, an error message appears.

Browse Function

# Browse Function
def browse_path():
   directory = filedialog.askdirectory()
   entry_path.delete(0, END)
   entry_path.insert(0, directory)

After that, we create a function that opens a filedialog for the user to choose where he wants to save his downloaded video.

URL Entry

# URL Entry
label_url = Label(root, text="Enter YouTube URL:")
label_url.pack()
entry_url = Entry(root, width=40)
entry_url.pack()

Following that, we create a label that asks the user to “Enter the YouTube URL“, and below it, we make an entry widget where the user can input the URL.

Path Entry

# Path Entry
label_path = Label(root, text="Select save path:")
label_path.pack()
entry_path = Entry(root, width=40)
entry_path.pack()
button_browse = Button(root, text="Browse", command=browse_path)
button_browse.pack()

For this step, we create a label that asks the user to select the save path and below it, we make an entry widget where the desired save path will be inputted, once the user clicks the button we made in this part that says “Browse“, triggering the browse_path function by doing so.

Download Button and Status Label

# Download Button
button_download = Button(root, text="Download", command=download_video)
button_download.pack()

# Status Label
status_label = Label(root, text="Status: Ready")
status_label.pack()

This one creates a button that says “Download“, once clicked it triggers the download_video function. Then we make a label that displays the status of the download process and set it as Status: Ready.

Main Loop

root.mainloop()

Lastly, this part ensures that the main window keeps running and is responsive to the user.

Example

Full Code

from tkinter import *
from tkinter import filedialog
from pytube import YouTube


# Create main window
root = Tk()
root.title("Youtube Downloader - The Pycodes")
root.geometry("400x200")
root.resizable(False, False)


# Download Function
def download_video():
   url = entry_url.get()
   save_path = entry_path.get()
   print(f"URL: {url}")
   print(f"Save Path: {save_path}")


   try:
       yt = YouTube(url)
       stream = yt.streams.filter(progressive=True, file_extension="mp4").order_by('resolution').desc().first()
       stream.download(save_path)
       status_label.config(text="Download complete")
   except Exception as e:
       status_label.config(text=f"Error: {str(e)}")


# Browse Function
def browse_path():
   directory = filedialog.askdirectory()
   entry_path.delete(0, END)
   entry_path.insert(0, directory)


# URL Entry
label_url = Label(root, text="Enter YouTube URL:")
label_url.pack()
entry_url = Entry(root, width=40)
entry_url.pack()


# Path Entry
label_path = Label(root, text="Select save path:")
label_path.pack()
entry_path = Entry(root, width=40)
entry_path.pack()
button_browse = Button(root, text="Browse", command=browse_path)
button_browse.pack()


# Download Button
button_download = Button(root, text="Download", command=download_video)
button_download.pack()


# Status Label
status_label = Label(root, text="Status: Ready")
status_label.pack()


root.mainloop()

Happy Coding!

Leave a Comment

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

Scroll to Top