Article

Creating a YouTube Video Downloader Using Python And Youtube-dl

Last updated Sept. 19, 2022

...

Creating a Youtube Downloader Using Python and Youtube-dl

There are some incredible tools already built for downloading youtube videos and audio files. However, this is a blog for hobbyists who like tinkering with stuff just because they can. The following Script offers you the ability to create and customize a downloader that fits your needs. It gives you the ability to download either video or audio files and also offers the ability to download a whole playlist. The code can also be found on GitHub.

For you to run the script you will need to install FFmpeg and set it to your computer's path for easy accessibility. You will also need to install youtube-dl via pip:

pip install youtube-dl

The code is given below:

from __future__ import unicode_literals
from tkinter import * 
import tkinter as tk
import youtube_dl
import os
from tkinter import filedialog, ttk

win = tk.Tk()

win.title('Downloader')
win.geometry('500x200')
# Minimum size of the window
win.minsize(300, 300)
# Maximum size of the window
win.maxsize(600, 600)
win['bg'] = "blue"

# Enter the url - text field
txt = Entry(win, width=50)
txt.grid(column=0, row=6, sticky=W)


########################## Logic ######################################

# Getting the destination folder.
def getdir():
    savedir = filedialog.askdirectory()
    os.chdir(savedir)
    dirpath = os.path.basename(os.getcwd())
    save_path = dirpath
    destination_display_label.config(text="Destination Folder= " + save_path)


# Getting the selection from the Radio Buttons
def sel():
    selection = str(var.get())
    return selection


# Downloading using the selected option
def download():
    selectionType = sel()
    if (selectionType == "audio"):
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl': '%(title)s.%(ext)s',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
            }]
        }
    elif (selectionType == "bvideo"):
        ydl_opts = {
            'format': 'bestvideo + bestaudio',
            'outtmpl': '%(title)s.%(ext)s',
            'writesubtitles': True,
            'writeautomaticsub': True,
            'subtitleslangs': 'en',
            'postprocessors': [{
                'key': 'FFmpegVideoConvertor',
                'preferedformat': 'mp4'
            }]
        }
    else:
        ydl_opts = {
            'format': 'best',
            'outtmpl': '%(title)s.%(ext)s',
            'writesubtitles': True,
            'writeautomaticsub': True,
            'subtitleslangs': 'en'
        }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([txt.get()])


######################### View ##########################################

# Label
label = Label(win, text="Choose Option to Download", bg="blue", font = ("arial", 10, "bold"))
label.grid(column=0, row=0)


# Radio buttons for selecting options
var = StringVar()
audio = Radiobutton(win, text="Audio", variable=var,
                    value="audio", activebackground="green", bg="blue", padx=3, font = ("arial", 10, "bold"), command=sel)
audio.grid(column=0, row=2, sticky=W)

video = Radiobutton(win, text="BestVideo", variable=var,
                    value="bvideo", activebackground="green", bg="blue", padx=3, font = ("arial", 10, "bold"), command=sel)
video.grid(column=0, row=2)

nvideo = Radiobutton(win, text="NormalVideo",
                        variable=var, value="video", activebackground="green", bg="blue", font = ("arial", 10, "bold"), command=sel)
nvideo.grid(column=0, row=2, sticky=E)

# Browse Label
browse_label = Label(win, text="Choose Download Folder", bg="blue", font = ("arial", 10, "bold"))
browse_label.grid(column=0, row=3)

# Browse button
btn = ttk.Button(win, text = 'Browse', command = lambda : getdir())
btn.grid(column=0, row=4)

# Destination label
destination_display_label = Label(win,bg="blue",font = ("arial", 10, "bold"))
destination_display_label.grid(column=0, row=5)

# Download button
btn = Button(win, text="Download", fg="white", bg="blue",
             activebackground="green", command=download)
btn.grid(column=1, row=6)


########################################################################

win.mainloop()

You can customize it further to enable you to download content that is geographically restricted by following the official documentation on youtube-dl

You can refer to this video for instructions:

Post a Comment

To leave a comment, click the button below to sign in with Google.

Signup To My Newsletter

By subscribing, you will get one email every month on tips, tutorials, and resources to improve your skills as a developer. You will get early access to my courses and videos and also access to special bonus of the time. No spam, unsubscribe at any time





Subscribe
Contact
Contact form