Download Youtube videos on Ubuntu 22.04 via command line

Use the youtube-dl tool in the Ubuntu 22.04 LTS Jammy Linux to download and save Youtube or various online videos directly on your local drive using the command line terminal.

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 you 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 on Ubuntu 22.04 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: ClipGrab on Ubuntu 22.04 LTS to download Youtube videos.

1. Install Youtube-DL on Ubuntu 22.04

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 repositories will have their 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 command: pip install youtube-dl

Now run the Youtube-dl installation command:

sudo apt install 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

youtube-dl --help

3. Download the Youtube video on Ubuntu 22.04

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.

youtube-dl https://youtu.be/4oi81N_AKBs
command Download the Youtube video on Ubuntu 22.04

Note: On Ubuntu or Linux Mint, if you get an error like this:

[youtube] 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. Command to check resolutions 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 resolutions 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 used in the previous step:

youtube-dl -F https://youtu.be/4oi81N_AKBs
Command to check resolutions and formats

5. Download the 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
Download the youtube by extension and resolution

6. Extract the 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 want to get the same, then simply use its Format code to download the video in only audio format only.

Extract the audio track only using the command

However,  this will not be 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 -x option.

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 the 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

nano ~/.config/youtube-dl.conf

Now add the following lines

#Directory to save files with format
-o '~/Videos/%(title)s.%(ext)s'

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.

Ending thoughts:

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.

Other Articles:

How to Download Youtube videos on Ubuntu 22.04 using GUI APP
How to restore the Trash can icon on Ubuntu 22.04 Desktop
Install and use WinSCP on Ubuntu 22.04 LTS Linux
2 ways for Installing FFmpeg on Ubuntu 22.04 LTS
Install Docker Desktop Dashboard on Ubuntu 22.04 LTS Linux

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.