Page Body

Download Media With yt-dlp

Often, it is convenient to have a local copy of media that you would like to access. You may be in an area that does not have a strong Internet connection, or any connection at all, so having local copies can be a boon when streaming is not viable. Also, you may want to keep a content copy for archival purposes.

Using a content provider's download options can be inefficient. A Command Line Interface (CLI) tool called yt-dlp can help you streamline the download process.

Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.


You can install yt-dlp from PyPI, the Python Package Index. On a GNU/Linux distribution like Fedora, your system will be set up to use pip (Pip Installs Packages), the package installer for Python. On a Debian system, you may need to install it:

# apt install python3-pip && python3 -m pip install -U pip setuptools wheel

The above command installs pip and upgrades the pip package manager, setuptools, and wheel using the Python 3 interpreter.

You can install yt-dlp with the following command:

python3 -m pip install --user yt-dlp


The prior pip install --user command installs software to ${HOME}/.local/bin. Before proceeding, ensure that this location is in your shell environment's ${PATH} variable, so that the shell can find the software when you issue germane commands. Details on how to configure this variable are on the Conventions page.

GNU/Linux distributions like Debian and Fedora often incorporate ${HOME}/.local/bin into the ${PATH} variable, but if this location did not exist when you began your shell session, you may need to restart your system to ensure that it is a searchable location for your shell.

By default, the yt-dlp command will save its output to the directory that you issue the command from. You can change this by creating a yt-dlp configuration file and specifying a new download directory/output file format:

$ mkdir -p "${HOME}/.config/yt-dlp" &&
        echo '# Save media to Downloads directory in your home directory'
        echo '-o "${HOME}/Downloads/%(title)s.%(ext)s"'
    } > "${HOME}/.config/yt-dlp/config"

The above command will create the yt-dlp directory in ${HOME}/.config (if this directory does not exist on your system, the -p option will ensure that it is created), create a config file in that directory, and add lines to the config file to have the yt-dlp command download media to your home directory's Downloads directory. Files will be saved with the media content's title and extension as the filename.


It is important to keep your software up-to-date. If you followed the prior installation instructions, you can upgrade yt-dlp like so:

python3 -m pip install --user -U yt-dlp


yt-dlp should work with a vast amount of sites. Also, the command is highly configurable through myriad options. You can customize your commands to alter output filenames with filename templates and specify which video/audio format to obtain.

To see if multiple formats are available for a given piece of media, you can use yt-dlp's -F (--list-formats) option. For example:

$ yt-dlp -F \
[PeerTube] 0a6feebf-a2d6-4965-9942-34bf63d92054: Downloading JSON metadata
[info] Available formats for 0a6feebf-a2d6-4965-9942-34bf63d92054:
format code  extension  resolution note
480p         mp4        480p       80.12MiB
1080p        mp4        1080p      190.49MiB (best)

However, it is simplest to let the yt-dlp command select the best format of a given piece of content for you. You can use the following special names to select the case formats:

  • b Selects the best quality format represented by a single file with both video and audio
  • bv Selects the best quality video-only format (may not be available)
  • ba Selects the best quality audio-only format (may not be available)

Then, if you just want the best quality format file available that has both video and audio in a single file, you can use a command like this (the -f option is short for --format):

yt-dlp -f 'b' ''

Some media providers keep the best quality video and audio in separate video-only and audio-only files, respectively. There is a way to utilize yt-dlp to create a combined best quality file from these disparate files.

First, make sure that you have ffmpeg installed. This program is likely available in the repository of your GNU/Linux distribution (if not, you may need to add/enable an additional repository to obtain it).

For example, here is how you can install ffmpeg on Debian:

# apt install ffmpeg

Then, you can obtain the best video-only file, the best audio-only file, and merge them into a single file with both video and audio like so:

yt-dlp -f 'bv[ext=mp4]+ba[ext=m4a]/b[ext=mp4]/b' ex_url...

You can find more examples regarding comparable commands in yt-dlp's documentation.

If you encounter a media provider that only has video file formats available, and you only want the video's audio, you can use yt-dlp to download the video, extract the audio, and provide an audio-only file. This will require ffmpeg, as well.

To obtain just the audio from a file with both video and audio, you can use yt-dlp's -x (--extract-audio) option:

yt-dlp -f 'b' -x ex_url...


Instead of continually entering these commands, it might be helpful to create Bash functions that you can easily call up on the command line and pass content URLs as arguments. For example, the following Bash functions, by default, download either the b or ba formats for media content:

# Download video content
yv() {
    if [[ "${1}" == -c || "${1}" == --comprehensive ]] &&
            command -v ffmpeg > '/dev/null' 2>&1; then 
        # Download best video file
        yt-dlp \
            -f 'bv[ext=mp4]+ba[ext=m4a]/b[ext=mp4]/b' "${2}"
        yt-dlp -f 'b' "${1}"

# Download audio content
ya() {
    if [[ "${1}" == -c || "${1}" == --comprehensive ]] &&
            command -v ffmpeg > '/dev/null' 2>&1; then 
        # Download best audio file
        yt-dlp -f 'ba[ext=m4a]' "${2}"
        yt-dlp -f 'ba' "${1}"

However, if you pass the -c (--comprehensive) option to the functions, they try to download the best quality files available and merge them into a single file. If that is not possible, they fall back to the default behavior:

yv -c \

You will need to add these functions to your shell's configuration file to make them persistently available. For example, you can add them to your user's .bashrc file like so:

$ echo -e '\n# Download video content
yv() {
    if [[ "${1}" == -c || "${1}" == --comprehensive ]] &&
            command -v ffmpeg > '\''/dev/null'\'' 2>&1; then 
        # Download best video file
        yt-dlp \
            -f '\''bv[ext=mp4]+ba[ext=m4a]/b[ext=mp4]/b'\'' "${2}"
        yt-dlp -f '\''b'\'' "${1}"

# Download audio content
ya() {
    if [[ "${1}" == -c || "${1}" == --comprehensive ]] &&
            command -v ffmpeg > '\''/dev/null'\'' 2>&1; then 
        # Download best audio file
        yt-dlp -f '\''ba[ext=m4a]'\'' "${2}"
        yt-dlp -f '\''ba'\'' "${1}"
}' >> "${HOME}/.bashrc"

Either stop and restart your terminal, or run source "${HOME}/.bashrc" to see the changes reflected in your shell.

Now, you can use the functions to download content on the command line, e.g., yv ''.

Multiple Downloads

Running yt-dlp commands or the functions above are fine for single downloads, but if you ever need to download many files in a short period of time, it can become tedious.

The following is a Bash script that you can use to improve this process:

In short, the script gives you the opportunity to continually add URLs for content that you would like to download.

As with the prior yv and ya functions, yt-dlp downloads the b files by default, but will attempt for higher quality outputs if you pass the -c (--comprehensive) option to the script when you call it (e.g., if you name the script download_media.bash, you could call it as download_media.bash -c to have yt-dlp attempt to produce the highest quality output file).

After the script starts, you choose which mode that you want to run it in (i.e., Audio or Video mode). On each iteration of the program's main loop, you are asked to enter a new URL. From here, you also have the option to either change the script's mode (m), debug the script (d), or quit (q).

yt-dlp's output has been suppressed to prevent the script from being disrupted. So, if you ever try to download content and you do not get the expected result, check your system's /tmp/ directory (or whichever log directory you have set for the script) for a subdirectory that matches the name of the script. This subdirectory will contain a .log file that begins with a UNIX epoch timestamp and the name of the script. yt-dlp's error output is redirected to this file.


For more information on yt-dlp, check out its man page by running man 1 yt-dlp or visit its project repository.

Enjoyed this post?

Subscribe to the feed for the latest updates.