module documentation

Utilities for working with YouTube.

Copyright 2017-2025, Voxel51, Inc.

Function download_youtube_videos Downloads a list of YouTube videos.
Variable logger Undocumented
Variable pytube Undocumented
Function _build_tasks_list Undocumented
Function _do_download Undocumented
Function _download Undocumented
Function _download_clip Undocumented
Function _download_multi Undocumented
Function _download_video Undocumented
Function _ensure_pytube Undocumented
Function _find_nearest Undocumented
Function _get_stream Undocumented
Function _parse_num_workers Undocumented
Function _validate_video Undocumented
def download_youtube_videos(urls, download_dir=None, video_paths=None, clip_segments=None, ext='.mp4', only_progressive=True, resolution='highest', max_videos=None, num_workers=None, skip_failures=True, quiet=False): (source)

Downloads a list of YouTube videos.

The urls argument accepts a list of YouTube "watch" URLs:

urls = [
    "https://www.youtube.com/watch?v=-0URMJE8_PB",
    ...,
]

Use either the download_dir or video_paths argument to specify where to download each video.

You can use the optional clip_segments argument to specify a specific segment, in seconds, of each video to download:

clip_segments = [
    (10, 25),
    (11.1, 20.2),
    None,               # entire video
    (None, 8.0),        # through beginning of video
    (8.0, None),        # through end of video
    ...
]

You can also use the optional ext and resolution arguments to specify a deisred video codec and resolution to download, if possible.

YouTube videos are regularly taken down. Therefore, this method provides an optional max_videos argument that you can use in conjunction with skip_failures=True and a large list of possibly non-existent videos in urls in cases where you need a certain number of videos to be successfully downloaded but are willing to tolerate failures.

Parameters
urlsa list of YouTube URLs to download
download_dir:Nonea directory in which to store the downloaded videos
video_paths:Nonea list of paths to which to download the videos. When downloading entire videos, a stream matching the video format implied by each file's extension is downloaded, if available, or else the extension of the video path is changed to match the available stream's format
clip_segments:Nonea list of (first, last) tuples defining a specific segment of each video to download
ext:".mp4"a video format to download for each video, if possible. Only applicable when a download_dir is used. This format will be respected if such a stream exists, otherwise the format of the best available stream is used. Set this value to None if you want to download the stream with the best match for resolution and progressive regardless of format
only_progressive:Truewhether to only download progressive streams, if possible. Progressive streams contain both audio and video tracks and are typically only available at <= 720p resolution
resolution:"highest"

a desired stream resolution to download. This filter is applied after respecting the desired video format and only_progressive restriction, if applicable. The supported values are:

  • "highest" (default): download the highest resolution stream
  • "lowest": download the lowest resolution stream
  • A target resolution like "1080p". In this case, the stream whose resolution is closest to this target value is downloaded
max_videos:Nonethe maximum number of videos to successfully download. By default, all videos are downloaded
num_workers:Nonea suggested number of threads/processes to use when downloading videos
skip_failures:Truewhether to gracefully continue without raising an error if a video cannot be downloaded
quiet:Falsewhether to suppress logging, except for a progress bar
Returns
a tuple of
  • downloaded: a dict mapping integer indexes into urls to paths of successfully downloaded videos
  • errors: a dict mapping integer indexes into urls to error messages for videos that were attempted to be downloaded, but failed

Undocumented

Undocumented

def _build_tasks_list(urls, download_dir, video_paths, clip_segments, tmp_dir, ext, only_progressive, resolution): (source)

Undocumented

def _do_download(task): (source)

Undocumented

def _download(tasks, max_videos, skip_failures, quiet): (source)

Undocumented

def _download_clip(stream, clip_segment, video_path): (source)

Undocumented

def _download_multi(tasks, max_videos, skip_failures, quiet, num_workers, use_threads): (source)

Undocumented

def _download_video(stream, video_path): (source)

Undocumented

def _ensure_pytube(): (source)

Undocumented

def _find_nearest(array, target): (source)

Undocumented

def _get_stream(pytube_video, ext, only_progressive, resolution): (source)

Undocumented

def _parse_num_workers(num_workers, use_threads=False): (source)

Undocumented

def _validate_video(pytube_video): (source)

Undocumented