Skip to main content
In this tutorial, we will learn how to download YouTube videos using Python with the yt-dlp library.
yt-dlp on PyPI: https://pypi.org/project/yt-dlp/Before starting, make sure you have Python installed. You can download it from python.org.
1
Install the yt-dlp library
2
This example uses uv as the package manager, but you can use pip or any other package manager.
3
uv init --bare
uv add yt-dlp
4
Verify the installation
5
uv run yt-dlp --version
6
Install ffmpeg
7
The yt-dlp library requires ffmpeg to process video and audio files.
8
Ubuntu
sudo apt update
sudo apt install ffmpeg -y
ffmpeg -version
Windows (winget)
winget install "FFmpeg (Essentials Build)"
ffmpeg -version
9
(Optional) Download a video via CLI
10
You can refer to the format selection docs for more information about format options.
11
Download a video in mp3 format:
12
uv run yt-dlp -f bestaudio/best -x --audio-format mp3 -o <path-to-save> <youtube-video-url>
13
Sample output:
14
[youtube] Extracting URL: https://www.youtube.com/watch?v=IViaa3o7BbU
[youtube] IViaa3o7BbU: Downloading webpage
[info] IViaa3o7BbU: Downloading 1 format(s): 251
[download] Sleeping 5.00 seconds as required by the site...
[download] Destination: output\test.webm
[download] 100% of    4.11MiB in 00:00:00 at 23.39MiB/s
[ExtractAudio] Destination: output\test.mp3
Deleting original file output\test.webm (pass -k to keep)
15
Create and execute a Python script
16
Create a Python script that uses the yt-dlp library to download YouTube videos programmatically.
17
yt.py (yt_dlp library)
import os
import yt_dlp

# Replace with your desired YouTube video URL
url = "youtube-video-url"
ydl_opts = {
  'format': 'bestaudio/best',
  'postprocessors': [{
    'key': 'FFmpegExtractAudio',
    'preferredcodec': 'mp3',
    'preferredquality': '320', # 0 for highest quality VBR
  }],
  'outtmpl': os.path.join("./", '%(title)s.%(ext)s'),
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
  ydl.download([url])
yt.py (subprocess)
import os
import subprocess as s

# Replace with your desired YouTube video URL
url = "youtube-video-url"
download_format = 'bestaudio/best'
s.call(['yt-dlp', '-f', download_format, '-x', '--audio-format', 'mp3', '-o', os.path.join('./', '%(title)s.%(ext)s'), url])
18
On preferredquality:
  • '320' — guaranteed 320kbps constant bitrate (CBR)
  • '0' — highest quality variable bitrate (VBR), often 220–260kbps but perceptually equivalent or better
VBR typically produces better quality at similar file sizes because it allocates bits more efficiently based on audio complexity.
19
Run the script:
20
uv run yt.py

Build docs developers (and LLMs) love