Programster's Blog

Tutorials focusing on Linux, programming, and open source

Download Youtube Videos With Youtube-dl

Installation

You can install youtube-dl through ubuntu's package management system...

sudo apt-get install youtube-dl

However, doing this results in me getting the following error when trying to grab some videos:

ERROR: Signature extraction failed: Traceback (most recent call last):
...

The best way to install the latest version is through the Python package management system (pip). Make sure you have installed pip and then run the following commmand:

sudo pip install --upgrade youtube-dl

Usage

youtube-dl [youtube video url]

Specify A Format

The tool should download the best resolution file by default, but doesn't always get the type of file you want. For example, I find that with Level1techs it will always grab the webm format. To resolve this, I just specify that I want the highest resolution mp4 file like so:

youtube-download -f "mp4" [youtube video url]

List All Options

If you want, you can list the different formats and resolutions available using the following command:

youtube-dl -F [youtube video url]

...which will give you something like:

format code  extension  resolution note
249          webm       audio only DASH audio   53k , opus @ 50k (48000Hz), 15.05MiB
250          webm       audio only DASH audio   71k , opus @ 70k (48000Hz), 18.85MiB
171          webm       audio only DASH audio  124k , vorbis@128k (44100Hz), 29.31MiB
140          m4a        audio only DASH audio  130k , m4a_dash container, mp4a.40.2@128k (44100Hz), 37.45MiB
251          webm       audio only DASH audio  131k , opus @160k (48000Hz), 33.97MiB
278          webm       256x144    DASH video  102k , webm container, vp9, 30fps, video only, 22.92MiB
160          mp4        256x144    DASH video  120k , avc1.4d400c, 30fps, video only, 32.57MiB
242          webm       426x240    DASH video  235k , vp9, 30fps, video only, 35.37MiB
133          mp4        426x240    DASH video  255k , avc1.4d4015, 30fps, video only, 72.56MiB
134          mp4        640x360    DASH video  331k , avc1.4d401e, 30fps, video only, 66.79MiB
243          webm       640x360    DASH video  442k , vp9, 30fps, video only, 67.08MiB
135          mp4        854x480    DASH video  629k , avc1.4d401f, 30fps, video only, 139.30MiB
244          webm       854x480    DASH video  792k , vp9, 30fps, video only, 112.19MiB
136          mp4        1280x720   DASH video 1191k , avc1.4d401f, 30fps, video only, 281.08MiB
247          webm       1280x720   DASH video 1626k , vp9, 30fps, video only, 247.77MiB
137          mp4        1920x1080  DASH video 2327k , avc1.640028, 30fps, video only, 533.65MiB
248          webm       1920x1080  DASH video 2773k , vp9, 30fps, video only, 534.38MiB
17           3gp        176x144    small , mp4v.20.3,  mp4a.40.2@ 24k
36           3gp        320x180    small , mp4v.20.3,  mp4a.40.2
43           webm       640x360    medium , vp8.0,  vorbis@128k
18           mp4        640x360    medium , avc1.42001E,  mp4a.40.2@ 96k
22           mp4        1280x720   hd720 , avc1.64001F,  mp4a.40.2@192k (best)

Use External Downloader To Speed Things Up

Youtube-dl supports using external downloaders. This can help mitigate artificial throttling that Google appears to be implementing. I found that using aria2 made a massive difference in download speed. To use external downloaders, add the --external-downloader [downloader] option. The list of supported external downloaders is listed in the download options on the github page, but I found it did not work with axel, even though it's listed.

Here is an example usage:

youtube-download --external-downloader aria2c [youtube video url]

Install aria2c with sudo apt install aria2.

You can even pass in arguments that should go to the external downloader. This makes an even bigger difference as it allows us to tell aria2 to use 16 connections like so:

youtube-download \
--external-downloader aria2c \
--external-downloader-args "-x 16 -s 16" \
https://youtu.be/AItOfUS0pT0

Cheat - Use Aliases

To save effort, I set youtube-download as an alias for youtube-dl with parameters as shown below: youtube-download -f "mp4" [youtube video url]

alias youtube-download="youtube-dl -f 'bestvideo[ext=webm]+bestaudio[ext=webm]/bestvideo[ext=mp4]+bestaudio[ext=m4a]/best'"

This will download the highest quality webm (vp9) video before resorting back to mp4 (x264). This is because webm vp9 is somewhat comparable to HEVC (which youtube doesn't offer) in terms of compression, yet still plays perfectly on my now ancient nexus10 through the chrome browser.

References