Site icon TechVidvan

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()

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


def reset(l_strvar, d_strvar, fn_strvar):
    l_strvar.set('')
    d_strvar.set('')
    fn_strvar.set('')

Explanation:

3. Initializing the window and placing all its components:

# Initializing the window
root = Tk()
root.title('TechVidvan Youtube Video Downloader')
root.geometry('700x200')
root.resizable(0, 0)
root.config(bg='Coral')

# 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)
link_entry.place(relx=0.5, 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)
dir_entry.place(relx=0.5, 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)
filename_entry.place(relx=0.5, 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
root.update()
root.mainloop()

Explanation:

Python YouTube Downloader Output

Summary

Congratulations! You have now created your own GUI-based YouTube Video Downloader using the Tkinter and pytube modules in Python. Now you will be able to download YouTube videos on your laptop without any hassles. There are many more changes that you can make to this project, and this will be more amazing! Have fun coding!

Exit mobile version