【初学ffmpeg】ffmpeg获取视频信息

=====================================================

相关代码可以参考我的:

【初学ffmpeg】ffmpeg解码保存为yuv

=====================================================

一般视频的信息主要包括以下信息,而这些信息绝大部分都可以再 AVFormatContext 中获得

播放时长duration

比特率 bit_rate

帧率fps

视频帧数nb_frame

分辨率

视频编码器名称

 

1. 通过 avformat_open_input 打开流

2. 再调用 avformat_find_stream_info 找到流的信息

    AVFormatContext	*pFormatCtx; 
    char filepath[]="D:\\test.mp4";
    if(avformat_open_input(&pFormatCtx,filepath,NULL, NULL)!=0){
        printf("Couldn't open input stream.\n");
        return -1;
    }
    if(avformat_find_stream_info(pFormatCtx,NULL)<0){
        printf("Couldn't find stream information.\n");
        return -1;
    }

到这里我们已经可以从 AVFormatContext   中读取 播放时长duration 和 比特率 bit_rate了

    fprintf(stderr, "duration = %.2fs\n", (double)pFormatCtx->duration / 1000000);
    fprintf(stderr, "bitrate = %.2fkb/s\n", (double)pFormatCtx->bit_rate / 1000);

打印结果为:

duration = 245.48s
bitrate = 781.84kb/s

 

3.想要获取其他信息,需要从AVFormatContext   找到视频流信息

    int videoindex=-1;
    for(i=0; i<pFormatCtx->nb_streams; i++) {
        if(pFormatCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){
            videoindex=i;
            break;
        }   
    }
    if(videoindex==-1){
        printf("Didn't find a video stream.\n");
        return -1;
    }

找到之后我们就能获取 帧率fps 、视频帧数nb_frame 和分辨率了

    AVStream * pVStream = pFormatCtx->streams[videoindex];
    fprintf(stderr, "nb_frames = %lld\n", pVStream->nb_frames);
    fprintf(stderr, "fps = %.2f\n", (double)pVStream->nb_frames / pFormatCtx->duration * 1000000);
    fprintf(stderr, "fps = %.2f\n", (double)pVStream->avg_frame_rate.num / pVStream->avg_frame_rate.den);
    fprintf(stderr, "width  = %d,  height = %d\n", pVStream->codecpar->width, pVStream->codecpar->height);

打印结果:

nb_frames = 5883
fps = 23.96 (通过总帧数 / 时长 计算出的fps)
fps = 23.98(直接读取出来的fps)
width  = 768,  height = 432

 

4.获取编码名称,需要先通过 avcodec_find_decoder 找到解码器

    AVCodecParameters *pCodecParam=pFormatCtx->streams[videoindex]->codecpar;
    AVCodec * codec = avcodec_find_decoder(pCodecParam->codec_id);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }
    fprintf(stderr, "codecName = %s\n", codec->long_name);

打印结果:

codecName = H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10

 

5.最后,其实可以使用 av_dump_format 直接打印出相关信息

    fprintf(stderr, "------------- File Information ------------------\n");
    av_dump_format(pFormatCtx,0,filepath,0);
    fprintf(stderr, "-------------------------------------------------\n");

打印结果:

------------- File Information ------------------
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'D:\output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    artist          : yinyuetai.com
    album           : Yinyuetai
    date            : 09/29/19 20:39:28
    encoder         : Lavf58.12.100
    comment         : Yinyuetai-1TR1166
  Duration: 00:04:05.48, start: 0.000000, bitrate: 781 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 768x432, 681 kb/s, 23.98 fps, 23.98 tbr, 24k tbn,
 47.95 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 95 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
-------------------------------------------------

参考:

https://blog.csdn.net/leixiaohua1020/article/details/8652605

https://blog.csdn.net/qq_41824928/article/details/103611366

https://blog.csdn.net/zhoubotong2012/article/details/79340722

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值