Converting PCM-ALAW to OPUS using FFMPEG API

0

In my project, I receive RTP packets with a payload of type 8. I extract this load from the RTP packet and save it to a temporary buffer. After filling a temporary buffer of a certain size with a payload, I process this buffer, namely, fill the audio file with it. To do this, I use the API - ffmpeg. But I have a problem. Since the payload of the RTP packet is type 8, I have to convert it from PCM-ALAW to OPUS. I do not know how to do this ?

Why did I decide to convert PCM-ALAW to OPUS ?

When trying to do this using ffmpeg itself:


ffmpeg -f alaw -ar 8000 -ac 1 -i rawData -ar 8000 -ac 1 test.mka

I got these messages:

[alaw @ 0x562c23829fc0] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, alaw, from 'rawData':
  Duration: 00:00:19.08, bitrate: 64 kb/s
    Stream #0:0: Audio: pcm_alaw, 8000 Hz, mono, s16, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_alaw (native) -> opus (libopus))
Press [q] to stop, [?] for help
[libopus @ 0x562c2384a800] No bit rate set. Defaulting to 64000 bps.
Output #0, matroska, to 'test.mka':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Audio: opus (libopus) ([255][255][255][255] / 0xFFFFFFFF), 8000 Hz, mono, s16, 64 kb/s
    Metadata:
      encoder         : Lavc57.107.100 libopus
size=     176kB time=00:00:19.09 bitrate=  75.3kbits/s speed= 342x    
video:0kB audio:168kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 4.347094%

I am most interested in these lines:

Stream mapping:
  Stream #0:0 -> #0:0 (pcm_alaw (native) -> opus (libopus))

When selecting the AVCodecID = AV_CODEC_ID_PCM_ALAW:

m_AudioCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW);
if (m_AudioCodec == nullptr) {
    return;
}

I get a corrupted .mka audio file.

How do I make this stream mapping ? And most importantly, I have to save the audio data in mka format.

PS : As an example, you can look at this code.

c++
ffmpeg
encode
asked on Stack Overflow Sep 9, 2020 by bbdd • edited Sep 9, 2020 by eyllanesc

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0