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.
- 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_folderhere so the jar disappears when thewithblock 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.Nonefalls back to theYOUTUBE_HELPER_MAX_HEIGHTenvironment variable, and if that is empty too, yt-dlp picks best-available as before. Seeheight_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_FALLBACKSon failure.
- Returns:
A dictionary of options for yt-dlp.
- Return type:
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:
- Returns:
Path of downloaded audio
- Return type:
Notes
This function uses yt-dlp to download the best quality audio from the given URL. When the extension yt-dlp delivers (
.webm/.m4aon YouTube) differs fromoutput_path’s, the file is converted throughaudio_helper.sound_converter:target_sample_rateis 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:
- Returns:
Path of downloaded thumbnail
- Return type:
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.Nonefalls back toYOUTUBE_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 (seeheight_capped_format()).
- Returns:
Path of downloaded video
- Return type:
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^=httpkeepshttpsandhttp_dash_segmentswhile excludingm3u8_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”.
- 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:
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:
Notes
The function checks for metadata extraction success, logs the title and a part of the description, and returns metadata.