Page Body

Download Media With youtube-dl

You may not always have a fast Internet connection for your devices, or any connection at all. On one hand, this is OK, since device storage is now relatively large and cheap. On the other hand, many online content providers do not provide an easy way to download their content.

A Command Line Interface (CLI) tool called youtube-dl can help you.

Most likely, youtube-dl is available in your GNU/Linux distribution's repository. However, you are likely better off downloading it directly from its developers to make sure that you stay up-to-date with the latest version. The online services that the tool interacts with frequently change, and having the latest version of the application will ensure that you have the best experience.

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


You can install youtube-dl with the following command (you will need to make sure that you have the wget command installed first, which you can verify by running the which wget command):

# wget '' -O '/usr/local/bin/youtube-dl'

This will download the application from and save it as youtube-dl in your /usr/local/bin directory.

Now, verify the application file's hash, and compare it to the value posted here:

sha256sum '/usr/local/bin/youtube-dl'

Download and import the developer's public keys into your GNU/Linux system's GPG keyring. For example:

$ wget '' -O A4826A18.asc &&
    gpg --import A4826A18.asc
$ wget '' -O 18A9236D.asc &&
    gpg --import 18A9236D.asc

Now, check the signature:

gpg --verify '/usr/local/bin/youtube-dl.sig' '/usr/local/bin/youtube-dl'

The output should say that there is a Good signature from one of the keys that were listed on the page above and that you imported into your keyring.

Next, run the following command to set the appropriate permissions on the application:

# chmod 755 '/usr/local/bin/youtube-dl'


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

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

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


youtube-dl 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 file names with file name 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 youtube-dl's -F option. For example:

$ youtube-dl -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 youtube-dl 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:

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

Then, if you just want the best quality format with both video and audio in a single file:

youtube-dl -f best ''

Sometimes, you may be dealing with a media provider that only has video file formats available for download (like the example above). In this case, if you only want audio, you can use youtube-dl to download the video, extract the audio, and provide an audio-only file (with assistance from ffmpeg).

To do this, install ffmpeg. 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-get install ffmpeg

Now, to obtain just the audio from a file with both video and audio, you can use the command's -x option:

youtube-dl -f best -x example_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, here are two bash functions that will grab either the best or bestaudio formats for media content.

# Download Video Content
yv() {
  youtube-dl -f best "${1}"

# Download Audio Content
ya() {
  youtube-dl -f bestaudio "${1}"

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 ''
  echo '# Download Video Content'
  echo 'yv() {'
  echo '  youtube-dl -f best "${1}"'
  echo '}'
  echo ''
  echo '# Download Audio Content'
  echo 'ya() {'
  echo '  youtube-dl -f bestaudio "${1}"'
  echo '}'
} >> "${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 youtube-dl 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 you can use to improve this process.

#!/usr/bin/env bash
# Author:
# Purpose: Download audio/video content

# Program

# Obtain script name and save to variable

# Remove suffix from script name and save to variable

# Create temporary directory for script error log
# and save to variable
error_dir="$(mktemp -d "/tmp/${sn_no_suffix}.XXXXXXXX")"

# Create file in temporary directory and
# save error log location to variable
error_log="$(touch "${error_dir}/${sn_no_suffix}.log" &&
    echo "${error_dir}/${sn_no_suffix}.log")"

# Give option to continue or end main loop
while read -p 'Continue (y or n)? ' -r cont_loop; do
  # Break loop if user selects 'n'
  if [[ "${cont_loop}" == 'n' ]]; then
  # Continue loop if user selects 'y'
  elif [[ "${cont_loop}" == 'y' ]]; then
    # Prompt for audio or video download, save to variable
    read -p 'Audio (a) or Video (v)? ' -r choice

    # Check for invalid input, reprompt if necessary
    while [[ "${choice}" != 'a' && "${choice}" != 'v' ]]; do
      echo -e '\nInvalid choice. Try again.\n'
      read -p 'Audio (a) or Video (v)? ' -r choice

    # Obtain URL and save to variable
    read -p 'Enter URL: ' -r url
    echo ''

    # Check for invalid URL input, reprompt if necessary
    while [[ ! "${url}" =~ htt(p|ps)://.* ]]; do
      echo -e 'Please enter a valid URL.\n'
      read -p 'Enter URL: ' -r url
      echo ''

    # If choice is audio
    if [[ "${choice}" == 'a' ]]; then
      # Download best audio file
      youtube-dl -f bestaudio[ext=m4a] -q "${url}" 2> "${error_log}" &
    # If choice is video
    elif [[ "${choice}" == 'v' ]]; then
      # Download best video file
      youtube-dl -f best -q "${url}" 2> "${error_log}" &
    # Reprompt for valid input
    echo -e '\nInvalid choice. Try again.\n'

# Exit script with successful return value
exit 0

In short, the script will give you the opportunity to continually add URLs for content that you would like to download. On each iteration of the program's main loop, you will be asked whether you want an audio or video file, and the content's URL.

youtube-dl'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 for a subdirectory that begins with the name of the script. This subdirectory will contain a file that also begins with the name of the script and ends with .log. youtube-dl's error output is redirected to this file.


Enjoyed this post?

Subscribe to the feed for the latest updates.