DEV Community

loading...

Python Download YouTube Video With PyTube How to Download YouTube videos in Python.

George Karanikolas
Mobile Developer, IT Student
・1 min read

Hello everyone!

This tutorial is "How to Download Youtube videos in Python.

_________________________________________________

Download Pytube Library

pip install pytube # python2
Enter fullscreen mode Exit fullscreen mode
pip3 install pytube # python3
Enter fullscreen mode Exit fullscreen mode
pip install pytube3 # if not work with pytube.
Enter fullscreen mode Exit fullscreen mode

First of all, we need to import Pytube library :

import pytube
Enter fullscreen mode Exit fullscreen mode

After that, You need copy the URL of the Youtube video :

url = 'https://www.youtube.com/watch?v=4SFhwxzfXNc'
Enter fullscreen mode Exit fullscreen mode

Load url in function Youtube :

 youtube = pytube.YouTube(url)
Enter fullscreen mode Exit fullscreen mode

Set Streams Resolution :

video = youtube.streams.first()
# or
video = youtube.streams.get_highest_resolution()
Enter fullscreen mode Exit fullscreen mode

Download Video :

video.download() # In Same Folder
# or
video.download('/Downloads') # In Other Folder
Enter fullscreen mode Exit fullscreen mode

Get Information of Video :

video.title # Title
video.video_id # Id
video.age_restricted # Age
Enter fullscreen mode Exit fullscreen mode

Streams Format :

video.streams.all()
stream = video.streams.all()
for i in stream:
  print(i)
Enter fullscreen mode Exit fullscreen mode

Example 1 :

import pytube

url = 'https://www.youtube.com/watch?v=4SFhwxzfXNc'

youtube = pytube.YouTube(url)
video = youtube.streams.first()
video.download('../Video')
Enter fullscreen mode Exit fullscreen mode

Example 2 :

import pytube

print("Give URL:")
url = input()

pytube.YouTube(url).streams.get_highest_resolution().download('../Video')
Enter fullscreen mode Exit fullscreen mode

I hope you liked it!

Library Creator : https://github.com/nficano/pytube

Discussion (18)

Collapse
mwangidenis11 profile image
Mwangi Denis

i getting this error:

AttributeError: partially initialized module 'pytube' has no attribute 'YouTube' (most likely due to a circular import)

My file:

`import pytube

print("Give URL:")
url = 'input()'

pytube.YouTube(url).streams.get_highest_resolution().download()`

Collapse
ariaenggar profile image
Aria Enggar Pamungkas • Edited

I'm getting the same error.. python 3.8, using pytube3 library.

update: it worked after I uninstall then reinstall pytube3, I think you must install pytube3 in the first place and don't try to install pytube.

Collapse
seijind profile image
George Karanikolas Author

Have you download the module?
With this command - > pip install pytube

Collapse
mwangidenis11 profile image
Mwangi Denis

That was the first thing I did.

Thread Thread
seijind profile image
George Karanikolas Author

Try to install pytube3 (sometimes is need it)

Thread Thread
mwangidenis11 profile image
Mwangi Denis

Still Nothing;

youtube = pytube.YouTube(url)
AttributeError: partially initialized module 'pytube' has no attribute 'YouTube' (most likely due to a circular import)

Thread Thread
tangopj profile image
TangoPJ

I got this error: ImportError: cannot import name 'quote' from 'pytube.compat'. Then I installed pytube3 - now it works. Thank you a lot :)

Thread Thread
brahimbc profile image
brahimbc

Hi,
verify that the name of your script is not "pytube.py"

Collapse
mwangidenis11 profile image
Mwangi Denis

That was the first thing I did

Collapse
mariemn profile image
MarieMN

I would so much love to use this!! (So yes, I liked it!) :) But unfortunately I also get an error message. The problem seems to be in line 202 of a file called extract.py

I have been trying to change a little in it, but since my knowledge of Python is still in the basics I don't really want to mess up the file without really knowing what I am doing, so please come with tips on how I can solve the following problem:

File "C:\ProgramData\Anaconda3\lib\site-packages\pytube\extract.py", line 202, in get_ytplayer_config
raise RegexMatchError(caller="get_ytplayer_config", pattern="config_patterns")

RegexMatchError: get_ytplayer_config: could not find match for config_patterns

With kind regards,
Marie

Collapse
mahdy2020 profile image
Mahdy2020

I think you're using an old version of pytube. Please run the following to uninstall pytube, and then reinstall it from the source code:

python -m pip uninstall pytube pytube3 pytubex
python -m pip install git+github.com/nficano/pytube

I had the same problem last week and i have solved it with above instructions.

Collapse
kerryn profile image
Kerry Gip

I have this issue too. I'm using Python 3.9 and in it's own IDLE
pytube.exceptions.RegexMatchError: get_ytplayer_config: could not find match for config_patterns

Collapse
atul15x profile image
Atul Chandra Dash

If use that module and it working but sometimes it said http error. When i try 3/4 time’s then it working. I don’t know why i am facing this problem. And also i get only 360p high resolution video with audio. Have any solutions on this module.

Collapse
manologk profile image
Manologk

at first, I had this error:

RegexMatchError: get_ytplayer_config: could not find match for config_patterns

I did some reinstalling, then it was able to get the video, but now a new error:

KeyError: 'content-length'

Collapse
jabbarbie profile image
Jabbar Bie

The problem's

get_highest_resolution() will look for videos that have the codec for audio, not the highest quality video

example
720p
480p
360p
140p

if you use the get_highest_resolution function, and only 360p and 140p are available (for audio codecs) .. then 360p is selected.

According to my observations, downloadable videos with video + audio are just under 480p

the rest will be forced to use video compress like ffmpeg

Collapse
chrisgreening profile image
Chris Greening

Thanks George! Been a while since I downloaded some vids with Python, maybe this'll be my motivation to get back into it 🙌

Collapse
rafaelrrs profile image
RAFAEL RODRIGUES DA SILVA

Bem simples de usar. Funcionou comigo dessa forma:

import pytube

url = 'youtube.com/watch?v=xva71wynxS0'
youtube = pytube.YouTube(url)
video = youtube.streams.first()
video.download()