Build your own YouTube Downloader software with Python

In this tutorial, we are going to develop YouTube video Downloader software using python programming language. YouTube video downloader is a software that allows us to download YouTube videos by pasting the video’s URL, this allows us to download videos and watch these videos offline.

To download this application you need to know the basics of Python programming language, Python is one of the most popular and advanced programming languages which allows us to develop all kinds all applications. We have developed a Python Tutorial for beginners to advance, learn Python from here.

Python Youtube Video Downloader Project

Python YouTube downloader project allows us to download any type of YouTube video in a faster and easy way.

In this software, the user needs to copy the URL from that youtube video and he needs to paste that. He can also give his own location or else it will show a default download location, there is also another feature that allows us to give a custom name for that video, after that, you can click on the Download button so that the video stat’s to download that location.

Follow these steps to develop python YouTube downloader

Downloading Python Libraries:

To implement this project we need to install two packages that are Tkinter and pytube.

  • Tkinter is a GUI library, which allows us to create and build GUI applications.
  • pytube is a YouTube video downloading library.

To install these packages go to command prompt and run these commands one by one.

pip install tkinter
Pip install pytube

Project File Structure:

Follow these steps to build YouTube downloader project.

Import Libraries:

import os
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import *
from tkinter import messagebox as mb
from pytube import YouTube

Create Display Window:

root = Tk()
root.title('Youtube Video Downloader')
frame = Frame(root)
frame.grid()
tabControl = ttk.Notebook(root)
tabControl.configure(width=450, height=300)
download_tab = ttk.Frame(tabControl)
tabControl.add(download_tab, text="Download")
tabControl.grid()
about_tab = ttk.Frame(tabControl)
tabControl.add(about_tab,text="About")
tabControl.grid()
  • Tk() function is used to initialize tkinter library to create display window.
  • title() widget will display a title on the window.
save_path = os.path.join(os.path.expanduser('~'), 'Downloads')
os.chdir(save_path)

The above code is used to set path for the video file to store. If the use did mention the file path it will show a default path.

Label(download_tab, text='Enter the Youtube link:', font=("Times New Roman", 12)).place(relx=0.05, rely=0.2)
link_strvar = StringVar(root)
link_entry = Entry(download_tab, width=35, textvariable=link_strvar)
link_entry.place(relx=0.5, rely=0.2)
Label(download_tab, text='Default Save location:', font=("Times New Roman", 12)).place(relx=0.05, rely=0.4)
dir_strvar = StringVar(root)
dir_entry = Entry(download_tab, width=35, textvariable=dir_strvar)
dir_entry.place(relx=0.5, rely=0.4)
dir_entry.insert(0,str(save_path))

Label(download_tab, text='Enter the filename(optional)', font=("Times New Roman", 12)).place(relx=0.05, rely=0.6)
filename_strvar = StringVar(root)
filename_entry = Entry(download_tab, width=35, textvariable=filename_strvar)
filename_entry.place(relx=0.5, rely=0.6)

download_btn = Button(download_tab, text='Download', font=("Times New Roman", 14),border="0",borderwidth="0", bg='#0078ff',
                      command=lambda: downloader(link_entry, dir_entry, filename_entry)).place(relx=0.3, rely=0.75)
reset_btn = Button(download_tab, text='Reset', font=("Times New Roman", 14),border="0",borderwidth="0", bg='#0078ff',
                   command=lambda: reset(link_strvar, dir_strvar, filename_strvar)).place(relx=0.6, rely=0.75)

Label(about_tab, text='YouTube Downloader developed by Regu Ram S V', font=("Times New Roman", 13)).place(relx=0.05, rely=0.2)
Label(about_tab, text='Version: 1.1', font=("Times New Roman", 12)).place(relx=0.05, rely=0.4)

Defining the downloader function:

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

In this download() function we are passing three parameters, and reset() function is used to reset all the three entry objects.

root.update()
root.mainloop()

root.mainloop() method executes when we want to run the program.

Here is the full source code:

import os
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import *
from tkinter import messagebox as mb
from pytube import YouTube


root = Tk()
root.title('Youtube Video Downloader')
frame = Frame(root)
frame.grid()
tabControl = ttk.Notebook(root)
tabControl.configure(width=450, height=300)
download_tab = ttk.Frame(tabControl)
tabControl.add(download_tab, text="Download")
tabControl.grid()
about_tab = ttk.Frame(tabControl)
tabControl.add(about_tab,text="About")
tabControl.grid()

save_path = os.path.join(os.path.expanduser('~'), 'Downloads')
os.chdir(save_path)

Label(download_tab, text='Enter the Youtube link:', font=("Times New Roman", 12)).place(relx=0.05, rely=0.2)
link_strvar = StringVar(root)
link_entry = Entry(download_tab, width=35, textvariable=link_strvar)
link_entry.place(relx=0.5, rely=0.2)

Label(download_tab, text='Default Save location:', font=("Times New Roman", 12)).place(relx=0.05, rely=0.4)
dir_strvar = StringVar(root)
dir_entry = Entry(download_tab, width=35, textvariable=dir_strvar)
dir_entry.place(relx=0.5, rely=0.4)
dir_entry.insert(0,str(save_path))

Label(download_tab, text='Enter the filename(optional)', font=("Times New Roman", 12)).place(relx=0.05, rely=0.6)
filename_strvar = StringVar(root)
filename_entry = Entry(download_tab, width=35, textvariable=filename_strvar)
filename_entry.place(relx=0.5, rely=0.6)

download_btn = Button(download_tab, text='Download', font=("Times New Roman", 14),border="0",borderwidth="0", bg='#0078ff',
                      command=lambda: downloader(link_entry, dir_entry, filename_entry)).place(relx=0.3, rely=0.75)
reset_btn = Button(download_tab, text='Reset', font=("Times New Roman", 14),border="0",borderwidth="0", bg='#0078ff',
                   command=lambda: reset(link_strvar, dir_strvar, filename_strvar)).place(relx=0.6, rely=0.75)

Label(about_tab, text='YouTube Downloader developed by Regu Ram S V', font=("Times New Roman", 13)).place(relx=0.05, rely=0.2)
Label(about_tab, text='Version: 1.1', font=("Times New Roman", 12)).place(relx=0.05, rely=0.4)

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

root.update()
root.mainloop()    

Output:

Conclusion:

Now you have successfully created your own GUI based YouTube video downloader using python. If you face any issue please post that on comment, we will solve that issue. If you like this post please share this post with your friends.

More Reading

Post navigation

Leave a Comment

Leave a Reply

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