-1

I am working on a small application in cpp that will convert audio, apply filters and generate waveform using ffmpeg. I am using ffmpeg as a library and not using ffmpeg binary.

My reference was from here. https://ffmpeg.org/doxygen/4.0/transcode_aac_8c-example.html

I have commented out the portion where they exit the method throwing the below exception.

Expected one audio input stream, but found 2

I am using the example of ffmpeg for transcoding. However, I get an error when trying to transcode an audio file containing more than 1 stream. Usually the audio will be an mp3 audio with an image file as the second stream. The error I get is

[mp3 @ 0x1d487a0] Header missing
Could not decode frame (error 'Invalid data found when processing input')

However, when I open the audio file in audacity and export without the image file it works. What changes are required to handle more than 1 stream?

llogan
  • 121,796
  • 28
  • 232
  • 243
Andrews
  • 895
  • 3
  • 15
  • 30
  • How is this a duplicate of https://stackoverflow.com/questions/9725273/decode-audio-stream-channels-to-multiple-wavs-using-ffmpeg. The other question doesn't talk about code but about ffmpeg binary. It talks about channel and this one is about streams (tracks). Absolutely ridiculous marking of duplicate. – Andrews Sep 15 '18 at 11:21
  • Everything available as command for the binary is also available from the programming API. It shouldn't be that hard to figure out how to do this via the API. – πάντα ῥεῖ Sep 15 '18 at 11:24
  • Please don't make stackoverflow such a place. Why do you think they provide examples if they have APIs for all the binary executable stuff. What are you talking about? Please show me the link that talks about this in API, I will sincerely apologise. I am talking about an exception I am getting and you are asking me to refer to a question that talks about a binary file argument. Also, if you do not know the difference between Audio Stream and Audio Channel with regards to ffmpeg please stay away. – Andrews Sep 15 '18 at 11:32
  • 1
    Well, you might start to post a [MCVE] of what you've done, such that everyone can inspect your code. If not a dupe, your question appears _too broad_ as is. – πάντα ῥεῖ Sep 15 '18 at 11:34
  • Well if you feel its too broad, it does make some sense. I will try my best to show the relevant piece of code if possible. The crux of my code is based on the example provided in the link. The only other way for me is to upload my code in github and post here. I do not want to post the code in SO from the link because I am not sure of any licensing implications. – Andrews Sep 15 '18 at 11:44

1 Answers1

0

I found the source of the issue. Please refer this link for example. https://ffmpeg.org/doxygen/4.0/transcode_aac_8c-example.html

After more debugging, it seems like decode_audio_frame has to be modified to ignore or skip non audio streams. I am skipping them as below. Instead of directly calling avcodec_decode_audio4 the method, I am verifying the stream index of the packet and ignoring as necessary. audio_index is a field declared globally, that will be set when opening the input file.

int strm_index = input_packet.stream_index;
    av_log(NULL, AV_LOG_INFO, "Stream index %i\n",
                           strm_index);
    if(strm_index!=audio_index){
        *data_present=0;
    }else{
        if ((error = avcodec_decode_audio4(input_codec_context, frame,
                                               data_present, &input_packet)) < 0) {
                av_log(NULL, AV_LOG_ERROR, "Could not decode frame (error '%s')\n",
                        av_err2str(error));
                av_packet_unref(&input_packet);
                return error;
        }
    }
Andrews
  • 895
  • 3
  • 15
  • 30