Youtube-dl

From LinuxReviews
Jump to navigationJump to search

youtube-dl is a very handy general-purpose python script for downloading audio and video files from websites with video such as YouTube. The name implies that it is a youtube-specific tool but it's not, youtube-dl has specific support for a wide range of websites and there is also general-purpose support for any web page containing video.

youtube-dl is a command-line tool which is used from a terminal. Many popular video-related programs like the media player mpv use it as a helper if it's installed.

HOWTO download videos from websites[edit | edit source]

The basic command for using youtube-dl is as simple as

youtube-dl https://example.com/pagewithvideo.html

This will download a video file embedded in that page in the best possible format.

There could be multiple formats available. youtube-dl has the -F switch for listing available formats. It will not download anything when this switch is used, it will just list all the formats that are available. Example:

youtube-dl -F https://example.com/pagewithvideo.html

This will produce a list of both audio formats, video formats and combined file formats available if there are supported video files embedded on a web page. The supported formats will have numbers. This information can be used to download the file using specific formats listed using the -f videoformat+audioformat switch.

Running youtube-dl -F https://www.youtube.com/watch?v=ujjZDX7HHNs will produce a list similar to this:

format code  extension  resolution note
140    m4a  audio only DASH audio  130k , m4a_dash container, mp4a.40.2@128k, 3.05MiB
160    mp4  256x144    144p  138k , avc1.4d400c, 30fps, video only, 2.93MiB
278    webm 256x144    144p  142k , webm container, vp9, 30fps, video only, 2.92MiB
133    mp4  426x240    240p  310k , avc1.4d4015, 30fps, video only, 6.34MiB
242    webm 426x240    240p  316k , vp9, 30fps, video only, 6.70MiB
243    webm 640x360    360p  630k , vp9, 30fps, video only, 12.23MiB
134    mp4  640x360    360p  669k , avc1.4d401e, 30fps, video only, 13.58MiB
244    webm 854x480    480p 1161k , vp9, 30fps, video only, 23.03MiB
135    mp4  854x480    480p 1196k , avc1.4d401f, 30fps, video only, 25.67MiB
136    mp4  1280x720   720p 2428k , avc1.4d401f, 30fps, video only, 51.66MiB
247    webm 1280x720   720p 2457k , vp9, 30fps, video only, 46.41MiB
248    webm 1920x1080  1080p 3602k , vp9, 30fps, video only, 68.44MiB
137    mp4  1920x1080  1080p 5004k , avc1.640028, 30fps, video only, 107.56MiB
18     mp4  640x360    medium , avc1.42001E, mp4a.40.2@ 96k, 18.62MiB (best)

This file is only available with one audio format and the code for it is 140. If you like 1920x1080 VP9, which is identified as code 248 then -f 248+140 can be used to download the file with VP9 video and MP4 audio. The only container supported by youtube-dl capable of holding that combination is mkv so the output will be put in that container. The full command for downloading would be:

youtube-dl -f 248+140 https://www.youtube.com/watch?v=ujjZDX7HHNs

YouTube video ujjZDX7HHNs used in the above example refers to WJSN's fantastic hit "Boogie up" performed on The Show June 11th, 2019. They are so beautiful and talented and gorgeous, you will absolutely enjoy that file if you download it.

youtube-dl will by default download to a filename it thinks makes sense. You can force another filename with the -o option:

youtube-dl -o myvideo.mkv "http://www.youtube.com/watch?v=ujjZDX7HHNs"

You can use a proxy to download by setting the http_proxy environment which is, if set, honored by youtube-dl.

youtube-dl has a whole lot of other options which you likely won't need. See the very long manual page for all of them.

HOWTO tell if a website is supported or not[edit | edit source]

Just run youtube-dl against the site with -F and it will tell you if there are supported media on it or not: youtube-dl -F https://example.com/pagewithvideo.html

Note: Constant Updates are Required[edit | edit source]

youtube-dl downloads and parses websites and even runs JavaScript from those sites in order to figure out what video files are available and how they can be downloaded. The rules used to parse a given website will fail when a website changes and youtune-dl will simply fail to extract the information it needs. Sites like YouTube change their website on a regular basis - breaking youtube-dl.

youtube-dl tends to be updated very quickly when a website changes. This will of course not help you if you are using an older version. The first thing you should do if you can't download a video from a website, specially one which used to be supported, is to check if there is an updated version of youtube-dl available.

Download and Installation[edit | edit source]

You can download and install the latest version of youtube-dl from https://yt-dl.org/

It is possible to install it with these fine commands:

curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
chmod a+rx /usr/local/bin/youtube-dl

It may be handy to put those commands in a script named something like /usr/local/bin/update-youtube-dl.sh - but do keep in mind that there is no md5sum or GPG key check or anything like that involved in those commands; someone could compromise that site and give you the virus instead of youtube-dl.