3

I am trying to decode opus to pcm file using libavcodec.So,I use ffmpeg example from https://ffmpeg.org/doxygen/trunk/decode_audio_8c-example.html .And I change AV_CODEC_ID_MP2 to AV_CODEC_ID_OPUS.But I get an error.

codec = avcodec_find_decoder((AV_CODEC_ID_MP2);
codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);

error:

    codec ./decode_audio ./out.opus ./out.pcm                  
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.

So I try to change opus AV_CODEC_ID_OPUS to AV_CODEC_ID_MP3 and try again.

    codec ./decode_audio ./out.mp3 ./out.pcm            
    [mp3float @ 0x7fe564002000] Header missing
    Error submitting the packet to the decoder

Why does the example from ffmpeg get error?What should I do ?

hello
  • 35
  • 6
  • Are you sure your input file isn't corrupted in any way? Have you tried other opus files? Perhaps you can find a download of an opus file known to be "good" (download and check hash to make sure no corruption). – mittmemo Jun 02 '20 at 16:38
  • I use ffmpeg command to get my opus file and mp3 file. `ffmpeg -f avfoundation -i :0 out.opus`. And I use ffplay command to play it. And use ffmpeg command to decode it.`ffmpeg -i test.opus -acodec pcm_s16le -f s16le -ac 1 -ar 16000 ./out.pcm`.And play it.`ffplay -ar 16000 -ac 1 -f s16le -i ./out.pcm` .So,I am not sure whether I get an "bad" file.My application can get a opus file but the Third API should put into the pcm file.How can I solve it? – hello Jun 03 '20 at 01:54

1 Answers1

3

You can't decode opus this way. Mp3 packets are self-delimiting, opus is not. Which means that opus requires a container (usually ogg). That container must be parsed to determine the start and end of an opus packet that you can then decode. libavformat can be used to read AVPackets from the file.

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • So the example does not reach my goal?And libavformat can help me finish my goal.Or may I transcode opus to mp3 ? And decode mp3 to pcm file using the example. Beacuse I am a novice in this field.I need some examples to learn but I just find a little. – hello Jun 03 '20 at 02:10
  • Sorry, But stackoverflow is not the place to ask for example code, but ffmpeg includes plenty of it. Yes, you can convert opus to mp3, then decode with that code if you wish. If you wish to move forward without that extra step, you first need to research the difference between codecs, containers and elementary streams. – szatmary Jun 03 '20 at 05:00
  • Sorry for my request.But in this ffmpeg example ,do not decode successfully.` ./decode_audio ./out.mp3 ./out.pcm [mp3float @ 0x7fe564002000] Header missing Error submitting the packet to the decoder` – hello Jun 03 '20 at 06:27
  • New questions should get a new post. – szatmary Jun 03 '20 at 07:25
  • In my original question, this question also is mentioned.So, do I need to put a new post? – hello Jun 03 '20 at 07:36