For many reasons, you might want to download a video to save for later or to play offline. The youtube-dl is a free and open-source utility that lets you download YouTube and other videos from the Linux command line. It not only works for Linux but also for Windows and MacOS. Among many other utilities, youtube-dl has a large set of features and its updates are released more frequently.
- Supported on Linux, Windows, and MacOS
- Download videos in particular quality and format.
- Other than just YouTube, it supports downloading videos from many sites
- Download subtitles of videos
- Set maximum download rate in bytes per sec
- Download only audio from a video.
- Download video in specific format and quality
In this post, we will show you how to install the youtube-dl utility and use it to download YouTube videos through the command line in Linux.
- Sudo user
- Python version 2.6, 2.7, or 3.2+ installed
Note: We have explained the whole procedure on Ubuntu 20.04 LTS. However, the same procedure also applies for all Linux distributions.
Installing youtube-dl (recommended way)
The youtube-dl is an open-source python script. We will download this script and save it in the /usr/local/bin. Here is the command to do so:
$ sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl
Now using the following command, make the script executable:
$ sudo chmod a+rx /usr/local/bin/youtube-dl
Now the youtube-dl latest version has been installed on your system. To view what version has been installed, use the following command:
$ youtube-dl --version
The following output shows the current installed version of youtube-dl is 2021.01.24.1 which is the latest version.
The youtube-dl releases its updates once a month or a week. To update your youtube-dl version, use the following command:
$ youtube-dl -U
Installing youtube-dl via snap
The youtube-dl is also available as a snap package. To install youtube-dl snap, first enable snapd on your system. Use the following command to do so:
$ sudo apt install snapd
Then to install youtube-dl, use the following command:
$ sudo snap install youtube-dl
Download YouTube videos
To download a YouTube or any other video via youtube-dl, use the following syntax:
$ youtube-dl <video_url>
Replace <video_url> with the URL of the required video. The downloaded video will be saved as ‘title-id.ext’ in the current directory.
$ youtube-dl https://www.youtube.com/watch?v=ksonXnMiUXI
You can see the downloaded video has been saved as ‘Exclusive clip from UP!-ksonXnMiUXI.mp4’ in the current directory. Here, Exclusive clip from UP! is the title of the video, ksonXnMiUXI is the ID of the video and .mp4 is the video extension.
Save the video in a different location
By default, youtube-dl saves the downloaded video in the current Terminal directory. You can also save the file in a different location while keeping the original video name format. For example, to download the video in /home/user/Videos directory, use the following syntax and replace <video_url> with actual URL:
$ youtube-dl -o ‘~/Videos/%(title)s-%(id)s.%(ext)s’ <video_url>
Save the video with a different name
You can save the file with a different name other than the original name:
$ youtube-dl -o ‘<filename>’ <video_url>
$ youtube-dl -o ‘UP.mp4’ https://www.youtube.com/watch?v=ksonXnMiUXI
The downloaded video will be saved as‘UP.mp4” in the current directory.
You can also include custom details in the filename, such as release date, uploader name, channel name, etc. This can be done using the -o option. The -o option allows you to specify a template for the file names. For example, the following command will download a YouTube video and include title, upload date, and video duration in the filename:
$ youtube-dl -o '%(title)s.%(upload_date)s.%( duration)s' https://www.youtube.com/watch?v=ksonXnMiUXI
You can see in the following output, the video is saved as ‘Exclusive clip from UP!.20090202.45 .mp4’ where ‘Exclusive clip from UP is the video title, 20090202 is the upload date and 45 is the video duration in seconds.
Download multiple videos
Sometimes, we need to download multiple videos. Instead of running the download command for all videos one by one, you can download all of them with a single command. To do so, list the URLs of all the videos in a file, one URL per line, and then call that file using the -a option. Here is the command syntax to download multiple videos:
$ youtube-dl -a <file-name>
Where <file-name> is the file containing all the URLs.
This command will extract the audio from the specified video and save it in the current directory.
This command will download all the URLs specified in the file and save them in the current directory.
Download only audio from a video
If you need to download only audio from a video, use the -x option as follows:
$ youtube-dl -x <video-url>
This command will extract the audio from the specified URL and save it in the current directory.
Download video in specific format and quality
Using youtube-dl, you can also download a video in a specific format and quality. You can list the available formats of a specific video using the following command:
$ youtube-dl -F <video_url>
The following output shows the list of available formats for a specific URL.
Now from the list, you can choose to download a specific format and quality using the format code:
$ youtube-dl -f <format code> <video_url>
For example, to download this video in MP4 format and 144p quality, the command would be:
$ youtube-dl -f 160 <video_url>
You can also remove youtube-dl from your system if you need to do so. As we have installed youtube-dl by downloading its script in the system path /usr/local/bin. So now in order to remove youtube-dl, you will need to remove the script from the path as follows:
$ sudo rm -rf /usr/local/bin/youtube-dl
If you have installed youtube-dl via snap, you can remove it as follows:
$ sudo snap remove youtube-dl
This is how you can download YouTube videos on Linux using the youtube-dl utility. The youtube-dl is a handy utility with a large feature set. Here, we have discussed only a few and common options. However, there is a lot more you can do with this utility. To explore more features, visit its official documentation. To play the downloaded videos, you can use any media players like VLC, FFmpeg, etc.