51

I want to use a format selector that is something like -f bestvideo+best audio whose extension is compatible with the video extension so that they don't need to be muxed into an mkv (WARNING: Requested formats are incompatible for merge and will be merged into mkv.).

Note: I know about -f best and I don't want that. I want the best possible qualities of both audio and video while ensuring that they are compatible. How to do that?

Shravan
  • 625
  • 1
  • 6
  • 7

5 Answers5

73

You can download the best video and audio by using:

youtube-dl -f bestvideo+bestaudio "link to youtube video"

If that gives you an error, try the following instead:

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

Here you download the best video and audio seperately and then merge into a, in this case mp4 file. You can change the output format on the merged video as well.

Good luck!

11

old question, but first answer on google, so:

with defining the following function, it worked for me (also possible to place it in ~/.bashrc):

youtube-dl_video_and_audio_best_no_mkv_merge () {
  video_type=$(youtube-dl -F "$@" | grep "video only" | awk '{print $2}' | tail -n 1)
  case $video_type in
    mp4)
      youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]' "$@";;
    webm)
      youtube-dl -f 'bestvideo[ext=webm]+bestaudio[ext=webm]' "$@";;
    *)
      echo "new best videoformat detected, please check it out! -> aborted";;
  esac
}

now you can download with

youtube-dl_video_and_audio_best_no_mkv_merge "https://www.youtube.com/watch?v=*******" "https://https://www.youtube.com/watch?v=********"

source: own creation

David Foerster
  • 36,264
  • 56
  • 94
  • 147
noone
  • 111
  • 2
    Note that the function name is not technically valid, specifically the dashes and plus sign. So you might have problems using the function, especially exporting it. I would recommend to make this into a script (since it doesn't need to be a function), and change the name to something like youtube-dl_video_and_audio_best_no_mkv_merge. – wjandrea Oct 17 '17 at 16:18
  • @wjandrea: Dashes in shell function names are valid (as they are for commands in general). I don't know about exported functions though. – David Foerster Oct 17 '17 at 21:04
  • @DavidFoerster Yes, Bash accepts invalid function names, but can't export them. If you try, you get an error, not a valid identifier – wjandrea Oct 17 '17 at 21:11
10

mix'n'match formats

This will show all available formats for the linked vid.

youtube-dl -F [http_link]

so

then just pick formats and join with a plus.

137 = 2k, 30fps. For example, I dont want 60fps, but I want best audio.

youtube-dl -f 137+bestaudio --merge-output-format mkv [http_link]

Note that not all formats merge into mp4, so mkv is a safer bet.

agrov8
  • 101
7

you choose the audio and video format and then combine with +.

For example:

youtube-dl -f 140+137 --merge-output-format mp4 youtube_link

But ffmpeg is necessary.

user43
  • 71
1

Similar to the above I use the following to download the best video and best audio for all videos in a playlist

There's a manual component and you need FFMpeg folder in the same director as youtube-dl

First run .\youtube-dl.exe --list-formats https://www.youtube.com/watch?v=cTcMtgA6iYt

sample output:

Output of .\youtube-dl.exe --list-formats https://www.youtube.com/watch?v=cTcMtgA6iYt


Now pick the format numbers you want to merge [137 for video and 140 for audio] Note | you need to pick the same format container e.g. 313 for video won't work with 140, you need a webm audio only file like 251

then run the following

.\youtube-dl.exe --ffmpeg-location "D:\FFmpeg\ffmpeg-N-100072-g1555cfedf6-win64-gpl-shared-vulkan\bin\ffmpeg.exe" --format 137+140 --yes-playlist -i https://www.youtube.com/playlist?list=PLFfX4Mdr5gLs52KWVl3KQNrRpTmLOn


once that finishes downloading all files where 137 was available, then re-run the same command, but this time with the next best code like 136+140

and what i usually do is...

at the very end...i run

.\youtube-dl.exe --ffmpeg-location "D:\FFmpeg\ffmpeg-N-100072-g1555cfedf6-win64-gpl-shared-vulkan\bin\ffmpeg.exe" --format 22 --yes-playlist -i https://www.youtube.com/playlist?list=PLFfX4Mdr5gLs52KWVl3ZQNrSoTmLOnQdk

22 - will pick up all videos missed, its a video and audio merged file that youtube-dl downloads by default, when you ommmit the format switch

  • Welcome to askubuntu! There appears to be a discrepancy in your answer. You state that "you need FFMpeg folder in the same director as youtube-dl" but your command uses --ffmpeg-location to specify what appears to be a Windows path. I recomment that you take the tour. Cheers! – Elder Geek Jan 19 '21 at 13:24
  • --ffmpeg-location did the trick nicely... thanks for the tip!!! ;-) – ZEE May 27 '21 at 22:08