Use the youtube-dl tool in the Command line terminal of your Linux to download and save various online videos directly on your local drive.
YouTube is one of the popular platforms to watch videos online and in case you want to download some of your videos from it on Linux such as Ubuntu, Debian, RHEL, Fedora, and CentOS; then we don’t need some GUI software. This can be done directly using the command terminal of the Linux distro you are using.
Moreover, many of us are not regular YouTube video downloaders, thus for once in a while getting some video for offline usage doesn’t need software. We can use a simple command-line tool called youtube-dl for downloading online videos. However, this tutorial is for educational purposes, thus before saving anyone’s video please take care of all copyright laws applicable to that.
- Download Youtube Videos using the Linux command line
- 1. Install Youtube-DL
- 2. Youtube-DL commands Usage
- 3. Download Youtube video
- 4. Check all available resolution and formats
- 5. Download Video by extension and resolution
- 6. Extract audio track only using the command
- 7. Stream video using Youtube-DL to media player
- 8. Set Default directory to save all video files
Download Youtube Videos using the Linux command line
Well, if you are not fond of using the command-line interface, then we can get online videos using GUI. For that see our article: Install youtube-dlg GUI in Ubuntu 20.04 LTS or ClipGrab on Ubuntu 20.04 LTS to download Youtube videos.
1. Install Youtube-DL
It is an open-source command-line tool that is available to install on Debian, Ubuntu, RHEL, and other similar Linux. Youtube-dl is written in Python. It is not just limited to Youtube but supports huge numbers of websites to download online videos such as Dailymotion, BBC, DiscoveryPlus, GoogleDrive, and more, you can see the complete list on the official page. Most of the popular official repository will have its package to install, thus no need to fiddle around to add some.
First, run the update command:
sudo apt update
Note: (optional) If you already have python & pip package manager on your system then it is possible to install this tool using
pip install youtube-dl
Now run Youtube-dl installation command:
For RHEL, CentOS, Fedora, and other derivatives:
sudo dnf install youtube-dl
For Debian, Ubuntu, Linux Mint, Kali Linux, and other similar Linux run:
sudo apt-get install youtube-dl
To install on Arch Linux
sudo pacman -Sy youtube-dl
2. Youtube-DL commands Usage
This command-line tool can be used with the help of the syntax given below.
Usage: youtube-dl [OPTIONS] URL [URL…]
To know all the available commands we can use
Options: General Options: -h,--help Print this help text and exit --version Print program version and exit -i, --ignore-errors Continue on download errors, for example, to skip unavailable videos in a playlist --abort-on-error Abort downloading of further videos (in the playlist or the command line) if an error occurs --dump-user-agent Display the current browser identification --list-extractors List all supported extractors --extractor-descriptions Output descriptions of all supported extractors --force-generic-extractor Force extraction to use the generic extractor --default-search use this prefix for unqualified URLs. ---more---
3. Download Youtube video
Let’s run this command-line utility to know how it works to download online videos not only from Youtube but from other platforms as well.
What we have to do is, just type the command
youtube-dl and then the link of the video you want to download. That’s it.
Note: On Ubuntu 20.04 or Linux Mint , if you get an error like this:
4oi81N_AKBs: Downloading webpage ERROR: 4oi81N_AKBs: YouTube said: Unable to extract video data
Then you have to first download the youtube-dl latest version from- https://packages.debian.org/sid/all/youtube-dl/download
And install it:
sudo apt install ./youtube-dl_*-*_all.deb
4. Check all available resolution and formats
In the above command, we cannot control the video format and resolution. Thus, to do that first check what are the available formats and res. available for that particular you want to download.
This can be done simply by adding the
-F flag in the above command. For example, if I want to see this information for the URL we have used in the previous step:
youtube-dl -F https://youtu.be/4oi81N_AKBs
5. Download Video by extension and resolution
Once you get the list of all the available resolutions and extensions including audio-only ones, we can download any of them if we want using the Format code given in front of every video/audio extension option. You can see that in the given screenshot.
The command syntax for that is
youtube-dl -f format-code video-URL
So, let’s say we want to download mp4 in 256×128 res., the format code for that is 160, so the above command for our URL will be
youtube-dl -f 160 https://youtu.be/4oi81N_AKBs
6. Extract audio track only using the command
When we check the available extensions for some videos, we will also get the audio-only format, as we have for our video. If you get the same, then simply use its Format code to download the video in only audio format only.
However, this will not true for every video in such a situation we can use this Youtube-DL command-line tool to extract the audio from the given URL of the video. For that, we can use the
youtube-dl -x https://youtu.be/4oi81N_AKBs
But, the above command will extract and download the audio in default “opus” format, so, if you want other audio formats such as “aac”, “FLAC”, “mp3”, “m4a” and more, then we have to mention that as well in the command. Suppose, I want to extract the audio but in mp3 format then the above command will be:
youtube-dl -x --audio-format mp3 https://youtu.be/4oi81N_AKBs
7. Stream video using Youtube-DL to media player
Although you won’t require the streaming of online videos on some local media player when they are already playable in a browser, yet, if you want then we can use this command-line tool for that. In the below command, first, we type the command of this online video downloader and then guide it to give stdout (standard output) to VLC or any other player you have on your system.
youtube-dl -o - https://youtu.be/4oi81N_AKBs | vlc -
8. Set Default directory to save all video files
Right now, when you run any of the above commands, it will save the file in your current directory. So, if you need to download multiple files but want them to save in some particular folder of your Linux PC, then create a configuration file for it.
Create a configuration file
Now add the following lines
#Directory to save files with format
The explanation for the above lines
#Directory to save files with format: Just for describing what we are doing, this is an optional one.
In the next line, ~/Videos is the default video directory of your Linux OS, you can change that to some other folder by giving it a path instead of this.
%(title)s – This will save or write the video to the specified location with its title.
%(ext)s – This option in the above line will print or save the file with its current video or audio extension.
Save the file by pressing Ctrl+X, type Y, and then hit the enter key.
These are just a few command commands we can use for Youtube-DL to download and save the various videos. You can go through its documentation to know all other commands.