Repository: youtube-dl
Issue: Prefer higher sample rates (48kHZ > 44.1kHz) when streams have the same bitrate
Labels: request

<!--

######################################################################
  WARNING!
  IGNORING THE FOLLOWING TEMPLATE WILL RESULT IN ISSUE CLOSED AS INCOMPLETE
######################################################################

-->


## Checklist

<!--
Carefully read and work through this check list in order to prevent the most common mistakes and misuse of youtube-dl:
- First of, make sure you are using the latest version of youtube-dl. Run `youtube-dl --version` and ensure your version is 2021.03.14. If it's not, see https://yt-dl.org/update on how to update. Issues with outdated version will be REJECTED.
- Search the bugtracker for similar feature requests: http://yt-dl.org/search-issues. DO NOT post duplicates.
- Finally, put x into all relevant boxes (like this [x])
-->

- [x] I'm reporting a feature request
- [x] I've verified that I'm running youtube-dl version **2021.03.14**
- [x] I've searched the bugtracker for similar feature requests including closed ones


## Description

<!--
Provide an explanation of your issue in an arbitrary form. Please make sure the description is worded well enough to be understood, see https://github.com/ytdl-org/youtube-dl#is-the-description-of-the-issue-itself-sufficient. Provide any additional information, suggested solution and as much context and examples as possible.
-->

Hello, and thank you so much for your work here! youtube-dl has been an invaluable tool in downloading and archiving online videos. However, as the title suggest, I have one minor criticism (or question) about how the *bestaudio* option picks the best audio stream.

When I run `youtube-dl -F "https://www.youtube.com/watch?v=vQHVGXdcqEQ"`, I get:
```
[debug] System config: []
[debug] User config: ['--verbose', '--format', 'bestvideo+bestaudio/best', '--embed-subs', '--all-subs', '--add-metadata', '--merge-output-format', 'mkv', '--output', '%(title)s.%(ext)s', '--external-downloader', 'aria2c']
[debug] Custom config: []
[debug] Command-line args: ['-F', 'https://www.youtube.com/watch?v=vQHVGXdcqEQ']
[debug] Encodings: locale cp1252, fs mbcs, out cp65001, pref cp1252
[debug] youtube-dl version 2021.03.14
[debug] Python version 3.4.4 (CPython) - Windows-10-10.0.19041
[debug] exe versions: ffmpeg 2021-03-14-git-1d61a31497-full_build-www.gyan.dev, ffprobe 2021-03-14-git-1d61a31497-full_build-www.gyan.dev
[debug] Proxy map: {}
[youtube] vQHVGXdcqEQ: Downloading webpage
[info] Available formats for vQHVGXdcqEQ:
format code  extension  resolution note
249          webm       audio only tiny   49k , webm_dash container, opus @ 49k (48000Hz), 1.53MiB
250          webm       audio only tiny   65k , webm_dash container, opus @ 65k (48000Hz), 2.03MiB
251          webm       audio only tiny  129k , webm_dash container, opus @129k (48000Hz), 4.02MiB
140          m4a        audio only tiny  129k , m4a_dash container, mp4a.40.2@129k (44100Hz), 4.02MiB
394          mp4        256x144    144p   66k , mp4_dash container, av01.0.00M.08@  66k, 30fps, video only, 2.07MiB
160          mp4        256x144    144p   79k , mp4_dash container, avc1.4d400c@  79k, 30fps, video only, 2.46MiB
278          webm       256x144    144p   83k , webm_dash container, vp9@  83k, 30fps, video only, 2.60MiB
395          mp4        426x240    240p  126k , mp4_dash container, av01.0.00M.08@ 126k, 30fps, video only, 3.91MiB
133          mp4        426x240    240p  144k , mp4_dash container, avc1.4d4015@ 144k, 30fps, video only, 4.49MiB
242          webm       426x240    240p  157k , webm_dash container, vp9@ 157k, 30fps, video only, 4.89MiB
396          mp4        640x360    360p  249k , mp4_dash container, av01.0.01M.08@ 249k, 30fps, video only, 7.73MiB
243          webm       640x360    360p  275k , webm_dash container, vp9@ 275k, 30fps, video only, 8.55MiB
134          mp4        640x360    360p  277k , mp4_dash container, avc1.4d401e@ 277k, 30fps, video only, 8.62MiB
135          mp4        854x480    480p  401k , mp4_dash container, avc1.4d401f@ 401k, 30fps, video only, 12.44MiB
397          mp4        854x480    480p  427k , mp4_dash container, av01.0.04M.08@ 427k, 30fps, video only, 13.27MiB
244          webm       854x480    480p  439k , webm_dash container, vp9@ 439k, 30fps, video only, 13.65MiB
136          mp4        1280x720   720p  668k , mp4_dash container, avc1.4d401f@ 668k, 30fps, video only, 20.72MiB
247          webm       1280x720   720p  781k , webm_dash container, vp9@ 781k, 30fps, video only, 24.25MiB
398          mp4        1280x720   720p  799k , mp4_dash container, av01.0.05M.08@ 799k, 30fps, video only, 24.79MiB
399          mp4        1920x1080  1080p 1371k , mp4_dash container, av01.0.08M.08@1371k, 30fps, video only, 42.53MiB
248          webm       1920x1080  1080p 1796k , webm_dash container, vp9@1796k, 30fps, video only, 55.72MiB
137          mp4        1920x1080  1080p 1978k , mp4_dash container, avc1.640028@1978k, 30fps, video only, 61.36MiB
18           mp4        640x360    360p  640k , avc1.42001E, 30fps, mp4a.40.2 (44100Hz), 19.86MiB (best)
```

As you can see, format `251` (opus @ 129k, 48000Hz) has the same bitrate as `140` (mp4a.40.2@129k, 44100Hz).
When I download the above video with `-f bestaudio`, I get the AAC stream of the file or format `140`.

I'd like to request that youtube-dl prefer the format with a higher sample rate (48000Hz) over the format with a lower sample rate (44100Hz) when they have the same bitrate, as shown above. Or, you could make it prefer opus over AAC when they have the same bitrate (as I believe opus might be better given the same bitrate, though feel free to correct me on that).

Additionally, I would like to clarify how youtube-dl picks its "bestaudio" format. Does it inherently prefer AAC over opus?
How does it determine the best format in general? By bitrate? Does it emulate YouTube's behavior when picking a format?

I'm sorry that this feature request is so loaded, but I do want to understand how youtube-dl works so I can understand if this feature is really something that is necessary or not. Thank you for your time!