How to Download YouTube videos in Python.

seijind

George Karanikolas

Posted on May 21, 2020

How to Download YouTube videos in Python.

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
💖 💪 🙅 🚩
seijind
George Karanikolas

Posted on May 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related