Prince Mathew (

Installing youtube-dl in Ubuntu

Youtube-dl is a command-line program to download videos from and a few more sites (approximately 1,155 listed sites). It requires the Python interpreter (2.6, 2.7, or 3.2+), and it is not platform specific. That means you can install it on your Unix box, Windows or Mac OS.

Installation in Windows

In Windows, you easily install it with Chocolatey with all the dependencies using the command:

choco install youtube-dlCode language: PowerShell (powershell)

Installation in Linux

To install it right away for all Linux users, type:

sudo apt-get install youtube-dlCode language: Bash (bash)

You can also use pip:

sudo -H pip install --upgrade youtube-dlCode language: Bash (bash)

Youtube-dl uses -f bestvideo+bestaudio/best as the default format selection which results in downloading bestvideo and bestaudio separately. So youtube-dl needs ffmpeg or avconv to mux the files together into a single file giving the best overall quality available.

sudo apt-get install ffmpegCode language: Bash (bash)

To validate that the package is installed properly use the ffmpeg -version command which prints the FFmpeg version:

ffmpeg -versionCode language: Bash (bash)

The output should look something like this:

ffmpeg version 4.2.2-1ubuntu1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 9 (Ubuntu 9.3.0-3ubuntu1)Code language: Bash (bash)

Configuring Youtube-dl

Now that we have installed all the neccessary packages, it is time to configure the youtube-dl by placing any supported command line option to a configuration file. This will avoid the need to pass the command line arguments each and everytime.

First we will create a config file and move it to /etc

touch youtube-dl.confCode language: Bash (bash)

Open the file in your favourite command line text editor. I use vi here,

vi youtube-dl.confCode language: Bash (bash)

This is what my config file looks like. Copy and paste this in your config file

# Default Output Directory and Pattern
-o ~/downloads/youtube-dl/%(extractor_key)s/%(extractor_key)s_%(title)s_%(id)s.%(ext)s
-f 'bestvideo[height<=480]+bestaudio/best[height<=480]'
--download-archive ~/.config/youtube-dl/downloaded.txtCode language: Bash (bash)

I will explain the above options

-o ~/downloads/youtube-dl/%(extractor_key)s/%(extractor_key)s_%(title)s_%(id)s.%(ext)s
Here we pass the location and naming format of output file. ~/downloads/youtube-dl/%(extractor_key)s/ will save the file in downloads ⇨ youtube-dl ⇨ ‘site_name’.

extractor_key (string) : Key name of the extractor
title (string) : Video title
id (string) : Video identifier
ext (string) : Video filename extension

Full list of template arguments
  • id (string): Video identifier
  • title (string): Video title
  • url (string): Video URL
  • ext (string): Video filename extension
  • alt_title (string): A secondary title of the video
  • display_id (string): An alternative identifier for the video
  • uploader (string): Full name of the video uploader
  • license (string): License name the video is licensed under
  • creator (string): The creator of the video
  • release_date (string): The date (YYYYMMDD) when the video was released
  • timestamp (numeric): UNIX timestamp of the moment the video became available
  • upload_date (string): Video upload date (YYYYMMDD)
  • uploader_id (string): Nickname or id of the video uploader
  • channel (string): Full name of the channel the video is uploaded on
  • channel_id (string): Id of the channel
  • location (string): Physical location where the video was filmed
  • duration (numeric): Length of the video in seconds
  • view_count (numeric): How many users have watched the video on the platform
  • like_count (numeric): Number of positive ratings of the video
  • dislike_count (numeric): Number of negative ratings of the video
  • repost_count (numeric): Number of reposts of the video
  • average_rating (numeric): Average rating give by users, the scale used depends on the webpage
  • comment_count (numeric): Number of comments on the video
  • age_limit (numeric): Age restriction for the video (years)
  • is_live (boolean): Whether this video is a live stream or a fixed-length video
  • start_time (numeric): Time in seconds where the reproduction should start, as specified in the URL
  • end_time (numeric): Time in seconds where the reproduction should end, as specified in the URL
  • format (string): A human-readable description of the format
  • format_id (string): Format code specified by --format
  • format_note (string): Additional info about the format
  • width (numeric): Width of the video
  • height (numeric): Height of the video
  • resolution (string): Textual description of width and height
  • tbr (numeric): Average bitrate of audio and video in KBit/s
  • abr (numeric): Average audio bitrate in KBit/s
  • acodec (string): Name of the audio codec in use
  • asr (numeric): Audio sampling rate in Hertz
  • vbr (numeric): Average video bitrate in KBit/s
  • fps (numeric): Frame rate
  • vcodec (string): Name of the video codec in use
  • container (string): Name of the container format
  • filesize (numeric): The number of bytes, if known in advance
  • filesize_approx (numeric): An estimate for the number of bytes
  • protocol (string): The protocol that will be used for the actual download
  • extractor (string): Name of the extractor
  • extractor_key (string): Key name of the extractor
  • epoch (numeric): Unix epoch when creating the file
  • autonumber (numeric): Five-digit number that will be increased with each download, starting at zero
  • playlist (string): Name or id of the playlist that contains the video
  • playlist_index (numeric): Index of the video in the playlist padded with leading zeros according to the total length of the playlist
  • playlist_id (string): Playlist identifier
  • playlist_title (string): Playlist title
  • playlist_uploader (string): Full name of the playlist uploader
  • playlist_uploader_id (string): Nickname or id of the playlist uploader

You can customize the output file name or directory using these template arguments. Really comes handy if you are a data hoarder like me.

-f 'bestvideo[height<=480]+bestaudio/best[height<=480]'
-f or --format is the general syntax for format selection. Above argument will download the best video with the maximum of 480p height.

Format selection examples

# Download best mp4 format available or any other best if no mp4 available
youtube-dl -f ‘bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best’

# Download best format available but no better than 480p
youtube-dl -f ‘bestvideo[height<=480]+bestaudio/best[height<=480]'

# Download best video only format but no bigger than 50 MB
youtube-dl -f ‘best[filesize<50M]'

# Download best format available via direct link over HTTP/HTTPS protocol
youtube-dl -f ‘(bestvideo+bestaudio/best)[protocol^=http]’

# Download the best video format and the best audio format without merging them
youtube-dl -f ‘bestvideo,bestaudio’ -o ‘%(title)s.f%(format_id)s.%(ext)s’

--download-archive ~/.config/youtube-dl/downloaded.txt
--download-archive FILE record the IDs of all downloaded videos in a file. This way youtube-dl downloads only the videos not listed in the archive file and skip duplicates.

After you are done with all the customization, you can save the config file. To save a file and/or leave vi you must switch to command mode, if you are not already in it. You can always enter command mode by pressing ESC key. There are several ways to save files and leave vi, each of which begins with you typing a colon character (:).

Save the current file (write file) but do not exit. This command fails if the file is read-only.
Quit vi. This command fails if you have made changes to a file since the last time you saved it.
Save the current file and exit vi. The command :x is equivalent to this, except that it only saves the current file if you have changed it.

Now we need to move the config file to /etc to use it as the global config.

sudo cp ~/youtube-dl.conf /etcCode language: Bash (bash)

You can directly create a config file and save it in /etc directory rather than going this route.

Usage Example

Now you can download videos using the command

youtube-dl [OPTIONS] URL [URL…]Code language: Bash (bash)

For example:

youtube-dl language: Bash (bash)

You can download multiple videos by passing urls separated by space.

Leave a Comment