索引地址:系列教程索引地址
上一篇:FFmpeg5入门教程10.02:输出版本号
解析视频并输出视频信息,步骤为:

测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| AVFormatContext *fmt_ctx = avformat_alloc_context(); int ret=0; char* fileName="/home/jackey/Videos/Sample.mkv";
do{ if ((ret = avformat_open_input(&fmt_ctx, fileName, NULL, NULL))<0) break;
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梦视频中随便选的。
测试输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| [flv @ 0x564983eb44c0] Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found 0 Input 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 Stream
|
从代码可以看出:
- AVFormatContext对象是经常用到的,这个在开发时必须要有。
- 文件使用完成之后要及时关闭。网上的代码是使用goto语句,但是在开发C++时不推荐使用goto语句。所以我在此使用do{}while(0),此语句只会执行{}中的代码一次,将释放资源的代码放在do-while代码段之外。
- 虽然视频扩展名为mkv,但是实际内容为flv。
GitHub项目地址(源代码):ffmpeg_beginner中的4.get_stream_info
下一篇:FFmpeg5入门教程10.04:解码视频流过程