
Installing youtube-dl in Ubuntu
Youtube-dl is a command-line program to download videos from YouTube.com 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-dl
Code language: PowerShell (powershell)
Installation in Linux
To install it right away for all Linux users, type:
sudo apt-get install youtube-dl
Code language: Bash (bash)
You can also use pip:
sudo -H pip install --upgrade youtube-dl
Code 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 ffmpeg
Code language: Bash (bash)
To validate that the package is installed properly use the ffmpeg -version
command which prints the FFmpeg version:
ffmpeg -version
Code 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.conf
Code language: Bash (bash)
Open the file in your favourite command line text editor. I use vi here,
vi youtube-dl.conf
Code 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.txt
Code 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
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.
--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 (:
).
:w
Save the current file (write file) but do not exit. This command fails if the file is read-only.:q
Quit vi. This command fails if you have made changes to a file since the last time you saved it.:wq
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 /etc
Code 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 https://www.youtube.com/watch?v=dQw4w9WgXcQ
Code language: Bash (bash)
You can download multiple videos by passing urls separated by space.