Today lets talk about a command-line tool that we can use to download YouTube videos. Its called `youtube-dl`, you may have heard it and use it. But today I am gonna tell you some hidden secrets of it.
There are many guides about how to install it in Linux environment. But I highly recommend to follow the steps given in there official-site. If you use sudo apt install youtube-dl it would work, but it will install some previous version of youtube-dl. You would get errors or warnings with some videos you try to download. You can also use pip for installing as youtube-dl written in python.
Also youtube-dl is not restricted for YouTube, you can use it to download any video you want, you just need to have the url.
For all Linux and MacOS users :
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl
Done, you have successfully installed youtube-dl latest version.
Download a YouTube video
This will download the best quality video. You can select what is the video quality you want to download. Happy reading, you would find it later in this articular.
Downloading a playlist
youtube-dl -cit youtube-playlist-url
Here you just have to paste the YouTube playlist url. youtube-dl will automatically download the full playlist for you.
-c ? Countinue the download
-i ? Ignore errors
-t ? With titles
This is a cheatsheet, so I am not going to explain what these options do in later sections. But you can read them all from youtube-dl official documentation.
Download specific videos from a playlist
When you just need some certain videos to download from a play list, you can use.
As an example, if you just need to download from 5th to 10th video. you can use :
youtube-dl –playlist-items 5-10 youtube-playlist-url
Video quality selection
If you no need to have the best quality video to be downloaded, you can easily select the best quality you want to have using following commands.
First we have to find,what are the available qualities for the video:
youtube-dl -F youtube-video-url
Output would be like :
[youtube] youtube-url: Downloading webpage[youtube] youtube-url: Downloading video info webpage[info] Available formats for youtube-url:format code extension resolution note249 webm audio only DASH audio 51k , opus @ 50k, 5.87MiB250 webm audio only DASH audio 80k , opus @ 70k, 8.53MiB140 m4a audio only DASH audio 131k , m4a_dash container, mp4a.40.2@128k, 14.99MiB251 webm audio only DASH audio 144k , opus @160k, 15.32MiB242 webm 426×240 240p 112k , vp9, 30fps, video only, 4.96MiB135 mp4 854×480 480p 130k , avc1.4d401f, 30fps, video only, 6.47MiB136 mp4 1280×720 720p 227k , avc1.4d401f, 30fps, video only, 10.71MiB243 webm 640×360 360p 236k , vp9, 30fps, video only, 7.56MiB244 webm 854×480 480p 358k , vp9, 30fps, video only, 10.64MiB137 mp4 1920×1080 1080p 576k , avc1.640028, 30fps, video only, 30.06MiB247 webm 1280×720 720p 668k , vp9, 30fps, video only, 17.77MiB248 webm 1920×1080 1080p 1445k , vp9, 30fps, video only, 48.80MiB43 webm 640×360 medium , vp8.0, vorbis@128k, 31.06MiB18 mp4 640×360 medium 309k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 35.83MiB22 mp4 1280×720 hd720 221k , avc1.64001F, mp4a.40.2@192k (44100Hz) (best)
Here you have to be careful, as you can see 248 webm 1920×1080 1080p 1445k , vp9, 30fps, video only, 48.80MiB
if you select this option, it will download only the video, not sound track. Oh, I forgot to mention earlier. youtube-dl downloads video and audio separately. Video first and then audio. Then it automatically merge them properly.
Downloading the video format you want youtube-dl -f 18 youtube-video-urlHere it will download medium quality video. 360p as I remember.
Downloading as mp3
If you are a mp3 audio fan, you can easily download just mp3 format. You can use two ways, first as I explained above, using -F and finding audio only format. Using above sample output :
251 webm audio only DASH audio 144k , opus @160k, 15.32MiB
so you just have to use youtube-dl -f 251 youtube-video-url
If you just need mp3 audio only, then you have to use:
youtube-dl –extract-audio –audio-format mp3 youtube-video-url
Possible errors :
/usr/bin/env: ?python?: No such file or directory
to solve this you can use : python3 /usr/local/bin/youtube-dl
Simply make an alias for this as youtube-dl.
These are the basics youtube-dl commands, there are tons of them described in their documentation. If I have missed a useful cheat command, please comment it out.