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)

Cheat

To save effort, I set youtube-download as an alias for youtube-dl with parameters as shown below:

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