Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Encoding raw audio file to opus using ffmpeg vs opusenc (Read 7718 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Encoding raw audio file to opus using ffmpeg vs opusenc

I have several hundred Flac audio files, and having done some research recently I find that the most efficient lossy codec is Opus. So I tried several different encoders (I'm on Ubuntu) and chose 2: ffmpeg (-c:a libopus) and opusenc.

I created test files to see if there were any significant differences between these two:
- ffmpeg always drops the album photo (unlike opusenc).
- opusenc creates a file about 800 bytes larger than ffmpeg (negligible).
The rest of the differences only occur at the level of the output file metadata (acquired with the MediaInfo software and given by opusenc in command-li) :
- the "Comment" tag is renamed "Description" only with ffmpeg (negligible, but how to explain that ????)
- only opusenc adds the tag "ENCODER_OPTIONS".
- "Writing application": opusenc from opus-tools 0.1.10 vs Lavc58.54.100 libopus
- "Writing library": libopus 1.3.1 vs Lavf58.29.100
- "Sampling rate" : same as the original one for opusenc (even 44.1 kHz), but still 48 kHz for ffmpeg (without specifying anything in the encoding parameters).

The things I find the most shocking are the abandonment of the album photo by ffmpeg (I don't mind because I wanted to remove it anyway) and the sampling.
Let's take a closer look at the sampling:
ffmpeg supports ONLY 48 kHz (I get an error if I try with "-ar 44100" or "-ar 96000"). Personally, I would like either 44.1 or 48 kHz (no need for more because the human ear would not be able to hear it and "files at [96 and 192 kHz] are internally converted to 48 kHz", so I find the 48 kHz of ffmpeg totally coherent and understandable.
Here we come to a bigger problem: on the same site, we are told "[For now], the best way to encode audio into Opus files is to use the opusenc command-line tool". "Unfortunately", by encoding a 96 kHz source file, the output file has the same sampling, which is inconsistent with the internal conversion at 48 kHz . Unless maybe it is really done and opusenc add "empty data" to the output file...
First observation: a 96 kHz input file gives one of 48 kHz with ffmpeg and one of 96 kHz with opusenc. The latter is about 800 bytes heavier than the ffmpeg one, how is this possible ???? So we see that there is no "empty data", maybe opusenc defines the sampling ceiling without adding anything ?
So I try to modify this sampling with "--raw-rate 48000" (always with a 96 kHz input file). The new file is now at 48 kHz, but weighs 3.2 Mbytes instead of 2.4 Mbytes without this parameter ... And more important: the sound is completely corrupted! (A continuous sound like a fog). So this solution is not possible. Is there a way to change the sampling rate correctly to 48 kHz ?

My most important question, why the output file with opusenc is set to 96 kHz while the input file is converted internally at 48 kHz ? Is it still better to use opusenc compared to ffmpeg ? My other questions are all underlined.
A huge thank you in advance to all of you !  :D

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #1
Hello,

First observation: a 96 kHz input file gives one of 48 kHz with ffmpeg and one of 96 kHz with opusenc.

After some testing (Windows 10, ffmpeg latest & opusenc 1.3.1), I can't reproduce this. Encoding a 96kHz file with opusenc results in a 48kHz opus file, checked with foobar2000 and ffprobe. The Properties dialog in foobar says that :
- Sample rate is 48000 Hz
- Original sample rate is 96000

    AiZ

 

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #2
When you encode audio with opusenc, it adds metadata to tell the decoder what the original sample rate was. When you decode an Opus file with opusdec, it resamples the audio to the original sample rate according to the metadata. You can run opusdec with the parameter "--rate 48000" to disable the resampling.

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #3
To add even more confusion, here's a third option for conversion. This uses sox for the resampling and passes an intermediate flac (should preserve most tags) into opusenc. It also does a bunch of files in parallel (adjust 5 to your amount of cpu cores for more speed). I'm not making any claims about quality

Code: [Select]
find "/media/folder" -type f -iname '*.flac' -print0 | nice -n 15 xargs -0 -P 5 -I {} sh -c 'sox "{}" -b 24 -C 0 -t flac - rate -v -L 48k | opusenc --vbr --bitrate 144 - /tmp/"$(basename "{}")"-$(date +%s).opus

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #4
Quote
[opusenc] adds metadata to tell the decoder what the original sample rate was.
Ah okay, I was wondering why Opusenc was "lying" about the output sampling (or if the file I was getting was corrupted). I understand its usefulness now.
Thanks for the clarification. Personally I don't really want to prepare all files in advance with the resampler like Sox (even if I read everywhere that it shouldn't affect the quality).

One more thing I'm struggling with: which software is to be preferred between ffmpeg and opusenc (from opus-tools) to encode only opus?
I would prefer opusenc, because it explicitly says which opus version it uses (libopus 1.3.1) while ffmpeg does not (Lavf58.29.100), what's your opinion on this?

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #5
I would prefer opusenc, because it explicitly says which opus version it uses (libopus 1.3.1) while ffmpeg does not (Lavf58.29.100), what's your opinion on this?

Pretty much what you say. If I were to encode purely to opus, I would also prefer the dedicated library/program. I would fall back to ffmpeg whenever I must support multiple codecs "well enough" with just a simple switch.

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #6
Thanks !
Now I know what I have to do and work with :D

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #7
When you encode audio with opusenc, it adds metadata to tell the decoder what the original sample rate was. When you decode an Opus file with opusdec, it resamples the audio to the original sample rate according to the metadata. You can run opusdec with the parameter "--rate 48000" to disable the resampling.

What is the advantage in telling the decoder about the original sample rate?

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #8
According to RFC 7845, it's to imitate the behavior of other codecs where compressing and decompressing the audio usually doesn't change the sample rate. I'd guess the main advantage is that it makes it easier for users to compare Opus against other codecs.

Re: Encoding raw audio file to opus using ffmpeg vs opusenc

Reply #9
When you encode audio with opusenc, it adds metadata to tell the decoder what the original sample rate was. When you decode an Opus file with opusdec, it resamples the audio to the original sample rate according to the metadata. You can run opusdec with the parameter "--rate 48000" to disable the resampling.

What is the advantage in telling the decoder about the original sample rate?
If you don't, you will usually get back a 48 kHz decoded stream which well be inconvenient.  If you want to encode, for example, a 16 kHz stream of audio in Opus then it might come as a surprise to not get a 16 kHz stream back when you decode it.  As mentioned, this is what most codecs would do and what a lot of people will expect Opus to do, but also suddenly getting 48 kHz audio would be a pain in many applications.  You can force the decoded sample rate. but that can be inconvenient if you have different streams at different rates, or if you simply can't be bothered.