文章目录

  • 采样位数
  • 采样率
  • 声道布局
  • 码率
  • 使用FFmpeg音频滤镜进行转码
  • 参考链接

很多时候为了让视频文件适应不同的播放领域,我们需要对音频文件进行转码操作,转码操作其实主要就是修改音频文件的各种参数包括:采样位数、采样率、音频布局、码率等等。下面分别介绍一下各个参数的意义和作用。

采样位数

采样位数也称为位深度、分辨率, 它是指声音的连续强度被数字表示后可以分为多少级。N-bit的意思声音的强度被均分为2^N级。16位的就是65535级。这是一个很大的数了,人可能也分辨不出1/65536的音强差别。也可以说是声卡的分辨率,它的数值越大,分辨率也就越高,所发出声音的能力越强。这里的采样倍数主要针对的是信号的强度特性,采样率针对的是信号的时间(频率)特性这是两个不一样的概念。

ffmpeg常用的采样位数对应的格式如下所示:

enum AVSampleFormat {AV_SAMPLE_FMT_NONE = -1,AV_SAMPLE_FMT_U8,          ///< unsigned 8 bitsAV_SAMPLE_FMT_S16,         ///< signed 16 bitsAV_SAMPLE_FMT_S32,         ///< signed 32 bitsAV_SAMPLE_FMT_FLT,         ///< floatAV_SAMPLE_FMT_DBL,         ///< doubleAV_SAMPLE_FMT_U8P,         ///< unsigned 8 bits, planarAV_SAMPLE_FMT_S16P,        ///< signed 16 bits, planarAV_SAMPLE_FMT_S32P,        ///< signed 32 bits, planarAV_SAMPLE_FMT_FLTP,        ///< float, planarAV_SAMPLE_FMT_DBLP,        ///< double, planarAV_SAMPLE_FMT_S64,         ///< signed 64 bitsAV_SAMPLE_FMT_S64P,        ///< signed 64 bits, planarAV_SAMPLE_FMT_NB           ///< Number of sample formats. DO NOT USE if linking dynamically
};

采样率

音频采样,是把声音从模拟信号转换为数字信号。采样率,就是每秒对声音进行采集的次数,同样也是所得的数字信号的每秒样本数。在对声音进行采样时,常用的采样率有:
8,000 Hz - 电话所用采样率, 对于人的说话已经足够
11,025 Hz - AM调幅广播所用采样率
22,050 Hz~24,000 Hz - FM调频广播所用采样率
32,000 Hz - miniDV 数码视频 camcorder、DAT (LP mode)所用采样率
44,100 Hz - 音频 CD, 也常用于 MPEG-1 音频(VCD, SVCD, MP3)所用采样率
47,250 Hz - 商用PCM录音机所用采样率
48,000 Hz - miniDV、数字电视、DVD、DAT、电影和专业音频所用的数字声音所用采样率
50,000 Hz - 商用数字录音机所用采样率
96,000 或者192,000 Hz - DVD-Audio、一些 LPCM DVD 音轨、BD-ROM(蓝光盘)音轨、和 HD-DVD (高清晰度 DVD)音轨所用所用采样率
2.8224 MHz - Direct Stream Digital 的 1 位 sigma-delta modulation 过程所用采样率。

采样越高,声音的还原就越真实越自然,人对频率的识别范围是20HZ - 20000HZ, 如果每秒钟能对声音做 20000 个采样, 回放时就足可以满足人耳的需求.所以 22050 的采样频率是常用的, 44100已是CD音质, 超过48000的采样对人耳已经没有意义。这和电影的每秒 24 帧图片的道理差不多。

声道布局

当人听到声音时,能对声源进行定位,那么通过在不同的位置设置声源,就可以造就出更好的听觉感受。常见的声道有:

  1. 单声道, mono
  2. 双声道, stereo, 最常见的类型,包含左声道以及右声道
  3. 2.1声道,在双声道基础上加入一个低音声道
  4. 5.1声道,包含一个正面声道、左前方声道、右前方声道、左环绕声道、右环绕声道、一个低音声道,最早应用于早期的电影院
  5. 7.1声道,在5.1声道的基础上,把左右的环绕声道拆分为左右环绕声道以及左右后置声道,主要应用于BD以及现代的电影院

码率

码率也就是每秒的传输速率(也叫比特率),压缩的音频文件常用倍速来表示,比如达到CD音质的MP3是128kbps/44100HZ。注意这里的单位是bit而不是Byte,一个Byte等于8个bit(位),bit是最小的单位,一般用于网络速度的描述和各种通信速度,Byte则用于计算硬盘,内存的大小。

使用FFmpeg音频滤镜进行转码

不同领域对音频的播放要求是不一样的,所以需要针对不同的领域对音频参数进行调整,这里介绍一下如何通过音频滤镜调整音频数据的相关参数,对应的实现如下:

#include "../audio_filter.h"extern "C"
{#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavfilter/avfilter.h>
#include <libswresample/swresample.h>
}#include <string>/**@brief 转换音频数据的格式
* @param[in]  output_filename 输出文件名称
* @param[in]  input_filename 输入文件名称
* @param[in]  sample_fmt 采样格式
* @param[in]  sample_rate 采样率
* @param[in]  channel_layout 通道布局
* @param[in]  bitrate 码率
* @return  函数执行结果
* - 0     成功
* - 其它  失败
*/
int transcode_audio(const char *output_filename, const char *input_filename, AVSampleFormat sample_fmt,int sample_rate, uint64_t channel_layout, uint64_t bitrate)
{//输入输出格式AVFormatContext *inFmtCtx = nullptr;AVFormatContext *outFmtCtx = nullptr;//解码器和编码器AVCodecContext *aDecCtx = nullptr;AVCodecContext *aEncCtx = nullptr;//输出流AVStream *aOutStream = nullptr;int ret;// open input fileret = avformat_open_input(&inFmtCtx, input_filename, nullptr, nullptr);ret = avformat_find_stream_info(inFmtCtx, nullptr);// open output fileavformat_alloc_output_context2(&outFmtCtx, nullptr, nullptr, output_filename);for (int i = 0; i < inFmtCtx->nb_streams; ++i){AVStream *inStream = inFmtCtx->streams[i];if (inStream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO){//输入流的解码器AVCodec *decoder = avcodec_find_decoder(inStream->codecpar->codec_id);aDecCtx = avcodec_alloc_context3(decoder);ret = avcodec_parameters_to_context(aDecCtx, inStream->codecpar);ret = avcodec_open2(aDecCtx, decoder, nullptr);//输出流的编码器AVCodec *encoder = avcodec_find_encoder(outFmtCtx->oformat->audio_codec);aOutStream = avformat_new_stream(outFmtCtx, encoder);aOutStream->id = outFmtCtx->nb_streams - 1;aEncCtx = avcodec_alloc_context3(encoder);aEncCtx->codec_id = encoder->id;aEncCtx->sample_fmt = sample_fmt ? sample_fmt : aDecCtx->sample_fmt;aEncCtx->sample_rate = sample_rate ? sample_rate : aDecCtx->sample_rate;aEncCtx->channel_layout = channel_layout;aEncCtx->channels = av_get_channel_layout_nb_channels(channel_layout);aEncCtx->bit_rate = bitrate ? bitrate : aDecCtx->bit_rate;aEncCtx->time_base = { 1, aEncCtx->sample_rate };aOutStream->time_base = aEncCtx->time_base;if (outFmtCtx->oformat->flags & AVFMT_GLOBALHEADER)aEncCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;avcodec_open2(aEncCtx, encoder, nullptr);ret = avcodec_parameters_from_context(aOutStream->codecpar, aEncCtx);av_dict_copy(&aOutStream->metadata, inStream->metadata, 0);break;}}if (!(outFmtCtx->oformat->flags & AVFMT_NOFILE)) {ret = avio_open(&outFmtCtx->pb, output_filename, AVIO_FLAG_WRITE);if (ret < 0) {return -1;}}ret = avformat_write_header(outFmtCtx, nullptr);if (ret < 0) {return -1;}AVFrame *inAudioFrame = av_frame_alloc();AVFrame *outAudioFrame = av_frame_alloc();outAudioFrame->format = aEncCtx->sample_fmt;outAudioFrame->sample_rate = aEncCtx->sample_rate;outAudioFrame->channel_layout = aEncCtx->channel_layout;outAudioFrame->nb_samples = aEncCtx->frame_size;ret = av_frame_get_buffer(outAudioFrame, 0);int64_t audio_pts = 0;//修改音频数据包格式的滤镜AudioFilter filter;char description[512];AudioConfig inConfig(aDecCtx->sample_fmt, aDecCtx->sample_rate, aDecCtx->channel_layout, aDecCtx->time_base);AudioConfig outConfig(aEncCtx->sample_fmt, aEncCtx->sample_rate, aEncCtx->channel_layout, aEncCtx->time_base);char ch_layout[64];av_get_channel_layout_string(ch_layout, sizeof(ch_layout),av_get_channel_layout_nb_channels(aEncCtx->channel_layout), aEncCtx->channel_layout);snprintf(description, sizeof(description),"[in]aresample=sample_rate=%d[res];[res]aformat=sample_fmts=%s:sample_rates=%d:channel_layouts=%s[out]",aEncCtx->sample_rate,av_get_sample_fmt_name(aEncCtx->sample_fmt),aEncCtx->sample_rate,ch_layout);filter.create(description, &inConfig, &outConfig);filter.dumpGraph();while (true) {//解析音频帧并通过滤镜进行处理AVPacket inPacket{ nullptr };av_init_packet(&inPacket);ret = av_read_frame(inFmtCtx, &inPacket);if (ret == AVERROR_EOF) {break;}else if (ret < 0) {return -1;}if (inPacket.stream_index == aOutStream->index) {ret = avcodec_send_packet(aDecCtx, &inPacket);if (ret != 0) {printf("send packet error\n");}ret = avcodec_receive_frame(aDecCtx, inAudioFrame);if (ret == 0) {ret = filter.addInput1(inAudioFrame);av_frame_unref(inAudioFrame);if (ret < 0){printf("add filter input1 error\n");}do {outAudioFrame->nb_samples = aEncCtx->frame_size;ret = filter.getFrame(outAudioFrame);if (ret == 0) {outAudioFrame->pts = audio_pts;audio_pts += outAudioFrame->nb_samples;ret = avcodec_send_frame(aEncCtx, outAudioFrame);if (ret < 0) {printf("unable to send frame: %s\n");}}else {printf("unable to get filter audio frame: %s\n");break;}do {AVPacket outPacket{ nullptr };av_init_packet(&outPacket);ret = avcodec_receive_packet(aEncCtx, &outPacket);if (ret == 0) {av_packet_rescale_ts(&outPacket, aEncCtx->time_base, aOutStream->time_base);outPacket.stream_index = aOutStream->index;ret = av_interleaved_write_frame(outFmtCtx, &outPacket);if (ret < 0) {printf("unable to write packet\n");break;}}else {printf("unable to receive packet\n");break;}} while (true);} while (true);}else {printf("unable to receive frame\n");}}}//清理数据缓存int eof = 0;do {ret = filter.getFrame(outAudioFrame);if (ret == 0) {outAudioFrame->pts = audio_pts;audio_pts += outAudioFrame->nb_samples;}else {printf("filter queue finished\n");}ret = avcodec_send_frame(aEncCtx, ret == 0 ? outAudioFrame : nullptr);do {AVPacket outPacket{ nullptr };ret = avcodec_receive_packet(aEncCtx, &outPacket);if (ret == 0) {av_packet_rescale_ts(&outPacket, aEncCtx->time_base, aOutStream->time_base);outPacket.stream_index = aOutStream->index;ret = av_interleaved_write_frame(outFmtCtx, &outPacket);if (ret < 0) {eof = 1;break;}}else if (ret == AVERROR_EOF) {eof = 1;break;}else {break;}} while (true);} while (!eof);//释放对应的资源filter.destroy();av_write_trailer(outFmtCtx);avformat_close_input(&inFmtCtx);av_frame_free(&inAudioFrame);av_frame_free(&outAudioFrame);avcodec_free_context(&aDecCtx);avcodec_free_context(&aEncCtx);avformat_free_context(inFmtCtx);avformat_free_context(outFmtCtx);return 0;
}

这里我们将音频文件的采样格式修改为AV_SAMPLE_FMT_FLTP,同时我们将采样率降低为22050,码率修改为80kbps。

int main(int argc, char* argv[])
{if (argc != 3){printf("usage:%1 input filepath %2 outputfilepath");return -1;}//输入文件地址、输出文件地址std::string fileInput = std::string(argv[1]);std::string  fileOutput = std::string(argv[2]);transcode_audio(fileOutput.c_str(), fileInput.c_str(),(AVSampleFormat)AV_SAMPLE_FMT_FLTP,22050, AV_CH_LAYOUT_STEREO,80000);
}

参考链接

参考链接:https://www.cnblogs.com/yongdaimi/p/10722355.html

FFmpeg进阶: 采用音频滤镜对音频进行转码相关推荐

  1. FFmpeg进阶:音视频滤镜概述

    文章目录 滤镜示例 滤镜的格式 滤镜语法规则 滤镜图 基于时间的滤镜控制 滤镜API介绍 常用滤镜 FFmpeg的滤镜模块(libavfilter)提供了各种各样的滤镜特效.通过滤镜模块,我们可以实现 ...

  2. ffmpeg音频滤镜

    音频滤镜 分离声道 1 转码(源文件没问题可以省略) ffmpeg -i jy.ts  -vcodec h264  -acodec aac jy1.ts 2 取一个声道 ffmpeg -i jy1.t ...

  3. FFmpeg进阶: 转码输出视频文件中的音频

    很多时候为了方便收听视频文件中的音频信息,我们会将视频文件中的音频流转码输出成音频文件,方便在对应的平台上进行播放.这里就介绍一下如何通过FFmpeg将视频文件中的音频流转码成特定编码格式的音频文件. ...

  4. FFmpeg进阶: 音频滤镜大全

    在做音频处理模块的时候,为了对声音进行优化处理,我很多时候会使用各种算法对音频进行变换,效果包括变音变调.声音降噪等等.其实FFmpeg库里的滤镜模块包含了很多有用的音频滤镜算法,这对于提升开发效率避 ...

  5. FFmpeg进阶: 音频变声滤镜

    声音最重要的两个元素就是语速和语调,改变声音的辨识度主要也是从这两方面入手.我们可以通过对音频数据进行插值或者抽值修改,以达到降低语速和增加语速的目的.同时我们也可以通过对数据进行线性拉伸来调节音调. ...

  6. FFmpeg源码分析:音频滤镜介绍(上)

    FFmpeg在libavfilter模块提供音视频滤镜.所有的音频滤镜都注册在libavfilter/allfilters.c.我们也可以使用ffmpeg -filters命令行来查看当前支持的所有滤 ...

  7. android 混音 源码,FFmpegAndroid android 端基于 FFmpeg 实现音频剪切、拼接、转码、混音、编解码;视频剪切、水印、截图、转码、编 @codeKK c开源站...

    android 端基于 FFmpeg 库的使用 添加编译 ffmpeg.shine.mp3lame.x264 源码的参考脚本 目前音视频相关处理: 音频剪切.拼接 音频混音 音频转码 音视频合成 音频 ...

  8. 《ffmpeg basics》中文版 -- 16.数字音频

    16.数字音频 ​ 术语数字音频和术语数字视频类似,数字视频是一种用于处理和显示可移动图像的技术,而数字音频则是处理声音.数字音频是一种可以使用 PCM(pulse-code modulation:脉 ...

  9. FFMPEG常用的一些命令介绍:音频录制、视频录制

    1.视频和音频单独抓取 如果指定输入格式和设备,则ffmpeg可以直接捕获视频和音频. Linux下捕获摄像头的数据保存成视频文件: # ffmpeg -f video4linux2 -s 1280x ...

最新文章

  1. RHEL7系统管理之内核管理
  2. html中::before 爬虫_反反爬虫系列(四)
  3. 惠普微型计算机怎么样,垃圾佬的养成①日记之惠普HP 400G3 DM迷你小主机入手日记...
  4. Oracle存储过程小解
  5. python列表操作
  6. SpringMVC流程图示
  7. Linux 命令快捷键
  8. [ASP.NET]动态页面调用JS错误。保存为HTML文件就不报错了。
  9. 网络同传软件有哪些_2020年软件工程现状:Python或将成为第一大编程语言,中国开源涨势最猛...
  10. CSS图片重叠效果~秒懂
  11. 20170910算法工程师在线笔试之求第n个丑数
  12. 计算机文化基础概念,练习题_计算机文化基础概念.doc
  13. Could NOT find LibXml2 (missing: LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)Call Stack
  14. ads1115多片并联
  15. 用七段数码管显示学号python_用七段数码管显示学号python
  16. 活体检测的几种手段分析
  17. 快速发表论文的技巧有哪些
  18. 3分钟学会制作动态折线图!
  19. 面试必问题之Docker分布式搭建
  20. 什么是 Smurf 攻击?

热门文章

  1. “神工坊”性能测试系列之一:Fluent旋转机械瞬态分析
  2. 畅聊吧!来Discord加入InterSystems开发者社区!
  3. 公告关心我的成长的网人
  4. 剑指offer 22. 链表中倒数第k个结点
  5. 符合语言习惯的 Python 优雅编程技巧
  6. HTML CSS 的鼠标悬浮,点击的样式
  7. 【CCNA考完啥时候可以注册?】
  8. lte接口流程图_LTE--随机接入流程
  9. 安卓手机-百度浏览器-部分js不生效原因
  10. 全栈工程师的辛苦路程