youtube_helper.main module

YouTube Helper — download and metadata utilities.

Module summary

Utility functions to download videos, audio, and thumbnails from URLs such as YouTube, Vimeo, or DailyMotion using yt-dlp, plus the post-processing steps (format conversion / merging via ffmpeg) needed to land the result in the exact container / sample-rate the caller asked for. It consumes a user-facing page URL and produces a file on disk (or, for video_url_meta_data(), a plain metadata dict).

Dependencies

  • yt-dlp : extraction and download engine.

  • ffmpeg : media conversion (invoked via the helper packages).

  • PIL : thumbnail format conversion.

  • os_helper : filesystem / URL utilities.

  • audio_helper : audio validation and conversion.

  • video_helper : video validation and conversion.

Author

Project maintainers.

youtube_helper.main.default_ytdlp_options(overwrites=True, audio=False, video=False, cookie_dir=None, max_height=None, player_clients=None)[source]

Generate default yt-dlp options based on provided parameters.

Parameters:
  • overwrites (bool, optional) – Whether to overwrite existing files. Default is True.

  • audio (bool, optional) – Whether to download audio only. Default is False.

  • video (bool, optional) – Whether to download video only (overrides audio). Default is False.

  • cookie_dir (str or None, optional) – Directory in which yt-dlp’s session cookie jar is written. The download helpers pass their temporary_folder here so the jar disappears when the with block exits; when omitted it falls back to the system temp directory. It is never written to the current working directory.

  • max_height (int or str or None, optional) – Cap the video height, e.g. 720. None falls back to the YOUTUBE_HELPER_MAX_HEIGHT environment variable, and if that is empty too, yt-dlp picks best-available as before. See height_capped_format() for why the generated selector also prefers non-HLS.

  • player_clients (sequence of str or None, optional) – Ask YouTube as these clients instead of yt-dlp’s defaults (web_embedded, tv, android_vr, mweb, …). Mostly useful for pinning one explicitly; callers normally leave this alone and let the retry ladder try the clients in _PLAYER_CLIENT_FALLBACKS on failure.

Returns:

A dictionary of options for yt-dlp.

Return type:

dict

Notes

The function sets various options for yt-dlp. If audio is set to True, the format is set to download the best audio available. If video is set to True, it overrides the audio flag and downloads video+audio.

youtube_helper.main.download_audio(url, output_path=None, target_sample_rate=44100)[source]

Download the best quality audio from a given URL and save it to the specified output path.

Parameters:
  • url (str) – The URL of the video to download the audio from.

  • output_path (str, optional) – The path where the downloaded audio should be saved.

  • target_sample_rate (int, optional) – The sample rate of the output audio file. Defaults to 44100.

Returns:

Path of downloaded audio

Return type:

str

Notes

This function uses yt-dlp to download the best quality audio from the given URL. When the extension yt-dlp delivers (.webm / .m4a on YouTube) differs from output_path’s, the file is converted through audio_helper.sound_converter: target_sample_rate is applied there and the result is mono. When the extensions match, the file is copied untouched and keeps the source’s rate and channels.

youtube_helper.main.download_thumbnail(url, output_path=None)[source]

Download the thumbnail of a video from a given URL and save it to the specified output path.

Parameters:
  • url (str) – The URL of the video from which to download the thumbnail.

  • output_path (str, optional) – The path where the downloaded thumbnail should be saved.

Returns:

Path of downloaded thumbnail

Return type:

str

Notes

This function uses yt-dlp to download the thumbnail of the specified video. It handles different output formats and ensures the thumbnail is saved in the desired format using PIL.

youtube_helper.main.download_video(url, output_path=None, max_height=None)[source]

Download video from a given URL and save it to the specified output path.

Parameters:
  • url (str) – The URL of the video to be downloaded.

  • output_path (str, optional) – The path where the downloaded video should be saved.

  • max_height (int or str or None, optional) – Cap the video height, e.g. 720. None falls back to YOUTUBE_HELPER_MAX_HEIGHT, and if that is empty too, yt-dlp picks best-available as it always has. Worth setting when the destination has a size limit, or when speed matters more than resolution: the capped selector also prefers direct https formats over fragmented HLS, which YouTube throttles heavily (see height_capped_format()).

Returns:

Path of downloaded video

Return type:

str

Notes

This function downloads the best quality video from the given URL using yt-dlp. It handles different formats and ensures that the video is saved in the desired format using ffmpeg if necessary. The function checks whether the downloaded video is valid and converts it to the specified output format if needed.

youtube_helper.main.height_capped_format(max_height)[source]

yt-dlp format selector for “no taller than max_height, and avoid HLS if possible”.

Non-HLS first on purpose. YouTube throttles fragmented HLS far more aggressively than a direct https format: on one measured episode, 75-300 KiB/s across 1507 fragments against ~2 MiB/s for the equivalent https stream. protocol^=http keeps https and http_dash_segments while excluding m3u8_native.

Every rung degrades gracefully: height-capped non-HLS, then height-capped anything, then best available, so a video that simply has no small format still downloads instead of failing with “Requested format is not available”.

Parameters:

max_height (int) – Maximum video height in pixels, e.g. 720.

Returns:

A yt-dlp format string.

Return type:

str

youtube_helper.main.is_valid_video_url(url)[source]

Check if a given URL is a valid video URL using yt-dlp.

Parameters:

url (str) – The URL to check.

Returns:

True if the URL is valid, False otherwise.

Return type:

bool

Notes

This function extracts metadata from the URL using yt-dlp to determine if the URL points to a valid video.

youtube_helper.main.video_url_meta_data(url)[source]

Retrieve metadata from a video URL without downloading the video.

Parameters:

url (str) – The URL of the video.

Returns:

A dictionary containing the video’s metadata, including title and description.

Return type:

dict

Notes

The function checks for metadata extraction success, logs the title and a part of the description, and returns metadata.