FFmpeg音频重采样

函数

1> swr_alloc:申请内存

2> swr_alloc_set_opts:设置参数

    @param SwrContext *s:音频重采样上下文

    @param int64_t out_ch_layout:输出的声道样式

    @param AVSampleFormat out_sample_fmt:输出的采样格式

    @param int out_sample_rate:输出采样率

    @param int64_t in_ch_layout:输入的声道样式

    @param AVSampleFormat in_sample_fmt:输入的采样格式

    @param int in_sample_rate:输入的采样率

    @param int log_offset, void *log_ctx:日志参数

3> swr_init:初始化

4> swr_convert:转换

    @param SwrContext *s:音频重采样上下文

    @param uint8_t **out, int out_count:输出的数据和单通道数据大小

    @param uint8_t **in, int in_count:输出的数据和单通道数据大小

5> swr_free:释放

示例程序

代码:

void transAudioSamples(AVFrame *frame)
{
    // 申请上下文空间
    SwrContext *ctx = swr_alloc();

    // 设置上下文空间参数
    swr_alloc_set_opts(ctx,
                       av_get_default_channel_layout(2), AV_SAMPLE_FMT_S16, frame->sample_rate,
                       av_get_default_channel_layout(frame->channels), (AVSampleFormat)frame->format, frame->sample_rate,
                       0, 0);

    // 初始化
    int ret = swr_init(ctx);
    if (0 != ret) {
        // 错误处理
        swr_free(&ctx);
        return;
    }

    uint8_t *pcm = new uint8_t[frame->nb_samples * 2 * frame->channels];
    uint8_t *dstData[2] = {0};
    dstData[0] = pcm;

    // 重采样
    ret = swr_convert(ctx, dstData, frame->nb_samples, (const uint8_t **)frame->data, frame->nb_samples);
    cout << "swr sample = " << ret << endl;

    // 释放上下文空间
    swr_free(&ctx);
}

输出:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperYang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值