496

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

muru
  • 197,895
  • 55
  • 485
  • 740
A J
  • 11,367
  • 17
  • 44
  • 59
  • 3
    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." – Jos Jun 21 '14 at 10:08
  • 3
    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. – thomasrutter Oct 01 '14 at 03:19
  • 2
    I know I'm a little late to the party, but here's my experience: https://askubuntu.com/a/1097056/327339. Use the -f best option. – Gabriel Staples Nov 29 '18 at 17:50

10 Answers10

658

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

mchid
  • 43,546
  • 8
  • 97
  • 150
  • 9
    Make sure that you do not choose DASH. That will be unplayable! – xyz Sep 13 '14 at 04:21
  • @prakharsingh95 the DASH audio only can be converted to standard wave or mp3 format using gnac or similar software. – mchid Sep 17 '14 at 12:09
  • 3
    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. – xyz Sep 24 '14 at 19:41
  • 1
    @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. – mchid Sep 25 '14 at 05:05
  • 2
    Yes. Dash is of course playable, but you need to mux the streams with something like avconv. Too slow for 4K. – xyz Sep 29 '14 at 08:39
  • The option '-f bestaudio+bestvideo' Did not work with, 2012.02.27 version of youtube-dl, it threw "ERROR: requested format not available" – AjayKumarBasuthkar Jan 26 '16 at 17:22
  • 1
    @AjayKumarBasuthkar Wow, that is an ancient version of youtube-dl. I'm surprised it actually even works! I usually recommend sticking with the Ubunttu repositories for software but for youtube-dl it is almost essential to have the latest and most up to date version. (see next comment) – mchid Jan 26 '16 at 23:48
  • @AjayKumarBasuthkar You have two options. The latest version is available through github (https://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: https://github.com/rg3/youtube-dl. – mchid Jan 26 '16 at 23:53
  • 1
    @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 – mchid Jan 27 '16 at 00:03
  • @AjayKumarBasuthkar Actually, there is a THIRD OPTION, here it is. First, run: sudo wget https://yt-dl.org/downloads/2016.01.23/youtube-dl -O /usr/local/bin/youtube-dl Then, run: sudo chmod a+rx /usr/local/bin/youtube-dl See here for more info: http://rg3.github.io/youtube-dl/download.html Again, to update this version, run the following command: sudo youtube-dl -U – mchid Jan 27 '16 at 00:11
  • Also good to add --restrict-filenames for some videos with ", &, etc to download successfully. – Aleksey Kontsevich Apr 20 '16 at 15:51
  • @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. – Aleksey Kontsevich Apr 22 '16 at 01:33
  • 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 Nov 29 '18 at 09:15
  • I decided to make this an answer: https://askubuntu.com/a/1097056/327339. – Gabriel Staples Nov 29 '18 at 09:18
  • @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 – mchid Dec 03 '18 at 22:43
  • @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". – Gabriel Staples Dec 04 '18 at 02:45
  • @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. – mchid Dec 04 '18 at 23:28
  • How to download best quality but not greater than 1280x720? – mrgloom Jun 21 '19 at 11:58
  • @mrgloom Please refer to the following answer: https://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 Jun 23 '19 at 01:31
  • 1
    @mrgloom Also, you can use the following command: youtube-dl -f 'best[height<=720]' 'http://www.youtube.com/watch?v=P9pzm5b6FFY' – mchid Jun 23 '19 at 03:30
  • 1
    By default, if ffmpeg is installed, youtube-dl will choose the best video and best audio streams and mux them together. – qwr Feb 20 '21 at 11:29
  • @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. – mchid Feb 21 '21 at 08:04
216

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.

mchid
  • 43,546
  • 8
  • 97
  • 150
  • 34
    thank you very much, didn't know about 137+141 – whitesiroi May 05 '15 at 14:48
  • 3
    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. – user643722 Jun 10 '15 at 12:13
  • 2
    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! – Antony Jun 02 '16 at 23:19
  • 9
    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. – GreenReaper Apr 06 '18 at 22:16
  • WARNING: You have requested multiple formats but ffmpeg or avconv are not installed. The formats won't be merged. – ACV Dec 10 '20 at 14:14
  • @ACV ffmpeg or avconv is required for any youtube-dl postprocessing. I would recommend ffmpeg. – applemonkey496 Jan 14 '21 at 01:17
90

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.

Wilf
  • 30,194
  • 17
  • 108
  • 164
  • 2
    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. – ThatOneDude Jan 22 '17 at 21:37
  • 3
    +1 for ~/.config/youtube-dl/config – Marinos An May 22 '17 at 18:15
43

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

pomsky
  • 68,507
  • 1
    How to combine ext=mp4 && height<=480 && filesize<50M ? – mrgloom Jun 21 '19 at 12:14
  • I use this youtube-dl -f 'best[ext=mp4]+best[height<=480]+best[filesize<100M]' https://www.youtube.com/watch?v=oInZYvMAjyw – ejabu Feb 12 '20 at 00:53
  • 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. – baptx May 31 '21 at 11:53
20

Update: use yt-dlp instead of youtube-dl! It's a fork off of youtube-dl and is much better-maintained and works much better! See a little more on yt-dlp in my answer here.


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 also:

  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
  3. Super User: How to download only subtitles of videos using youtube-dl

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

  • 5
    odd that I see listings for 1920x1080 options, but the 1280x720 (hd720) option still says "best" – nmz787 Jan 21 '19 at 18:07
  • 2
    All your screenshots are showing some electronics stuff. – localhost Jun 16 '19 at 06:36
  • @localhost, that's right. They are all screenshots from the video whos URL you see in all of my code snippets throughout the post: https://youtu.be/FWGC9SqA3J0. – Gabriel Staples Jun 16 '19 at 08:21
  • @GabrielStaples Aah. Yes, that makes sense. Sorry about that. – localhost Jun 16 '19 at 23:23
  • 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? – bomben Sep 24 '19 at 07:21
  • 2
    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. – Wlerin Apr 09 '20 at 04:33
  • 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. – Gabriel Staples Apr 09 '20 at 05:01
  • @Wlerin At least the audio part is because opus is the best quality audio that youtube provides. – localhost Oct 06 '21 at 00:39
7

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 https://m.youtube.com/watch?v=Dax_tnZRExc
    [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 #
ccpizza
  • 1,452
  • 17
  • 18
5

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 https://youtube.com/watch?v=iYWzMvlj2RQ
[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.

qwr
  • 2,802
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 https://www.youtube.com/watch?v=puPUJlV1-W4
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

1

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

Nerol
  • 63
  • 2
    Yes, as per the readme You also have option(s) to select intended format, see here: https://github.com/rg3/youtube-dl/blob/master/README.md#format-selection – AjayKumarBasuthkar Jan 26 '16 at 17:18
0

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

pi@rpi-lounge:~ $ youtube-dl -F https://youtu.be/Xj3gU3jACe8
[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  https://youtu.be/Xj3gU3jACe8

produces far better results than this usage:

youtube-dl -f 18  https://youtu.be/Xj3gU3jACe8 (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]" https://youtu.be/Xj3gU3jACe8
pi@rpi-lounge:~ $ youtube-dl --version
2020.06.16.1
Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
  • 1
    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. – iGadget Jan 26 '21 at 11:47
  • 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. – qwr Feb 20 '21 at 11:48