13

I am downloading some music from Youtube, and it seems that in most cases (popular videos), the best quality audio is an opus file.

I know that (1) Opus is the newest and more efficient codec, and (2) converting from one lossy codec to another is not recommended.

However, as I want to use the same file, i.e. using same container, across Windows/iOs/car, I want to convert to AAC or MP3.

I have read here that you can mitigate the generation loss to a certain extent by using a higher bitrate for the target codec.

So what settings (e.g. ffmpeg) would you recommend to convert an opus file to AAC to keep same audio quality (I know this is subjective ...)?

According to the youtube-dl -F data it is 160kbs, but when downloaded, MediaInfo gives 127Kbps overall bitrate and 48Khz sampling rate.

Would 192kbs be a good choice for the AAC file? (I presume AAC is in any case a better choice than MP3?) I know that from CD or lossless, VBR is most recommended, but for transcoding from Opus, would CBR be better? And should I keep the 48khz sample rate from the opus file, or downsample to the usual 44.1khz of AAC?

And finally, youtube-dl also can download an AAC file of 128 kbs. Given that opus is more efficient/better, quality will be higher at the same bitrate. But would transcoding the opus to AAC (e.g. at 192kbs/212kbs) maintain that quality difference, or would the transcoding (even to a higher bitrate) induce artefacts so that the audio quality of the 192kbs AAC file would actually be worse than the AAC 128kbs file ?

EDIT

Let me explain why I “conceptually” struggle with understanding how to determine optimized settings for transcoding.

In most cases, you start with lossless file (e.g. a 50Mb wav), and you choose encoder settings for quality (VBR) or desired bitrate (CBR/ABR). The encoder then does its hocus pocus (discard inaudible info, compression, etc.), which then give you a smaller lossy file, based on the settings you choose. For arguments sake, let’s say maybe 3Mb for a 128kbs MP3, 5-6Mb for a top quality VBR ( i.e. highest settings for lame or qaac), and 7-8Mb for a 320kbs CBR).

But in this case, the source file is a 5Mb 128kbs opus file. The first step in transcoding is to first decode the file (to a PCM stream?), and then encode again in the new format.

So is my understanding correct that the maximum information in the file is not more than 5Mb, so to encode (and if it is not a requirement to reduce file size) I should just use the highest possible quality VBR, or possibly a high CBR (256 or 320) to capture virtually all information still present in the opus file. The resulting file would in any case not be larger than the 5Mb, as I assume no new information can be invented/generated in the process (I will do some experimenting over the weekend to test this).

The first answer given seems to suggest this.

5
  • Heh, nice that you found this old answer of mine. I updated it with some new information. Back when I wrote it in 2013, Opus was relatively new, but now it's much more commonly used.
    – slhck
    May 24, 2018 at 18:25
  • @slhck Your answer was quite useful indeed ! But opus probably has "matured" less than anticipated in 2013. Not in respect to browser support, but with regards to usage in stand-alone music files (no commonly / across platforms accepted container format -- well, at least according to wikipedia ...).
    – Peter K.
    May 25, 2018 at 6:51
  • Yes, it's more used in YouTube and VoIP implementations.
    – slhck
    May 25, 2018 at 8:42
  • 1
    youtube-dl has an option to convert the downloaded audio to an mp3 (or other formats): youtube-dl -i --extract-audio --audio-format mp3 --audio-quality 0 YT_URL. See: gist.github.com/umidjons/8a15ba3813039626553929458e3ad1fc
    – raidfive
    Jun 5, 2019 at 19:30
  • or if you want to convert afterwards, this line will batch-convert files with correct names: find . -name "*.opus" -exec sh -c 'ffmpeg -i "$0" -c:a libfdk_aac -vbr 5 -cutoff 18000 "${0%.opus}".aac' {} \; Dec 15, 2023 at 10:22

3 Answers 3

10

More generally, I would recommend to have a look at the FFmpeg Wiki page on AAC encoding. Here you will find tips on how to achieve the best quality, which involves:

  • using the libfdk_aac encoder instead of the built-in one (the encoder can make a huge difference in quality!)
  • using VBR — there is no need to constrain the rate or waste bits
  • turning off the high pass filter, as by default some high frequencies are lost during conversion

Thus, the following would work:

ffmpeg -i input.opus -c:a libfdk_aac -vbr 5 -cutoff 18000 output.m4a

This should be absolutely transparent to anyone's ears. You may even use -vbr 4 and check whether you can detect the difference. I guess you won't.


More specifically, you ask:

And finally, youtube-dl also can download an AAC file of 128 kbs. (…) But would transcoding the opus to AAC (…) maintain that quality difference, or would the transcoding (…) induce artefacts so that the audio quality (…) would actually be worse?

Great question that I don't know how to answer without conducting a formal listening test.

However, if you assume that stereo AAC at 128 kbps is at least good quality (but not excellent), you may forgo any conversion and use that directly if it sounds good to you. I think I would listen to the original Opus and AAC samples that you directly downloaded and try to do a blind test — can you reliably detect the difference? If no, then just use AAC and save yourself the conversion.

PS: I listened to a few music clips from YouTube with both the Opus and AAC variants and I personally liked Opus better — it seemed to have a bit more high-frequency spatiality to it. But don't consider that a scientifically sound conclusion!


Finally, there is a thread discussing a similar issue, and here, one user thinks that there is more aggressive psycho-acoustic shaping in VBR encoding, which may enhance already present artifacts when encoding a lossy file again. He concludes that ABR at a high bitrate may be better suited for such tasks.

19
  • If anyone wants to know their frequency limit for customized low-pass filter -cutoff value (at arbitrary 44100 Hz sample rate): ffplay -f lavfi -i sine=f=18000. Decrease until "shrieking bat call" is audible or until your ears bleed. In a noisy office with open headphones mine is ~16100.
    – llogan
    May 24, 2018 at 19:08
  • 2
    @LordNeckbeard There's a popping noise when you start the command (at least on my Mac), which I guess is due to the sudden onset. It can be avoided with sine=f=18000,afade=d=0.1.
    – slhck
    May 24, 2018 at 19:21
  • @slhck Thanks for your contribution. So by using presets (e.g. -vbr 5), the encoder will "find" optimal avg bitrate to preserve quality? I do use presets when ripping from source (CD), but I thought that when source is lossy (opus 128kbs), setting parameters manually would be better. And what about sample rate? Set 48Khz manually as in the opus file, or keep default (which implies resampling at 44.1Khz I assume)?
    – Peter K.
    May 25, 2018 at 7:40
  • @PeterK Yes, VBR will ensure optimal rate and quality. The sample rate should stay at 48 kHz unless you specify a different one.
    – slhck
    May 25, 2018 at 8:41
  • @slhck I have been looking at libfdk_aac, but I do not think I have the skills/time to make a custom ffmpeg build with libfdk_aac included. I assume I could use qaac as an alternative (either command line or with fb2k) ? But then I still have to find out the recommended settings. Also for qaac, there is much info for encoding from source (CD or lossless), but not much info for recommended settings based on various lossy input files.
    – Peter K.
    May 25, 2018 at 8:46
2

Just my 2 cents as I asked myself the same question:

  1. best quality available is Opus at the time of download. Forcing a different format will cause an irreversible loss (as observed e.g. in Spek)
  2. However, my old MP3 only supports AAC, not Opus
  3. And then, simply using ffmpeg with -map_metadata to convert Opus to AAC while preserving the metadata retrieved by Youtube-dl will fail on Windows 10, because the metadata is written in the "stream" part of the opus file, and not the "format" part, by youtube-dl.
  4. Therefore, you are stuck with adding the metadata manually if you want to convert your compact, high res Opus library to an AAC library that can fit in your old MP3, on Windows 10.

Unless...you use the following batch file

@Echo off
SetLocal EnableDelayedExpansion
Set _InputPath=Opus\*.opus
Set _OutputPath=M4A_from_Opus
For %%A in (%_InputPath%) Do (
    FOR /F "tokens=* USEBACKQ" %%F IN (`ffprobe -v error -show_format -show_streams "%%A" ^| FINDSTR artist`) DO (
        SET ArtistLine=%%F
    )
    FOR /F "tokens=* USEBACKQ" %%F IN (`ffprobe -v error -show_format -show_streams "%%A" ^| FINDSTR title`) DO (
        SET TitleLine=%%F
    )
    FOR /F "tokens=* USEBACKQ" %%F IN (`ffprobe -v error -show_format -show_streams "%%A" ^| FINDSTR album`) DO (
        SET AlbumLine=%%F
    )
    for /F "tokens=2 delims==" %%b in ("!ArtistLine!") do (
        Set Artist=%%b
    )
    for /F "tokens=2 delims==" %%b in ("!TitleLine!") do (
        Set Title=%%b
    )
    for /F "tokens=2 delims==" %%b in ("!AlbumLine!") do (
        Set Album=%%b
    )
    Echo Converting %%A...
    ffmpeg -i "%%A" -c:a aac -b:a 160k -metadata artist="!Artist!" -metadata title="!Title!" -metadata album="!Album!" "%_OutputPath%/%%~nA".m4a
)
SET "_OutputPath="
SET "_InputPath="
SET ArtistLine=
SET AlbumLine=
SET TitleLine=
SET Artist=
SET Album=
SET Title=
0

Not so much an answer as a bit of 'expert' opinion, for what it's worth [I'm a sound engineer, but not a coding/algorithm guy]

For iOS/car you just get iTunes to resample at 128k AAC - the option is in iTunes itself, it will make new versions just for your devices - you'll never hear the difference, certainly in the car, & from mobiles only in silent surroundings with seriously expensive headphones.

Window crushed down to save space...

enter image description here

If you want to archive at "maximum" use Apple lossless [ALAC].
It won't get back anything already gone, but it won't lose any more.

btw, iOS itself can now play Opus files [since iOS 11] - but I don't know whether that's via iTunes or if only as a streamed format.

7
  • AAC cannot be lossless, do you mean ALAC?
    – slhck
    May 24, 2018 at 18:33
  • I mean "Apple Lossless Encoder", so yes I probably do mean ALAC. [Told you I wasn't a tech ;-) Fixed.
    – Tetsujin
    May 24, 2018 at 18:35
  • Heh! Using ALAC though would not give you much compatibility other than macOS (and maybe iOS? I don't even know…)
    – slhck
    May 24, 2018 at 18:38
  • I assumed windows/ios/car would essentially be an iTunes environment. I guess FLAC would work too, but as iTunes doesn't like it, it would be the same issue in reverse. tbh, I usually work & archive in WAV & drop to whatever I need as required - but I start from scratch, not from someone else's source material ;-))
    – Tetsujin
    May 24, 2018 at 18:44
  • @Tetsujin Thanks! I do try to keep my music library free from having various versions of a file for different players (and for the car, I have to manually select the music files and copy these directly from my library). If I would go the lossless route, I would rather go for FLAC than for ALAC. But preference (for filesize and universal playability) is to use AAC (or MP3).
    – Peter K.
    May 25, 2018 at 7:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .