FFmpeg PCM编码为AAC
使用FFmpeg库把PCM文件编码为AAC文件,FFmpeg版本为4.4.2-0
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/mem.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>int main(int argc, char *argv[])
{int ret = -1;int64_t pts = 0;const char *input_file = argv[1];const char *output_file = argv[2];FILE *input_pcm = NULL;AVFormatContext *format_context = NULL;AVCodecContext *codec_context = NULL;AVStream *stream = NULL;AVCodec *codec = NULL;AVFrame *frame = NULL;AVPacket *packet = NULL;struct SwrContext *swr_ctx = NULL;enum AVSampleFormat in_sample_fmt = AV_SAMPLE_FMT_S16; // 输入pcm文件的采样格式int in_sample_rate = 44100; // 输入pcm文件的采样率uint64_t in_channel_layout = AV_CH_LAYOUT_STEREO; // 输入pcm文件的声道布局if (argc < 3){fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);return -1;}input_pcm = fopen(input_file, "rb");if (!input_pcm){fprintf(stderr, "Could not open input file %s\n", input_file);goto end;}// 分配输出格式上下文avformat_alloc_output_context2(&format_context, NULL, NULL, output_file);if (!format_context){fprintf(stderr, "Could not allocate output format context\n");goto end;}// 查找编码器, 这里使用ffmpeg内置的aac编码器,// 如果需要使用其他编码器, 可以使用 avcodec_find_encoder_by_name 按名称查找codec = avcodec_find_encoder(AV_CODEC_ID_AAC);if (!codec){fprintf(stderr, "Codec not found\n");goto end;}// 创建新的音频流stream = avformat_new_stream(format_context, NULL);if (!stream){fprintf(stderr, "Could not allocate stream\n");goto end;}// 分配编码器上下文codec_context = avcodec_alloc_context3(codec);if (!codec_context){fprintf(stderr, "Could not allocate codec context\n");goto end;}/* 设置编码器参数 */// 编码器IDcodec_context->codec_id = AV_CODEC_ID_AAC;// 媒体类型codec_context->codec_type = AVMEDIA_TYPE_AUDIO;// 设置编码器接收的输入音频样本的采样格式,这里设置为内置aac编码器支持的采样格式,// 不同的编码器支持的采样格式可能不同。codec_context->sample_fmt = AV_SAMPLE_FMT_FLTP;// 设置采样率codec_context->sample_rate = in_sample_rate;// 设置声道布局codec_context->channel_layout = in_channel_layout;// 设置声道数codec_context->channels = av_get_channel_layout_nb_channels(codec_context->channel_layout);// 设置比特率codec_context->bit_rate = 128000;// 设置AAC的profilecodec_context->profile = FF_PROFILE_AAC_LOW;// 打开编码器if (avcodec_open2(codec_context, codec, NULL) < 0){fprintf(stderr, "Could not open codec\n");goto end;}// 将编码器参数复制到流int result = avcodec_parameters_from_context(stream->codecpar, codec_context);if (result < 0){fprintf(stderr, "Could not copy codec parameters\n");goto end;}// 打开输出文件if (!(format_context->oformat->flags & AVFMT_NOFILE)) // 检查输出格式是否需要文件存储{result = avio_open(&format_context->pb, output_file, AVIO_FLAG_WRITE);if (result < 0){fprintf(stderr, "Could not open output file %s\n", output_file);goto end;}}// 写文件头result = avformat_write_header(format_context, NULL);if (result < 0){fprintf(stderr, "Error occurred when opening output file\n");goto end;}frame = av_frame_alloc();packet = av_packet_alloc();if (!frame || !packet){fprintf(stderr, "Could not allocate frame or packet\n");goto end;}// 设置帧参数frame->nb_samples = codec_context->frame_size; // 一帧音频数据的采样个数,AAC编码器的frame_size默认为1024frame->format = codec_context->sample_fmt;frame->channel_layout = codec_context->channel_layout;// 分配帧数据缓冲区result = av_frame_get_buffer(frame, 0);if (result < 0){fprintf(stderr, "Could not allocate audio data buffers\n");goto end;}// 初始化重采样上下文swr_ctx = swr_alloc_set_opts(NULL,codec_context->channel_layout, codec_context->sample_fmt, codec_context->sample_rate,codec_context->channel_layout, in_sample_fmt, codec_context->sample_rate,0, NULL);if (!swr_ctx || swr_init(swr_ctx) < 0){fprintf(stderr, "Could not allocate resampler context\n");goto end;}// 计算每帧音频数据的大小 = 采样个数 * 采样格式大小 * 声道数int fsize = frame->nb_samples * av_get_bytes_per_sample(in_sample_fmt) * codec_context->channels;// 分配缓冲区uint8_t *input_buffer = (uint8_t *)av_malloc(fsize);if (!input_buffer){fprintf(stderr, "Could not allocate input buffer\n");goto end;}// 编码循环while (1){// 读取PCM数据到缓冲区result = fread(input_buffer, 1, fsize, input_pcm);if (result <= 0){break;}// 处理不足一帧的情况,不足一帧的数据用0填充if (result < fsize){memset(input_buffer + result, 0, fsize - result);}// 重采样result = swr_convert(swr_ctx, frame->data, frame->nb_samples,(const uint8_t **)&input_buffer, frame->nb_samples);if (result < 0){fprintf(stderr, "Error while converting\n");goto end;}frame->pts = pts;pts += frame->nb_samples; // 计算下一帧的pts,每个帧的时间戳应该是前一个帧的时间戳加上该帧的样本数// 发送帧到编码器result = avcodec_send_frame(codec_context, frame);if (result < 0){fprintf(stderr, "Error sending frame to codec\n");goto end;}// 接收编码后的数据包while (result >= 0){result = avcodec_receive_packet(codec_context, packet);if (result == AVERROR(EAGAIN) || result == AVERROR_EOF){break;}else if (result < 0){fprintf(stderr, "avcodec_receive_packet failed (errmsg '%s')\n", av_err2str(result));goto end;}packet->stream_index = stream->index;// 将时间戳从编码器时间基转换到流时间基av_packet_rescale_ts(packet, codec_context->time_base, stream->time_base);// 写数据包到输出文件result = av_interleaved_write_frame(format_context, packet);if (result < 0){fprintf(stderr, "Error writing audio packet\n");av_packet_unref(packet);goto end;}av_packet_unref(packet);}}av_free(input_buffer);// 发送NULL帧到编码器,刷新编码器内部缓冲区result = avcodec_send_frame(codec_context, NULL);while (result >= 0){result = avcodec_receive_packet(codec_context, packet);if (result == AVERROR_EOF){break;}else if (result < 0){fprintf(stderr, "avcodec_receive_packet failed (errmsg '%s')\n", av_err2str(result));goto end;}packet->stream_index = stream->index;av_packet_rescale_ts(packet, codec_context->time_base, stream->time_base);result = av_interleaved_write_frame(format_context, packet);if (result < 0){fprintf(stderr, "Error writing audio packet\n");av_packet_unref(packet);goto end;}av_packet_unref(packet);}// 写文件尾av_write_trailer(format_context);ret = 0;end:if (frame)av_frame_free(&frame);if (packet)av_packet_free(&packet);if (codec_context)avcodec_free_context(&codec_context);if (format_context)avformat_free_context(format_context);if (swr_ctx)swr_free(&swr_ctx);if (input_pcm)fclose(input_pcm);return ret;
}
相关文章:
FFmpeg PCM编码为AAC
使用FFmpeg库把PCM文件编码为AAC文件,FFmpeg版本为4.4.2-0 代码如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <…...
React@16.x(16)Render Props
目录 1,问题描述2,解决方式2.1,Render Props2.2,HOC 3,使用场景 1,问题描述 当使用组件时,标签中的内容,会被当做 props.children 来渲染: 子组件: import…...
STM32 定时器问题
stm32通用定时器中断问题 STM32 定时器有时一开启就进中断的问题 /// STM32 TIM1高级定时器RCR重复计数器的理解 /// /// /// /// /// /// /// ///...
CSS学习笔记目录
CSS学习笔记之基础教程(一) CSS学习笔记之基础教程(二) CSS学习笔记之中级教程(一) CSS学习笔记之中级教程(二) CSS学习笔记之中级教程(三) CSS学习笔记之高级…...
随笔-我在武汉一周了
做梦一样,已经来武汉一周了,回顾一下这几天,还真是有意思。 周一坐了四个小时的高铁到了武汉站,照着指示牌打了个出租车。司机大姐开得很快,瞅了眼,最快速度到了110,差点把我晃晕。一下车就感觉…...
Python 爬虫零基础:探索网络数据的神秘世界
Python 爬虫零基础:探索网络数据的神秘世界 在数字化时代,网络数据如同无尽的宝藏,等待着我们去发掘。Python爬虫,作为获取这些数据的重要工具,正逐渐走进越来越多人的视野。对于零基础的学习者来说,如何入…...
微信小程序的view的属性值和用法
在微信小程序中,view 是一个基础的视图组件,用于承载其他视图组件或者展示文本、图片等内容。view 组件具有多种属性,用于控制其行为和样式。以下是一些常用的 view 属性及其用法: class / style: 控制视图的样式,可以…...
Python优化、异常处理与性能提升技巧
Python作为一种高效的编程语言,其灵活性和强大的功能使得它成为了许多开发者的首选。在日常的编程实践中,掌握一些高效的Python技巧可以极大地提升开发效率和代码质量。本文将介绍五个关于Python使用技巧,帮助你更加熟练地运用Python解决问题…...
Flink状态State | 大数据技术
⭐简单说两句⭐ ✨ 正在努力的小叮当~ 💖 超级爱分享,分享各种有趣干货! 👩💻 提供:模拟面试 | 简历诊断 | 独家简历模板 🌈 感谢关注,关注了你就是我的超级粉丝啦! &a…...
go语言方法之方法值和方法表达式
我们经常选择一个方法,并且在同一个表达式里执行,比如常见的p.Distance()形式,实际上 将其分成两步来执行也是可能的。p.Distance叫作“选择器”,选择器会返回一个方法"值"->一 个将方法(Point.Distance)绑定到特定接…...
TDMQ CKafka 版弹性存储能力重磅上线!
导语 自 2024年5月起,TDMQ CKafka 专业版支持弹性存储能力,这种产品形态下,存储可按需使用、按量付费,一方面降低消费即删除、存储使用波动大场景下的存储成本,另一方面存储空间理论上无穷大。 TDMQ CKafka 版产品能…...
24、Linux网络端口
Linux网络端口 1、查看网络接口信息ifconfig ens33 eth0 文件 ifconfig 当前设备正在工作的网卡,启动的设备。 ifconfig -a 查看所有的网络设备。 ifconfig ens33 查看指定网卡设备。 ifconfig ens33 up/down 对指定网卡设备进行开关 基于物理网卡设备虚拟的…...
Mysql全文搜索和LIKE搜索有什么区别
全文搜索和LIKE的区别 性能:在大数据集上,全文搜索通常比LIKE查询更快,因为它使用了专门的索引结构。 功能:全文搜索提供了更丰富的查询功能,如多个关键词的搜索、自然语言搜索、布尔搜索等。而LIKE通常只支持简单的…...
elementplu父级页面怎么使用封装子组件原组件的方法
一、使用原因: 封装了el-table,表格中有多选,父级要根据指定状态,让其选择不上,需要用到elementplus中table原方法toggleRowSelection 附加小知识点:(el-tree刷新树后之前选中的保持高亮setCurr…...
el-date-picker选择开始日期的近半年
<el-date-pickerv-model"form[val.key]":type"val.datePickerType || daterange":clearable"val.clearable && true"range-separator"~"start-placeholder"开始日期"end-placeholder"结束日期"style&q…...
C++
封装一个矩形类(Rect),拥有私有属性:宽度(width)、高度(height), 定义公有成员函数: 初始化函数:void init(int w, int h) 更改宽度的函数:set_w(int w) 更改高度的函数:set_h(int h) 输出该矩形的周长和面积函数:void show()...
nginx源码阅读理解 [持续更新,建议关注]
文章目录 前述一、nginx 进程模型基本流程二、源码里的小点1.对字符串操作都进行了原生实现2.配置文件解析也是原生实现待续 前述 通过对 nginx 的了解和代码简单阅读,发现这个C代码的中间件确实存在过人之处,使用场景特别多,插件模块很丰富…...
笔试训练2
牛客.单词搜索 刚开始我就想是搜索,但是不清楚bfs还是dfs更好,我尝试了bfs但是队列存东西,没有我想象的那么好写,所以我决定试试dfs import java.util.*;public class Solution {static int m 0;static int n 0;static int […...
构建坚不可摧的Web安全防线:深入剖析二阶注入与全面防御策略
引言 在数字化时代,数据安全是企业和个人最为关注的问题之一。网络攻击手段层出不穷,其中SQL注入攻击尤为狡猾,它允许攻击者通过Web应用的漏洞对数据库进行非法操作。更隐蔽的是二阶注入攻击,它不仅威胁当前操作,还能…...
(4) qml动态元素
文章目录 概述注意 动画元素变化的策略Animation on 变化behavior on⽤standalone animation注意 缓冲曲线(Easing Curves)动画分组 概述 这⼀章介绍如何控制属性值的变化,通过动画的⽅式在⼀段时间内来改变属性值。这项技术是建⽴⼀个现代化…...
可靠性+灵活性:电力载波技术在楼宇自控中的核心价值
可靠性灵活性:电力载波技术在楼宇自控中的核心价值 在智能楼宇的自动化控制中,电力载波技术(PLC)凭借其独特的优势,正成为构建高效、稳定、灵活系统的核心解决方案。它利用现有电力线路传输数据,无需额外布…...
深入理解JavaScript设计模式之单例模式
目录 什么是单例模式为什么需要单例模式常见应用场景包括 单例模式实现透明单例模式实现不透明单例模式用代理实现单例模式javaScript中的单例模式使用命名空间使用闭包封装私有变量 惰性单例通用的惰性单例 结语 什么是单例模式 单例模式(Singleton Pattern&#…...
R语言速释制剂QBD解决方案之三
本文是《Quality by Design for ANDAs: An Example for Immediate-Release Dosage Forms》第一个处方的R语言解决方案。 第一个处方研究评估原料药粒径分布、MCC/Lactose比例、崩解剂用量对制剂CQAs的影响。 第二处方研究用于理解颗粒外加硬脂酸镁和滑石粉对片剂质量和可生产…...
A2A JS SDK 完整教程:快速入门指南
目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库ÿ…...
Selenium常用函数介绍
目录 一,元素定位 1.1 cssSeector 1.2 xpath 二,操作测试对象 三,窗口 3.1 案例 3.2 窗口切换 3.3 窗口大小 3.4 屏幕截图 3.5 关闭窗口 四,弹窗 五,等待 六,导航 七,文件上传 …...
在鸿蒙HarmonyOS 5中使用DevEco Studio实现企业微信功能
1. 开发环境准备 安装DevEco Studio 3.1: 从华为开发者官网下载最新版DevEco Studio安装HarmonyOS 5.0 SDK 项目配置: // module.json5 {"module": {"requestPermissions": [{"name": "ohos.permis…...
MyBatis中关于缓存的理解
MyBatis缓存 MyBatis系统当中默认定义两级缓存:一级缓存、二级缓存 默认情况下,只有一级缓存开启(sqlSession级别的缓存)二级缓存需要手动开启配置,需要局域namespace级别的缓存 一级缓存(本地缓存&#…...
认识CMake并使用CMake构建自己的第一个项目
1.CMake的作用和优势 跨平台支持:CMake支持多种操作系统和编译器,使用同一份构建配置可以在不同的环境中使用 简化配置:通过CMakeLists.txt文件,用户可以定义项目结构、依赖项、编译选项等,无需手动编写复杂的构建脚本…...
【Veristand】Veristand环境安装教程-Linux RT / Windows
首先声明,此教程是针对Simulink编译模型并导入Veristand中编写的,同时需要注意的是老用户编译可能用的是Veristand Model Framework,那个是历史版本,且NI不会再维护,新版本编译支持为VeriStand Model Generation Suppo…...
rknn toolkit2搭建和推理
安装Miniconda Miniconda - Anaconda Miniconda 选择一个 新的 版本 ,不用和RKNN的python版本保持一致 使用 ./xxx.sh进行安装 下面配置一下载源 # 清华大学源(最常用) conda config --add channels https://mirrors.tuna.tsinghua.edu.cn…...
