FFmpeg4/5/6/7入门教程2:输出视频信息

解析视频并输出视频信息,步骤为:

测试代码:

#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。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值