DEV Community

Cover image for How to download Youtube Music and Videos with Python

How to download Youtube Music and Videos with Python

Shittu Olumide on February 07, 2023

This article will discuss how to easily download music and videos from Youtube using the Python programming language. The reason why we will be usi...
Collapse
 
valentinopereira profile image
Valentino Pereira

Getting this error

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 1348, in do_open h.request(req.get_method(), req.selector, req.data, headers, File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1276, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1322, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1271, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1031, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 969, in send self.connect() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 1448, in connect self.sock = self._context.wrap_socket(self.sock, File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 512, in wrap_socket return self.sslsocket_class._create( File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1070, in _create self.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1341, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/valentinopereira/Documents/practice/youtube/index.py", line 17, in <module> download("https://www.youtube.com/watch?v=eFTLKWw542g") File "/Users/valentinopereira/Documents/practice/youtube/index.py", line 7, in download video = video.streams.get_highest_resolution() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pytube/__main__.py", line 295, in streams self.check_availability() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pytube/__main__.py", line 210, in check_availability status, messages = extract.playability_status(self.watch_html) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pytube/__main__.py", line 102, in watch_html self._watch_html = request.get(url=self.watch_url) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/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.10/lib/python3.10/site-packages/pytube/request.py", line 37, in _execute_request return urlopen(request, timeout=timeout) # nosec File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 216, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 519, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 536, in _open result = self._call_chain(self.handle_open, protocol, protocol + File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 496, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 1391, in https_open return self.do_open(http.client.HTTPSConnection, req, File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/urllib/request.py", line 1351, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)> 
Enter fullscreen mode Exit fullscreen mode

I tried running it inside venv and even on my system python.

Collapse
 
valentinopereira profile image
Valentino Pereira

Update: Fixed it by doing these timonweb.com/python/fixing-certifi... and restarting terminal

Collapse
 
shittu_olumide_ profile image
Shittu Olumide

Glad you fixed it!

Collapse
 
dshaw0004 profile image
Dipankar Shaw

It's working but not on my local IDE (vs code). I have to use replit to run it so it is not downloading the file to my device.
Is there any way to run this on vscode ?

Collapse
 
shittu_olumide_ profile image
Shittu Olumide

Hello, thanks for reading!
It actually works on my IDE (VSCode)

Collapse
 
dshaw0004 profile image
Dipankar Shaw

I don't know how but it is working.
Now I will make a tkinter gui to give this program an application like experience.
Do I have permission to use your code to make a gui ?

Thread Thread
 
shittu_olumide_ profile image
Shittu Olumide

Yeah sure, you can go ahead.
If you also need me to collaborate, we can work on it together.
It's good to know you want to use something of mine, which is yours now 😅

Thread Thread
 
dshaw0004 profile image
Dipankar Shaw

Already done, you can check it now
YTdownloader

Thread Thread
 
shittu_olumide_ profile image
Shittu Olumide

This looks nice! 👍
Checking it out now

Thread Thread
 
dshaw0004 profile image
Dipankar Shaw

please rate this app 😊

Collapse
 
dshaw0004 profile image
Dipankar Shaw

It is printing "Video downloaded!" but the folder where i want to install it is empty.
In case of Replit it actually create a folder of that name and add the file to it.

Collapse
 
chrisgreening profile image
Chris Greening

I've had a good bit of fun messing around with pytube for various projects but it's been a while, I'll have to check it out again - thanks for the tutorial!

Collapse
 
shittu_olumide_ profile image
Shittu Olumide

You are welcome!

Collapse
 
valentinopereira profile image
Valentino Pereira

The function calls in your main function are different than your definitions.

  1. download_audio is meant to be YoutubeAudioDownload
  2. download is meant to be YoutubeVideoDownload
Collapse
 
shittu_olumide_ profile image
Shittu Olumide

Opps, thanks for pointing this out!
I have edited the code 😃

Collapse
 
shittu_olumide_ profile image
Shittu Olumide

Follow for more content like this :)
I also have a YouTube channel where I post video tutorials; feel free to check it out here:

youtube.com/channel/UCNhFxpk6hGt5u...

Thanks

Collapse
 
erins5927 profile image
NerfWeb

Thanks for sharing! really helpful!

Collapse
 
shittu_olumide_ profile image
Shittu Olumide

You are welcome 🤗

Collapse
 
johadtech profile image
SmartWizz

I need exactly something like this in PHP, I have searched the web for any library but found non, Can anyone provide me with access to YouTube music API thanks.

Hopefully I find my way around it when I gets it