FFmpeg IOS 解封装
2018-07-30 本文已影响50人
遇见猫的大鱼
解封装流程:
引入头文件:
//核心库-音视频编解码库
#import <libavcodec/avcodec.h>
//封装格式库
#import <libavformat/avformat.h>
(1)av_register_all() 注册所有的封装格式、加封装格式;也可以在使用之前单个注册。(新版本该方法弃用了)
在我最新使用的4.0.2版本中,已经不需要调用了。
![](https://img.haomeiwen.com/i1970907/bc9c2f9c0569f06d.png)
(2)avformat_network_init() 初始化网络模块,例如用于解封装rtsp数据
(3)avformat_open_input(...) 打开文件并解析 解析出文件格式 音视频流,视频帧索引
使用之前要确保已经注册封装格式,比如说调用了av_register_all()
(4)avformat_find_stream_info(...) 查找文件格式、索引
(5)av_find_best_stream(...) 找对应的音频流 视频流 /( 遍历返回值的streams[]数
组,音频视频字幕扩展信息,根据标志位来确定信息)
(6)封装的上下文 :AVFormatContext (加解分装都用)
AVStream结构体存储音视频流的参数信息
AVPacket解封装玩后的数据包,用av_read_frame(...)读取,里面包含包的 pts dts 视频是否是关键帧 等 (去掉了 00 00 00 01间隔符)
//打开一个视频文件
+ (void)ffmpegOpenFile:(NSString *)filePath{
// 1 注册组件
// av_register_all();
// 2 初始化网络 如果需要的话
avformat_network_init()
// 2 打开封装格式文件
//封装格式上下文
AVFormatContext *avformat_context = avformat_alloc_context();
//文件路径
const char *url = [filePath UTF8String];
/*
• AVFormatContext **ps 传指针的地址
• const char *url 文件路径(本地的或者网络的http rtsp 地址会被存在AVFormatContext 结构体的 fileName中)
• AVInputFormat *fmt 指定输入的封装格式 一般情况传NULL即可,自行判断即可
• AVDictionary **options 一般传NULL
*/
int avformat_open_input_result = avformat_open_input(&avformat_context, url, NULL, NULL);
if (avformat_open_input_result != 0) {
NSLog(@"打开文件失败");
char *error_info = NULL;
av_strerror(avformat_open_input_result, error_info, 1024);
return;
}
NSLog(@"打开文件成功");
}