How to Build a YouTube Video Downloader in Python: A Beginner's Guide

Usecase:

Online websites for downloading YouTube videos are often full of ads and complicated to use. For educational purposes, here’s a simple, ad-free way to create your own YouTube video downloader using Python.

Result:

Lets get started!

Let me show you the essential script.

#pip install pytube (if module not installed yet)
from pytube import YouTube

#insert the link you want to download
yt = YouTube("https://www.youtube.com/watch?v=K9tOnIJyvRw")
#streams object able to use multiple method to call
yt.streams.get_highest_resolution().download()
print(f"Video downloaded successfully: '{yt.title}'")

That's all you need.

  • yt.streams: This gets all available video streams for the YouTube video.

  • get_highest_resolution(): This method selects the stream with the highest resolution.

  • download(): This method downloads the selected video stream.

From the test, it seems that unable to call method directly but need to call method from object streams which is get_highest_resolution()

Full explanation:

You cannot call the download method directly on the YouTube object because the download method is a part of the Stream object, not the YouTube object. Here’s a more detailed explanation:

  1. YouTube Object:

    • When you create a YouTube object, it represents a specific YouTube video. This object contains various attributes and methods to interact with the video.
  2. Stream Object:

    • The YouTube object has a streams attribute, which is a list of Stream objects. Each Stream object represents a different format or quality option for downloading the video (e.g., 360p, 720p, audio-only, etc.).
  3. download Method:

    • The download method is a method of the Stream class, not the YouTube class. Therefore, you need to first select a specific Stream object from the streams attribute of the YouTube object before you can call the download method.

We want to make it better by stating the path to save downloaded video and also URL for video.

#pip install pytube (if module not installed yet)
from pytube import YouTube

Importing Youtube class from pytube library which will be used to interacting with Youtube videos

# Create function to download a YouTube video
def download_video(video_url, output_path):
    try:
        video = YouTube(video_url)
        stream = video.streams.get_highest_resolution()
        stream.download(output_path=output_path)
        print(f"Video downloaded successfully: '{video.title}'")
    except Exception as e:
        print(f"Failed to download video. Error: {e}")

def download_video(video_url, output_path) is defining a function name as download video that takes two arguments which is video url and output path

  • video_url: The URL of the YouTube video to be downloaded.

  • output_path: The directory where the downloaded video will be saved.

video = YouTube(video_url) create 'Youtube' object for specified video URL that will called later as variable video

stream = video.streams.get_highest_resolution() Selects the highest resolution stream available for the video.

stream.download(output_path=output_path) Downloads the selected stream to the specified directory.

print(f"Video downloaded successfully: '{video.title}'") output if the video downloaded successfully

except Exception as e: print(f"Failed to download video. Error: {e}") when the download not able to process, this output will trigger

video_url = input('Enter URL of YouTube video: ') 
output_path = input('Enter the directory to save the video: ')

input means that user will need to fill the URL and also the path which will be stored to the variable video_url and output_path

download_video(video_url, output_path)

This last script will call download_video function with the argument that has been given by user.

FULL CODE

# pip install pytube (if module not installed yet)
from pytube import YouTube

# Function to download a YouTube video
def download_video(video_url, output_path):
    try:
        video = YouTube(video_url)
        stream = video.streams.get_highest_resolution()
        stream.download(output_path=output_path)
        print(f"Video downloaded successfully: '{video.title}'")
    except Exception as e:
        print(f"Failed to download video. Error: {e}")

# Request URL of YouTube Video
video_url = input('Enter URL of YouTube video: ')
output_path = input('Enter the directory to save the video: ')

# Download the video
download_video(video_url, output_path)

Let me know if you have any comment.