Create Python YouTube Downloader using Pytube
FREE Online Courses: Enroll Now, Thank us Later!
YouTube Video Downloader is an application that we require many times. The user can use this application to directly download a video by entering the link in this application. Let’s try building this YouTube Video Downloader project with the help of Python.
About YouTube Video Downloader
The objective of a YouTube Video Downloader is to let the user download a video available on Youtube. In the YouTube Video Downloader, one has to enter the link of YouTube video and the video gets downloaded and saved in the system. You can download as many videos as you want with it.
Python Youtube Video Downloader Project Details
In this project, we are going to create a GUI Window that will have the following widgets:
1. An Entry Box – To enter the link of a YouTube video.
2. Checkboxes – To check the quality/resolution of the video.
3. Button – To download the video.
Using this project, you can download a video from YouTube in any quality that you desire.
Project Prerequisites
To develop this project, we need to pre install the following modules:
1. Tkinter Module – this is the module that helps us create an easy GUI using Python.
2. Pytube Module – this is the module that will help us download videos from YouTube.
Download Python YouTube Downloader Code
Before proceeding with the project, please download the python youtube downloader source code from the following link: YouTube Video Downloader Project Code
Steps to Build the Project
Let us look at the steps to build the YouTube Video Downloader Project:
1. Importing the Modules
2. Creating the GUI and adding components.
3. Download Function
1. Importing the Modules:
#importing modules from pytube import YouTube from tkinter import * import tkinter as tk
- We are importing the required modules.
2. Creating the GUI Window and Adding Components:
# Create an instance of tkinter frame or window window=Tk() # Set the size of the tkinter window window.geometry("700x350") window.title("PythonGeeks")#give title to the window Label(window, text="YOUTUBE VIDEO DOWNLOADER",bg='grey', font=('Calibri 15')).pack()# a label Label(window, text="Enter the link to download", font=('Calibri 12')).pack()# a label Entry(window,textvariable=text,width=50).pack()#entry field Checkbutton(text='360p',onvalue=18, offvalue=0,variable=res1).pack()#creating checkbox Checkbutton(text='720p',onvalue=22, offvalue=0,variable=res2).pack()#creating checkbox Checkbutton(text='1080p',onvalue=37, offvalue=0,variable=res3).pack()#creating checkbox #creating a button Button(window,text="Download",bg='green',command=downloader).pack()
1. Tk() – The Tk() method helps us create a blank GUI Window.
2. geometry() – The geometry() method defines the size of the GUI Window.
3. Title – The title() method gives an appropriate title to the GUI Window.
4. Label() -The label() method creates a widget on the GUI Window which helps us display a piece of text. You can specify the following attributes inside a Label() method:
- Text – what text to be displayed.
- Bg – Background colour.
- Fg- Foreground colour.
- Font – The font of the text
5. Entry() – The Entry() method helps us create an entry field on the GUI Window. We can specify the following things inside the Entry() method:
- Width – The width of the entry field.
- Textvariable – what variable will be associated with the entry field.
6. Checkbutton() – The CheckButton() method creates check buttons on the GUI Window. For one checkbutton the method is used one. Inside a checkbutton() method, we can specify the following things:
- Onvalue – The value assigned when the check button is checked.
- Text – The text visible with the checkbutton.
- Textvariable – The variable associated with the checkbutton.
7. Button() – The button() method helps us create a Button on the GUI Window. Inside a Button() method, we can specify the following attributes:
- Text – Text on the button.
- Command – The function to be evoked when the button is clicked.
- Bg – Colour of the button.
- Fg – Foreground colour.
8. pack() – pack function s to display all the widgets on the GUI Window.
3. Download Function:
def downloader():#defining a function global res#global variable t=text.get()#getting the value video = YouTube(t) if res1==18: res=18 elif res2==22: res=22 elif res3==37: res=37 video_streams = video.streams.filter(file_extension = 'mp4').get_by_itag(res) video_streams.download(filename = "Untitled", output_path = "video_path") Label(window,text="Downloaded Successfully").pack()
- We have created the downloader function to download a video from YouTube.
- We have created a global variable res.
- get() – Using the get() method, we get the value entered in the entry field.
- YouTube() – Extracts the video from YouTube.
- video.streams.filter() – Is to set the filter to the video.
- get_by_itag() – Is to specify the resolution of the video.
- download() – Is to download the video.
window.mainloop()
- mainloop() – mainloop() function displays our window and the components on it.
Python YouTube Downloader Output
Summary
Hurray! We have successfully completed the YouTube Video Downloader Project in Python. We learned how to use the Tkinter Module and Pytube Module. You can add more resolution options to it.
I hope you enjoyed building this project with PythonGeeks!
Traceback (most recent call last):
File “C:\Users\rocke\Documents\test_file.py”, line 13, in
Entry(window,textvariable=text,width=50).pack()#entry field
NameError: name ‘text’ is not defined. Did you mean: ‘Text’?
Can anyone tell me why there is a error when i ran the program?
You got declare this variables,
text = StringVar(window, value=’YouTube URL’)
res1 = IntVar()
res2 = IntVar()
res3 = IntVar()
Good
I need a project by using python the name of the project is chatboat
everything is working correctly but there is error in tkinter library. Here is the error >>>>>
Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users\Admin\AppData\Local\Programs\Python\Python311-32\Lib\tkinter\__init__.py”, line
1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File “c:\Users\Admin\Desktop\python2.py”, line 36, in downloader
video_streams = video.streams.filter(file_extension=’mp4′).get_by_itag(r)
^^^^^^^^^^^^^
File “C:\Users\Admin\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\pytube\__main__.py”, line 296, in streams
return StreamQuery(self.fmt_streams)
^^^^^^^^^^^^^^^^
File “C:\Users\Admin\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\pytube\__main__.py”, line 181, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File “C:\Users\Admin\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\pytube\extract.py”, line 409, in apply_signature
cipher = Cipher(js=js)
^^^^^^^^^^^^^
File “C:\Users\Admin\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\pytube\cipher.py”, line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Admin\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\pytube\cipher.py”, line 411, in get_throttling_plan
transform_plan_raw = find_object_from_startpoint(raw_code, match.span()[1] – 1)
^^^^^^^^^^
AttributeError: ‘NoneType’ object has no attribute ‘span’
everything is okey but the video didn’t get downloaded.