How to Download YouTube Videos From the Terminal

If you happen to have a Linux server or Mac and want to know how to download videos from the terminal I have good news, youtube-dl can do that and more! I’ll will show you the basics on this guide.

youtube-dl is a command-line program to download videos from YouTube.com and a few more sites. It requires the Python interpreter, version 2.6, 2.7, or 3.2+, and it is not platform specific. It should work on your Unix box, on Windows or on Mac OS X. It is released to the public domain, which means you can modify it, redistribute it or use it however you like. You can find the source and installation instructions here. There is quite a lot of information on the project page and it can be a bit overwhelming, that’s why I decided to create this guide, I will show you how to get downloading stuff quickly with a couple of real life examples.

The general call for youtube-dl is:

youtube-dl [OPTIONS] URL [URL...]

if we add a real YouTube link:

youtube-dl -o /destination/filename https://www.youtube.com/watch?v=QH2-TGUlwu4

The -o option let’s you specify a destination and new name for your download. The command I use for downloads 99% of the time is shown below.

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' -ci -o /destination/'%(title)s.%(ext)s' https://www.youtube.com/watch?v=QH2-TGUlwu4

Let’s unpack all that is happening on this command.

-f    is telling youtube-dl that I want a specific version/format of the video.

The string 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' is telling youtube-dl that it should download the best possible mp4 video stream and the best possible m4a audio stream it finds (it will mux them together once it downloads each using ffmpeg if installed), else it will download the best possible mp4 version of the video I want or finally just download the best possible version of the video.

-ci    is telling youtube-dl that it should resume partially downloaded files and ignore errors like unavailable videos on a playlist.

-o    is specifying the destination for the download. '%(title)s.%(ext)s' is telling youtube-dl that you want the title of the video as the filename and that it should include the extension as well.

The final argument is just the link for the video, you can use the link for a playlist as well and youtube-dl will download all videos in the playlist.

Extra
If you want to batch downloads videos you can create a text file with the links of all the videos you want to download and youtube-dl will download those one at a time. You just need to add --batch-file='/path/file.txt' instead of the video link, command below.

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' -ci -o /destination/'%(title)s.%(ext)s' --batch-file='/path/batch_file.txt'

No Comments, Be The First!

Leave a Reply