youtube-dl optimal video resolution within a max limit

Typically youtube-dl aims to shoot for the highest quality possible. That's not always practical. Flip side I've seen was for minimum quality possible. That's not practical either. What would be a command to make youtube-dl find the highest quality format that is within a set limit?

Say: having resolution width equal to or below a certain value. See this example for available formats for a given video link:

youtube -F <link>
...
format code               extension  resolution note
2242414685833597ad        m4a        audio only DASH audio   21k , m4a_dash container, mp4a.40.2 (48000Hz)
572715713158985v          mp4        426x240    DASH video  216k , mp4_dash container, avc1.4D401E, video only
110928776481884vd         mp4        640x360    DASH video  420k , mp4_dash container, avc1.4D401E, video only
896314927220784v          mp4        854x480    DASH video  738k , mp4_dash container, avc1.4D401E, video only
313835759204486v          mp4        1280x720   DASH video 1372k , mp4_dash container, avc1.4D401F, video only
dash_sd_src               mp4        unknown    
dash_sd_src_no_ratelimit  mp4        unknown    (best)

I want to tell youtube-dl to choose the highest quality video stream whose width does not exceed 640 pixels. That would be the 110928776481884vd in this case.

If its not possible to do with resolution width, we could do with bitrate. So in this same example, bitrate should be 500k or lower.

Note: This question is specifically for non-youtube videos from different providers having different kinds of formats. The youtube ones have standard format names so I've already fixed for that.


Read the fine manual:

   Format selection examples
       Note  that on Windows you may need to use double quotes instead of sin‐
       gle.

              # 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]'