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
相关推荐
hjjdebug9 小时前
ffmpeg -map 是什么意思?
ffmpeg·map
AI营销快线12 小时前
车企AI营销内容生产:2025图文与视频生成实战指南
人工智能·音视频
EasyCVR13 小时前
视频汇聚平台EasyCVR助力农场实现全场景可视化管理
大数据·人工智能·音视频
漫长的~以后14 小时前
Qwen2.5-Omni横空出世:四模态统一模型如何破解音视频同步难题?
音视频
Likeadust15 小时前
视频推流平台EasyDSS无人机推流直播技术在国土测绘中的创新应用
音视频·无人机
小咖自动剪辑15 小时前
提升电商素材剪辑效率:多场景内容自动生成流程解析
人工智能·实时互动·音视频·语音识别·视频编解码
优选资源分享16 小时前
MCC音频剪辑工具v1.1.0.0:自动处理配音气口间隙
音视频
昨日之日200616 小时前
HunyuanVideo-Foley V2版 - AI视频配音 自动识别视频内容并配音 支持50系显卡 一键整合包下载
人工智能·音视频
@YDWLCloud17 小时前
出海 APP 如何降低延迟?腾讯云国际版 GME 音视频深度评测
大数据·服务器·云计算·音视频·腾讯云
黑客思维者17 小时前
Python自动化截图/录屏3大方案(PIL/mss/ffmpeg)深度拆解
python·ffmpeg·自动化·录屏