Open knarrff opened 3 years ago
^^^^^^^
Was about to make an issue about some videos having unacceptable quality when downloaded as opus. Have reproduced many times -- seems to happen more on older videos?
Confirming this bug. It's intermittent though, different URLs give different results.
youtube-dl --extract-audio --audio-format opus "https://www.youtube.com/watch?v=azOiTtlN-6E"
^gives good quality output
youtube-dl --extract-audio --audio-format opus "https://www.youtube.com/watch?v=t1C83sIxUyg"
^gives 5k quality output
I figured out what triggers the different results.
Confirming this bug. It's intermittent though, different URLs give different results.
youtube-dl --extract-audio --audio-format opus "https://www.youtube.com/watch?v=azOiTtlN-6E"
^gives good quality output
This is downloaded as a webm, so a lossless conversion is performed (the ffmpeg command doesn't specify -b:a because it's -vcodec copy)
youtube-dl --extract-audio --audio-format opus "https://www.youtube.com/watch?v=t1C83sIxUyg"
^gives 5k quality output
This is downloaded as an m4a, so a lossy conversion is performed (and the ffmpeg command does specify -b:a)
The --audio-quality 0-10
syntax was originally designed for mp3. Due to this, similar issues exist for other conversions as well. For example, while 0
is "best" for mp3
, it refers to the worst quality for vorbis
.
yt-dlp deals with these issues by re-scaling the user input according to format. Same can be done here. I opted to not convert the 0-10 scale to bitrate for formats that do not support -qa
, but I can see the appeal of doing that (might be difficult to agree on the factor to use).
0 being best is what --help describes, so vorbis should change accordingly. Different codecs will warrant different scales, as 128 kb/s MP3 is vastly different from 128 kb/s Opus. Opus is just a much better codec that can do more with less.
With this issue I did not necessarily want to create the best possible solution, but to get to a workable solution fast. The current state of a default of 5 and this being interpreted as 5 kb/s for opus is not really usable. I agree that using an codec-provided quality scale would be ideal. However, as this does not seem to exist, we are left with the question what to choose as default. Assuming we stick with a default of 5 and assuming we stick with the interpretation of values in the range of 0<x<10 as quality rating and not bit rate, we have to come up with some mapping of that to bit rates on our own, thus my original suggestion in the ticket description.
@knarrff in case it wasn't obvious, I filed pull request #30957 fixing this issue, and used your scaling idea (but ended up using 24 instead of 38.4 giving a range of 6 kb/s to 210 kb/s)
Indeed I was not aware of the request. Thanks!
Checklist
Verbose log
Description
When extracting audio, and the containing format isn't 'opus', but the requested format is, then youtube-dl will call ffmpg to do the convertion. Without specifying the preferred quality, youtube-dl defaults to '5'. This is fine. However, libopus does not support quality-based encoding, which is even mentioned in the youtube-dl source code: https://github.com/ytdl-org/youtube-dl/blob/c6a14755bb9629967fb12536ee8660ca67ff4345/youtube_dl/postprocessor/ffmpeg.py#L307
However, the following check does this:
So, in case no quality is set on the command line and opus is requested, self._preferredquality is 5, resulting in execution of the 'else'-branch and an encoding with a bitrate of 5k, also visible in the log above, second to last line. The encoding does not fail, but you can imagine it's quality ...
It is not immediately clear how to resolve this. Assuming we treat values < 10 as quality (like the rest of the code also does), then we could at least set the bitrate to a typical value for music. According to https://wiki.hydrogenaud.io/index.php?title=Opus, this could be 192k. If you want to be really nice, it could be set to (self._preferredquality * 38.4) This would result in 192k for the default of 5, but would make other quality settings at least mean something.