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

标志位

代码

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

结果

index:1

音频采样率

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

结果

samplerate:48000Hz

音频采样格式

代码

c 复制代码
// 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");
}

结果

sampleformat:AV_SAMPLE_FMT_FLTP

采样格式枚举量简单介绍

c 复制代码
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
};

音频信道数

代码

c 复制代码
// channels: 音频信道数目
printf("channel number:%d\n", in_stream->codecpar->channels);

结果

channel number:2

音频编解码格式

代码

c 复制代码
// 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);
}

结果

audio codec:AAC

编解码器枚举量

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

音频长度

代码

c 复制代码
// 音频总时长,单位为秒。注意如果把单位放大为毫秒或者微妙,音频总时长跟视频总时长不一定相等的
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");
}

结果

audio duration: 00:05:17

av_q2d作用

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

static inline double av_q2d(AVRational a){
    return a.num / (double) a.den;
}

typedef struct AVRational{
    int num; ///< 分子
    int den; ///< 分母
} AVRational;

完整代码

c 复制代码
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)  //如果是视频流,则打印视频的信息
    {...}
}

结果

----- Audio info:
index:1
samplerate:48000Hz
sampleformat:AV_SAMPLE_FMT_FLTP
channel number:2
audio codec:AAC
audio duration: 00:05:17
相关推荐
远方2.07 分钟前
AI视频生成工具清单(附网址与免费说明)
人工智能·音视频
EasyCVR8 小时前
EasyRTC嵌入式视频通话SDK的跨平台适配,构建web浏览器、Linux、ARM、安卓等终端的低延迟音视频通信
android·arm开发·网络协议·tcp/ip·音视频·webrtc
ihmhm1234513 小时前
2025-03-06 ffmpeg提取SPS/PPS/SEI ( extradata )
ffmpeg
StudyWinter13 小时前
FFmpeg-chapter7和chapter8-使用 FFmpeg 解码视频(原理篇和实站篇)
ffmpeg·音视频
T风呤13 小时前
ffmpeg windows 基本命令
windows·ffmpeg
firstime_tzjz13 小时前
windows下使用msys2编译ffmpeg
windows·ffmpeg
余~~1853816280014 小时前
碰一碰发视频系统之写卡功能开发了,支持OEM
线性代数·矩阵·音视频
q5673152314 小时前
用Go的resty库批量下载公开网站视频
开发语言·golang·音视频
AJi16 小时前
FFmpeg学习(五):音视频数据转换
ffmpeg·音视频开发·视频编码
JAVA叶知秋18 小时前
完美解决uni-app打开页面无法自动播放视频的问题
前端·uni-app·音视频