There is no shortage of video content on the web. Almost all of them require an active internet connection to enjoy. What if you don’t always have an internet connection? What if you want to download the videos for offline viewing? In this Linux quick tip we will show you how to download YouTube videos from the Linux command line.
Before we go any further, it must be said that downloading videos is against the YouTube terms of service. It is likely against other the terms of service of most other platforms as well.
Table of Contents
Introducing youtube-dl – Command Line Video Downloader
Youtube-dl is a Python program for downloading videos from the command line. Because it is written in Python, it can be used on just about any operating system. It will work on UNIX, Linux, MacOS, and Windows.
Is supports a large number of video sites, including Youtube, Break, BuzzFeed, and yes, of course, PornHub, YouPorn and many other adult sites. For a full list of supported sites, see the resources link at the end of this article.
Installing youtube-dl Video Downloader
You can easily install youtube-dl with most package managers. Keep in mind that you may not get the newest version, depending on what is in your repositories. If you want the most up to date version, I recommend installing directly from their website or using pip. Another benefit of installing directly from their website is you can update it as soon as a new version is available. If you update via a package manager, you will have to wait for the package to be updated in the repository.
DNF Installation on Fedora, CentOS, or Red Hat
sudo dnf install youtube-dl
APT Installation on Ubuntu, Debian, or Mint
sudo apt install youtube-dl
Installation Directly from yt-dl.org
You can download the ready to use Python script directly from their site using curl. All you have to do is set the permissions and you are up and running.
Download the file and save it to /usr/local/bin/youtube-dl:
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
Set the permissions to allow it to be executed:
sudo chmod a+rx /usr/local/bin/youtube-dl
Installation Using PIP
Alternately, you can easily install via pip as well.
sudo -H pip install --upgrade youtube-dl
If you installed from a package manager you will automatically get the update when one is available in the repository. You will get notified of such if you try to update.
[[email protected] ~]$ youtube-dl -U It looks like you installed youtube-dl with a package manager, pip, setup.py or a tarball. Please use that to update.
However, if you installed from their website, you can update the program at anytime using the
[[email protected] ~]$ youtube-dl -U youtube-dl is up-to-date (2020.05.03)
Basic Usage and Downloading Your First Video
Once you have youtube-dl installed, it is simple to download your first video. Simple copy the URL from YouTube and pass it as an argument to the command. I usually like to wrap the URL in single quotes to avoid any of the special characters being interpreted by the shell.
I have a special config that is forcing mp4 and best possible quality (more on that later). Sometimes, the video and audio files are downloaded separately and merged into an mkv file. If that is the case, you can easily covert mkv to mp4.
The number of options available for youtube-dl is impressive. Of course we aren’t going to cover them all here, but we will touch on the more common options.
Check Available Formats
-F (–list-formats) option allows you to see the available formats, each with a format code. This allows you to download the desired format/resolution/fps. Here is a list of available options using our example video:
[[email protected] YouTube]$ youtube-dl -F 'https://www.youtube.com/watch?v=hG0yCiXQzpI&t=8s' [youtube] hG0yCiXQzpI: Downloading webpage [youtube] hG0yCiXQzpI: Downloading MPD manifest [info] Available formats for hG0yCiXQzpI: format code extension resolution note 139 m4a audio only DASH audio 57k , m4a_dash container, [email protected] 48k (22050Hz) 251 webm audio only DASH audio 133k , webm_dash container, opus @160k (48000Hz) 140 m4a audio only DASH audio 166k , m4a_dash container, [email protected] (44100Hz) 278 webm 256x144 DASH video 95k , webm_dash container, vp9, 30fps, video only 160 mp4 256x144 DASH video 108k , mp4_dash container, avc1.4d400b, 30fps, video only 134 mp4 640x360 DASH video 331k , mp4_dash container, avc1.4d401e, 30fps, video only 244 webm 854x480 DASH video 752k , webm_dash container, vp9, 30fps, video only 135 mp4 854x480 DASH video 1155k , mp4_dash container, avc1.4d4014, 30fps, video only 247 webm 1280x720 DASH video 1505k , webm_dash container, vp9, 30fps, video only 137 mp4 1920x1080 DASH video 2476k , mp4_dash container, avc1.640028, 30fps, video only 248 webm 1920x1080 DASH video 2646k , webm_dash container, vp9, 30fps, video only 18 mp4 640x360 360p 281k , avc1.42001E, [email protected] 96k (44100Hz), 8.70MiB 22 mp4 1280x720 720p 909k , avc1.64001F, [email protected] (44100Hz) (best)
As you can see there are several different formats available. The highest quality video is always highlighted with (best) at the end.
Downloading Desired Format
You can download the desired format by using the
-f option followed by the format code in the output above. In this example we want to download the best possible quality so we will choose format code 22.
youtube-dl -f 22 'https://www.youtube.com/watch?v=hG0yCiXQzpI&t=8s'
Download A Complete YouTube Playlist
The beauty of this tool is that it is simple, yet robust. To download all the videos in a playlist, just pass the playlist as an argument just like it was a video.
[[email protected] ~]$ youtube-dl 'https://www.youtube.com/watch?v=eC8Qygp2lvs&list=PL29tgMqrJrhhRSm4FReJSi6nRxT_0UXvj' [youtube:playlist] Downloading playlist PL29tgMqrJrhhRSm4FReJSi6nRxT_0UXvj - add --no-playlist to just download video eC8Qygp2lvs [youtube:playlist] PL29tgMqrJrhhRSm4FReJSi6nRxT_0UXvj: Downloading webpage [download] Downloading playlist: Linux Quick Tips [youtube:playlist] playlist Linux Quick Tips: Downloading 9 videos ..OUTPUT TRUNCATED...
You can also download the playlist in reverse order (
--playlist-reverse) or random order (
Bypass Geo Restriction
You can bypass the geo restriction on most websites by using the
--geo-bypass option to change the X-Forwarded-For HTTP header.
[[email protected] ~]$ youtube-dl --geo-bypass 'https://www.youtube.com/watch?v=eC8Qygp2lvs&list=PL29tgMqrJrhhRSm4FReJSi6nRxT_0UXvj'
Limit Download Rate
Some sites have controls in place to block your IP address if you download large amounts of data too fast. In this case you can rate limit your download to something reasonable. This also comes in handy (especially on slower connections) to avoid saturating your connection. If you limit your download rate, you can potentially still use your connection for other things while the video downloads.
Just pass the
-r (–limit-rate) option followed by the desired speed. You can reference the speed in K or M.
Limit download rate to 150K:
youtube-dl -r 150K https://www.youtube.com/watch?v=eC8Qygp2lvs
Limit download rate to 2MB:
[[email protected] ~]$ youtube-dl -r 2M https://www.youtube.com/watch?v=eC8Qygp2lvs [youtube] eC8Qygp2lvs: Downloading webpage [youtube] eC8Qygp2lvs: Downloading MPD manifest [dashsegments] Total fragments: 56 [download] Destination: Creating Encrypted and Password Protected Archives on Linux Command Line-eC8Qygp2lvs.f137.mp4 [download] 16.1% of ~18.31MiB at 1.99MiB/s ETA 00:16
Building a Custom Configuration
If you find options that you like, you can easily set them as default. Simply create a file called
config in your
/home/<username>/.config/youtube-dl/ directory. This directory does not exist by default, so you will have to create it.
Now create a file called
config in the above directory and fill it with your favorite options. These options will be used by default every time your run youtube-dl.
I use the following options in mine default configuration. This allows me to easily download the best audio and video quality in the mp4 container.
[[email protected] ~]$ cat ~/.config/youtube-dl/config -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]'
This article should serve as a good introduction to youtube-dl. However, there is MUCH more you can do with this incredible utility. Please see the resources below for more information.
This site uses Akismet to reduce spam. Learn how your comment data is processed.