youtube-dl detailed usage tutorial

In this article, I will introduce you a very popular and practical Youtube audio/video downloading special program called youtube-dl. youtube-dl is a free, open source, command-line program written in Python that can download a single video, multiple videos or entire playlists at once, and supports GNU/Linux, macOS and Microsoft Windows at the same time as the three major operating system platforms

Install youtube-dl tool

The official recommended Youtube-dl installation method is very simple, just save it to the PATH path according to the operating system platform you are using, you can execute it and start using it immediately.

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl

If you don’t have curl in your system, you can use wget instead:

sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl

After the download is complete, you need to specify its execution permissions:

sudo chmod a+rx /usr/local/bin/youtube-dl

Alternatively, you can also use pip to install it:

sudo -H pip install --upgrade youtube-dl

youtube-dl detailed tutorials and examples for beginners

download video or playlist

To download videos or entire video playlists from Youtube, just use the URL directly:

youtube-dl https://www.youtube.com/watch\?v\=F8JbUcGu5xo

Then it will download the video to your current path.

➜  Movies youtube-dl https://www.youtube.com/watch\?v\=F8JbUcGu5xo
[youtube] F8JbUcGu5xo: Downloading webpage
[download] Destination: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f298.mp4
[download] 100% of 164.39MiB in 00:48
[download] Destination: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f140.m4a
[download] 100% of 6.99MiB in 00:02
[ffmpeg] Merging formats into "3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.mp4"
Deleting original file 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f298.mp4 (pass -k to keep)
Deleting original file 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f140.m4a (pass -k to keep)
➜  Movies l -t
total 351480
drwxr-xr-x+ 82 harry  staff   2.6K Jul 14 11:25 ..
drwx------+  6 harry  staff   192B Jul 14 11:25 .
-rw-r--r--   1 harry  staff   172M Jul 14 10:36 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.mp4

If you want to specify the name of the video after downloading, you can use the following method:

youtube-dl -o 'Shohei Ohtani starts All-Star Game.mp4' https://www.youtube.com/watch\?v\=F8JbUcGu5xo

Of course, you can also attach more detailed information when downloading the video. The available parameters are: title, uploader name (channel name), and video upload date, etc.:

youtube-dl -o '%(title)s by %(uploader)s on %(upload_date)s in %(playlist)s.%(ext)s' https://www.youtube.com/watch?v=F8JbUcGu5xo

Download multiple videos

Sometimes, we need to download multiple different videos from Youtube at once, at this time we only need to separate multiple URLs with spaces:

youtube-dl <url> <url2> <url3>

Or, you can put all the URLs of the videos you want to download in a text file and pass them as a parameter to youtube-dl:

youtube-dl -a url.txt

The above command will download all the videos pointed to by the URL in the url.txt file.

Download only audio (in the video)

youtube-dl allows us to download its audio only from Youtube videos, for example:

youtube-dl -x https://www.youtube.com/watch?v=F8JbUcGu5xo

Then it will download the audio to your current path.

➜  Movies youtube-dl -x https://www.youtube.com/watch\?v\=F8JbUcGu5xo
[youtube] F8JbUcGu5xo: Downloading webpage
[download] Destination: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.m4a
[download] 100% of 6.99MiB in 00:01
[ffmpeg] Correcting container in "3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.m4a"
[ffmpeg] Post-process file 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.m4a exists, skipping
➜  Movies l -t
total 365792
drwxr-xr-x+ 82 harry  staff   2.6K Jul 14 11:34 ..
drwx------+  7 harry  staff   224B Jul 14 11:34 .
-rw-r--r--   1 harry  staff   7.0M Jul 14 10:36 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.m4a

By default, Youtube-dl will save audio in Ogg (opus) format. If you want to download audio in any other format, such as mp3, please run:

youtube-dl -x --audio-format mp3 https://www.youtube.com/watch\?v\=F8JbUcGu5xo

This command will download audio from the given video/playlist, convert it to MP3 and save it in the current directory.

Download videos with description, metadata, annotations, subtitles and thumbnails

To download the video and other detailed information, such as description, metadata, comments, subtitles, and thumbnails, use the following command:

youtube-dl --write-description --write-info-json --write-annotations --write-sub --write-thumbnail https://www.youtube.com/watch?v=F8JbUcGu5xo
➜  Movies youtube-dl --write-description --write-info-json --write-annotations --write-sub --write-thumbnail https://www.youtube.com/watch\?v\=F8JbUcGu5xo
[youtube] F8JbUcGu5xo: Downloading webpage
[info] Writing video description to: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.description
WARNING: There are no annotations to write.
[info] Writing video description metadata as JSON to: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.info.json
[youtube] F8JbUcGu5xo: Downloading thumbnail ...
[youtube] F8JbUcGu5xo: Writing thumbnail to: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.jpg
[download] Destination: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f298.mp4
[download] 100% of 164.39MiB in 00:48
[download] Destination: 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f140.m4a
[download] 100% of 6.99MiB in 00:02
[ffmpeg] Merging formats into "3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.mp4"
Deleting original file 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f298.mp4 (pass -k to keep)
Deleting original file 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.f140.m4a (pass -k to keep)
➜  Movies l -t
total 351848
drwxr-xr-x+ 82 harry  staff   2.6K Jul 14 11:39 ..
drwx------+  9 harry  staff   288B Jul 14 11:39 .
-rw-r--r--   1 harry  staff   146K Jul 14 11:38 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.jpg
-rw-r--r--   1 harry  staff    32K Jul 14 11:38 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.info.json
-rw-r--r--   1 harry  staff   428B Jul 14 11:38 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.description
-rw-r--r--   1 harry  staff   172M Jul 14 10:36 3 UP, 3 DOWN! Shohei Ohtani starts All-Star Game with 1-2-3 inning! (Gets Tatis Jr., Muncy, Arenado)-F8JbUcGu5xo.mp4

List all available audio/video formats

The videos and audios on the Youtube website will be automatically transcoded into multiple audio/video formats. To view all downloadable audio/video formats for a certain video or playlist, please use the following command:

youtube-dl --list-formats https://www.youtube.com/watch?v=F8JbUcGu5xo
# Commonly used abbreviations
youtube-dl -F https://www.youtube.com/watch?v=F8JbUcGu5xo
➜  Movies youtube-dl -F https://www.youtube.com/watch\?v\=F8JbUcGu5xo
[youtube] F8JbUcGu5xo: Downloading webpage
[info] Available formats for F8JbUcGu5xo:
format code  extension  resolution note
249          webm       audio only tiny   43k , webm_dash container, opus @ 43k (48000Hz), 2.37MiB
250          webm       audio only tiny   50k , webm_dash container, opus @ 50k (48000Hz), 2.75MiB
251          webm       audio only tiny   92k , webm_dash container, opus @ 92k (48000Hz), 5.02MiB
140          m4a        audio only tiny  129k , m4a_dash container, mp4a.40.2@129k (44100Hz), 6.99MiB
278          webm       256x144    144p   83k , webm_dash container, vp9@  83k, 30fps, video only, 4.51MiB
160          mp4        256x144    144p   83k , mp4_dash container, avc1.4d400c@  83k, 30fps, video only, 4.53MiB
242          webm       426x240    240p  177k , webm_dash container, vp9@ 177k, 30fps, video only, 9.59MiB
133          mp4        426x240    240p  181k , mp4_dash container, avc1.4d4015@ 181k, 30fps, video only, 9.77MiB
134          mp4        640x360    360p  355k , mp4_dash container, avc1.4d401e@ 355k, 30fps, video only, 19.20MiB
243          webm       640x360    360p  377k , webm_dash container, vp9@ 377k, 30fps, video only, 20.36MiB
135          mp4        854x480    480p  567k , mp4_dash container, avc1.4d401f@ 567k, 30fps, video only, 30.63MiB
244          webm       854x480    480p  680k , webm_dash container, vp9@ 680k, 30fps, video only, 36.75MiB
247          webm       1280x720   720p 1344k , webm_dash container, vp9@1344k, 30fps, video only, 72.60MiB
136          mp4        1280x720   720p 1853k , mp4_dash container, avc1.4d401f@1853k, 30fps, video only, 100.10MiB
302          webm       1280x720   720p60 2268k , webm_dash container, vp9@2268k, 60fps, video only, 122.47MiB
298          mp4        1280x720   720p60 3044k , mp4_dash container, avc1.4d4020@3044k, 60fps, video only, 164.39MiB
18           mp4        640x360    360p  682k , avc1.42001E, 30fps, mp4a.40.2 (44100Hz), 36.86MiB
22           mp4        1280x720   720p 1986k , avc1.64001F, 30fps, mp4a.40.2 (44100Hz) (best)

As shown in the figure above, Youtube-dl lists all available formats for a given video, from left to right: format code (video format code), extension (extension), resolution (resolution) and note (note) . When you want to download a video in a specific quality or format, it is very convenient to first check what is available.

Download videos in a certain quality and/or format

By default, youtube-dl will independently select the best quality video download. However, it is also possible to download videos or playlists in a specific quality or format.

youtube-dl supports the following qualities:

  • best Choose the best quality audio/video file
  • worst Choose the worst quality format (video and audio)
  • bestvideo Choose the best quality only video format (such as DASH video), which may not be available.
  • worstvideo Choose the purest video format with the worst quality, which may not be available
  • bestaudio Choose the best quality audio format, which may not be available
  • worstaudio Choose the audio format with the worst quality, which may not be available

For example, if you want to automatically select and download the best quality format (audio and video), just use the following command:

youtube-dl -f best https://www.youtube.com/watch?v=F8JbUcGu5xo

Similarly, to download only audio with the best quality, you can execute:

youtube-dl -f bestaudio https://www.youtube.com/watch?v=F8JbUcGu5xo

You can also combine the following different format options:

youtube-dl -f bestvideo+bestaudio https://www.youtube.com/watch?v=F8JbUcGu5xo

The above command will download the highest quality video only and highest quality pure audio format respectively, and then use ffmpeg or avconv to merge them into a mkv file of the best quality; if you don’t want to merge, please replace + (plus sign) with, (comma ) To get the highest quality audio and video (two files) respectively, as shown below:

youtube-dl -f 'bestvideo,bestaudio' https://www.youtube.com/watch?v=F8JbUcGu5xo

What needs special explanation here is that if you need to download video and audio in combination, you need to install ffmpeg or avconv on this machine, because youtube-dl itself does not have the ability to handle encoding and decoding, and you need to use third-party tools to achieve

Download files via video code (common method)

As mentioned earlier, all Youtube videos have a format code, and we can use it to download videos of a specific quality.

First use to view all available audio/video formats and their corresponding format code (video format code):

➜  Movies youtube-dl -F https://www.youtube.com/watch\?v\=F8JbUcGu5xo 
[youtube] F8JbUcGu5xo: Downloading webpage
[info] Available formats for F8JbUcGu5xo:
format code  extension  resolution note
249          webm       audio only tiny   43k , webm_dash container, opus @ 43k (48000Hz), 2.37MiB
250          webm       audio only tiny   50k , webm_dash container, opus @ 50k (48000Hz), 2.75MiB
251          webm       audio only tiny   92k , webm_dash container, opus @ 92k (48000Hz), 5.02MiB
140          m4a        audio only tiny  129k , m4a_dash container, mp4a.40.2@129k (44100Hz), 6.99MiB
278          webm       256x144    144p   83k , webm_dash container, vp9@  83k, 30fps, video only, 4.51MiB
160          mp4        256x144    144p   83k , mp4_dash container, avc1.4d400c@  83k, 30fps, video only, 4.53MiB
242          webm       426x240    240p  177k , webm_dash container, vp9@ 177k, 30fps, video only, 9.59MiB
133          mp4        426x240    240p  181k , mp4_dash container, avc1.4d4015@ 181k, 30fps, video only, 9.77MiB
134          mp4        640x360    360p  355k , mp4_dash container, avc1.4d401e@ 355k, 30fps, video only, 19.20MiB
243          webm       640x360    360p  377k , webm_dash container, vp9@ 377k, 30fps, video only, 20.36MiB
135          mp4        854x480    480p  567k , mp4_dash container, avc1.4d401f@ 567k, 30fps, video only, 30.63MiB
244          webm       854x480    480p  680k , webm_dash container, vp9@ 680k, 30fps, video only, 36.75MiB
247          webm       1280x720   720p 1344k , webm_dash container, vp9@1344k, 30fps, video only, 72.60MiB
136          mp4        1280x720   720p 1853k , mp4_dash container, avc1.4d401f@1853k, 30fps, video only, 100.10MiB
302          webm       1280x720   720p60 2268k , webm_dash container, vp9@2268k, 60fps, video only, 122.47MiB
298          mp4        1280x720   720p60 3044k , mp4_dash container, avc1.4d4020@3044k, 60fps, video only, 164.39MiB
18           mp4        640x360    360p  682k , avc1.42001E, 30fps, mp4a.40.2 (44100Hz), 36.86MiB
22           mp4        1280x720   720p 1986k , avc1.64001F, 30fps, mp4a.40.2 (44100Hz) (best)

Then use the code to download the specified audio/video format. For example, to download the best quality (format code 22) video file, execute the following command:

youtube-dl -f 22 https://www.youtube.com/watch?v=F8JbUcGu5xo

When downloading videos from a playlist, some videos may not have the same format. In this case, multiple format codes can be specified in the preferred order, for example:

youtube-dl -f 22/17/18 <playlist_url>

According to the example above, Youtube-dl will download the video in format 22 (if available); if format 22 is not available, it will download format 17 (if available); if format 22 and 17 are not available, finally try to download format 18 . If all the format codes do not match, Youtube-dl will report a prompt. It should also be noted that the slash is left-associative, that is, the leftmost format code is preferred.

Download audio/video via file extension

To download the video in your preferred format, such as MP4, simply execute:

youtube-dl --format mp4 https://www.youtube.com/watch?v=F8JbUcGu5xo
# or 
youtube-dl -f mp4 https://www.youtube.com/watch?v=F8JbUcGu5xo

As mentioned earlier, some videos may not be available in your preferred format. In this case, Youtube-dl will download the other best available formats. For example, this command will download the best quality MP4 format file. If the MP4 format is not available, it will download the other best available format

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' https://www.youtube.com/watch?v=F8JbUcGu5xo

Limit the size of downloaded videos

When downloading multiple videos from a Youtube playlist, you may only want to download videos of a certain size. For example, this command will not download any video smaller than the specified size, such as 100MB:

youtube-dl --min-filesize 100M <playlist_url>

If you don’t want to download a video larger than a given size, you can do this:

youtube-dl --max-filesize 100M <playlist_url>

We can also use combined formats and select operators to download videos of a specific size. For example, the following command will download videos in the best video format but not larger than 100MB:

youtube-dl -f 'best[filesize<100M]' https://www.youtube.com/watch?v=F8JbUcGu5xo

Download videos by date

youtube-dl allows us to filter and download videos or playlists based on upload date. For example, to download videos uploaded on February 1, 2021, you can use:

youtube-dl --date 20210201 <URL>

Download videos uploaded on or before a specific date:

youtube-dl --datebefore 20210201 <URL>

Download videos uploaded on or after a specific date:

youtube-dl --dateafter 20210201 <URL>

Download only videos uploaded in the past 6 months:

youtube-dl --dateafter now-6months <URL>

Download videos uploaded during a specific time period (e.g. February 1, 2020 to February 1, 2021):

youtube-dl --dateafter 20200201 --datebefore 20210201 <URL>

Download a specific video from the playlist

Downloading specific videos from playlists is another very useful feature of Youtube-dl. For example, to download the 10th file from the playlist, use:

youtube-dl --playlist-items 10 <playlist_url>

Similarly, to download multiple specified files, just separate them with commas:

youtube-dl --playlist-items 2,3,7,10 <playlist_url>

Of course, you can also specify the download range by sequence number, for example, starting from the 10th, download the complete list directly:

youtube-dl --playlist-start 10 <playlist_url>

Or download only the files from 2 to 5 in the playlist:

youtube-dl --playlist-start 2 --playlist-end 5 <playlist_url>

Download only videos that are suitable for a certain age

Another feature of Youtube-dl is that it allows us to download only videos that are suitable for a specified age. For example, to download all “Let’s Play” videos from a playlist that are not marked as NSFW or whose age limit is 7 years old, you can use:

youtube-dl --match-title "let's play" --age-limit 7 --reject-title "nsfw" <playlist_url>

Using help

Through the introduction of the above examples, I believe that it has been able to meet the needs of most users for Youtube video download and youtube-dl use. For more detailed information, please refer to the youtube-dl help:

youtube-dl --help

And you can watch the video below to learn how to use youtube-dl