[Python] I made a Youtube Downloader with Tkinter.

1.First of all

Using Tkinter and Pytube, I made a GUI program to download Youtube videos.

image.png

2.1.pytyube

Please refer to the link below for how to install and use Pytube.

https://python-pytube.readthedocs.io/en/latest/

2.2. Introduction of Threading

Introduce Multi-threading to Freeze while maintaining GUI responsiveness.

2.2.1. Generation order

Design Threads in the following order. Call Back function of Start button → Thread generation method → Execution method

from threading import Thread

#Call Back function
def click_me(self):
    self.create_thread()

#Thread generation method
def create_thread(self):
    self.run_thread = Thread( target=self.method_in_a_thread )
    self.run_thread.start()
    print(self.run_thread)

#Execution method
def method_in_a_thread(self):
    print('New Thread is Running.')
    self.get_youtube( self.URL_name.get(), self.Folder_name.get())

2.2.2. Call

self.btn_Start = tk.Button(self.frame_form, text = 'Start')
self.btn_Start.configure( font= self.font02 )
self.btn_Start.grid( column=1, row=2, padx=20, pady=20, sticky= tk.W + tk.E )
self.btn_Start.configure( command = self.click_me)

3. How to use

Enter the URL of the Youtube video to download and specify the folder to download. Then press the Start button. Take, for example, the video of Mr. Samurai, who is making a leap forward in the commentary video of the NHK Taiga drama "Kirin ga Kuru". https://www.youtube.com/watch?v=NYUYYMs-5UY After downloading, it looks like it will be played on the player. image.png

4. Summary

  1. Use a module called Pytube to download Youtube.
  2. Use Tkinter to create a GUI.
  3. Introduced Multi-threading to maintain GUI responsiveness.

5. Code

GUI_youtube.py


from pytube import YouTube
import tkinter as tk

from tkinter import Menu
from tkinter import messagebox as msg
from tkinter import font
from tkinter import filedialog

from tkinter import ttk
import os
from time import sleep
from threading import Thread

class Application(tk.Frame):
    def __init__(self,master):
        super().__init__(master)
        self.pack()

        self.master.geometry("800x500")
        self.master.title("Youtube Downloader")
        self.master.resizable(False, False)

        # ---------------------------------------
        # Favicon
        # ---------------------------------------
        self.iconfile = "./favicon.ico"
        self.master.iconbitmap(default=self.iconfile)
        self.create_widgets()



    def create_widgets(self):


        # ---------------------------------------
        # font
        # ---------------------------------------
        self.font01 = font.Font(family="Helvetica", size=15, weight="normal")
        self.font02 = font.Font(family='Helvetica', size=15, weight='bold')
        self.font03 = font.Font(family='Helvetica', size=30, weight='bold')

        # ---------------------------------------
        # Menu
        # ---------------------------------------
        self.menu_bar = Menu( self.master )
        self.master.config( menu=self.menu_bar )

        self.file_menu = Menu( self.menu_bar, tearoff=0 )
        self.file_menu.add_command( label='Exit', command=self._quit )
        self.menu_bar.add_cascade( label='File', menu=self.file_menu )



        # Add another help menu
        self.help_menu = Menu( self.menu_bar, tearoff=0 )
        self.help_menu.add_command( label='About', command=self._msgBox )  # Display messagebox when clicked
        self.menu_bar.add_cascade( label='Help', menu=self.help_menu )

        # ---------------------------------------
        # Main Label
        # ---------------------------------------
        self.lbl_main = ttk.Label(self.master, text = 'Youtube Downloader', font=self.font03)

        self.lbl_main.place(relx = 0.25, rely = 0.02)

        # ---------------------------------------
        # Frame : URL Input Form, Downloader Folder
        # ---------------------------------------
        self.frame_form = tk.Label(self.master)
        self.frame_form.place(relx = 0.01 , rely = 0.25, height = 400 , width= 780 )
        # ---------------------------------------
        # URL Input Form
        # ---------------------------------------

        self.lbl_URL = ttk.Label(self.frame_form, text = 'URL')
        self.lbl_URL.configure(font=self.font01)
        self.lbl_URL.grid(column=0, row=0, padx = 20, pady= 20)

        self.URL_name = tk.StringVar()

        self.ent_URL = ttk.Entry(self.frame_form, textvariable = self.URL_name)
        self.ent_URL.configure(width = 35, font= self.font01)
        self.ent_URL.grid(column=1, row=0, padx = 20, pady= 20)
        self.ent_URL.focus()


        # ---------------------------------------
        # Download Folder
        # ---------------------------------------

        self.Folder_name = tk.StringVar()

        self.lbl_folder = ttk.Label( self.frame_form, text='Download\n  Folder' )
        self.lbl_folder.configure( font=self.font01 )
        self.lbl_folder.grid( column=0, row=1, padx=20, pady=20 )

        self.ent_Folder = ttk.Entry(self.frame_form,textvariable = self.Folder_name)
        self.ent_Folder.configure(width =35,font= self.font01)
        self.ent_Folder.grid(column=1, row=1, padx=20, pady=20)


        self.btn_Folder = tk.Button( self.frame_form, text = 'Set Folder Path')
        self.btn_Folder.configure(font = self.font02)
        self.btn_Folder.grid(column=2, row=1, padx=20, pady=20, sticky=tk.W + tk.E )
        self.btn_Folder.configure( command= self._get_Folder_Path)



        # ---------------------------------------
        # Start Button
        # ---------------------------------------

        self.btn_Start = tk.Button(self.frame_form, text = 'Start')
        self.btn_Start.configure( font= self.font02 )
        self.btn_Start.grid( column=1, row=2, padx=20, pady=20, sticky= tk.W + tk.E )
        self.btn_Start.configure( command = self.click_me)
        #
        # ---------------------------------------
        # Progress Bar
        # ---------------------------------------

        self.progress_bar = ttk.Progressbar(self.frame_form, orient='horizontal', length=286, mode = 'determinate')
        self.progress_bar.grid(column=1, row=3, padx=20, pady=12,sticky=tk.W + tk.E)


    # ---------------------------------------
    # Create Callback Functions
    # ---------------------------------------

    #Python Treading to prevent GUI freezing.

    def click_me(self):
        self.create_thread()

    def create_thread(self):
        self.run_thread = Thread( target=self.method_in_a_thread )
        self.run_thread.start()
        print(self.run_thread)

    def method_in_a_thread(self):
        print('New Thread is Running.')
        self.get_youtube( self.URL_name.get(), self.Folder_name.get())


    # Display a Message Box
    def _msgBox(self):
        msg.showinfo('Program Information', 'Youtube Downloader with Tkinter \n (c) 2020 Tomomi Research Inc.')

    # Youtube Download Function
    def get_youtube(self, y_url, download_folder):

        #Youtube Instance
        yt = YouTube(y_url)
        yt.streams.filter(progressive=True ,subtype='mp4' ).get_highest_resolution().download( download_folder )

        #progress bar

        self.progress_bar["maximum"] = 100

        for i in range(101):
            sleep(0.05)
            self.progress_bar["value"]= i
            self.progress_bar.update()

    # Exit GUI cleanly
    def _quit(self):
        self.master.quit()
        self.master.destroy()
        exit()

    # Get Folder Path
    def _get_Folder_Path(self):
        iDir = os.path.abspath(os.path.dirname(__file__))
        folder_Path = filedialog.askdirectory(initialdir = iDir)

        self.Folder_name.set(folder_Path)

def main():
    root = tk.Tk()
    app = Application(master=root)#Inherit
    app.mainloop()

if __name__ == "__main__":
    main()


Reference material

  1. Let's use pytube
  2. GUI tool for downloading videos from YouTube (Python) 3.Threading in Tkinter python improve the performance

Recommended Posts

[Python] I made a Youtube Downloader with Tkinter.
I made a fortune with Python.
I made a daemon with Python
I made a simple typing game with tkinter in Python
I made a character counter with Python
I made a puzzle game (like) with Tkinter in Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made blackjack with python!
I made a python text
I made blackjack with Python.
I made wordcloud with Python.
I made a Twitter BOT with GAE (python) (with a reference)
I made a Christmas tree lighting game with Python
I made a window for Log output with Tkinter
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
I made a payroll program in Python!
I drew a heatmap with seaborn [Python]
I tried a functional language with Python
What I did with a Python array
I made a life game with Numpy
I made a stamp generator with GAN
After studying Python3, I made a Slackbot
I made a WEB application with Django
Life game with Python [I made it] (on the terminal & Tkinter)
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a package that can compare morphological analyzers with Python
I made a Nyanko tweet form with Python, Flask and Heroku
I made a lot of files for RDP connection with Python
[Python] I made an image viewer with a simple sorting function.
I made a shuffle that can be reset (reverted) with Python
I made a poker game server chat-holdem using websocket with python
I made a segment tree with python, so I will introduce it
I made a program that automatically calculates the zodiac with tkinter
I made a stamp substitute bot with line
I made a python dictionary file for Neocomplete
〇✕ I made a game
I made a tool to automatically browse multiple sites with Selenium (Python)
GUI image cropping tool made with Python + Tkinter
I want to make a game with Python
Procedure for creating a LineBot made with Python
I made a simple Bitcoin wallet with pycoin
I made a downloader for word distributed expression
I made a LINE Bot with Serverless Framework!
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a tool that makes decompression a little easier with CLI (Python3)
I made a random number graph with Numpy
I want to write to a file with Python
I made a Caesar cryptographic program in Python.
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I made a module PyNanaco that can charge nanaco credit with python
I tried to make a simple mail sending application with tkinter of Python