头文件:
#include <sys/time.h>
获取当前时间方法
//当前时间戳 毫秒级别(一秒解多少帧)
long long getNowMs(){
struct timeval tv;
gettimeofday(&tv, NULL);
long long sec = tv.tv_sec%360000; //秒 只取100小时内
long long t = sec*1000+tv.tv_usec/1000;
return t;
}
我的手机是 iphone 6s plus 真机测试解码一个 mp4格式102.4 MB的视频:
//打开一个视频文件
+ (void)ffmpegOpenFile:(NSString *)filePath{
// 1 注册组件
// av_register_all();
// 2 初始化网络 如果需要的话
avformat_network_init();
// 初始化 解码器
// avcodec_register_all();
// 3 打开封装格式文件
//封装格式上下文
AVFormatContext *ic = avformat_alloc_context();
//文件路径
const char *url = [filePath UTF8String];
/*
• AVFormatContext **ps 传指针的地址
• const char *url 文件路径(本地的或者网络的http rtsp 地址会被存在AVFormatContext 结构体的 fileName中)
• AVInputFormat *fmt 指定输入的封装格式 一般情况传NULL即可,自行判断即可
• AVDictionary **options 一般传NULL
*/
int re = avformat_open_input(&ic, url, NULL, NULL);
if (re != 0) {
NSLog(@"打开文件失败");
char *error_info = NULL;
av_strerror(re, error_info, 1024);
return;
}
NSLog(@"打开文件成功");
//获取流信息 读取部分视频做探测
re = avformat_find_stream_info(ic, 0);
if (re != 0) {
NSLog(@"avformat_find_stream_info failed!");
}
//总时长,流的信息
NSLog(@"duration =%lld nb_streams = %d",ic->duration,ic->nb_streams);
//打印相关信息
int fps = 0;
int videoStream = 0;
int audioStream = 1;
//(1)遍历
for (int i = 0; i < ic->nb_streams; i++) {
AVStream *as = ic->streams[i];
if (as->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {