本文用libswresample转换原始音频格式。程序输入一个原始音频文件:pcm格式,采样率48000,2声道,AV_SAMPLE_FMT_FLT格式。输出:pcm格式,采样率44100,1声道,AV_SAMPLE_FMT_S16P格式。
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
#define INPUT_SAMPLERATE 48000
#define INPUT_FORMAT AV_SAMPLE_FMT_FLT
#define INPUT_CHANNEL_LAYOUT AV_CH_LAYOUT_STEREO
#define OUTPUT_SAMPLERATE 44100
#define OUTPUT_FORMAT AV_SAMPLE_FMT_S16P
#define OUTPUT_CHANNEL_LAYOUT AV_CH_LAYOUT_MONO
static void filter_audio()
{
int64_t src_ch_layout = INPUT_CHANNEL_LAYOUT, dst_ch_layout = OUTPUT_CHANNEL_LAYOUT;
int src_rate = INPUT_SAMPLERATE, dst_rate = OUTPUT_SAMPLERATE;
uint8_t **src_data = NULL, **dst_data = NULL;
int src_nb_channels = 0, dst_nb_channels = 0;
int src_linesize, dst_linesize;
int sr