Python YouTube Downloader with Pytube
Python course with 57 real-time projects - Learn Python
Python YouTube Video Downloader is an application to download videos from YouTube. This provides users to download videos they need in their devices and watch them offline.
Youtube Video Downloader Python Project
The Youtube downloader project is a python project. The object of this project is to download any type of video in a fast and easy way from youtube in your device.
In this python project, user has to copy the youtube video URL that they want to download and simply paste that URL in the ‘paste link here’ section and click on the download button, it will start downloading the video. When video downloading finishes, it shows a message ‘downloaded’ popup on the window below the download button.
Steps to Develop Python YouTube Downloader
Project Prerequisites
To implement this project we use basic concept of python, tkinter, pytube library.
- Tkinter is a standard GUI library and it is one of the easiest ways to build a GUI application.
- pytube used for downloading videos from youtube
To install the required modules run pip installer command on the command line:
pip install tkinter Pip install pytube
Download Youtube Downloader Python Code
Please download the source code of YouTube video downloader: Python YouTube Downloader
These are the following steps to build youtube video downloader project in python :
- Import libraries
- Create display window
- Create field to enter link
- Create function to start downloading
1. Import Libraries
Start the project by importing the required modules.
from tkinter import * from pytube import YouTube
In this python project, we import Tkinter and pytube modules.
2. Create Display Window
root = Tk() root.geometry('500x300') root.resizable(0,0) root.title("DataFlair-youtube video downloader")
- Tk() used to initialize tkinter to create display window
- geometry() used to set the window’s width and height
- resizable(0,0) set the fix size of window
- title() used to give the title of window
Label(root,text = 'Youtube Video Downloader', font ='arial 20 bold').pack()
- Label() widget use to display text that users can’t able to modify.
- root is the name of the window
- text which we display the title of the label
- font in which our text is written
- pack organized widget in block
3. Create Field to Enter Link
link = StringVar() Label(root, text = 'Paste Link Here:', font = 'arial 15 bold').place(x= 160 , y = 60) link_enter = Entry(root, width = 70,textvariable = link).place(x = 32, y = 90)
- link is a string type variable that stores the youtube video link that the user enters.
- Entry() widget is used when we want to create an input text field.
- width sets the width of entry widget
- textvariable used to retrieve the value of current text variable to the entry widget
- place() use to place the widget at a specific position
4. Create Function to Start Downloading
def Downloader(): url =YouTube(str(link.get())) video = url.streams.first() video.download() Label(root, text = 'DOWNLOADED', font = 'arial 15').place(x= 180 , y = 210) Button(root,text = 'DOWNLOAD', font = 'arial 15 bold' ,bg = 'pale violet red', padx = 2, command = Downloader).place(x=180 ,y = 150) root.mainloop()
Url variable gets the youtube link from the link variable by get() function and then str() will convert the link in string datatype.
The video is download in the first present stream of that video by stream.first() method.
Button() widget used to display button on the window.
- text which we display on the label
- font in which the text is written
- bg sets the background color
- command is used to call the function
root.mainloop() is a method that executes when we want to run the program.
Python YouTube Downloader Output
Summary
With this project in python, we have successfully developed the youtube video downloader project using python. We used the popular Tkinter library that used for rendering graphics. We use the pytube library to download videos from youtube.
Did you like this article? If Yes, please give DataFlair 5 Stars on Google
Awesum project. Thanks for sharing..
where does the downloaded video get saved
idk
what is idk ?
it means “i dont know”
I don’t know
change to… video.download(‘/Users/johndoe/Videos’)
Your same project files path.
can you change the folder in which the video will get downloaded?that would be really helpful … ? if yes, how ?
yes you can, just enter the location where you want to save, inside brackets of video.download()
Thanks sarvesh for helping the community. Keep Growing, keep learning and happy coding.
change to video.download(‘/Users/johndoe/Videos’)
The code is running well but after pressing the download, nothing is happening. Please help me through this.
Please paste the error stacktrace (It will be in your terminal or cmd)
Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py”, line 1884, in __call__
return self.func(*args)
File “C:\Users\rajes\PycharmProjects\Echo\main.py”, line 456, in Downloader
url = YouTube(str(link.get()))
File “C:\Users\rajes\PycharmProjects\Echo\venv\lib\site-packages\pytube\__main__.py”, line 91, in __init__
self.prefetch()
File “C:\Users\rajes\PycharmProjects\Echo\venv\lib\site-packages\pytube\__main__.py”, line 183, in prefetch
self.js_url = extract.js_url(self.watch_html)
File “C:\Users\rajes\PycharmProjects\Echo\venv\lib\site-packages\pytube\extract.py”, line 143, in js_url
base_js = get_ytplayer_config(html)[“assets”][“js”]
File “C:\Users\rajes\PycharmProjects\Echo\venv\lib\site-packages\pytube\extract.py”, line 202, in get_ytplayer_config
raise RegexMatchError(caller=”get_ytplayer_config”, pattern=”config_patterns”)
pytube.exceptions.RegexMatchError: get_ytplayer_config: could not find match for config_patterns
It is giving me this error.
This is because the video you are trying to download is missing / deleted / private. Try it with another video.
I’ve tried many other video links but getting the same error.Can you help pls?
I’ve tried many other videos,but getting the same error.Can you help pls?
Actually this code isn’t working in my pc.
I write it as it is but still not working.
Maybe you are missing some dependencies, try again after installing pytube.
Actually this code isn’t working in my pc.
I write it as it is but still not working.
Can you fixed it.
Hey
Did you got this error sorted? Please let me know. I’m getting same error.
Thanks in advance!
We would recommend you to download the code, and then run it again. You might have some syntactical errors in your code (indentation error is very common)
in case if the issue persists try to install pytube again
I was literally cracking my head …what to do for the python project but then I saw this project and hope that it worked. But somehow worked it and the code is really easy to understand…
Thank you so much again….
Happy to hear that you are able to do this Python project and we are able to help you. Share with your friends and others to spread the knowledge.
where is downloaded vdo saved ?
change to video.download(‘/Users/johndoe/Videos’)
I have tried this.but when ever I paste the URL and ask to download then it was trowing an error regex somthing like that will u plz help me out from this
when I send a URL and click download I get this error !!!! please help
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
when you click on download and it shows downloaded then you wanna go to back to the folder where the main.py file is located at ..thats where you’ll find the downloaded video..you’re welcome
“Label is not defined” error is coming.. anyone help me out
Lable(root,text=’YouTube Video Downloader’,font=’arial 20 bold’).pack()
NameError: name ‘Lable’ is not defined
This error generally occurs when you misspelling a variable, function etc. In this situation the correct term is “label”, not “lable”. Please ensure the necessary correction.
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 555, in error
result = self._call_chain(*args)
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 494, in _call_chain
result = func(*args)
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 747, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 523, in open
response = meth(req, response)
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 632, in http_response
response = self.parent.error(
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 561, in error
return self._call_chain(*args)
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 494, in _call_chain
result = func(*args)
File “C:\Users\mores\AppData\Local\Programs\Python\Python39\lib\urllib\request.py”, line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
” I’m getting this type of error”
Hey
Did you got this error sorted? Please let me know. I’m getting same error.
Thanks in advance!
How you guys are commenting. I am unable to comment. I am getting the following error.
ImportError: cannot import name ‘youtube’ from ‘pytube’ (c:\users\appdata\local\programs\python\python38-32\lib\site-packages\pytube\__init__.py)
xception in Tkinter callback
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”, line 1346, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”, line 1257, in request
self._send_request(method, url, body, headers, encode_chunked)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”, line 1303, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”, line 1252, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”, line 1012, in _send_output
self.send(msg)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”, line 952, in send
self.connect()
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”, line 1426, in connect
self.sock = self._context.wrap_socket(self.sock,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”, line 500, in wrap_socket
return self.sslsocket_class._create(
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”, line 1040, in _create
self.do_handshake()
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”, line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py”, line 1892, in __call__
return self.func(*args)
File “/Users/nobelna/Documents/Youtube Downloader/yt.py”, line 22, in Downloader
video = url.streams.first()
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytube/__main__.py”, line 284, in streams
self.check_availability()
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytube/__main__.py”, line 199, in check_availability
status, messages = extract.playability_status(self.watch_html)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytube/__main__.py”, line 91, in watch_html
self._watch_html = request.get(url=self.watch_url)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytube/request.py”, line 53, in get
response = _execute_request(url, headers=extra_headers, timeout=timeout)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pytube/request.py”, line 37, in _execute_request
return urlopen(request, timeout=timeout) # nosec
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”, line 214, in urlopen
return opener.open(url, data, timeout)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”, line 517, in open
response = self._open(req, data)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”, line 534, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”, line 494, in _call_chain
result = func(*args)
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”, line 1389, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”, line 1349, in do_open
raise URLError(err)
urllib.error.URLError:
How can i resolve this issue?
i cant find the video if the application download it
Useful tutorial. How does one change the quality of the video being downloaded?
under the def function you could change video = url.streams.first() to video = url.streams.get_highest_resolution() and you would get the highest resolution possible
is it possible to change the resolution? it seems like it downloads the smallest one
It’s working for me but video i’ve downloaded is in such low quality its hard to watch. Any tips how to increase downloaded quality?
How can I increase the quality of the downloaded video? Or at least how can I add an option to pick the quality I’d like to download the video in?
How to improve the quality of the downloaded video?
how to add a progress bar to this
Any way to make the quality better? Thanks in advance.
Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users\Shree\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py”, line 1892, in __call__
return self.func(*args)
File “F:\Prashant\pythonProject\youtube_download.py”, line 24, in Downloader
video = url.streams.first()
File “F:\Prashant\pythonProject\venv\lib\site-packages\pytube\__main__.py”, line 292, in streams
return StreamQuery(self.fmt_streams)
File “F:\Prashant\pythonProject\venv\lib\site-packages\pytube\__main__.py”, line 177, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File “F:\Prashant\pythonProject\venv\lib\site-packages\pytube\extract.py”, line 409, in apply_signature
cipher = Cipher(js=js)
File “F:\Prashant\pythonProject\venv\lib\site-packages\pytube\cipher.py”, line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
File “F:\Prashant\pythonProject\venv\lib\site-packages\pytube\cipher.py”, line 387, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File “F:\Prashant\pythonProject\venv\lib\site-packages\pytube\cipher.py”, line 301, in get_throttling_function_code
code_lines_list = find_object_from_startpoint(js, match.span()[1]).split(‘\n’)
AttributeError: ‘NoneType’ object has no attribute ‘span’
Please help
Hello, thanks for this code all working properly 🙂
but one question. I downloaded video I want, but quality is very very low. Then I see video on youtube quality is awesome and fine, but after download its very poor. Is it possible to make it better?
change video to this
video = url.streams.get_highest_resolution()
i had the same problem
‘pip install pytube’ on the Command Prompt