How to download the best video along with the best compatible audio with youtube-dl?

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?


Solution 1:

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!

Solution 2:

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

Solution 3:

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.