Pytube YouTube Downloader

15.0.0 · active · verified Thu Apr 09

Pytube is a Python 3 library designed for downloading YouTube videos and playlists. It handles various resolutions, formats, and audio-only extractions. Currently at version 15.0.0, it maintains an active release cadence, primarily updating to adapt to frequent changes in YouTube's frontend, which it scrapes to function.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a YouTube object with a video URL and download the highest resolution stream to a local 'downloads' directory. It includes basic error handling.

from pytube import YouTube

# Example: Download the highest resolution of a video
video_url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' # Rick Astley - Never Gonna Give You Up

try:
    yt = YouTube(video_url)
    print(f"Downloading: {yt.title}...")
    
    # Get the highest resolution stream
    stream = yt.streams.get_highest_resolution()
    
    # Or, filter by resolution and file extension
    # stream = yt.streams.filter(res="720p", file_extension='mp4').first()

    if stream:
        stream.download(output_path='./downloads')
        print(f"Successfully downloaded '{yt.title}' to ./downloads")
    else:
        print("No suitable stream found.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →