当前位置: 首页 > news >正文

FFmpeg处理音视频的常用API及一般流程

FFmpeg是一个开源的音视频处理库,提供了丰富的API用于音视频的编解码、转码、过滤、播放等操作。

一、使用FFmpeg API解码涉及到的函数及一般流程如下:

1. av_register_all(): 注册所有的编解码器和格式。

av_register_all();

2. avformat_open_input(): 打开输入文件(流)并获取文件(流)的格式信息。

AVFormatContext *fmt_ctx = NULL;
if (avformat_open_input(&fmt_ctx, input_file, NULL, NULL) < 0) {// 打开文件失败
}

3. avformat_find_stream_info(): 获取流的详细信息。
 

if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {// 获取流信息失败
}

4. avcodec_find_decoder(): 查找解码器。
 

AVCodec *codec = avcodec_find_decoder(stream->codecpar->codec_id);
if (!codec) {// 找不到解码器
}

5. avcodec_open2(): 打开解码器。
 

AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {// 打开解码器失败
}

6. av_read_frame(): 读取一帧音视频数据。
 

AVPacket packet;
while (av_read_frame(fmt_ctx, &packet) >= 0) {// 处理音视频数据av_packet_unref(&packet);
}

7. avcodec_send_packet()和avcodec_receive_frame(): 解码音视频数据。
 

AVFrame *frame = av_frame_alloc();
while (av_read_frame(fmt_ctx, &packet) >= 0) {if (packet.stream_index == audio_stream_index) {// 解码音频数据avcodec_send_packet(codec_ctx, &packet);while (avcodec_receive_frame(codec_ctx, frame) >= 0) {// 处理解码后的音频帧}} else if (packet.stream_index == video_stream_index) {// 解码视频数据avcodec_send_packet(codec_ctx, &packet);while (avcodec_receive_frame(codec_ctx, frame) >= 0) {// 处理解码后的视频帧}}av_packet_unref(&packet);
}

二、与解码类似,编码的流程一般为:

8. avformat_alloc_output_context2(): 创建输出格式上下文。
 

AVFormatContext *out_fmt_ctx = NULL;
if (avformat_alloc_output_context2(&out_fmt_ctx, NULL, NULL, output_file) < 0) {// 创建输出格式上下文失败
}

9. avcodec_find_encoder(): 查找编码器。
 

AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!encoder) {// 找不到编码器
}

10. avcodec_open2(): 打开编码器。
 

AVCodecContext *encoder_ctx = avcodec_alloc_context3(encoder);
if (avcodec_open2(encoder_ctx, encoder, NULL) < 0) {// 打开编码器失败
}

11. avformat_write_header(): 写入输出文件的头部信息。
 

if (avformat_write_header(out_fmt_ctx, NULL) < 0) {// 写入头部信息失败
}

12. avcodec_send_frame()和avcodec_receive_packet(): 编码音视频数据。
 

AVPacket encoded_packet;
while (av_read_frame(fmt_ctx, &packet) >= 0) {if (packet.stream_index == audio_stream_index) {// 编码音频数据avcodec_send_frame(encoder_ctx, frame);while (avcodec_receive_packet(encoder_ctx, &encoded_packet) >= 0) {// 处理编码后的音频数据}} else if (packet.stream_index == video_stream_index) {// 编码视频数据avcodec_send_frame(encoder_ctx, frame);while (avcodec_receive_packet(encoder_ctx, &encoded_packet) >= 0) {// 处理编码后的视频数据}}av_packet_unref(&packet);
}

13. av_write_frame()和av_write_trailer(): 写入编码后的音视频数据。
 

if (av_write_frame(out_fmt_ctx, &encoded_packet) < 0) {// 写入音视频数据失败
}

三、解码和编码结束后都要及时释放内存:

14. avformat_close_input(): 关闭输入文件。
 

avformat_close_input(&fmt_ctx);

15. avcodec_free_context(): 释放编解码器上下文。
 

avcodec_free_context(&codec_ctx);

16. av_frame_free(): 释放帧对象。
 

av_frame_free(&frame);

17. avformat_free_context(): 释放格式上下文。
 

avformat_free_context(fmt_ctx);

了解这些常用API及流程对使用FFmpeg开发将大有裨益。

http://www.lryc.cn/news/274406.html

相关文章:

  • Kotlin协程学习之-01
  • 214.【2023年华为OD机试真题(C卷)】测试用例执行计划(排序题-JavaPythonC++JS实现)
  • 数一下 1到 100 的所有整数中出现多少个数字9并输出这些数字
  • 07. HTTP接口请求重试怎么处理?
  • 分割数组的最大差值 - 华为OD统一考试
  • 基于 Python+Django 技术栈,我开发了一款视频管理系统
  • Python从入门到网络爬虫(内置函数详解)
  • Python新年烟花代码
  • oracle语法学习
  • 网络安全常见漏洞类型总结
  • C++自制小游戏《屠夫躲猫猫》
  • LabVIEW在高级结构监测中的创新应用
  • 关于GitHub的git推送命令时报错密码授权失败问题
  • WPF Blend for visual studio使用
  • 云卷云舒:【实战篇】Redis迁移
  • [C#]yolov8-onnx在winform部署手势识别模型
  • 【uniapp】 uniapp 修改tabBar图标大小和navigationBar字体大小
  • Visual Studio 2017 + opencv4.6 + contribute + Cmake(Aruco配置版本)指南
  • 自定义事件总线
  • 212.【2023年华为OD机试真题(C卷)】堆内存申请(排序和贪心算法-JavaPythonC++JS实现)
  • Flink Watermark和时间语义
  • HarmonyOS UI框架简介
  • 编程羔手解决Maven引入多个版本的依赖包,导致包冲突了
  • C#,入门教程(08)——基本数据类型及使用的基础知识
  • 分类预测 | Matlab实现DBO-SVM蜣螂算法优化支持向量机多特征分类预测
  • 计算机二级Python选择题考点——公共基础部分
  • 《微机原理与应用》期末考试题库(附答案解析)
  • 如何在Android Glide中结合使用CenterCrop和自定义圆角变换(图片部分圆角矩形)
  • 华为机考-手拍球游戏
  • 【线上问题】两台服务器的时间不一致导致jwt解析错误