How to select video quality from youtube-dl?

906,250

Solution 1

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

youtube-dl 'http://www.youtube.com/watch?v=P9pzm5b6FFY'

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

youtube-dl -F 'http://www.youtube.com/watch?v=P9pzm5b6FFY'

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 'http://www.youtube.com/watch?v=P9pzm5b6FFY'

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 'http://www.youtube.com/watch?v=P9pzm5b6FFY'

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 'http://www.youtube.com/watch?v=P9pzm5b6FFY'

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 'http://www.youtube.com/watch?v=P9pzm5b6FFY'

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: www.webupd8.org/2014/02/video-downloader-youtube-dl-gets.html

Source: github.com/rg3/youtube-dl

Solution 2

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 https://www.youtube.com/watch\?v\=-pxRXP3w-sQ

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 https://www.youtube.com/watch\?v\=-pxRXP3w-sQ

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.

Solution 3

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.

Solution 4

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

Solution 5

First, ensure you have the latest version of youtube-dl installed.

How to install the latest version of youtube-dl

See:

  1. https://github.com/ytdl-org/youtube-dl/#installation
  2. https://github.com/ytdl-org/youtube-dl/#how-do-i-update-youtube-dl

Run these commands:

# On Linux Ubuntu:

# 1. Check your current version
youtube-dl --version

# 2.Uninstall your current version, if necessary
sudo apt remove -y youtube-dl

# 3. Now, install the latest version
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

# 4. Check your current version to ensure it updated
youtube-dl --version

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 always. 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 'https://youtu.be/FWGC9SqA3J0'

In short: to get the best quality, you may need to use the -f best option, NOT the -f bestvideo+bestaudio option! Try both to see. It could vary from video to video.

Quick reference of various examples of youtube-dl in general

I need a quick place to look for common commands. Here are some:

# View the help menu (then press `/` to do a regular expression search`)
youtube-dl --help | less

# List all available format you can download for this video with 
# `-F` or `--list-formats`
youtube-dl -F 'https://www.youtube.com/watch?v=-FzqOdRpCGw'
youtube-dl --list-formats 'https://www.youtube.com/watch?v=-FzqOdRpCGw'

# Download the best **single file** containing both audio and video already 
# merged into one file
youtube-dl -f best \
    'https://www.youtube.com/watch?v=-FzqOdRpCGw'

# Download the best **stand-alone video file** and the best **stand-alone audio
# file** and then **combine** them into one file
youtube-dl -f 'bestvideo+bestaudio' \
    'https://www.youtube.com/watch?v=-FzqOdRpCGw'

# Use `-o` or `--output` to specify the name of the output file. 
# - See the "OUTPUT TEMPLATE" section here for all of the metadata tags you can 
#   use to auto-generate parts of the filename: 
#   https://github.com/ytdl-org/youtube-dl/#output-template
youtube-dl -f 'bestvideo+bestaudio' \
    'https://www.youtube.com/watch?v=-FzqOdRpCGw' --merge-output-format mkv \
    -o 'best_merged.mkv'

# [includes auto-generated subtitles]
# `time` the whole download, saving **YouTube's autogenerated subtitles** into
# the video too, AND choosing a custom name for the output file, with the
# extension specified using the meta-data template format `$(ext)s`. See link
# below for more metadata format examples.
time youtube-dl -f best --write-auto-sub \
    'https://www.youtube.com/watch?v=2WoDQBhJCVQ' -o 'greatest_shot.%(ext)s'

See:

  1. https://github.com/ytdl-org/youtube-dl/#output-template
  2. More metadata format examples for the filename: https://github.com/ytdl-org/youtube-dl/#output-template-examples

Proof that -f best is better than -f bestvideo+bestaudio in at least some cases:

(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 'https://youtu.be/FWGC9SqA3J0'

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

gabriel ~ $ youtube-dl -F https://youtu.be/FWGC9SqA3J0
[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' 'https://youtu.be/FWGC9SqA3J0'

OR:

youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' --merge-output-format mp4 'https://youtu.be/FWGC9SqA3J0'

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 'https://youtu.be/FWGC9SqA3J0'):
  • 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:

  1. See man youtube-dl for details.
  2. My answer here: How can I update youtube-dl?. This also solves the problem:

    WARNING: unable to download video info webpage: HTTP Error 410: Gone

Share:
906,250

Related videos on Youtube

A J
Author by

A J

Computer programmer

Updated on September 18, 2022

Comments

  • A J
    A J over 1 year

    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

    • Jos
      Jos almost 10 years
      The description above is obsolete. From the man page: "youtube-dl now defaults to downloading the highest available quality as reported by YouTube, which will be 1080p or 720p in some cases."
    • thomasrutter
      thomasrutter over 9 years
      Note that YouTube has employed some sort of protection that prevents downloading tools from downloading (or even seeing) the 1080p version, but you can still download all other resolutions up to and including 720p.
    • Gabriel Staples
      Gabriel Staples over 5 years
      I know I'm a little late to the party, but here's my experience: askubuntu.com/a/1097056/327339. Use the -f best option.
  • mchid
    mchid over 9 years
    @prakharsingh95 the DASH audio only can be converted to standard wave or mp3 format using gnac or similar software.
  • xyz
    xyz over 9 years
    I meant DASH Video. I tried to download 4K, but that's all in DASH format (DASH allows youtube to preserve bandwidth). It's radically different it will mostly be unplayable.
  • mchid
    mchid over 9 years
    @prakharsingh95 Yeah if anything you might be able to view the mp4 using VLC as VLC allows you to sync the video with a separate file for audio. I haven't tried it though. Have you tried converting the mp4 file from the DASH download to avi format? I think they are similar.
  • xyz
    xyz over 9 years
    Yes. Dash is of course playable, but you need to mux the streams with something like avconv. Too slow for 4K.
  • whitesiroi
    whitesiroi about 9 years
    thank you very much, didn't know about 137+141
  • user643722
    user643722 almost 9 years
    Your example and others present easy choices for the audio - that is, for high quality, choose the one with highest bitrate value. I recently however found myself faced with a choice between DASH audio , opus @160k or DASH audio 126k , audio@128k (44100Hz), 1.79MiB or DASH audio 127k , m4a_dash container, aac @128k (44100Hz), 1.94MiB. I had to choose but am uncertain about quality, or perhaps compatibility.
  • AjayKumarBasuthkar
    AjayKumarBasuthkar over 8 years
    Yes, as per the readme You also have option(s) to select intended format, see here: github.com/rg3/youtube-dl/blob/master/…
  • AjayKumarBasuthkar
    AjayKumarBasuthkar over 8 years
    The option '-f bestaudio+bestvideo' Did not work with, 2012.02.27 version of youtube-dl, it threw "ERROR: requested format not available"
  • mchid
    mchid over 8 years
    @AjayKumarBasuthkar You have two options. The latest version is available through github (github.com/rg3/youtube-dl) and is also available through Python's official Python pip repository. OPTION ONE. You can download the zip package of youtube-dl from github. See here for instruction: github.com/rg3/youtube-dl.
  • mchid
    mchid over 8 years
    @AjayKumarBasuthkar To update the downloaded or cloned version in the future, just run youtube-dl -U OPTION TWO You can install youtube-dl using pip by running the following commands: sudo apt-get install python-pip then, run: sudo pip install youtube-dl Also, in the future, all you have to do to update the pip version is run the following command: sudo pip install --upgrade youtube-dl
  • Aleksey Kontsevich
    Aleksey Kontsevich about 8 years
    Also good to add --restrict-filenames for some videos with \", &, etc to download successfully.
  • Aleksey Kontsevich
    Aleksey Kontsevich about 8 years
    @mchid double quotation mark does not work if name already contain double quote - Youtube has many videos with such problem. I checked - only --restrict-filenames helps do download such videos without a problem.
  • Antony
    Antony almost 8 years
    The best thing about youtube-dl is that it is multi-platform. This solution doesn't just work on Linux, but also on Mac and Windows. Thank you!
  • ssnobody
    ssnobody over 7 years
    I had an issue where different segments of the same video had different format codes for the same resolution (e.g. hls-1476 for video 1 of 2 and hls-1665 for video 2 of 2 where both were 540p) so I couldn't just use "-f hls-1476" or "-f hls-1665" or I'd get "ERROR: requested format not available" Your command examples helped me retrieve what I wanted.
  • Marinos An
    Marinos An almost 7 years
    +1 for ~/.config/youtube-dl/config
  • GreenReaper
    GreenReaper about 6 years
    Nowadays this is the default, but only if you have avconv (libav) or ffmpeg installed to do the file conversion. For Windows, you want to set --ffmpeg-location in %APPDATA%\youtube-dl\config.txt to the directory where avconv/ffmpeg is installed. If you've got a low-end system you might want -f bestvideo[fps<=30]+bestaudio in there as well, to avoid 60fps video.
  • Gabriel Staples
    Gabriel Staples over 5 years
    This answer should be right, 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. However, on the video I was looking at, the best quality was a single, merged file. the bestvideo+bestaudio option did NOT choose this best quality 720p file because it was looking only for separate video and audio files. Solution? Have it grab the best single file which contains both video and audio in one instead, with this: youtube-dl -f best https://youtu.be/FWGC9SqA3J0.
  • Gabriel Staples
    Gabriel Staples over 5 years
    I decided to make this an answer: askubuntu.com/a/1097056/327339.
  • mchid
    mchid over 5 years
    @AlekseyKontsevich My bad. Double quotation marks will give you problems with special characters like a question mark for example. To avoid this in the terminal, you can use single quotation marks 'like this instead'. This is not only an issue with youtube-dl but this is because of bash so it is often good practice to use single-quotation marks instead of double ones. However, if the problem is the name of the file and not the URL then yes, you may have to use --restrict-filenames or you can use single-quote-marks when calling on the downloaded file name in the terminal
  • Gabriel Staples
    Gabriel Staples over 5 years
    @mchid, no, -f bestvideo+bestaudio appears to be the default, as youtube-dl https://youtu.be/FWGC9SqA3J0 and youtube-dl -f bestvideo+bestaudio https://youtu.be/FWGC9SqA3J0 give me identical results, and the documentation (youtube-dl -h) does specify that --audio-format best is the default audio, so one can infer that it grabs the best audio-less video stream as well by default, which produces lower quality results than using youtube-dl -f best https://youtu.be/FWGC9SqA3J0. Note that my version (youtube-dl --version) is "2018.11.23".
  • mchid
    mchid over 5 years
    @GabrielStaples You are correct, the default is bestvideo+bestaudio. I believe the issue with the video quality is because the video format is considered "best" based on the bit-rate and not the height. Other people have filed bug reports about this issue and I think the bug reports were closed. I guess that "best" is subjective. However, I do think many people would agree with you and say that the 1080p is better.
  • nmz787
    nmz787 over 5 years
    odd that I see listings for 1920x1080 options, but the 1280x720 (hd720) option still says "best"
  • localhost
    localhost almost 5 years
    All your screenshots are showing some electronics stuff.
  • Gabriel Staples
    Gabriel Staples almost 5 years
    @localhost, that's right. They are all screenshots from the video whos URL you see in all of my code snippets throughout the post: youtu.be/FWGC9SqA3J0.
  • localhost
    localhost almost 5 years
    @GabrielStaples Aah. Yes, that makes sense. Sorry about that.
  • mrgloom
    mrgloom almost 5 years
    How to download best quality but not greater than 1280x720?
  • mrgloom
    mrgloom almost 5 years
    How to combine ext=mp4 && height<=480 && filesize<50M ?
  • mchid
    mchid almost 5 years
    @mrgloom Please refer to the following answer: askubuntu.com/a/866432/167115 The -f option is provided in the answer, not the entire command so for this particular example shown here the entire command would be: youtube-dl -f 'bestvideo[height<=720]+bestaudio/best[height<=720]' 'http://www.youtube.com/watch?v=P9pzm5b6FFY'
  • mchid
    mchid almost 5 years
    @mrgloom Also, you can use the following command: youtube-dl -f 'best[height<=720]' 'http://www.youtube.com/watch?v=P9pzm5b6FFY'
  • bomben
    bomben over 4 years
    Is it not weird, that the highest number on the left side does not correspond to the highest amount of pixels in the screen in your code example of available formats?
  • ejabu
    ejabu over 4 years
    I use this youtube-dl -f 'best[ext=mp4]+best[height<=480]+best[filesize<100M]' youtube.com/watch?v=oInZYvMAjyw
  • Wlerin
    Wlerin about 4 years
    For most HD videos the top answer is still the way to go, because "best" alone tends to prefer either format code 22 (even when 1080p+ is available) or it decides it would be brilliant to merge mp4 video (often not even the highest resolution) with opus audio, requiring an mkv container.
  • Gabriel Staples
    Gabriel Staples about 4 years
    Yeah there doesn't seem to be one perfect answer. I think you have to try both answers (the main one, and mine), and see which is better manually sometimes. I don't fully understand it, I just know in some cases--particularly in the case I present in this answer--the -f best option was definitely better than the -f bestvideo+bestaudio option, and the difference was significant: the first one had legible text allowing me to follow the video tutorial, and the 2nd one did not: it was too blurry.
  • Ivan
    Ivan about 4 years
    this is really nice script and it works with twitch !
  • ACV
    ACV over 3 years
    WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged.
  • applemonkey496
    applemonkey496 over 3 years
    @ACV ffmpeg or avconv is required for any youtube-dl postprocessing. I would recommend ffmpeg.
  • iGadget
    iGadget over 3 years
    Actually, the height filter works fine, you just got the syntax wrong. It's not [height<=1080,ext=mp4], but [height<=1080][ext=mp4]. Works like a charm here.
  • qwr
    qwr about 3 years
    By default, if ffmpeg is installed, youtube-dl will choose the best video and best audio streams and mux them together.
  • qwr
    qwr about 3 years
    This is not the fault of youtube-dl but the best it can do with the formats provided if you do not have ffmpeg installed. See my answer.
  • mchid
    mchid about 3 years
    @qwr Yeah, I believe the default behavior has changed a few times. In any case, it should be noted that the actual best formats are not always downloaded or marked as "best" for some reason. Also, sometimes the best format is a single file and not a mux of the two. Using one of the various methods above should ensure the best format is downloaded when the default fails to deliver.
  • baptx
    baptx almost 3 years
    It looks like youtube-dl -f 'bestvideo[height<=1080]+bestaudio[ext=m4a]' is enough to get the best mp4 file limited to 1080p on YouTube. bestvideo[ext=mp4] was not needed at the beginning, neither /best[ext=mp4]/best at the end.
  • localhost
    localhost over 2 years
    @Wlerin At least the audio part is because opus is the best quality audio that youtube provides.