About Me

header ads

[Guide] Youtube-dl: how to download videos from Youtube with the terminal on Linux

xyoutube-dl-linux

In today's mini-guide we will see how to use the terminal to download videos from YouTube and similar sites. We will use youtube-dl, a free and open-source tool, very simple to use, and highly configurable.

Youtube-dl: download of audio, video, and playlist

Youtube-dl is a program, to be run through a terminal, which allows you to download videos from many platforms. Find here a complete list of compatible sites, I have not counted them but there are several dozen, certainly more than 100. To work it needs the 2.6+ or 3.2+ interpreter of Python, so we can use it on GNU / Linux, but also on macOS and Windows. Although it is available in the main distributions repository, it is better to install it through the official website, to always have the latest version, as soon as it is released. This factor is essential for this type of program.

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 

By installing it with this procedure, we can obtain the latest release simply with the youtube-dl -U instruction.
To proceed with the download of a video, or an entire playlist, just use the syntax youtube-dl 'URL', for example:

youtube-dl ‘https://www.youtube.com/watch?v=n9YDz-Iwgyw’

All available options are accessible via the youtube-dl – -help instruction. Some of the most interesting are:

  • -F ‘URL’, to view all available video formats. You will have a screen like the one in the previous image. The first number corresponds to its file;
  • -f file_number ‘URL’, to download the chosen format;
  • -r 2M ‘URL’, to limit the download rate, in this example to 2MiB/s;
  • -a list_file.txt, to download a list of videos, the links of which are to be inserted in a generic file list_file.txt.

You can configure the operation of the program according to your needs. To do this we will have to go and create, if not existing, the configuration folder and its file:

mkdir ~/.config/youtube-dl
touch ~/.config/youtube-dl/config

An example of a configuration file would be the following. It allows you to download the audio of a video, limiting the download speed. In the second instance the file will be moved to your Android device and removed from the pc:

-r 2M
-f 'bestaudio[ext=m4a]'
--exec 'adb push {} /sdcard/Music/ && rm {}'

Bonus tip: a small GUI

Speaking of terminals, I cannot add a small bonus, for those approaching the GNU/Linux world for the first time. The possibilities provided by this operating system are indeed many. The bash scripting, in particular, gives us the opportunity to interact with the various programs.

youtube-dl-open-source-script

By giving the youtube-dl -F ‘URL’ command a couple of times, it can be seen that its output provides the " best " format on the bottom line. So I created a very small bash script, raw and not optimized, to speed up the use of the tool and provide a small GUI. To work, it requires zenity and that the custom configuration of the tool does not present instructions that lead to conflicts. In that case, you could directly insert the instructions in this script, rather than in the config file, to practice.

#!/usr/bin/env bash
URL=$(zenity --entry --title=”LFFL-Youtube-dl” --text="Enter the URL of the video to download")
youtube-dl -F $URL > ./youtube-dl-quality
NUM=$(awk '{w=$1} END{print w}' ./youtube-dl-quality)
rm ./youtube-dl-quality
youtube-dl -f $NUM $URL

For more details, I refer you to the official page of this project.

Post a Comment

0 Comments