<p>The libopus decoder in ffmpeg will only output at a fixed rate of 48000 Hz. Using <code>-ar</code> leads to resampling using the swresample library.</p>

CC BY-SA 4.0
2023-06-23T05:30:45.480Z
It appears that you're right and the `sample_rate` is reset at [`avc->sample_rate    = 48000;`](https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/libopusdec.c#L65). It's a shame that there is no way to save compute and resample directly to the requested `sample_rate`... I guess the proper way would be to introduce special libopus decoder options if `-ar` is supposed to only affect post-hoc `swresample` processing.
Vadim Kantorov
CC BY-SA 4.0
2023-06-23T06:43:57.827Z
*introduce special libopus decoder options* --> this possibility will depend on whether the resampling is taken care of by the decoder directly or does opusdec delegate it to a bundled resampler. If the latter, then it will be the responsibility of the libopus decoder *wrapper* in ffmpeg to insert a resampler and that won't be allowed since ffmpeg architecture guidelines don't allow mixing of resampling inside a decoder interface. That violates clean separation of library roles. That said, you haven't posted your command and log, so I can't check if you've unnecessary compute in your runtime.
Gyan
CC BY-SA 4.0
2023-06-26T09:21:09.780Z
I'll check if I can repro on a synthetic file and will post full ffmpeg command lines. `libopus` can do resampling to the target sample rate directly during decoding, but it appears that `ffmpeg` doesn't let the user access this functionality. It also appears post-hoc resampling severely affects wall clock time for the `builtin` opus decoder case (no such bad difference for `libopus` decoder)
Vadim Kantorov
CC BY-SA 4.0
2023-06-23T04:01:11.857Z
2023-06-23T04:01:11.857Z
Gyan
<p>As I understand, <code>ffmpeg</code> compiled with <code>--enable-libopus</code> has two opus decoders: builtin and <code>libopus</code>. I am decoding an opus file to wav.</p>
<ol>
<li><p>How can I know which opus decoder is being used by <code>ffmpeg</code>?</p>
</li>
<li><p><code>opusdec</code> has an option <code>--rate</code> which can directly downsample to the needed sample rate. When I use <code>ffmpeg</code> for decoding an opus file and set <code>-ar</code>, the decoding time grows x6 from the baseline 10sec (for a 3Mb, 2hour opus file). For <code>opusdec</code>, the decoding time is same regardless of used <code>--rate</code>. So I suspect that <code>ffmpeg</code> has some bug or duplicates resampling work. For workaround, is there a way to pass an option to <code>ffmpeg</code> to pass it down to <code>libopus</code> and to emulate the effect of <code>--rate</code>?</p>
</li>
</ol>
<p>Thank you!</p>
<p><strong>UPD</strong>
Adding <code>-acodec libopus</code> in the beginning of ffmpeg command clearly does something (forces <code>libopus</code> decoder?) and completes decoding in 6.2sec. When I add <code>-ar 8000</code>, it slows down to 7.3sec. I would assume, it's still too large of a change for a fair effect of downsampling (for <code>opusdec</code> <code>-ar 8000</code> actually speeds things up, not slows it), so maybe <code>ffmpeg</code> has a decoder configuration / resampling bug even when <code>libopus</code> decoder is used.</p>

<ffmpeg><audio>
opus decoding in ffmpeg: how to pass target sample rate and ensure libopus decoder?
Vadim Kantorov
# opus decoding in ffmpeg: how to pass target sample rate and ensure libopus decoder?

(Asked by: username_0 on 2023-06-22)

As I understand, `ffmpeg` compiled with `--enable-libopus` has two opus decoders: builtin and `libopus`. I am decoding an opus file to wav.

1. How can I know which opus decoder is being used by `ffmpeg`?
2. `opusdec` has an option `--rate` which can directly downsample to the needed sample rate. When I use `ffmpeg` for decoding an opus file and set `-ar`, the decoding time grows x6 from the baseline 10sec (for a 3Mb, 2hour opus file). For `opusdec`, the decoding time is same regardless of used `--rate`. So I suspect that `ffmpeg` has some bug or duplicates resampling work. For workaround, is there a way to pass an option to `ffmpeg` to pass it down to `libopus` and to emulate the effect of `--rate`?

Thank you!

**UPD**
Adding `-acodec libopus` in the beginning of ffmpeg command clearly does something (forces `libopus` decoder?) and completes decoding in 6.2sec. When I add `-ar 8000`, it slows down to 7.3sec. I would assume, it's still too large of a change for a fair effect of downsampling (for `opusdec` `-ar 8000` actually speeds things up, not slows it), so maybe `ffmpeg` has a decoder configuration / resampling bug even when `libopus` decoder is used.

**Tags:** ffmpeg, audio


---

## 1 answers

### Answer by username_1 on 2023-06-23. Score: 0 | Accepted answer

The libopus decoder in ffmpeg will only output at a fixed rate of 48000 Hz. Using `-ar` leads to resampling using the swresample library.

> **Comments:**
> **username_0**: It appears that you're right and the `sample_rate` is reset at [`avc->sample_rate    = 48000;`](https://github.com/FFmpeg/FFmpeg/blob/master/libavcodec/libopusdec.c#L65). It's a shame that there is no way to save compute and resample directly to the requested `sample_rate`... I guess the proper way would be to introduce special libopus decoder options if `-ar` is supposed to only affect post-hoc `swresample` processing.  
> **username_1**: *introduce special libopus decoder options* --> this possibility will depend on whether the resampling is taken care of by the decoder directly or does opusdec delegate it to a bundled resampler. If the latter, then it will be the responsibility of the libopus decoder *wrapper* in ffmpeg to insert a resampler and that won't be allowed since ffmpeg architecture guidelines don't allow mixing of resampling inside a decoder interface. That violates clean separation of library roles. That said, you haven't posted your command and log, so I can't check if you've unnecessary compute in your runtime.  
> **username_0**: I'll check if I can repro on a synthetic file and will post full ffmpeg command lines. `libopus` can do resampling to the target sample rate directly during decoding, but it appears that `ffmpeg` doesn't let the user access this functionality. It also appears post-hoc resampling severely affects wall clock time for the `builtin` opus decoder case (no such bad difference for `libopus` decoder)  

---

