42

With youtube-dl I first look what video quality is available and then in second step I download highest possible quality (in this example -f 137+140). Youtube-dl will automatically merge audio and video stream (DASH format)

How to automate that with shell script? With every URL, choose highest audio and video quality?

> youtube-dl -F 'https://www.youtube.com/watch?v=VAdRBLyjLRQ&feature=youtu.be&t=8m14s'                              
[youtube] Downloading login page                                                                                                      
[youtube] Logging in                                                                                                                  
[youtube] VAdRBLyjLRQ: Downloading webpage                                                                                            
[youtube] VAdRBLyjLRQ: Downloading video info webpage                                                                                 
[youtube] VAdRBLyjLRQ: Extracting video information                                                                                   
[youtube] VAdRBLyjLRQ: Downloading DASH manifest                                                                                      
[youtube] VAdRBLyjLRQ: Downloading DASH manifest                                                                                      
[info] Available formats for VAdRBLyjLRQ:                                                                                             
format code  extension  resolution note                                                                                               
249          webm       audio only DASH audio , opus @ 50k                                                                            
250          webm       audio only DASH audio , opus @ 70k                                                                            
251          webm       audio only DASH audio , opus @160k                                                                            
171          webm       audio only DASH audio  117k , vorbis@128k (44100Hz), 7.68MiB                                                  
140          m4a        audio only DASH audio  128k , m4a_dash container, aac  @128k (44100Hz), 11.58MiB                              
160          mp4        256x144    DASH video  112k , avc1.4d400c, 15fps, video only, 9.94MiB                                         
278          webm       256x144    DASH video  136k , webm container, VP9, 15fps, video only, 9.10MiB                                 
133          mp4        426x240    DASH video  248k , avc1.4d4015, 30fps, video only, 22.27MiB                                        
242          webm       426x240    DASH video  316k , vp9, 30fps, video only, 22.20MiB                                                
243          webm       640x360    DASH video  557k , vp9, 30fps, video only, 41.05MiB                                                
134          mp4        640x360    DASH video  609k , avc1.4d401e, 30fps, video only, 52.94MiB                                        
244          webm       854x480    DASH video 1021k , vp9, 30fps, video only, 74.05MiB                                                
135          mp4        854x480    DASH video 1115k , avc1.4d401f, 30fps, video only, 97.02MiB                                        
247          webm       1280x720   DASH video 2011k , vp9, 30fps, video only, 150.61MiB                                               
136          mp4        1280x720   DASH video 2282k , avc1.4d401f, 30fps, video only, 189.40MiB                                       
248          webm       1920x1080  DASH video 3838k , vp9, 30fps, video only, 286.61MiB                                               
137          mp4        1920x1080  DASH video 4209k , avc1.640028, 30fps, video only, 354.19MiB                                       
17           3gp        176x144                                                                                                       
36           3gp        320x240                                                                                                       
5            flv        400x240                                                                                                       
43           webm       640x360                                                                                                       
18           mp4        640x360                                                                                                       
22           mp4        1280x720   (best)                                                                                             
> youtube-dl -f 137+140 'https://www.youtube.com/watch?v=VAdRBLyjLRQ&feature=youtu.be&t=8m14s'                      
[youtube] Downloading login page                                                                                                      
[youtube] Logging in                                                                                                                  
[youtube] VAdRBLyjLRQ: Downloading webpage                                                                                            
[youtube] VAdRBLyjLRQ: Downloading video info webpage                                                                                 
[youtube] VAdRBLyjLRQ: Extracting video information                                                                                   
(...)
Saurabh
  • 5,176
  • 4
  • 32
  • 46
Sybil
  • 2,503
  • 3
  • 25
  • 36
  • E.g. for hi-res video-tutorials or music-"videos" it can sometimes make sense to combine best and worst options as for the former `bestvideo+worstaudio` and the latter `worstvideo+bestaudio`. Just for the sake of completeness. – TNT May 08 '17 at 18:10

5 Answers5

78

Just use -f bestvideo+bestaudio/best for highest resulting quality available.

If you wanted to prefer MP4 format containers instead of WebM, use:

-f bestvideo[ext!=webm]‌​+bestaudio[ext!=webm]‌​/best[ext!=webm].

Michael Foukarakis
  • 39,737
  • 6
  • 87
  • 123
dstftw
  • 2,941
  • 2
  • 16
  • 10
  • 1
    howto get only MP4? No WebM? – Sybil Oct 05 '15 at 20:13
  • 2
    `-f bestvideo[ext!=webm]+bestaudio[ext!=webm]/best[ext!=webm]`. – dstftw Oct 06 '15 at 17:20
  • 5
    Since version `2015.04.26` this is the default (see [here](https://github.com/rg3/youtube-dl/blob/master/README.md#format-selection)) so there's no need to specify it manually. – Adrian Frühwirth Dec 09 '15 at 10:38
  • 2
    It will not necessarily download the best audio. Since 2015, youtube-dl uses `-f bestvideo+bestaudio/best` as the default format. This means It will try to download the `bestaudio` stream served as a single file (DASH) when available, but if it doesn't exist it will download the `best` audio-video stream and extract the audio from it. The thing is that for some YT vids the highest quality DASH audio stream has a lower bitrate than the audio found in the `best` audio-video stream. There is an ongoing discussion on whether this should be addressed at https://github.com/rg3/youtube-dl/issues/9302 – Redoman Sep 19 '17 at 13:24
28

I've found some weird cases where the default (see #5447, #5456) bestvideo+bestaudio/best would download 720p video even when a 1080p was available. The reason that the bitrate on the 720p was very slightly higher, therefore it seemed better quality for youtube-dl. I prefer the higher res, also all other fancy youtube downloader applications downloaded the 1080p (as control).

Therefore I propose and use this:

-f ("bestvideo[width>=1920]"/bestvideo)+bestaudio/best

It will look for the best, at least 1920 wide video. If that's not available, it'll resort to what it would normally choose. The /best part at the end is part of the default config.

On a side note I recommend putting the %(format_id)s somewhere in the filename, so you know what that file is using. If you later want to re-download a bunch of videos without using archive, with --no-overwrites, a better/different quality will form a new filename next to the old one (so you can compare or whatever).

Community
  • 1
  • 1
10

Or you could just use,

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

This will download the highest quality video and audio and merge them automatically. And I think I have heard somewhere that you need ffmpeg installed to have this automatic behavior. But I bet most systems have it already.

Lilylakshi
  • 736
  • 7
  • 19
  • As shown in the screenshot "best" is in this sample case (as often) 1280x720. And that would be what you get this way. I can't afford to downvote this answer as it would deserve... – TNT May 08 '17 at 17:48
  • 1
    @TNT Actually no. The best in parenthesis implies the best complete video. But if you do not provide a quality, it actually goes ahead and downloads the best video and best audio separately. And combines them to form the final output and deletes the original video and audio files afterward. These steps are actually displayed in the terminal. And I have experienced the output firsthand. The OP would get a 1080p video in this example. – Lilylakshi May 10 '17 at 03:49
  • @Lilytakshi Sorry, you're right! Fortunately I didn't downvote your answer. Shame on me! I upvoted now. – TNT May 11 '17 at 19:17
  • As suggested, I had to install `ffmpeg` for it to grab the best files separately and merge them – gerry3 Jul 16 '17 at 19:36
  • 4
    It will not necessarily download the best audio. Since 2015, youtube-dl uses `-f bestvideo+bestaudio/best` as the default format. This means It will try to download the `bestaudio` stream served as a single file (DASH) when available, but if it doesn't exist it will download the `best` audio-video stream and extract the audio from it. The thing is that for some YT vids the (DASH) audio stream quality is lower than the audio found in the `best` audio-video stream. There is an ongoing discussion on whether this should be addressed at https://github.com/rg3/youtube-dl/issues/9302 – Redoman Sep 19 '17 at 12:19
8
youtube-dl -f best 'youtube_URL'

or,

youtube-dl -f 'bestvideo+bestaudio/bestvideo+bestaudio' --merge-output-format mp4 'youtube_URL'

or,

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

Hope it helps!

Pritom
  • 89
  • 1
  • 3
1

Since the numbers of format are limited I did:

youtube-dl -f 299+140 "http://www.youtube.com/watch?v=P9pzm5b6FFY"
youtube-dl -f 137+140 "http://www.youtube.com/watch?v=P9pzm5b6FFY"
youtube-dl -f best "http://www.youtube.com/watch?v=P9pzm5b6FFY"

If the 299+140 has been downloaded the 137+140 won't be and if none of those have you'll have the best starting with 22. The only problem is that you'll have the best in webm as a duplicate but you can use the ext!=webm to avoid this.

Of course you can start at 313+140 or whatever higher format you want.