youtube-dl is a python package that allows you to download videos from youtube. You can also use yt-dlp, that is an actively maintained fork of youtube-dl.
You can install it on Ubuntu with the command
apt install youtube-dl -y
On CentOS
yum install python-pip
pip install youtube-dl
To download a video, use
youtube-dl -f 18 https://www.youtube.com/watch?v=j9oDMb7S6iw
To download only audio, use
youtube-dl -f 140 https://www.youtube.com/watch?v=j9oDMb7S6iw
Replace youtube video URL with URL of the video you need to download.
To see all available formats for download, run
[root@server12 dn]# youtube-dl -F https://www.youtube.com/watch?v=VIDEO_ID
[youtube] Confirming age
[youtube] VIDEO_ID: Downloading webpage
[youtube] VIDEO_ID: Downloading video info webpage
[youtube] VIDEO_ID: Extracting video information
[youtube] VIDEO_ID: Downloading DASH manifest
[info] Available formats for VIDEO_ID:
format code extension resolution note
139 m4a audio only DASH audio 56k , audio@ 48k (22050Hz), 42.01MiB (worst)
140 m4a audio only DASH audio 136k , audio@128k (44100Hz), 111.80MiB
141 m4a audio only DASH audio 262k , audio@256k (44100Hz), 222.02MiB
160 mp4 256x144 DASH video 130k , video only, 89.70MiB
133 mp4 426x240 DASH video 274k , video only, 213.53MiB
134 mp4 640x360 DASH video 608k , video only, 259.05MiB
135 mp4 854x480 DASH video 1117k , video only, 559.52MiB
136 mp4 1280x720 DASH video 2244k , video only, 1.15GiB
17 3gp 176x144
36 3gp 320x240
5 flv 400x240
43 webm 640x360
18 mp4 640x360
22 mp4 1280x720 (best)
[root@server12 dn]#