Open In App
Related Articles

Create GUI for Downloading Youtube Video using Python

Like Article
Save Article
Report issue

Prerequisites: Python GUI – tkinter

YouTube is a very popular video-sharing website. Downloading a video’s/playlist from YouTube is a tedious task. Downloading that video through Downloader or trying to download it from a random website increase’s the risk of licking your personal data. Using the Python Tkinter package, this task is very simple-efficient-safe. Few bunch codes will download the video for you. For this, there are two Python libraries – Tkinter and pytube

Modules Required:

  • pytube :pytube is a lightweight, simple-to-use, dependency-free Python library that is used for downloading videos from the web.pytube, is not an auto-configured library. You need to install it before using it. Installation of pytube is easy when you have pip. In the Terminal or Command Prompt, type the following command to install pytube.
    If you are on Mac OS X or Linux, chances are that one of the following two commands will work for you:

pip install pytube 
git clone git:// pytube | cd pytube | python install

  • If you are on Window’s 
pip install pytube3
  • Tkinter: Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit or in simple words Tkinter is used as a Python Graphical User interface. Tkinter is the native library, you don’t need to install it externally, just import, while you use it.

Below is the implementation.


# Importing necessary packages
import tkinter as tk
from tkinter import *
from pytube import YouTube
from tkinter import messagebox, filedialog
# Defining CreateWidgets() function
# to create necessary tkinter widgets
def Widgets():
    head_label = Label(root, text="YouTube Video Downloader Using Tkinter",
                       font="SegoeUI 14",
    link_label = Label(root,
                       text="YouTube link :",
    root.linkText = Entry(root,
                          font="Arial 14")
    destination_label = Label(root,
                              text="Destination :",
    root.destinationText = Entry(root,
                                 font="Arial 14")
    browse_B = Button(root,
    Download_B = Button(root,
                        text="Download Video",
                        font="Georgia, 13")
# Defining Browse() to select a
# destination folder to save the video
def Browse():
    # Presenting user with a pop-up for
    # directory selection. initialdir
    # argument is optional Retrieving the
    # user-input destination directory and
    # storing it in downloadDirectory
    download_Directory = filedialog.askdirectory(
        initialdir="YOUR DIRECTORY PATH", title="Save Video")
    # Displaying the directory in the directory
    # textbox
# Defining Download() to download the video
def Download():
    # getting user-input Youtube Link
    Youtube_link = video_Link.get()
    # select the optimal location for
    # saving file's
    download_Folder = download_Path.get()
    # Creating object of YouTube()
    getVideo = YouTube(Youtube_link)
    # Getting all the available streams of the
    # youtube video and selecting the first
    # from the
    videoStream = getVideo.streams.first()
    # Downloading the video to destination
    # directory
    # Displaying the message
                        "DOWNLOADED AND SAVED IN\n"
                        + download_Folder)
# Creating object of tk class
root = tk.Tk()
# Setting the title, background color
# and size of the tkinter window and
# disabling the resizing property
root.resizable(False, False)
root.title("YouTube Video Downloader")
# Creating the tkinter Variables
video_Link = StringVar()
download_Path = StringVar()
# Calling the Widgets() function
# Defining infinite loop to run
# application



Code Explanation:

  1. The code starts by importing the required modules.
  2. The first module is tkinter, which provides the basic functionality for creating graphical user interfaces (GUIs).
  3. Next, the tkinter module helps create a Progressbar object.
  4. This object displays the progress of an operation or task.
  5. Finally, time is imported to help calculate how long it has been since the last sleep command was executed.
  6. Next, the intro class is created.
  7. The _init_() method is used to initialize the class variables and also open a hidden window.
  8. The wind variable stores information about the current window’s position and size onscreen.
  9. The resize() method allows you to change these values without closing or reopening the window.
  10. The next line of code uses the deiconify() function to hide the main window and then calls resizable() to make sure that it remains invisible while still allowing users to resize it as needed.
  11. The code firstly creates an instance of the intro class.
  12. This class will be used to display information about the app and its features.
  13. Next, the _init_() method is called.
  14. This method is used to initialize the class variables and also to open a hidden window.
  15. The hidden window will be used to display information about the app and its features.
  16. The last part of this code is used to resize the hidden window if needed.
  17. The code starts by creating a new GeeksforGeeks Unit Converter window.
  18. The window has a label (wind) and a background color (#008080).
  19. The text in the label is “Welcome to GeeksforGeeks Unit Converter!”
  20. The next line of code creates a Label object named entry.
  21. The entry object has the following properties: • x position (50, 30), • y position (30, 50), • width (410, 30), and • height (30).
  22. Next, the code sets up the load bar.
  23. This bar will show how much progress is being made in converting units.
  24. The bar has an orientation property of HORIZONTAL and a length property of 250 pixels.
  25. The mode property is set to “indeterminate.”
  26. This means that the progressbar will not display any specific values but will instead remain in an indeterminate state until it is clicked or interacted with.
  27. Finally, the code creates an Analyze() function which will be used later on to perform unit conversions.
  28. The code creates a Label object called entry and sets the text to “Welcome to GeeksforGeeks Unit Converter!”.
  29. The label is then placed at (x=50,y=30) with a width of 410 pixels and a height of 30 pixels.
  30. Next, the code creates a Progressbar object and sets its orientation to be HORIZONTAL.
  31. The progress bar’s length is set to 250 pixels and its mode is set to “indeterminate”.
  32. Finally, the code loads the GeeksforGeeks Unit Converter module onto the screen.
  33. The code starts with a self.start button.
  34. This button opens the converter window.
  35. Next, there is a follow label.
  36. The follow label has the same background color as the start button and has white text on a blue background.
  37. The text says “Follow Me On.”
  38. The next part of the code creates two buttons: self.start and follow.
  39. The self.start button is black with a white text box that says “START.”
  40. The follow button is blue with white text that says “Follow Me On.”
  41. Finally, the code sets up how these buttons will look onscreen by setting their widths and heights.
  42. The code creates a start button and a follow me label.
  43. The start button will open the converter window, while the follow me label will display the text “Follow Me On.”
  44. The code starts with a self.git variable that stores the image of your GitHub profile.
  45. Next, there are three buttons: one for Instagram, one for Facebook, and one for Twitter.
  46. The code to create each button is very simple.
  47. The first line creates a PhotoImage object and assigns it to the self.instag variable.
  48. The second line creates a Button object and assigns it to the self.fcb variable.
  49. Finally, the third line places each button on the screen at (x=190, y=190), (x=110, y=190), and (x=350, y=190), respectively.
  50. When you run this program, you’ll see three buttons on your screen: one for GitHub, one for Instagram, and one for Facebook.
  51. To use these buttons in your code, you need to add some code to handle user input.
  52. In this example, all three buttons have command properties that allow you to execute arbitrary Python commands when the user clicks on them.
  53. For example: self.instag.command(“upload_image”) This will upload an image from your computer’s hard drive to Instagram using the Instagram API!
  54. The code will create a toolbar at the top of your document that includes buttons for GitHub, Instagram, Facebook, and Twitter.
  55. The buttons will be placed at (x=186, y=150), (x=110, y=190), (x=350, y=190), and (x=-10, y=190).
  56. The width and height of each button will be 30 pixels.
  57. The command attribute of each button will contain a lambda function that will open the corresponding link in a new tab or window.
  58. The code first opens the Google Chrome browser.
  59. It then uses the links() function to open the author’s website in a new tab.
  60. The loading() function displays a ProgressBar while the website is being loaded.
  61. Finally, the start() and load() functions are used to position and display the buttons, respectively.
  62. The code will open the link in a new browser window and load the progress bar.
  63. 3:49 PM, 11/30/2022] Ankit Kumar: The code in this function creates the necessary widgets for downloading a YouTube video.
  64. The first widget is a label called “head_label.”
  65. This label will be used to display the name of the video being downloaded.
  66. The second widget is a Label called “video_label.”
  67. This label will be used to display the filename of the video being downloaded.
  68. The third widget is a Button called “download.”
  69. This button will be used to start downloading the video.
  70. The next line of code defines two variables: “filepath” and “duration.”
  71. These variables will store information about the file that is being downloaded.
  72. The filepath variable will store the full path to the file that is being downloaded.
  73. The duration variable will store how long it has been since the last time this file was updated (in seconds).
  74. The next line of code creates an instance of FileDialog().
  75. This object allows users to select files from their computer.
  76. In this case, we are asking users to select a file from their hard drive.
  77. Next, we create an instance of Tkinter’s MessageBox() object.
  78. This object allows us to display messages onscreen.
  79. We use this object to ask users if they want to download the video or not.
  80. Finally, we create an instance
  81. The code creates a function called Widgets().
  82. This function will be used to create the necessary tkinter widgets needed for our YouTube video downloader.
  83. First, we create a Label widget called head_label.
  84. This label will be used to display the title of our YouTube video downloader.
  85. We also specify that this label should have a width of 15 pixels and a height of 15 pixels.
  86. Next, we create a Button widget called DownloadButton.
  87. This button will be used to initiate the download process for our YouTube video.
  88. We specify that this button should have a width of 25 pixels and a height of 20 pixels.
  89. Finally, we create a FileDialog widget called FileDialog1.
  90. This file dialog will be used to allow the
  91. The code starts with a head_label and link_label.
  92. The head_label has a grid that will have three rows and three columns.
  93. The first row is the column number, the second row is the column width, and the third row is the column span.
  94. The link_label has a grid that will have two rows and two columns.
  95. The first row is the column number, and the second row is the column width.
  96. The link_label also has a pady value of 5, which means it will take up five pixels in each direction on each line of its grid.
  97. Next, there are two root objects created: one for the head_label and one for the link_label.
  98. Next, text variables are set for both objects: video_Link for the head_label’s text variable and download_Path for the link_labels text variable.
  99. These variables can be edited later if needed.
  100. Then buttons are created: browse_B with a command of Browse and Download B with a command of Download Video.
  101. Both buttons have grids that specify their dimensions (width x height), background colors (bisque for Browse and thistle1 for Download Video), relief values (GROOVE), as well as button
  102. The code creates a simple two-column layout with a heading and two labels.
  103. The heading has a grid that lays out the row and column values for each label.
  104. The first label, link_label, is the text that will be displayed on the YouTube link heading.
  105. The second label, destination_label, is the text that will be displayed on the destination heading.
  106. The root object contains all of the content for the layout.
  107. The first entry in this object is for the width of the video link heading.
  108. This value is set to 35 pixels wide.
  109. Next, there is a textvariable attribute which stores the actual text that will be displayed in this heading.
  110. In this case, it’s video_Link.
  111. Finally, font and color are
  112. The code begins by defining a function, Browse().
  113. The function takes one argument, initialdir.
  114. If initialdir is not provided, the program will use the current working directory as the destination folder for saving the video.
  115. Next, the code retrieves the user’s input destination directory using filedialog.askdirectory().
  116. This function returns a string value that represents the user’s selection.
  117. The code stores this value in downloadDirectory.
  118. The final part of the code defines and calls Browse() with initialdir as its only argument.
  119. When Browse() is called, it will first present a pop-up window asking the user to select a destination folder for saving their video.
  120. If initialdir is provided, it will be used as the source folder for downloading and saving the video in that location.
  121. Otherwise, if initialdir is not provided, then the program will use the current working directory as its source folder for downloading and saving videos
  122. The code first defines a function called Browse().
  123. This function will be used to select a destination folder for saving the video.
  124. The Browse() function takes two arguments: initialdir and optional retriveDir.
  125. The initialdir argument is optional, and if left blank, the program will use the current working directory as the destination folder.
  126. RetrieveDir is an optional argument and if left blank, the program will use the current user’s default download location.
  127. Once the destination folder has been selected, the code stores it in downloadDirectory.
  128. Next, a file dialog is displayed asking the user to input a filename for saving the video.
  129. The user’s input is stored in filename.
  130. Finally, Browse() returns True if successful or False if there was an error.

Last Updated : 11 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads