How to record opus audio programatically?

I use C++ QAudioRecorder to record audio. I know that it’s possible to set the codec and container by making an instance of QAudioEncoderSettings, using its method setCodec and then applying it to an audio recorder with QAudioRecorder::setEncodingSettings. The list of supported encodings can be queried from Qt using the QAudioRecorder::supportedAudioCodecs method. On my device, it includes these codecs: “audio/vorbis”, “audio/speex”, “audio/PCM”, “audio/FLAC”.

I also read here that QtMultimedia uses GStreamer internally. So, I installed gstreamer1.0-tools package and tried running gst-inspect-1.0. It showed that I have the opusenc codec installed. To test it, I ran this command: gst-launch-1.0 pulsesrc ! audioconvert ! opusenc ! oggmux ! filesink location=output.opus. It worked without any issues.

But still, the list from supportedAudioCodecs doesn’t include the opus codec, even though it works fine using GStreamer command line tools. So, why can’t I use the opus codec from Qt, and how can I workaround this?

If you want specifically opus in ogg, instead of vorbis in ogg then you probably would have to somehow employ encoder or like, look into recording audio from pulseaudio, or you could employ gstreamer pipeline to do it

Thank you but that does not help. You showed me how to record vorbis-encoded audio, and I am asking why Qt can see and use flac, vorbis, and other encoders but not opus, even though it functions correctly using GStreamer command line tools.

1 Like

I’d say black magic and/or witches but i doubt that helps either sorry :smiley:

The obvious answer is that the gstreamer tools are more modern than the version of QAudioEncoderSettings we have with our ancient QT. That's just a guess, mind.

2 Likes

Supported codecs information seems to be hard-coded in Qt 5.6, here. That is why it doesn’t work and codec names are different from ones found using GStreamer tools. A better implementation is present in Qt 5.6 and can be found here. I am not sure why it is not used in the GStreamer plugin.