• FFmpeg获取媒体文件的视频信息


    视频包标志位

    代码

    printf("index:%d\n", in_stream->index);
    
    • 1

    结果

    index:0
    
    • 1

    视频帧率

    // avg_frame_rate: 视频帧率,单位为fps,表示每秒出现多少帧
    printf("fps:%lffps\n", av_q2d(in_stream->avg_frame_rate));
    
    • 1
    • 2

    结果

    fps:29.970070fps
    
    • 1

    视频编解码器

    代码

    if (AV_CODEC_ID_MPEG4 == in_stream->codecpar->codec_id) //视频压缩编码格式
    {
        printf("video codec:MPEG4\n");
    }
    else if (AV_CODEC_ID_H264 == in_stream->codecpar->codec_id) //视频压缩编码格式
    {
        printf("video codec:H264\n");
    }
    else
    {
        printf("video codec_id:%d\n", in_stream->codecpar->codec_id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果

    video codec:H264
    
    • 1

    视频帧宽高

    代码

    // 视频帧宽度和帧高度
    printf("width:%d height:%d\n", in_stream->codecpar->width,
    in_stream->codecpar->height);
    
    • 1
    • 2
    • 3

    结果

    width:852 height:480
    
    • 1

    视频长度

    代码

    //视频总时长,单位为秒。注意如果把单位放大为毫秒或者微妙,音频总时长跟视频总时长不一定相等的
    if(in_stream->duration != AV_NOPTS_VALUE)
    {
        int duration_video = (in_stream->duration) * av_q2d(in_stream->time_base);
        printf("video duration: %02d:%02d:%02d\n",
               duration_video / 3600,
               (duration_video % 3600) / 60,
               (duration_video % 60)); //将视频总时长转换为时分秒的格式打印到控制台上
    }
    else
    {
        printf("video duration unknown\n");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结果

    video duration: 00:05:17
    
    • 1

    完整代码

    for (uint32_t i = 0; i < ifmt_ctx->nb_streams; i++)
    {
        AVStream *in_stream = ifmt_ctx->streams[i];// 音频流、视频流、字幕流
        //如果是音频流,则打印音频的信息
        if (AVMEDIA_TYPE_AUDIO == in_stream->codecpar->codec_type)
        {...}
        else if (AVMEDIA_TYPE_VIDEO == in_stream->codecpar->codec_type)  //如果是视频流,则打印视频的信息
        {
            printf("----- Video info:\n");
            printf("index:%d\n", in_stream->index);
            // avg_frame_rate: 视频帧率,单位为fps,表示每秒出现多少帧
            printf("fps:%lffps\n", av_q2d(in_stream->avg_frame_rate));
            if (AV_CODEC_ID_MPEG4 == in_stream->codecpar->codec_id) //视频压缩编码格式
            {
                printf("video codec:MPEG4\n");
            }
            else if (AV_CODEC_ID_H264 == in_stream->codecpar->codec_id) //视频压缩编码格式
            {
                printf("video codec:H264\n");
            }
            else
            {
                printf("video codec_id:%d\n", in_stream->codecpar->codec_id);
            }
            // 视频帧宽度和帧高度
            printf("width:%d height:%d\n", in_stream->codecpar->width,
                   in_stream->codecpar->height);
            //视频总时长,单位为秒。注意如果把单位放大为毫秒或者微妙,音频总时长跟视频总时长不一定相等的
            if(in_stream->duration != AV_NOPTS_VALUE)
            {
                int duration_video = (in_stream->duration) * av_q2d(in_stream->time_base);
                printf("video duration: %02d:%02d:%02d\n",
                       duration_video / 3600,
                       (duration_video % 3600) / 60,
                       (duration_video % 60)); //将视频总时长转换为时分秒的格式打印到控制台上
            }
            else
            {
                printf("video duration unknown");
            }
    
            printf("\n");
            videoindex = i;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    结果

    ----- Video info:
    index:0
    fps:29.970070fps
    video codec:H264
    width:852 height:480
    video duration: 00:05:17
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    FileNotFoundError: Could not find module ‘XXX\lib\site-packages\llvmlite
    基础复习(IDA调试器)
    LeetCode 刷题记录——从零开始记录自己一些不会的(二)
    【HDLBits 刷题 9】Circuits(5)Finite State Manchines 1-9
    【联通】数据编排技术在联通的应用
    Vue内置组件之KeepAlive原理
    文本挖掘day6 基于文本挖掘的化工事故致因网络分析
    Java项目模块占用CPU过高问题分析
    煤矿皮带撕裂检测系统
    Java读取Excel并生成Word&PDF
  • 原文地址:https://blog.csdn.net/qq_43537701/article/details/132912180