Command-Line Integration
yt-dlp makes the best effort to be a good command-line program and should be callable from any programming language.Python Integration
From a Python program, you can embed yt-dlp in a more powerful fashion.Basic Usage
Configuration Options
For a list of available options:- Check
yt_dlp/YoutubeDL.py - Use
help(yt_dlp.YoutubeDL)in a Python shell - Use
devscripts/cli_to_api.pyto translate CLI switches toYoutubeDLparams
Common Examples
Extracting Information
Extract video metadata without downloading:Download Using an Info JSON
Download videos from a previously saved info.json file:Extract Audio
Download and convert videos to audio-only files:Filter Videos
Download only videos that match specific criteria:Adding Logger and Progress Hook
Custom logging and progress tracking:Add a Custom PostProcessor
Create custom postprocessing logic:Use a Custom Format Selector
Implement custom format selection logic:API Reference
YoutubeDL Class
The main class for interacting with yt-dlp programmatically.Key Methods
extract_info(url, download=True)- Extract information from a URLdownload(url_list)- Download videos from a list of URLsdownload_with_info_file(info_file)- Download from an info.json filesanitize_info(info_dict)- Make info dict JSON-serializableadd_post_processor(pp, when='post_process')- Add a custom postprocessor
Common Options
Best Practices
Use Context Managers
Always use
with YoutubeDL() as ydl: to ensure proper resource cleanup.Sanitize Info
Use
sanitize_info() when you need JSON-serializable output.Handle Errors
Wrap extraction/download calls in try-except blocks to handle errors gracefully.
Check Return Codes
Check error codes from
download() methods to detect failures.