解析视频并输出视频信息,步骤为:
测试代码:
#include <stdio.h>
#include "libavcodec/avcodec.h"
#include "libavfilter/avfilter.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libavutil/ffversion.h"
#include "libswresample/swresample.h"
#include "libswscale/swscale.h"
#include "libpostproc/postprocess.h"
int main()
{
AVFormatContext *fmt_ctx = avformat_alloc_context();//创建对象并初始化
int ret=0;
char* fileName="C:\\Users\\hyper\\Videos\\Sample.wmv";//文件地址
do{
//打开文件
if ((ret = avformat_open_input(&fmt_ctx, fileName, NULL, NULL))<0)
break;//Cannot open video file
//查找流信息(音频流和视频流)
if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
printf("Cannot find stream information\n");
break;
}
av_dump_format(fmt_ctx,0,fileName,0);//输出视频信息
}while(0);
avformat_close_input(&fmt_ctx);//关闭文件
return ret;
}
测试的视频是我从我收集的哆啦A梦视频中随便选的。
测试输出:
[flv @ 0x564983eb44c0] Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found 0 Input #0, flv, from '/home/jackey/Videos/Sample.mkv': Metadata: creator : www.qiyi.com metadatacreator : Yet Another Metadata Injector for FLV - Version 1.2 hasKeyframes : true hasVideo : true hasAudio : true hasMetadata : true canSeekToEnd : false datasize : 40600924 videosize : 36538352 audiosize : 3942540 lasttimestamp : 645 lastkeyframetimestamp: 637 lastkeyframelocation: 40109260 Duration: 00:10:44.87, start: 0.000000, bitrate: 503 kb/s Stream #0:0: Video: h264 (High), yuv420p(tv, smpte170m/smpte170m/bt709, progressive), 624x480, 451 kb/s, 25 fps, 25 tbr, 1k tbn, 50 tbc Stream #0:1: Audio: aac (HE-AAC), 44100 Hz, stereo, fltp, 47 kb/s
从代码可以看出:
- AVFormatContext对象是经常用到的,这个在开发时必须要有。
- 文件使用完成之后要及时关闭。网上的代码是使用goto语句,但是在开发C++时不推荐使用goto语句。所以我在此使用do{}while(0),此语句只会执行{}中的代码一次,将释放资源的代码放在do-while代码段之外。
- 虽然视频扩展名为mkv,但是实际内容为flv。