Download youtube video on the command line

The program you need is youtube-dl

Install youtube-dl:
Get the latest version install instructions here

Eg:

sudo curl https://yt-dl.org/downloads/2016.05.16/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

Update to latest version:

youtube-dl -U

Sometimes youtube blocks downloads.
Eg:
#youtube-dl -F https://www.youtube.com/watch?v=r3fhuJJRcYc
[youtube] r3fhuJJRcYc: Downloading webpage
ERROR: Unable to download webpage: HTTP Error 429: Too Many Requests (caused by HTTPError()); please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; type youtube-dl -U to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

In this case, your ip is being blocked by Google. Use tor to mask your ip:
#torify youtube-dl -F https://www.youtube.com/watch?v=r3fhuJJRcYc
[youtube] r3fhuJJRcYc: Downloading webpage
[youtube] r3fhuJJRcYc: Downloading video info webpage
[youtube] r3fhuJJRcYc: Extracting video information
[youtube] r3fhuJJRcYc: Downloading js player en_US-vflGdEImZ
[info] Available formats for r3fhuJJRcYc:
format code extension resolution note
249 webm audio only DASH audio 54k , opus @ 50k, 1.80MiB
250 webm audio only DASH audio 77k , opus @ 70k, 2.43MiB
140 m4a audio only DASH audio 128k , m4a_dash container, [email protected], 4.45MiB
251 webm audio only DASH audio 149k , opus @160k, 4.75MiB
171 webm audio only DASH audio 150k , [email protected], 4.55MiB
278 webm 256x144 144p 111k , webm container, vp9, 12fps, video only, 3.26MiB
160 mp4 256x144 144p 111k , avc1.4d400c, 24fps, video only, 3.84MiB
133 mp4 426x240 240p 252k , avc1.4d4015, 24fps, video only, 8.62MiB
242 webm 426x240 240p 256k , vp9, 24fps, video only, 7.19MiB
243 webm 640x360 360p 472k , vp9, 24fps, video only, 13.00MiB
134 mp4 640x360 360p 638k , avc1.4d401e, 24fps, video only, 13.03MiB
244 webm 854x480 480p 822k , vp9, 24fps, video only, 22.17MiB
135 mp4 854x480 480p 1166k , avc1.4d401e, 24fps, video only, 25.19MiB
247 webm 1280x720 720p 1566k , vp9, 24fps, video only, 41.77MiB
136 mp4 1280x720 720p 2288k , avc1.4d401f, 24fps, video only, 44.77MiB
248 webm 1920x1080 1080p 2715k , vp9, 24fps, video only, 73.14MiB
137 mp4 1920x1080 1080p 3726k , avc1.640028, 24fps, video only, 77.35MiB
17 3gp 176x144 small , mp4v.20.3, mp4a.40.2@ 24k
36 3gp 320x180 small , mp4v.20.3, mp4a.40.2
5 flv 426x240 small , h263, mp3 @ 64k
43 webm 640x360 medium , vp8.0, [email protected]
18 mp4 640x360 medium , avc1.42001E, mp4a.40.2@ 96k
22 mp4 1280x720 hd720 , avc1.64001F, [email protected] (best)

Downloading mp3 songs from youtube video:

torify youtube-dl --extract-audio --audio-format mp3 https://www.youtube.com/watch?v=APJxwxZuA0E

Use a bash alias to simplify the process:

alias ymp3='torify youtube-dl --extract-audio --audio-format mp3'
ymp3 https://www.youtube.com/watch?v=Qs2ta9_CaKM

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.