FFmpeg音频格式重采样

最近做音频录制,有些PCM会录制失败,仔细查看后发现的format格式不对,由于FFmpeg已经废弃了AV_SAMPLE_FMT_S16格式PCM编码AAC,也就是说如果使用FFmpeg自带的AAC编码器,必须做音频的重采样转换为AV_SAMPLE_FMT_FLTP格式,否则AAC编码是失败的。

FFmpeg 库中支持的音频采样格式如下:

enum AVSampleFormat {
    AV_SAMPLE_FMT_NONE = -1,
    AV_SAMPLE_FMT_U8,          ///< 无符号 8 位整数
    AV_SAMPLE_FMT_S16,         ///< 有符号 16 位整数
    AV_SAMPLE_FMT_S32,         ///< 有符号 32 位整数
    AV_SAMPLE_FMT_FLT,         ///< 浮点数
    AV_SAMPLE_FMT_DBL,         ///< 双精度浮点数
    AV_SAMPLE_FMT_U8P,         ///< 无符号 8 位整数平面格式
    AV_SAMPLE_FMT_S16P,        ///< 有符号 16 位整数平面格式
    AV_SAMPLE_FMT_S32P,        ///< 有符号 32 位整数平面格式
    AV_SAMPLE_FMT_FLTP,        ///< 浮点数平面格式
    AV_SAMPLE_FMT_DBLP,        ///< 双精度浮点数平面格式
    AV_SAMPLE_FMT_S64,         ///< 有符号 64 位整数
    AV_SAMPLE_FMT_S64P,        ///< 有符号 64 位整数平面格式
    AV_SAMPLE_FMT_NB           ///< 样本格式数量
};	

1、使用swr_convert重采样

在 FFmpeg 中,可以使用 swr_alloc_set_opts() 函数创建一个 SwrContext 对象,然后使用 swr_convert() 函数将输入音频帧转换为指定格式的音频帧。

// 创建 SwrContext 对象
SwrContext *swr_ctx = swr_alloc_set_opts(NULL,
                                         av_get_default_channel_layout(output_channels),
                                         AV_SAMPLE_FMT_FLTP,
                                         output_sample_rate,
                                         av_get_default_channel_layout(input_channels),
                                         input_sample_fmt,
                                         input_sample_rate,
                                         0,
                                         NULL);

// 初始化 SwrContext 对象
swr_init(swr_ctx);

// 分配输出音频帧
AVFrame *output_frame = av_frame_alloc();
output_frame->nb_samples = input_frame->nb_samples;
output_frame->format = AV_SAMPLE_FMT_FLTP;
output_frame->channel_layout = av_get_default_channel_layout(output_channels);
av_frame_get_buffer(output_frame, 0);

// 转换音频帧格式
swr_convert(swr_ctx,
            output_frame->data,
            output_frame->nb_samples,
            (const uint8_t **)input_frame->data,
            input_frame->nb_samples);

// 释放 SwrContext 对象
swr_free(&swr_ctx);

2、使用av_audio_convert() 函数将一个音频帧从一种格式转换为另一种格式

// 分配输出音频帧
AVFrame *output_frame = av_frame_alloc();
output_frame->format = AV_SAMPLE_FMT_FLTP;
output_frame->sample_rate = output_sample_rate;
output_frame->channel_layout = av_get_default_channel_layout(output_channels);
output_frame->nb_samples = input_frame->nb_samples;
av_frame_get_buffer(output_frame, 0);

// 进行音频帧格式转换
av_audio_convert(output_frame->data, input_frame->data,
                 output_frame->nb_samples, input_frame->nb_samples,
                 input_frame->format, AV_SAMPLE_FMT_FLTP, 0);

3、使用filter graph过滤器,可以将多个 filter 组合起来,实现更复杂的音视频处理

// 创建 filter graph
AVFilterGraph *filter_graph = avfilter_graph_alloc();
AVFilterContext *src_ctx, *sink_ctx;
AVFilter *src_filter = avfilter_get_by_name("abuffer");
AVFilter *sink_filter = avfilter_get_by_name("abuffersink");
char args[512];
snprintf(args, sizeof(args),
         "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
         input_time_base.num, input_time_base.den,
         input_sample_rate, av_get_sample_fmt_name(input_sample_fmt),
         input_channel_layout);
avfilter_graph_create_filter(&src_ctx, src_filter, "src", args, NULL, filter_graph);
avfilter_graph_create_filter(&sink_ctx, sink_filter, "sink", NULL, NULL, filter_graph);
av_opt_set_int_list(sink_ctx, "sample_fmts", (int[]){ AV_SAMPLE_FMT_FLTP, -1 }, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
av_opt_set_int(sink_ctx, "sample_rates", output_sample_rate, AV_OPT_SEARCH_CHILDREN);
av_opt_set_bin(sink_ctx, "channel_layouts", (uint8_t*)&output_channel_layout, sizeof(output_channel_layout), AV_OPT_SEARCH_CHILDREN);

// 连接 filter
avfilter_link(src_ctx, 0, sink_ctx, 0);
avfilter_graph_config(filter_graph, NULL);

// 向 filter 输入音频帧
AVFrame *frame = av_frame_alloc();
frame->format = input_sample_fmt;
frame->sample_rate = input_sample_rate;
frame->channel_layout = input_channel_layout;
frame->nb_samples = input_nb_samples;
av_frame_get_buffer(frame, 0);
memcpy(frame->data[0], input_samples, input_samples_size);
av_buffersrc_add_frame_flags(src_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF);

// 从 filter 获取输出音频帧
AVFrame *output_frame = av_frame_alloc();
while (av_buffersink_get_frame(sink_ctx, output_frame) >= 0) {
    // 处理输出音频帧
    //…
    av_frame_unref(output_frame);
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值