Playing With Pytube

How to Download the Video from Youtube using pytube

  • To install pytube, run the following command in your terminal:
$ pip install pytube
  • import the Youtube from the pytube.
from pytube import YouTube
  • Get the link to download and create an Youtube Object.
link = input("Enter the video link to download")
yt = YouTube(f"{link}")
  • Choose the path to download and choose the highest resolution.
yd = yt.streams.get_highest_resolution()
download_path = input("Please Enter the path to save the File")
yd.download(f"{download_path}")

The whole Code looks Like This

from pytube import YouTube

link = input("Enter the video link to download")
yt = YouTube(f"{link}")
print("Title:",  yt.title)
yd = yt.streams.get_highest_resolution()
download_path = input("Please Enter the path to save the File")
yd.download(f"{download_path}")

Check the documentation for further references.

Downloading Youtube PlayList Using Pytube

from pytube import Playlist
from pytube import YouTube

#Function for downloading a Single Video
def video_download(link,download_path):
    yt = YouTube(f"{link}")
    yd = yt.streams.get_highest_resolution()
    yd.download(f"{download_path}")

#Getting the Playlist URL to be downloaded
playlis = input("Enter the link of Playlist")

# The Path to which the Videos are to be Downloaded
download_path = input("Please Enter the path to save the File[Please make sure'/' is seperator]")

#Creating a Playlist object of Pytube
playlist = Playlist(playlis)

# Getting No Of Videos in Playlist
print('Number of videos in playlist: %s' % len(playlist.video_urls))

# Traversing through each video Url and downloading
for i in playlist.video_urls:
    print("Downloading{}.....".format(i))
    video_download(i,download_path)