pytubefix

10.3.8 · active · verified Thu Apr 16

Python3 library for downloading YouTube Videos. Current version is 10.3.8. It's a lightweight, Pythonic, dependency-free, library (and command-line utility) for downloading YouTube Videos. pytubefix is an actively maintained fork of pytube that solves broken functionality and adds new features, regularly addressing YouTube's continuous changes. It supports progressive and DASH streams, callback functions for download progress, and caption extraction.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart example demonstrates how to download a YouTube video using `pytubefix`. It initializes a `YouTube` object with a video URL, registers a progress callback for real-time updates, retrieves the video's metadata, and then downloads the highest resolution progressive stream to a 'downloads' directory. It handles a placeholder URL and basic error reporting.

import os
from pytubefix import YouTube

def on_progress(stream, chunk, bytes_remaining):
    total_size = stream.filesize
    bytes_downloaded = total_size - bytes_remaining
    percentage_of_completion = bytes_downloaded / total_size * 100
    print(f"\rDownloading: {percentage_of_completion:.2f}%", end='')

video_url = os.environ.get('YOUTUBE_VIDEO_URL', 'https://www.youtube.com/watch?v=dQw4w9WgXcQ') # Rick Astley - Never Gonna Give You Up

try:
    yt = YouTube(video_url, on_progress_callback=on_progress)
    print(f"\nTitle: {yt.title}")
    print(f"Author: {yt.author}")
    print(f"Views: {yt.views}")

    # Get the highest resolution progressive stream
    # Progressive streams contain both video and audio
    stream = yt.streams.get_highest_resolution()
    if stream:
        print(f"Downloading '{yt.title}' in {stream.resolution}...")
        stream.download(output_path='./downloads')
        print("\nDownload complete!")
    else:
        print("No suitable progressive stream found.")

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

view raw JSON →