[ffmpeg]音频格式转换

devtools/2024/9/24 23:59:04/

本文主要梳理 ffmpeg 中的音频格式转换。由于采集的音频数据和编码器支持的音频格式可能不一样,所以经常需要进行格式转换。

API 调用

常用 API

struct SwrContext *swr_alloc(void);
int swr_init(struct SwrContext *s);
struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,int log_offset, void *log_ctx);
void swr_free(struct SwrContext **s);
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,const uint8_t **in , int in_count);
int swr_convert_frame(SwrContext *swr,AVFrame *output, const AVFrame *input);

初始化和销毁相关

swr_alloc 创建 context 上下文结构体
swr_init 初始化 context 结构体
swr_free 销毁结构体


SwsContext class 定义 libswresample\options.c

static const AVClass av_class = {.class_name                = "SWResampler",.item_name                 = context_to_name,.option                    = options,.version                   = LIBAVUTIL_VERSION_INT,.log_level_offset_offset   = OFFSET(log_level_offset),.parent_log_context_offset = OFFSET(log_ctx),.category                  = AV_CLASS_CATEGORY_SWRESAMPLER,
};

swr_init 之前需要配置 context 一些参数,才能正确初始化。

if (av_opt_set_int(s, "ocl", out_ch_layout,   0) < 0)goto fail;if (av_opt_set_int(s, "osf", out_sample_fmt,  0) < 0)goto fail;if (av_opt_set_int(s, "osr", out_sample_rate, 0) < 0)goto fail;if (av_opt_set_int(s, "icl", in_ch_layout,    0) < 0)goto fail;if (av_opt_set_int(s, "isf", in_sample_fmt,   0) < 0)goto fail;if (av_opt_set_int(s, "isr", in_sample_rate,  0) < 0)goto fail;if (av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> user_in_ch_layout), 0) < 0)goto fail;if (av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->user_out_ch_layout), 0) < 0)goto fail;

为了简化调用所以有了 swr_alloc_set_opts接口,其主要就是做了 1. swr_alloc调用;2.参数设置。
由于其不像视频转换的 sws_getContext 接口,内部不会调用 swr_init,所以还需要调用一下初始化接口。

类型转换

swr_convert
swr_convert_frame

输出结果是直接写在输入的内存上的,所以 data 需要提前分配好内存

demo 调用

m_asc = swr_alloc_set_opts(m_asc,m_ac->channel_layout, m_ac->sample_fmt, m_ac->sample_rate,av_get_default_channel_layout(m_inChannels), (AVSampleFormat)m_inSampleFmt, m_inSampleRate,0, 0);
ret = swr_init(m_asc);
const uint8_t* data[1];
data[0] = (uint8_t*)pcm;
int len = swr_convert(m_asc, m_pcm->data, m_pcm->nb_samples,data, m_pcm->nb_samples);
if (m_asc)
{swr_free(&m_asc);
}

其他

所有接口

const AVClass *swr_get_class(void);
struct SwrContext *swr_alloc(void);
int swr_init(struct SwrContext *s);
int swr_is_initialized(struct SwrContext *s);
struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,int log_offset, void *log_ctx);
void swr_free(struct SwrContext **s);
void swr_close(struct SwrContext *s);
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,const uint8_t **in , int in_count);
int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
int swr_build_matrix(uint64_t in_layout, uint64_t out_layout,double center_mix_level, double surround_mix_level,double lfe_mix_level, double rematrix_maxval,double rematrix_volume, double *matrix,int stride, enum AVMatrixEncoding matrix_encoding,void *log_ctx);
int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
int swr_drop_output(struct SwrContext *s, int count);
int swr_inject_silence(struct SwrContext *s, int count);
int64_t swr_get_delay(struct SwrContext *s, int64_t base);
int swr_get_out_samples(struct SwrContext *s, int in_samples);
unsigned swresample_version(void);
const char *swresample_configuration(void);
const char *swresample_license(void);
int swr_convert_frame(SwrContext *swr,AVFrame *output, const AVFrame *input);
int swr_config_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in);

http://www.ppmy.cn/devtools/116719.html

相关文章

yolo自动化项目实例解析(五)ui页面整理2 (1.85)

三、添加主窗口及其他窗口ui文件 窗口ui目录&#xff0c;下面操作都在ui目录中 1 、主窗口 main.ui <?xml version"1.0" encoding"UTF-8"?> <ui version"4.0"><class>mainWindow</class><widget class"QMa…

财富通公司开发维修售后小程序,解决售后维修问题

财富通公司为广大用户开发的维修售后小程序&#xff0c;旨在便捷地解决售后维修问题&#xff0c;提升用户体验&#xff0c;增强客户粘性。以下是该小程序如何具体解决售后维修问题的几个关键点&#xff1a; 一. 一站式报修流程 1.简化操作&#xff1a;用户只需通过小程序几步…

开源 AI 智能名片链动 2+1 模式 S2B2C 商城小程序与社交电商的崛起

摘要&#xff1a;本文深入探讨了社交电商迅速发展壮大的原因&#xff0c;并分析了开源 AI 智能名片链动 21 模式 S2B2C 商城小程序在社交电商中的重要作用。通过对传统电商与社交电商的对比&#xff0c;以及对各发展因素的剖析&#xff0c;阐述了该小程序如何为社交电商提供新的…

CVE-2024-46103

前言 CVE-2024-46103 SEMCMS的sql漏洞。 漏洞简介 SEMCMS v4.8中&#xff0c;SEMCMS_Images.php的search参数&#xff0c;以及SEMCMS_Products.php的search参数&#xff0c;存在sql注入漏洞。 &#xff08;这个之前就有两个sql的cve&#xff0c;这次属于是捡漏了&#x1f6…

算法打卡:第十一章 图论part05

今日收获&#xff1a;并查集理论基础&#xff0c;寻找存在的路径 1. 并查集理论基础&#xff08;from代码随想录&#xff09; &#xff08;1&#xff09;应用场景&#xff1a;判断两个元素是否在同一个集合中 &#xff08;2&#xff09;原理讲解&#xff1a;通过一个一维数组…

Spring Boot文件上传/下载问题

在现代的 Web 应用程序中&#xff0c;文件上传和下载是非常常见的功能。Spring Boot 提供了非常简便和强大的解决方案来处理文件上传和下载。虽然基本功能很容易实现&#xff0c;但在实际项目中&#xff0c;文件大小限制、并发上传、文件类型验证、安全性等问题也常常需要重点考…

20240924给荣品RD-RK3588-AHD开发板刷Rockchip原厂的Buildroot

20240924给荣品RD-RK3588-AHD开发板刷Rockchip原厂的Buildroot 2024/9/24 16:38 1、默认选择RK3588S编译&#xff1a;【EVB1】 viewproviewpro-ThinkBook-16-G5-IRH:~/rk3588s_20230620$ viewproviewpro-ThinkBook-16-G5-IRH:~/rk3588s_20230620$ ./build.sh lunch 4. rockchip…

Springboot常见问题(bean找不到)

如图错误显示userMapper bean没有找到。 解决方案&#xff1a; mapper包位置有问题&#xff1a;因为SpringBoot默认的包扫描机制会扫描启动类所在的包同级文件和子包下的文件。注解问题&#xff1a; 比如没有加mapper注解 然而无论是UserMapper所在的包位置还是Mapper注解都是…