[Libav-user] How to get raw audio frames from an audio file

Gonzalo Garramuño ggarra13 at gmail.com
Tue Oct 23 20:32:32 EEST 2018


El 23/10/18 a las 12:59, Matthieu Regnauld escribió:
> Thank you for your help.
>
> That said, I'm new to FFMpeg, and I still struggle.
> Could you tell me what I have to fix in my code to have a clear sound 
> and also, if possible, how to resample it (from 44100 Hz to 48000 Hz, 
> for example)?
>
> Here is my code: 
> https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a
>
> Thank you.

First, you need to set swrContext to NULL at the beginning, like:

AVFrame *frame = NULL; SwrContext *swrContext = NULL;

Then, here you should set the output frequency:

int out_sample_rate = 48000;
swr_alloc_set_opts(swrContext, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_FLT, 
out_sample_rate,
                    codecContext->channel_layout, 
codecContext->sample_fmt, codecContext->sample_rate, 0,
                    NULL);

Then, you should not need to memcpy anything as that's what swr_convert 
should do for you.  However, you might want to set your buffer bigger 
than two channels as avcodec_receive_frame might return multiple frames 
of sound.  By doing that, you won't have to worry about overrunning the 
buffer.  Also, you might want to use extended_data, for formats that 
have more than 4 channels.

// Somewhere else
localBuffer = av_malloc( sizeof(float) * out_sample_rate * nb_samples + 
padding);

//

swr_convert(swrContext, (uint8_t**)&localBuffer, frame->nb_samples, 
(const uint8_t **) frame->extended_data, frame->nb_samples);

-- 
Gonzalo Garramuño

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20181023/c42aa17f/attachment.html>


More information about the Libav-user mailing list