Python tkinter – YouTube content downloader (MP4 & MP3)

Here we will be creating a YouTube content downloader, it will make use of the tkinter framework which will enable us to a UI. The script will take input from the user and give the option to download content as either MP4 or MP3 format. To download content we will be using pytube which is a lightweight, Pythonic, dependency-free, library for downloading YouTube Videos. The library is very easy to use and quite intuitive.

We begin by creating and defining the various elements of our UI. This consists of an entry field by which the user will input the URL of the YouTube video, 2 radio buttons to chose from MP4 and MP3 formats and a start button. In addition to this we also have an empty label named ‘lbl3’ which we will use to notify of any output i.e. success/failure messages to the user.

window=Tk()
window.configure(bg='white')

lbl=Label(window, text="YouTube Tool.", bg='white', fg='red', font=("System", 34))
lbl.place(x=125, y=5)
lbl1=Label(window, text="-Scriptopia 2022-", bg='white', fg='black', font=("System", 11))
lbl1.place(x=200, y=50)

lbl2=Label(window, text="Video URL:", bg='white', fg='black', font=("System", 11))
lbl2.place(x=10, y=82)
txtfld=Entry(window, text="This is Entry Widget", bd="5",width="55")
txtfld.place(x=95, y=80)

btn=Button(window, text="Start",bg='white', fg='black',command=download)
btn.place(x=440, y=78)

var = IntVar()
R1 = Radiobutton(window, text="Download MP4",bg='white', fg='black', variable=var, value=1)
R1.place(x=120, y=125)

R2 = Radiobutton(window, text="Download MP3",bg='white', fg='black', variable=var, value=2)
R2.place(x=270, y=125)

lbl3=Label(window, text="", bg='white', fg='black', width="20", font=("System", 11))
lbl3.place(x=155, y=150)

window.title('Scriptopia')
window.geometry("500x180")
window.mainloop()

To our button we add a command binding that associates a function named ‘download’, meaning in the event of a button click the function will be called automatically.

Our download function starts by getting the value of the URL field, we first check for a blank – if blank, we output a message to the user saying “Please Enter a URL”. If the URL is not a blank we then check which radio button option has been selected i.e. MP4 or MP3.

Using the URL we then create a YouTube object. Next, we select the stream we are wanting (more information on this can be found here) before downloading the content. We use itag 18 for the MP4 format and the itag 251 for the MP3.

def download():
    Option = str(var.get())
    Link = str(txtfld.get())
    if Link == "":
        lbl3.config(text = "Please Enter a URL")
    if Link != "":   
        if Option == "0" or Option == "1":
            yt = YouTube(Link)
            stream = yt.streams.get_by_itag(18)
            stream.download()
            lbl3.config(text = "Complete!")
        if Option == "2":
            yt = YouTube(Link)
            stream = yt.streams.get_by_itag(251)
            stream.download()
            lbl3.config(text = "Complete!")

The full source code for this project can be found below:

from tkinter import *
from pytube import YouTube
import time

def download():
    Option = str(var.get())
    Link = str(txtfld.get())
    if Link == "":
        lbl3.config(text = "Please Enter a URL")
    if Link != "":   
        if Option == "0" or Option == "1":
            yt = YouTube(Link)
            stream = yt.streams.get_by_itag(18)
            stream.download()
            lbl3.config(text = "Complete!")
        if Option == "2":
            yt = YouTube(Link)
            stream = yt.streams.get_by_itag(251)
            stream.download()
            lbl3.config(text = "Complete!")

window=Tk()
window.configure(bg='white')

lbl=Label(window, text="YouTube Tool.", bg='white', fg='red', font=("System", 34))
lbl.place(x=125, y=5)
lbl1=Label(window, text="-Scriptopia 2022-", bg='white', fg='black', font=("System", 11))
lbl1.place(x=200, y=50)

lbl2=Label(window, text="Video URL:", bg='white', fg='black', font=("System", 11))
lbl2.place(x=10, y=82)
txtfld=Entry(window, text="This is Entry Widget", bd="5",width="55")
txtfld.place(x=95, y=80)

btn=Button(window, text="Start",bg='white', fg='black',command=download)
btn.place(x=440, y=78)

var = IntVar()
R1 = Radiobutton(window, text="Download MP4",bg='white', fg='black', variable=var, value=1)
R1.place(x=120, y=125)

R2 = Radiobutton(window, text="Download MP3",bg='white', fg='black', variable=var, value=2)
R2.place(x=270, y=125)

lbl3=Label(window, text="", bg='white', fg='black', width="20", font=("System", 11))
lbl3.place(x=155, y=150)

window.title('Scriptopia')
window.geometry("500x180")
window.mainloop()

Leave a Reply