loading...

How to Download YouTube videos in Python.

seijind profile image George Kara ・1 min read

Hello everyone!

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

_________________________________________________

Download Pytube Library

pip install pytube # python2
pip3 install pytube # python3
pip install pytube3 # if not work with pytube.

First of all, we need to import Pytube library :

import pytube

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

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

Load url in function Youtube :

 youtube = pytube.YouTube(url)

Set Streams Resolution :

video = youtube.streams.first()
# or
video = youtube.streams.get_highest_resolution()

Download Video :

video.download() # In Same Folder
# or
video.download('/Downloads') # In Other Folder

Get Information of Video :

video.title # Title
video.video_id # Id
video.age_restricted # Age

Streams Format :

video.streams.all()
stream = video.streams.all()
for i in stream:
  print(i)

Example 1 :

import pytube

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

youtube = pytube.YouTube(url)
video = youtube.streams.first()
video.download('../Video')

Example 2 :

import pytube

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

pytube.YouTube(url).streams.get_highest_resolution().download('../Video')

I hope you liked it!

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

Posted on by:

seijind profile

George Kara

@seijind

Junior Software Engineer, IT Student

Discussion

markdown guide
 

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()`

 

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.

 

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

 
 

That was the first thing I did.

Try to install pytube3 (sometimes is need it)

Still Nothing;

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

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