Recommended settings to convert Opus to AAC

14,886

Solution 1

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.

Solution 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=
Share:
14,886

Related videos on Youtube

Peter K.
Author by

Peter K.

Former strat consultant and investment banker, so quite proficient in Excel, Word, Powerpoint. Poweruser of Windows. Hobbyist webmaster and web developer.

Updated on September 18, 2022

Comments

  • Peter K.
    Peter K. over 1 year

    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.

    • Admin
      Admin almost 6 years
      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.
    • Admin
      Admin almost 6 years
      @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 ...).
    • Admin
      Admin almost 6 years
      Yes, it's more used in YouTube and VoIP implementations.
    • Admin
      Admin almost 5 years
      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
  • slhck
    slhck almost 6 years
    AAC cannot be lossless, do you mean ALAC?
  • Tetsujin
    Tetsujin almost 6 years
    I mean "Apple Lossless Encoder", so yes I probably do mean ALAC. [Told you I wasn't a tech ;-) Fixed.
  • slhck
    slhck almost 6 years
    Heh! Using ALAC though would not give you much compatibility other than macOS (and maybe iOS? I don't even know…)
  • Tetsujin
    Tetsujin almost 6 years
    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 ;-))
  • Elisa Cha Cha
    Elisa Cha Cha almost 6 years
    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.
  • slhck
    slhck almost 6 years
    @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.
  • Peter K.
    Peter K. almost 6 years
    @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.
    Peter K. almost 6 years
    @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).
  • slhck
    slhck almost 6 years
    @PeterK Yes, VBR will ensure optimal rate and quality. The sample rate should stay at 48 kHz unless you specify a different one.
  • Peter K.
    Peter K. almost 6 years
    @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.
  • slhck
    slhck almost 6 years
    @PeterK What operating system are you using? For qaac (which I've never used), setting --quality 2 and --tvbr 127 would give you the best quality. The default seems to be 90, so something in between is likely to be transparent.
  • Peter K.
    Peter K. almost 6 years
    @slhck I am using Windows 10. As qaac (basically the Apple AAC encoder) seems highly recommended (better than libfdk_aac according to hydrogenaud.io) and is easier to use, I think I will go that road. Thanks for the settings recommendation. I also edited my original question to better explain the background of my question, and why the transcoding is confusing to grasp for me (not having a technical audio or codec background). Your answer seem to corroborate this gut feeling. But maybe I am wrong ...
  • slhck
    slhck almost 6 years
    @PeterK Your assumptions are correct! You cannot generate more quality than there is in the original file anyway. To choose the highest VBR rate just means preserving most of it. You may however end up with a larger file, as that ultimately depends on how many bits the encoder chooses to spend.
  • Peter K.
    Peter K. almost 6 years
    @slhck I did some testing with various quality settings (I used ffmpeg/lame to convert into mp3; I know that aac is generally better than mp3 for lower bitrates, but aac doesn't play on my car stereo, and I don't want to install fb2k just to convert a few hundred songs). Source is a pop song of 3.6Mb (opus 126kbs), -q:a 0 gives a 7.1Mb file, -q:a 1 6.3Mb, -q:a 2 5.3Mb. So even if opus is more efficient in encoding, not sure that the file-size increase is reasonable ...
  • slhck
    slhck almost 6 years
    @PeterK I'm not sure I got your point. Are you saying the increase in filesize is not acceptable?
  • Peter K.
    Peter K. almost 6 years
    @slhck Well, the file increase is larger than I expected. My "layman's" explanation is that the decoding of opus (into PCM?) adds noise/interpolations, and that by choosing v0 (or equivalent), a lot of that (I assume non-essential) information is encoded again and kept in the new MP3 file. I am still not clear whether in that case it would make sense to actually use a lower-quality setting v1 or v2 or even v3, so that the information added by the opus decoder gets discarded by the MP3 encoder. But that implies of course that the encoder can make the difference between noise and audio info...
  • slhck
    slhck almost 6 years
    @PeterK Ah, that's not quite how it works. Decoding is lossless; no new info is being added. Encoding is like folding/crumpling a sheet of paper, and decoding is like unfolding it. All the info is there, but perhaps a little distorted (with “wrinkles”), depending on how well you can fold it. MP3 is just less efficient, therefore your output will never be as well-compressed as AAC. Or, if an MP3 ends up with the same size as AAC, it's for sure going to have more distortion. Thus I would expect the file size to go up with MP3, and I would stick with a rather high quality setting.
  • Peter K.
    Peter K. almost 6 years
    @slhck Thanks for your help and insights. So maybe I should go for AAC after all and make the effort to install fb2k with the Apple AAC codec. So I can use an higher VBR quality setting for the same resulting file size than MP3.
  • slhck
    slhck almost 6 years
    @PeterK You're welcome. Feel free to ask in case you have follow-up questions.
  • Peter K.
    Peter K. almost 6 years
    @slhck If you are interested, there is also a discussion on the HA forums on this topic. Not sure whether it is within the TOU of SU to add a link to another forum. In any case, someone recommended to use ABR, as the psy model used by (true) VBR might be "confused" because the source is lossy file.
  • slhck
    slhck almost 6 years
    @PeterK Interesting! Please share the link and I'll add it to the answer.
  • Peter K.
    Peter K. almost 6 years
    @slhck Here you go: article. Thanks again !
  • slhck
    slhck almost 6 years
    @PeterK. Ahh, thanks! Forums, where it's hard to find the relevant info :) (I tend to get lost in anything that is not Super User / Stack Overflow.) I've included a mention in my post.
  • Marc.2377
    Marc.2377 almost 4 years
    I found that for the FDK AAC encoder in VBR mode, no cutoff is applied by default at all, at least for quality level 5 - meaning you can ommit the -cutoff parameter to output at the maximum (full) frequency range.
  • Marc.2377
    Marc.2377 almost 4 years
    @Tetsujin, I noticed that sound engineers (and mastering engineers etc) in general prefer WAV or AIFF instead of lossless compression, eg., FLAC, wavpack or ALAC. Would you be able to tell me why that is?
  • Tetsujin
    Tetsujin almost 4 years
    You don't have to uncompress it on the fly to work with it.