Skip to main content
yt-dlp can be embedded in Python applications for programmatic access to its powerful downloading and extraction capabilities.

Command-Line Integration

yt-dlp makes the best effort to be a good command-line program and should be callable from any programming language.
Your program should avoid parsing the normal stdout since they may change in future versions. Instead, use options such as -J, --print, --progress-template, --exec etc. to create console output that you can reliably reproduce and parse.

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:
If you are porting code from youtube-dl to yt-dlp, note that we do not guarantee the return value of YoutubeDL.extract_info to be JSON serializable, or even be a dictionary. It will be dictionary-like, but if you want to ensure it is a serializable dictionary, pass it through YoutubeDL.sanitize_info as shown in the examples below.

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 URL
  • download(url_list) - Download videos from a list of URLs
  • download_with_info_file(info_file) - Download from an info.json file
  • sanitize_info(info_dict) - Make info dict JSON-serializable
  • add_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.

Migration from youtube-dl

The return value of YoutubeDL.extract_info is not guaranteed to be JSON serializable or a standard dictionary in yt-dlp. Always use sanitize_info() if you need serializable output.