Convert audio to Opus with FFmpeg

Forget about MP3, AAC, M4A, and other lossy audio codecs.

Opus is the best lossy codec for interactive speech and audio transmission over the Internet!

At the time of writing the Opus format outperforms every other lossy audio format, and it has been implemented by every major internet browser, Spotify, Netflix, YouTube, and other streaming services. Windows, iOS, and Android also support Opus. Opus outperforms every single lossy audio codec out there.

Opus in FFmpeg

Most builds of FFmpeg come with the Opus encoder. If you are compiling your own version, make sure to compile FFmpeg with the
--enable-libopus option. After that, you should be able to use libopus.

Note that FFmpeg already includes an experimental codec called opus. Don’t use that unless you know what you are doing.

Converting audio to Opus in FFmpeg

The basic gist is:

ffmpeg -i input.wav -c:a libopus -b:a 128k output.opus

This will default to 48 kHz and a bitrate of 96 kbps.

You don’t really need any other options unless you are converting surround sound with multiple channels.

Sampling rate – It’s always 48 kHz

Opus defaults to a sampling rate of 48 kHz, which might sound strange. Audio CDs and MP3 files are usually 44.1 kHz… However, sampling rates above 44.1 kHz do not add any audible quality improvement and have been considered redundant by the developers. Without going into details, for those who know a little bit about signal processing, a higher sampling rate does not equal higher quality if the sampling rate is above 44,1 kHz. There should not really be any situation where you would need to change the sampling rate for Opus files. If it is, you are doing something oddly specific, or a decoder is not working correctly.

Bit rate – It depends…

To set the actual bit rate use -b:a followed by the bit rate in kbps. For example: -b:a 128k.

The default is 96 kbps.

  • 24 kbps is recommended for mono podcasts and audiobooks.
  • 96 kbps is recommended when streaming is important.
  • 128 kbps is recommended for quality stereo music.
  • 256 kbps for 5.1 surround.
  • 450 kbps for 7.1 surround sound.

You can read more about the recommended settings here.

Variable bitrate is the default, so you don’t need to add that. And for music, it is recommended to use a variable bitrate. However, if you need a constant bit rate you can add -vbr off. Alternatively, you can constrain the variable bitrate using -vbr constrained.

Compression – Don’t think about it

Unlike AAC, you don’t need to set a compression level. The best compression always is applied. 😎👍

If you want to change the file size and quality you can of course use a different bitrate.

There is an option called compression_level which goes from 0 to 10. 0 is fast and low quality and 10 is slower, but higher quality. But this option is aimed at extremely low-performance devices. 99% of all devices today should be able to encode Opus at level 10, which is the default.

Examples

Downmixing a 5.1 surround sound video to stereo (keeping the video):

ffmpeg -i input.mkv -map 0 -c copy -c:a aac -ac 2 output.mkv

Converting a Dolby 5.1 AC3 audio track to Opus, preserving all channels:

ffmpeg -i input.ac3 -c:a libopus -af channelmap=channel_layout=5.1 -b:a 96k output.opus

Cover art 😢

At the moment it does not seem possible to add cover art with FFmpeg. At least not if you are trying to keep an already embedded cover from a different file format.

However, this is possible if you use the CLI tool opusenc instead of FFmpeg. You can download opusenc here. Or you can use a tagging tool like MusicBrainz Picard or foobar2000.


Sources:

3 Comments

    • Thank you. Most of my collection is FLAC. It is simply the best audio format out there for music. However, streaming those tracks to a mobile requires some transcoding. This is where Opus comes in for me.

  1. for %%f in (*.flac) do ( ffmpeg -i “%%f” -ab 450k “%%f.opus” )

    or

    for %%f in (*.flac) do ( ffmpeg -i “%%f” -ab 320k -map_metadata 0 -id3v2_version 3 “%%f.mp3” )

    in convert.bat file

Leave a Reply

Your email address will not be published. Required fields are marked *