其中 AVFormatContext 用来存储视音频封装格式(flv,mp4,rmvb,avi)中包含的所有信息 很多函数都要用到它作为参数。
avformat_open_input()该函数是打开多媒体数据并且获得一些相关的信息,其中填入的&pFormat是函数调用成功之后处理过的AVFormatContext结构体,path是打开的音视频流的url, 函数执行成功的话,其返回值大于等于0
avformat_find_stream_info () 该函数可以读取视音频流数据并且获得一些相关的信息,pFormat就是输入的AVFormatContext,函数执行成功的话,其返回值大于等于0;
#include <stdio.h>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
int main()
{
//测试DLL库是否成功
printf("%s/n", avcodec_configuration());;
AVFormatContext* pFormat = NULL;
const char* path = "22.mp4";
//打开视频文件
int ret = avformat_open_input(&pFormat, path, NULL, NULL);
//判断是否打开
if (ret) {
printf("打开失败\n");
return -1;
}
printf("打开成功\n");
//寻找流信息 H264 宽width 高height
ret = avformat_find_stream_info(pFormat, NULL);
//判断是否成功
if (ret) {
printf("失败\n");
return -1;
}
printf("成功\n");
//获取文件时长
int time = pFormat->duration;
printf("%d 时长",time);
//以分秒显示
int mbittime = (time / 1000000) / 60;
int mmintime = (time / 1000000) % 60;
printf("%d分/%d秒\n", mbittime, mmintime);
return 0;
}