How to download YouTube videos right from the terminal on Ubuntu

Downloading YouTube videos has been one of the most common user activities since the rise of the world's most popular entertainment content website. The reason to download something from YouTube can be to have access to it without having to be online, keep something that may get restricted in your geographic location in the future, or download music videos just to burn them on an audio CD. In this quick guide, we will take a look on how to download YouTube videos right from the terminal.

Our first step will be to download the video. While there are many ways to do this, we will use a tool that can work through our terminal for greater simplicity and power. The tool is called “youtube-dl” and Ubuntu users may install it by opening a terminal and typing “sudo apt-get youtube-dl”. Just make sure that you have “ffmpeg” also installed and that your libav version is newer than v10. This is not absolutely required for youtube-dl to work, but if your libav version is too old you will get limited functionality. To solve this in Ubuntu, run the following commands in a terminal as root:

sudo add-apt-repository ppa:heyarje/libav-11
sudo apt-get update
sudo apt-get install libav-tools

After this is done, you may simply download any youtube video by opening a terminal and typing “youtube-dl [video url]” as shown in the following screenshot:

Download videa from YouTube

This will download the video on default settings which is your Home folder as the location, max resolution as the selected quality, and mkv as the video format.

Format options

Now, let's take a look on how to set all this starting with the format options. First, we need to determine what the available formats for our target video are. To do this, use the “youtube-dl -F [video url]” command.

Available Video Formats

Now to choose a format from the list, you may simply use the corresponding code, or the filetype name preceded by the “-f” parameter in the command. For example, if I want to download the “audio only” type in the Opus format, I will use “youtube-dl -f 250 [video url].

Download audio only

If you want to download the video in all available formats, you may use the “--all-formats” parameters instead of downloading them one by one. In the case of playlists, you should know that those are reflected in the video url, so youtube-dl will download all videos contained in a playlist unless told otherwise (this works for version 2014.02.17 and above) This “telling” can be done by using the “--playlist-items” parameter followed by a space and the items numbers separated by commas. This will only download the specified items from the playlist. If the playlist items are continuous, you may also specify the range using numbers and dashes like 3-6 instead of 3,4,5,6.

Now that we're at it, in most cases of downloading playlists, we do it for the audio. You may skip the post-processing work of having to turn the videos into mp3s by adding the following two parameters in the command: “--extract-audio” and “
--audio format mp3”. This will download the specified video or playlist directly into an mp3 format.

Download as mp3 file

Another nice feature in regards to the playlist downloading is the ability to tell youtube-dl to download only the most popular videos out of a large playlist that has too many items for you to check one by one. This is done by using the “--min-views [number]” parameter.

Finally, youtube-dl can also download subtitles with your video, a feature that is very useful in many situations. We can check what languages are available by typing “youtube-dl --list-subs [video url]”. This will also display the subs that are automatically generated by YouTube's “audio to text” algorithms.

Download subtitles

To choose the subtitle that you need, use the “--sub-lang [language code] [video url]”. For example, if I wanted to download the particular video with English subs, I would use the command as “youtube-dl –sub-lang en [video url]”.

I believe the above commands to be adequate to cover the most common user needs, but it is important to note that youtube-dl has a much wider spectrum of functionality and operational capacity. If you want to discover this, use the “--help” parameter and start exploring further.

YouTube continuously filters out content that violates copyrights and it is often the case that you may have limited-time access to something that is otherwise “not free” in your country. HowToForge and the editor of this post don't encourage the downloading of copyrighted material, nor take the responsibility for such actions. This guide is provided with the note that its readers are responsible for checking the copyrights of the material and their application in their country before downloading it.

Share this page:

13 Comment(s)

Add comment

Comments

From: steve_dupuis at: 2016-09-01 14:48:01

One thing that always bothers me is where you specify [Video URL] in the examples and explanations. Where do we find these URLS?

From: Brian at: 2016-09-01 20:10:25

You're just being silly right?  You get the video url from the video on youtube that you're wanting to download.

From: Derek Broughton at: 2016-09-01 21:45:22

Where do you get the URL????  Off the  YouTube page... Or, Vimeo!  I just tried this with Vimeo, worked perfectly.

From: AtomickOnline at: 2016-09-01 23:47:41

It's just the URL of the YouTube video (When you are watching a YouTube video, copy the URL in the address bar).For example: https://www.youtube.com/watch?v=1234abcdAlso, if you look closely at the 5th picture that he's used (above the sentence starting "Another nice feature in regards...), you can see the command with an actual URL being used.Hope this helps.

From: zathras11 at: 2016-09-02 01:38:36

?

From: hpmc at: 2016-09-01 18:37:25

If you have to ask, then this probably isn't for you.

From: jed evnull at: 2016-09-01 19:21:55

Fails every time:

youtube-dl https://youtu.be/8IjCeN5XvUA

[youtube] Setting language

WARNING: unable to set language: <urlopen error [Errno 8] _ssl.c:510: EOF occurred in violation of protocol>

[youtube] 8IjCeN5XvUA: Downloading webpage

ERROR: Unable to download webpage: <urlopen error [Errno 8] _ssl.c:510: EOF occurred in violation of protocol>

From: KO at: 2016-09-02 02:44:30

The URL would be of the video you want, like this 

https://youtu.be/61_qsDeuFu8

From: dgrb at: 2016-09-02 03:19:18

From youtube.

Go there and find the video you want, copy the url from your browser (Control-C) and paste it into the command in your shell (almost certainl Control-Shift-V).

 

From: Zineddine at: 2016-09-02 09:30:00

Hi, first thank you, I really needed an easy way to download from youtube. second, I love your wallpaper where can I get it!!!!

From: Bill Toulas at: 2016-09-02 11:13:11

Hello Zineddine and thanks for your comment. Here's a link to the wallpaper: https://alpha.wallhaven.cc/wallpaper/400957

From: W.Willems at: 2016-09-05 19:49:06

Hello,

read your article....

I´ve been using cclive for years now....

try it...

do apt-get install cclive...

then cclive (video-url)

have fun...!

From: Paul C. at: 2016-09-05 21:03:30

I get the same error message everytime myself.

$ youtube-dl https://youtu.be/wkonaa_UZws?list=PL91A53ABF8155F139[youtube] Setting languageWARNING: unable to set language: <urlopen error [Errno 8] _ssl.c:510: EOF occurred in violation of protocol>[youtube] wkonaa_UZws: Downloading webpageERROR: Unable to download webpage: <urlopen error [Errno 8] _ssl.c:510: EOF occurred in violation of protocol>