loading...

Python Download YouTube Video With PyTube 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
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

pic
Editor guide
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

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 Kara 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

Collapse
mwangidenis11 profile image
Mwangi Denis

That was the first thing I did.

Thread Thread
seijind profile image
George Kara 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
chrismane profile image
chrismane

Nice one! I’m not a python programmer but could a beginner do it by him self? Now we pay real money for apps, plug-in and programs that can make our life easier. If you are skilled programmed you can create or change “standards” by yourself like youtube download possibility. For example, I have special tool Youtube-MP3.me, it’s very worthy because same one click to download works on other social medias. Why they did not add function to listen content while the browser is minimized, that would be a big game changer. Plus equalizer to add and people will use it for everything they need.