How to select video quality from youtube-dl?

I have installed youtube-dl in my 14.04.

I can download video by following command,

$ youtube-dl [youtube-link]

But I want to know how to select available pixel quality of youtube video(i.e 1080p, 720p, 480p, etc).

In software description they said it's possible(shown in image below), but how to do..

ss

3

10 Answers

To download a video, you type the URL after the command like so:

youtube-dl '

To select the video quality, first use the -F option to list the available formats, here’s an example,

youtube-dl -F '

Here’s the output:

[youtube] Setting language
[youtube] P9pzm5b6FFY: Downloading webpage
[youtube] P9pzm5b6FFY: Downloading video info webpage
[youtube] P9pzm5b6FFY: Extracting video information
[info] Available formats for P9pzm5b6FFY:
format code extension resolution note
140 m4a audio only DASH audio , audio@128k (worst)
160 mp4 144p DASH video , video only
133 mp4 240p DASH video , video only
134 mp4 360p DASH video , video only
135 mp4 480p DASH video , video only
136 mp4 720p DASH video , video only
17 3gp 176x144
36 3gp 320x240
5 flv 400x240
43 webm 640x360
18 mp4 640x360
22 mp4 1280x720 (best)

The best quality is 22 so use -f 22 instead of -F to download the MP4 video with 1280x720 resolution like this:

youtube-dl -f 22 '

Or optionally use the following flags to automatically download the best audio and video tracks that are available as a single file:

youtube-dl -f best '

If you encounter any error during the muxing process or an issue with the video quality selection, you can use one of the following commands:

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' --merge-output-format mp4 '

or as Gabriel Staples pointed out here, the following command will typically select the actual best single file video quality resolution instead of video quality bit-rate:

youtube-dl -f best '

These commands will ensure you download the highest quality mp4 video and m4a audio from the video as a single file or will merge them back into a single mp4 (using ffmpeg in my case). If ffmpeg or avconv is not available, youtube-dl should fall back to the single file -f best option instead of the default.

Click here for more detailed information and some different examples.

Also, click to see this related answer by Gabriel Staples.


Source:

Source:

22

You can download 1080p using youtube-dl, but you need to do a little extra work. Usually it will only download 720p as its max even if you can see 1080p on youtube.com.

Run with -F to see available formats:

youtube-dl -F
171 webm audio only DASH audio 115k , audio@128k (44100Hz), 2.59MiB (worst)
140 m4a audio only DASH audio 129k , audio@128k (44100Hz), 3.02MiB
141 m4a audio only DASH audio 255k , audio@256k (44100Hz), 5.99MiB
160 mp4 256x144 DASH video 111k , 12fps, video only, 2.56MiB
247 webm 1280x720 DASH video 1807k , 1fps, video only, 23.48MiB
136 mp4 1280x720 DASH video 2236k , 24fps, video only, 27.73MiB
248 webm 1920x1080 DASH video 3993k , 1fps, video only, 42.04MiB
137 mp4 1920x1080 DASH video 4141k , 24fps, video only, 60.28MiB
43 webm 640x360
18 mp4 640x360
22 mp4 1280x720 (best)

notice that youtube-dl has labeled the last option 1280x720 as the 'best' quality and that's what it will download by default, but that the line starting with 137 is actually higher quality 1920x1080. Youtube has separated the video and audio streams for the lines labeled DASH so we also need to pick the highest quality audio which in this case is the line starting with 141. Then we run youtube-dl again this time specifying the audio and video:

youtube-dl -f 137+141 

and it will download the 1080p video and auto-merge it with the highest-quality audio. It should also auto-deleted the separate downloaded parts. This method is a little extra work, but will get you the best results.

6

To select specific resolutions, you can specify the size and audio quality so they get selected automatically - so for 480p:

-f 'bestvideo[height<=480]+bestaudio/best[height<=480]'

with bestvideo[height<=720]+bestaudio/best[height<=720] for 720p etc. This can added to config file at ~/.config/youtube-dl/config (or even /etc/youtube-dl.conf) so you don't get oversized downloads:

mkdir ~/.config/youtube-dl
echo "-f 'bestvideo[height<=720]+bestaudio/best[height<=720]'" >> ~/.config/youtube-dl/config

You can use --ignore-config if you want to disable the configuration file for a particular youtube-dl run.

Please note that fairly often it will have to download a separate video and audio steam and merge them.

For more examples see youtube-dl's doucmentation.

2

Some of the other options to download the best quality videos other than that mentioned here depending on your convenience is given below:

Download best mp4 format available or any other best if no mp4 available

$ youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'

Download best format available but not better that 480p

$ youtube-dl -f 'bestvideo[height<=480]+bestaudio/best[height<=480]'

Download best video only format but no bigger than 50 MB

$ youtube-dl -f 'best[filesize<50M]'

Download best format available via direct link over HTTP/HTTPS protocol

$ youtube-dl -f '(bestvideo+bestaudio/best)[protocol^=http]'

Reference:

Directly from youtube-dl github page

3

How to get the best video quality available.

This answer here, with the most votes, should be right to give you the best video quality available, but....it's not. bestvideo+bestaudio seems to choose only the best video and best audio of the options that are video only and audio only, then it merges the two together. Note that this also appears to be identical to the default behavior of youtube-dl. However, on the video I was looking at, the best quality was a single, pre-merged file that was already in a format with combined video and audio. The bestvideo+bestaudio option did NOT choose this best quality 720p file because it was looking only for separate video and audio files. Details below.

Solution? Have it grab the best single file which contains both video and audio in one instead, with this:

youtube-dl -f best 

In short: use the -f best option, NOT the -f bestvideo+bestaudio option!

Proof:

(Note: all circuit schematic images below are actually screenshots from this electrical-engineering-related video tutorial: Video One- Getting started with LTspice).

enter image description here

More specifically, see below for the results of running

youtube-dl -F 

in order to see what video 'F'ormats are availabe for download:

gabriel ~ $ youtube-dl -F
[youtube] FWGC9SqA3J0: Downloading webpage
[youtube] FWGC9SqA3J0: Downloading video info webpage
[youtube] FWGC9SqA3J0: Downloading MPD manifest
[youtube] FWGC9SqA3J0: Downloading MPD manifest
[info] Available formats for FWGC9SqA3J0:
format code extension resolution note
139 m4a audio only DASH audio 50k , m4a_dash container, mp4a.40.5@ 48k (22050Hz), 2.30MiB
249 webm audio only DASH audio 51k , opus @ 50k, 2.34MiB
250 webm audio only DASH audio 62k , opus @ 70k, 2.85MiB
171 webm audio only DASH audio 103k , vorbis@128k, 4.68MiB
251 webm audio only DASH audio 109k , opus @160k, 5.10MiB
140 m4a audio only DASH audio 130k , m4a_dash container, mp4a.40.2@128k (44100Hz), 6.13MiB
160 mp4 256x138 DASH video 108k , mp4_dash container, avc1.4d400b, 24fps, video only
134 mp4 640x348 DASH video 142k , mp4_dash container, avc1.4d401e, 24fps, video only, 3.42MiB
133 mp4 426x232 DASH video 242k , mp4_dash container, avc1.4d400c, 24fps, video only
136 mp4 1280x694 DASH video 473k , mp4_dash container, avc1.4d401f, 24fps, video only, 8.01MiB
135 mp4 854x464 DASH video 1155k , mp4_dash container, avc1.4d4014, 24fps, video only
17 3gp 176x144 small , mp4v.20.3, mp4a.40.2@ 24k, 1.63MiB
36 3gp 320x174 small , mp4v.20.3, mp4a.40.2, 2.98MiB
43 webm 640x360 medium , vp8.0, vorbis@128k, 7.44MiB
18 mp4 640x348 medium , avc1.42001E, mp4a.40.2@ 96k, 8.54MiB
22 mp4 1280x694 hd720 , avc1.64001F, mp4a.40.2@192k (best) 

Notice that row 22 says "(best)" to the far right of it. This is the only option which offers hd720 quality, which is the best quality I can get when watching this video in a web browser on YouTube. It is the clearest and has the best definition. When I use either of the commands recommended by the top answer:

youtube-dl -f bestvideo+bestaudio 

OR:

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' --merge-output-format mp4 

I end up with a video that is both lower quality/lower resolution, and has a larger file size. I don't understand it exactly, but the -f best option is definitely the only one that truly gives me the best resolution.

Here's some screenshots from a tutorial video I was watching showing electronic circuit diagrams in the video. Notice how the latter one is much higher quality and more legible (click on each image and compare the tiny font in a zoomed-in view):

  1. Using the lower quality -f bestvideo+bestaudio option accepted here as the right answer (OR just using the default option: youtube-dl ):
  • enter image description here
  • Notice how fuzzy the icons are at the top, and how difficult to read are the open window and tiny words within it!
  • This is significantly worse than the quality I get when watching online at YouTube directly in the browser.
  • This option also takes up more memory for some reason: the video is 18.0 MB, and it took longer for my system to download and re-combine (audio + video) than the -f best option below, which only had to do one download and no recombining since it was already one file.
  1. Using the higher quality -f best option which I recommend:
  • enter image description here
  • Notice how much clearer the small icons at the top and small font in the window are!
  • This is the identical resolution to what I get when watching in the highest resolution possible directly in the browser on YouTube.
  • This option also takes up less memory for some reason: the video is 14.8 MB.

Additional Reading:

See man youtube-dl for details.

8

A bash wrapper script that prompts for the format, which includes all available renditions, including audio-only, video-only.

Accepts both absolute youtube URLs and video IDs.

#!/ usr/bin/env bash
# Download youtube video with desired quality
# youtube-dl accepts both fully qualified URLs and video id's such as AQcQgfvfF1M
url="$*"
echo "Fetching available formats for $url..."
youtube-dl -F "$url"
read -p "Please enter the desired format id: " FORMAT
## download the video
youtube-dl -f $FORMAT -g "$url"
## stream the video with mpv (no local file created)
# mpv --cache=1024 $(youtube-dl -f $FORMAT -g "$url")

Sample output:

 yt.sh [youtube] Dax_tnZRExc: Downloading webpage [youtube] Dax_tnZRExc: Downloading video info webpage [youtube] Dax_tnZRExc: Extracting video information [youtube] Dax_tnZRExc: Downloading MPD manifest [info] Available formats for Dax_tnZRExc: format code extension resolution note 139 m4a audio only DASH audio 49k , m4a_dash container, mp4a.40.5@ 48k (22050Hz), 308.75KiB 249 webm audio only DASH audio 50k , opus @ 50k, 271.91KiB 250 webm audio only DASH audio 70k , opus @ 70k, 366.63KiB 171 webm audio only DASH audio 118k , vorbis@128k, 652.50KiB 140 m4a audio only DASH audio 127k , m4a_dash container, mp4a.40.2@128k (44100Hz), 820.00KiB 251 webm audio only DASH audio 130k , opus @160k, 705.84KiB 160 mp4 256x144 DASH video 109k , avc1.4d400c, 13fps, video only, 703.64KiB 278 webm 256x144 144p 111k , webm container, vp9, 25fps, video only, 439.72KiB 242 webm 426x240 240p 243k , vp9, 25fps, video only, 623.95KiB 133 mp4 426x240 DASH video 252k , avc1.4d4015, 25fps, video only, 1.54MiB 134 mp4 640x360 DASH video 388k , avc1.4d401e, 25fps, video only, 1.24MiB 243 webm 640x360 360p 458k , vp9, 25fps, video only, 1.19MiB 135 mp4 854x480 DASH video 761k , avc1.4d401e, 25fps, video only, 2.40MiB 244 webm 854x480 480p 893k , vp9, 25fps, video only, 2.00MiB 136 mp4 1280x720 DASH video 1382k , avc1.4d401f, 25fps, video only, 4.56MiB 247 webm 1280x720 720p 1754k , vp9, 25fps, video only, 3.94MiB 137 mp4 1920x1080 DASH video 2350k , avc1.640028, 25fps, video only, 8.48MiB 248 webm 1920x1080 1080p 2792k , vp9, 25fps, video only, 8.09MiB 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) Please enter the desired format #
1

Here is an explanation of streams/formats for anyone new to youtube-dl.

Some video sites, such as youtube, offer not only different resolutions of video to download, but have options in youtube-dl called formats to download video and audio separately. For the case of youtube, it will only offer the highest quality video and highest quality audio separately. Here's an example output of using -F flag to show all formats available:

$ youtube-dl -F
[youtube] iYWzMvlj2RQ: Downloading webpage
[youtube] iYWzMvlj2RQ: Downloading embed webpage
[youtube] iYWzMvlj2RQ: Refetching age-gated info webpage
[info] Available formats for iYWzMvlj2RQ:
format code extension resolution note
249 webm audio only tiny 50k , opus @ 50k (48000Hz), 238.14KiB
250 webm audio only tiny 59k , opus @ 70k (48000Hz), 284.22KiB
251 webm audio only tiny 118k , opus @160k (48000Hz), 553.74KiB
140 m4a audio only tiny 157k , m4a_dash container, mp4a.40.2@128k (44100Hz), 628.52KiB
394 mp4 192x144 144p 51k , av01.0.00M.08, 30fps, video only, 241.57KiB
278 webm 192x144 144p 72k , webm container, vp9, 30fps, video only, 309.32KiB
395 mp4 320x240 240p 97k , av01.0.00M.08, 30fps, video only, 283.55KiB
160 mp4 192x144 144p 111k , avc1.4d400c, 15fps, video only, 524.55KiB
242 webm 320x240 240p 135k , vp9, 30fps, video only, 328.15KiB
396 mp4 480x360 360p 184k , av01.0.01M.08, 30fps, video only, 472.21KiB
243 webm 480x360 360p 221k , vp9, 30fps, video only, 560.40KiB
134 mp4 480x360 360p 240k , avc1.4d401e, 30fps, video only, 826.64KiB
133 mp4 320x240 240p 247k , avc1.4d400d, 30fps, video only, 1.16MiB
397 mp4 640x480 480p 302k , av01.0.04M.08, 30fps, video only, 801.75KiB
244 webm 640x480 480p 338k , vp9, 30fps, video only, 912.64KiB
135 mp4 640x480 480p 517k , avc1.4d401e, 30fps, video only, 1.71MiB
398 mp4 960x720 720p 541k , av01.0.05M.08, 30fps, video only, 1.48MiB
247 webm 960x720 720p 604k , vp9, 30fps, video only, 1.72MiB
399 mp4 1440x1080 1080p 893k , av01.0.08M.08, 30fps, video only, 2.55MiB
248 webm 1440x1080 1080p 981k , vp9, 30fps, video only, 2.99MiB
136 mp4 960x720 720p 1046k , avc1.4d401f, 30fps, video only, 3.47MiB
137 mp4 1440x1080 1080p 1923k , avc1.640028, 30fps, video only, 7.30MiB
18 mp4 480x360 360p 316k , avc1.42001E, 30fps, mp4a.40.2@ 96k (44100Hz), 1.50MiB
22 mp4 960x720 720p 858k , avc1.64001F, 30fps, mp4a.40.2@192k (44100Hz) (best)

So there's a lot of different formats, but some are labelled "audio only" or "video only". If I selected to download one of those formats by using the specified format code such as with -f 137, I would really get either an audio file or a video file with no audio, which is usually not what you want. If I had ffmpeg installed and specified both video and audio formats with -f 137+140, then youtube-dl would download video and audio and afterwards combine them together into one video file.

If you don't have ffmpeg installed, youtube-dl will by default select the highest quality format that has both audio and video. This maxes out at 720p (and is usually specified by format code 22 so you would download with -f 22). If you do have ffmpeg installed, youtube-dl will be able download the real best quality video (1080p or better if available) and best quality audio, and after downloading mux (combine) them together into one video file so this is what I recommend.

Summary: install ffmpeg and then you will be able to simply do youtube-dl [youtube url] without any flags to get the best quality video and audio automatically in one output video file.

By default, youtube-dl will pick the best quality.

1

I am a disk saver, so I download videos of the format 1280 x 720,
because 4k videos takes more size of my hard disk.
So I add youtube-dl in ~/.bashrc file like given below

Setting a work -

open terminal and type

  • nano ~/.bashrc
    now go to very bottom of the file and add given below command
  • alias yt='youtube-dl -if best'
  • save file by pressing Ctrl o then press Enter
  • exit file by pressing Ctrl x
  • and last run command source ~/.bashrc in terminal

Experiment -

Open terminal and type
yt
It will start downloading video with the best quality.

Explanation -
Bash allows us to define aliases which act like shortcuts to bash commands
in our case we define alias as -
alias yt='youtube-dl -if best'
where yt is the shortcut command for 'youtube-dl' which gives relief
our fingers to type long command.
You can write anything at the place of yt.
I set a flag -i in youtube-dl -if best
which means

It will Continue on download errors, for example to skip unavailable videos in a playlist

youtube-dl's idea of best is not perfect. Take this example for instance :

pi@rpi-lounge:~ $ youtube-dl -F
[youtube] Xj3gU3jACe8: Downloading webpage
[info] Available formats for Xj3gU3jACe8:
format code extension resolution note
249 webm audio only tiny 55k , opus @ 50k (48000Hz), 1.11MiB
250 webm audio only tiny 74k , opus @ 70k (48000Hz), 1.47MiB
140 m4a audio only tiny 130k , m4a_dash container, mp4a.40.2@128k (44100Hz), 2.92MiB
251 webm audio only tiny 143k , opus @160k (48000Hz), 2.89MiB
394 mp4 256x144 144p 81k , av01.0.00M.08, 25fps, video only, 1.54MiB
278 webm 256x144 144p 99k , webm container, vp9, 25fps, video only, 2.14MiB
160 mp4 256x144 144p 110k , avc1.4d400c, 25fps, video only, 2.38MiB
395 mp4 426x240 240p 184k , av01.0.00M.08, 25fps, video only, 3.39MiB
242 webm 426x240 240p 230k , vp9, 25fps, video only, 4.90MiB
133 mp4 426x240 240p 245k , avc1.4d4015, 25fps, video only, 5.19MiB
396 mp4 640x360 360p 395k , av01.0.01M.08, 25fps, video only, 7.20MiB
243 webm 640x360 360p 418k , vp9, 25fps, video only, 9.03MiB
134 mp4 640x360 360p 633k , avc1.4d401e, 25fps, video only, 13.61MiB
397 mp4 854x480 480p 712k , av01.0.04M.08, 25fps, video only, 13.13MiB
244 webm 854x480 480p 774k , vp9, 25fps, video only, 16.75MiB
135 mp4 854x480 480p 1160k , avc1.4d401e, 25fps, video only, 25.45MiB
398 mp4 1280x720 720p 1456k , av01.0.05M.08, 25fps, video only, 26.20MiB
247 webm 1280x720 720p 1539k , vp9, 25fps, video only, 33.49MiB
136 mp4 1280x720 720p 2316k , avc1.4d401f, 25fps, video only, 50.46MiB
399 mp4 1920x1080 1080p 2492k , av01.0.08M.08, 25fps, video only, 45.96MiB
248 webm 1920x1080 1080p 2700k , vp9, 25fps, video only, 58.80MiB
137 mp4 1920x1080 1080p 4337k , avc1.640028, 25fps, video only, 91.16MiB
400 mp4 2560x1440 1440p 7468k , av01.0.12M.08, 25fps, video only, 143.60MiB
271 webm 2560x1440 1440p 8993k , vp9, 25fps, video only, 189.90MiB
401 mp4 3840x2160 2160p 14110k , av01.0.12M.08, 25fps, video only, 278.39MiB
313 webm 3840x2160 2160p 18006k , vp9, 25fps, video only, 387.17MiB
18 mp4 640x360 360p 734k , avc1.42001E, 25fps, mp4a.40.2@ 96k (44100Hz), 16.55MiB (best)

The following command:

youtube-dl -f 137+251 

produces far better results than this usage:

youtube-dl -f 18 (marked as best)

There seems to be further bugs as trying to specify the height filters to limit to 1080p doesn't work. It still downloads the 4k variant (which notably is not that marked as best either)

youtube-dl -f "bestvideo[height<=1080,ext=mp4]+bestaudio[ext=m4a]" 
pi@rpi-lounge:~ $ youtube-dl --version
2020.06.16.1
2

You Might Also Like