Objective
Download YouTube videos from the command line using youtube-dl.
Distributions
Youtube-dl is a Python script that’s usable on any distribution.
Requirements
- A Linux install with root access.
- Python
- Pip Python package manager
Difficulty
Easy
Conventions
- # – requires given linux commands to be executed with root privileges either directly as a root user or by use of
sudo
command - $ – requires given linux commands to be executed as a regular non-privileged user
Introduction
People have wanted to download videos from YouTube since the very beginning. Several methods have worked over the years, but youtube-dl provides the most direct and reliable approach.
Youtube-dl is a Python script that pulls videos straight from YouTube and can format them on your computer. It can also separate audio or subtitles from videos.
Install YouTube-dl
Youtube-dl is available from a lot of distributions’ repositories, but it tends to lag somewhat behind the upstream releases. Normally, that wouldn’t be a problem, but YouTube changes, and those changes can break youtube-dl.
It’s best to stay current. You can do that by using Pip to manage youtube-dl as a Python package instead of a distribution one.
Install Python and Pip
Start off by installing Pip and Python through your distro.
Ubuntu/Debian
$ sudo apt install python python3 python-pip
Fedora
# dnf install python2 python3 python-pip
OpenSUSE
# zypper install python python3 python-pip
Arch Linux
# pacman -S python python2 python-pip
Gentoo
# emerge dev-python/pip
Using Pip
Pip is a Python package manager. There are a bunch of ways to use it, but in this case, a system-wide install is best. As root, you can use Pip like your distribution’s normal package manager.
# pip install youtube-dl
That’s it. When it finishes, you’re ready to use youtube-dl.
Downloading Videos
Downloading a video is very simple. All you need to do is give youtube-dl a URL, and it’ll do the rest.
$ youtube-dl https://www.youtube.com/watch?v=yVpbFMhOAwE
The file names aren’t the best, but you can easily rename them.
You can specify a file format, and youtube-dl will use FFMPEG to convert the video automatically.
$ youtube-dl --recode-video mp4 https://www.youtube.com/watch?v=yVpbFMhOAwE
Subtitles
You can also embed a video’s subtitles in it when you download.
$ youtube-dl --embed-subs https://www.youtube.com/watch?v=yVpbFMhOAwE
If you want the subtitles separate from a video, you can do that too.
$ youtube-dl --get-subs https://www.youtube.com/watch?v=yVpbFMhOAwE
That video actually doesn’t have subtitles, but you can get the automatically generated ones from YouTube.
$ youtube-dl --get-auto-subs https://www.youtube.com/watch?v=yVpbFMhOAwE
Audio
Youtube-dl is fully capable of extracting the audio from videos with the help of FFMPEG.
$ youtube-dl -x https://www.youtube.com/watch?v=yVpbFMhOAwE
You can specify the output format too.
$ youtube-dl -x --audio-format flac https://www.youtube.com/watch?v=yVpbFMhOAwE
If you’re concerned with audio quality, youtube-dl uses a scale of 0-9 to specify quality. Zero produces the highest grade output.
$ youtube-dl -x --audio-format flac --audio-quality 0 https://www.youtube.com/watch?v=yVpbFMhOAwE
Closing Thoughts
It’s probably a good time to mention that youtube-dl supports way more than just YouTube downloads. You can find the whole list on the youtube-dl Github page https://rg3.github.io/youtube-dl/supportedsites.html
.
Youtube-dl also has options for logins and spoofing a browser, if you need to.
Youtube-dl is simple, elegant, and powerful. Everything considered, it’s easily the best tool for pulling video content from the Internet.