yt-dlp
yt-dlp is a feature-rich command-line audio/video downloader with support for thousands of sites. It is an actively maintained fork of `youtube-dl`, with frequent releases, often multiple times a month, to adapt to changes in video platforms and add new features.
Warnings
- breaking YouTube frequently changes its backend mechanisms, which can temporarily break `yt-dlp`'s ability to download or extract information from YouTube videos. Functionality often breaks without a fixed schedule.
- gotcha `ffmpeg` and `ffprobe` are *essential external binaries* (not Python packages) for merging separate audio/video streams, format conversion, embedding metadata, and various post-processing tasks. Without them, `yt-dlp`'s functionality will be severely limited, often resulting in separate audio and video files or inability to download certain formats.
- gotcha For full YouTube support, an external JavaScript runtime (such as Deno, Node.js, or Bun) is often required in addition to the `yt-dlp-ejs` Python dependency. Recent YouTube changes necessitate a proper JS runtime to solve video extraction challenges, as `yt-dlp`'s built-in interpreter is often insufficient.
- breaking `yt-dlp` regularly updates its minimum required Python version. Using older Python versions can lead to `ModuleNotFoundError` for internal dependencies, runtime errors, or lack of support for newer features and fixes.
- gotcha For authentication on certain sites, particularly YouTube, using cookies is generally more reliable and recommended than OAuth. OAuth integration for `yt-dlp` has been noted to be unstable or broken.
Install
-
pip install yt-dlp -
pip install -U --pre "yt-dlp[default]"
Imports
- YoutubeDL
from yt_dlp import YoutubeDL
- YoutubeDL
from yt_dlp import YoutubeDL
Quickstart
import yt_dlp
def download_video(url):
ydl_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'outtmpl': '%(title)s.%(ext)s',
'merge_output_format': 'mp4'
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
video_title = info_dict.get('title', 'Unknown Title')
print(f"Downloading: {video_title}")
ydl.download([url])
print("Download complete!")
if __name__ == '__main__':
# Replace with a real video URL for testing. Use an anonymous public video.
# For authenticated downloads, configure 'cookiefile' in ydl_opts.
video_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" # Example URL
download_video(video_url)