I am very new with FFMpeg and I am currently trying to convert audio data from PCM AV_SAMPLE_FMT_S16 format to Mp3 AV_SAMPLE_FMT_FLTP format.
For this I am using the AudioResampleContext from FFMpeg
av_opt_set_int( audioResampleCtx, "in_sample_fmt", m_aplayer->aCodecCtx->sample_fmt, 0);
av_opt_set_int( audioResampleCtx, "in_sample_rate", m_aplayer->aCodecCtx->sample_rate, 0);
av_opt_set_int( audioResampleCtx, "in_channels", m_aplayer->aCodecCtx->channels,0);
av_opt_set_int( audioResampleCtx, "out_channel_layout", audioCodecCtx->channel_layout, 0);
av_opt_set_int( audioResampleCtx, "out_sample_fmt", audioCodecCtx->sample_fmt, 0);
av_opt_set_int( audioResampleCtx, "out_sample_rate", audioCodecCtx->sample_rate, 0);
av_opt_set_int( audioResampleCtx, "out_channels", audioCodecCtx->channels, 0);
The conversion works well since I can listen to my mp3 file but the problems is that my original file is 60 seconds long and the output mp3 file is just 34 seconds. I can hear that it is very accelerated just like if something speeded up the sound. When looking for information with FFMpeg I see that the bitrate just went from 128kbps to 64 kbps.
EDIT: To complete with more information, I want to compress some raw audio data with mp3 codec and have a output.mp3 output format. The raw audio data sample format is AV_SAMPLE_FMT_S16 and the supported sample format for mp3 codec is FLTP (or S16P). Therefore I am doing a sample format conversion from AV_SAMPLE_FMT_S16 to AV_SAMPLE_FMT_FLTP but it is missing half of the data.
Could anyone help me with this ? I know I'm missing something very simple but I just can't figure what.
EDIT:2 Here is the code that does the resampling (coming fromhttps://github.com/martin-steghoefer/debian-karlyriceditor/blob/master/src/ffmpegvideoencoder.cpp) . The audio source isn't an AVFrame but just an array of bytes :
// Resample the input into the audioSampleBuffer until we proceed the whole decoded data
if ( (err = avresample_convert( audioResampleCtx,
NULL,
0,
0,
audioFrame->data,
0,
audioFrame->nb_samples )) < 0 )
{
qWarning( "Error resampling decoded audio: %d", err );
return -1;
}
if( avresample_available( audioResampleCtx ) >= audioFrame->nb_samples )
{
// Read a frame audio data from the resample fifo
if ( avresample_read( audioResampleCtx, audioFrame->data, audioFrame->nb_samples ) != audioFrame->nb_samples )
{
qWarning( "Error reading resampled audio: %d", err );
return -1;
}
//Init packet, do the encoding and write data to file
Thank you for your help !