简体   繁体   中英

How to decode mp3 to pcm by ffmpeg

I need decode mp3 audio data to pcm. I have data which starts with mp3 header. Api-example.c doesn't work, output is strange:

在此处输入图片说明

command ffmpeg -i input.mp3 output.wav is great, this is what i need. But I cant find way how to do that in code. Does anybody know, where some tutorial with ffmpeg library is ? Thanks

Edit 2.7.13:

Hi again, I rebuilt the audio decode example method from ffmpeg and my problem is probably here:

len = avcodec_decode_audio4(avCodecContext,avFrame, &got_frame,&avPacket);    
int data_size = av_samples_get_buffer_size(NULL,avFrame->channels,avFrame->nb_samples,AV_SAMPLE_FMT_S16P,1);

data_size is size of data frame from decoder, it depends on number of channels, number of data samples and data type(my data are 16bit PCM stereo encoded to mp3 to 1152 samples of mp3 frame)

If I open an output file in audacity, correct parameters, which give correct output, are stereo (right), 8bit pcm (wrong) and half sample rate (also wrong), what's it happened?

data before encoding: 16bit PCM 44100Hz, stereo

data after decoding: 8bit PCM 22050Hz, stereo ---> ???!!!

I'm tired of this....

One way to do this is to call the executable in the code:

system("ffmpeg -i input.mp3 output.wav"); //assuming the executable name is ffmpeg and its location is in PATH environment variable

Another way is to use the API. Example here , see the audio_decode_example() function in that file.

Some tutorials:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM