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


    标志位

    代码

    // index: 每个流成分在ffmpeg解复用分析后都有唯一的index作为标识
    printf("index:%d\n", in_stream->index);
    
    • 1
    • 2

    结果

    index:1
    
    • 1

    音频采样率

    // sample_rate: 音频编解码器的采样率,单位为Hz
    printf("samplerate:%dHz\n", in_stream->codecpar->sample_rate);
    
    • 1
    • 2

    结果

    samplerate:48000Hz
    
    • 1

    音频采样格式

    代码

    // codecpar->format: 音频采样格式
    if (AV_SAMPLE_FMT_FLTP == in_stream->codecpar->format)
    {
        printf("sampleformat:AV_SAMPLE_FMT_FLTP\n");
    }
    else if (AV_SAMPLE_FMT_S16P == in_stream->codecpar->format)
    {
        printf("sampleformat:AV_SAMPLE_FMT_S16P\n");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    结果

    sampleformat:AV_SAMPLE_FMT_FLTP
    
    • 1

    采样格式枚举量简单介绍

    enum AVSampleFormat {
        AV_SAMPLE_FMT_NONE = -1,
        AV_SAMPLE_FMT_U8,          ///< unsigned 8 bits
        AV_SAMPLE_FMT_S16,         ///< signed 16 bits
        AV_SAMPLE_FMT_S32,         ///< signed 32 bits
        AV_SAMPLE_FMT_FLT,         ///< float
        AV_SAMPLE_FMT_DBL,         ///< double
    
        AV_SAMPLE_FMT_U8P,         ///< unsigned 8 bits, planar
        AV_SAMPLE_FMT_S16P,        ///< signed 16 bits, planar
        AV_SAMPLE_FMT_S32P,        ///< signed 32 bits, planar
        AV_SAMPLE_FMT_FLTP,        ///< float, planar
        AV_SAMPLE_FMT_DBLP,        ///< double, planar
        AV_SAMPLE_FMT_S64,         ///< signed 64 bits
        AV_SAMPLE_FMT_S64P,        ///< signed 64 bits, planar
    
        AV_SAMPLE_FMT_NB           ///< Number of sample formats. DO NOT USE if linking dynamically
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    音频信道数

    代码

    // channels: 音频信道数目
    printf("channel number:%d\n", in_stream->codecpar->channels);
    
    • 1
    • 2

    结果

    channel number:2
    
    • 1

    音频编解码格式

    代码

    // codec_id: 音频压缩编码格式
    if (AV_CODEC_ID_AAC == in_stream->codecpar->codec_id)
    {
        printf("audio codec:AAC\n");
    }
    else if (AV_CODEC_ID_MP3 == in_stream->codecpar->codec_id)
    {
        printf("audio codec:MP3\n");
    }
    else
    {
        printf("audio codec_id:%d\n", in_stream->codecpar->codec_id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结果

    audio codec:AAC
    
    • 1

    编解码器枚举量

    //avcodec.h 215行-710行
    enum AVCodecID {...}
    
    • 1
    • 2

    音频长度

    代码

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

    结果

    audio duration: 00:05:17
    
    • 1

    av_q2d作用

    将AVRational_分数格式的时间转换成浮点数

    static inline double av_q2d(AVRational a){
        return a.num / (double) a.den;
    }
    
    typedef struct AVRational{
        int num; ///< 分子
        int den; ///< 分母
    } AVRational;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    完整代码

    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)
        {
            printf("----- Audio info:\n");
            // index: 每个流成分在ffmpeg解复用分析后都有唯一的index作为标识
            printf("index:%d\n", in_stream->index);
            // sample_rate: 音频编解码器的采样率,单位为Hz
            printf("samplerate:%dHz\n", in_stream->codecpar->sample_rate);
            // codecpar->format: 音频采样格式
            if (AV_SAMPLE_FMT_FLTP == in_stream->codecpar->format)
            {
                printf("sampleformat:AV_SAMPLE_FMT_FLTP\n");
            }
            else if (AV_SAMPLE_FMT_S16P == in_stream->codecpar->format)
            {
                printf("sampleformat:AV_SAMPLE_FMT_S16P\n");
            }
            // channels: 音频信道数目
            printf("channel number:%d\n", in_stream->codecpar->channels);
            // codec_id: 音频压缩编码格式
            if (AV_CODEC_ID_AAC == in_stream->codecpar->codec_id)
            {
                printf("audio codec:AAC\n");
            }
            else if (AV_CODEC_ID_MP3 == in_stream->codecpar->codec_id)
            {
                printf("audio codec:MP3\n");
            }
            else
            {
                printf("audio codec_id:%d\n", in_stream->codecpar->codec_id);
            }
            // 音频总时长,单位为秒。注意如果把单位放大为毫秒或者微妙,音频总时长跟视频总时长不一定相等的
            if(in_stream->duration != AV_NOPTS_VALUE)
            {
                int duration_audio = (in_stream->duration) * av_q2d(in_stream->time_base);
                //将音频总时长转换为时分秒的格式打印到控制台上
                printf("audio duration: %02d:%02d:%02d\n",
                       duration_audio / 3600, (duration_audio % 3600) / 60, (duration_audio % 60));
            }
            else
            {
                printf("audio duration unknown");
            }
    
            printf("\n");
    
            audioindex = i; // 获取音频的索引
        }
        else if (AVMEDIA_TYPE_VIDEO == in_stream->codecpar->codec_type)  //如果是视频流,则打印视频的信息
        {...}
    }
    
    • 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
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    结果

    ----- Audio info:
    index:1
    samplerate:48000Hz
    sampleformat:AV_SAMPLE_FMT_FLTP
    channel number:2
    audio codec:AAC
    audio duration: 00:05:17
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    ros学习笔记13——unknown package [sensor_msgs] on search path [{{‘ros_to_deepstream
    VMware Horizon 8 运维系列(四)云桌面虚拟机被移除网卡
    MySQL进阶 —— 超详细操作演示!!!(上)
    vue3中<script setup> 和 setup函数的区别
    vscode 调试使用 make 编译的项目
    DNS 查询原理详解
    优质短视频的10个共同点,戳中两个就能提高爆款几率!
    猿创征文|【第5天】SQL快速入门-必会的常用函数(SQL 小虚竹)
    基于Spring Boot的体育馆管理系统的设计与实现
    【C语言】数组详解,初学者一看就懂
  • 原文地址:https://blog.csdn.net/qq_43537701/article/details/132912279