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
相关推荐
给大佬递杯卡布奇诺3 小时前
FFmpeg 基本API avcodec_alloc_context3函数内部调用流程分析
c++·ffmpeg·音视频
给大佬递杯卡布奇诺5 小时前
FFmpeg 基本API avio_open函数内部调用流程分析
c++·ffmpeg·音视频
Damon小智7 小时前
RedPlayer 视频播放器在 HarmonyOS 应用中的实践
音视频·harmonyos·鸿蒙·小红书·三方库·redplayer
云雾J视界9 小时前
Linux企业级解决方案架构:字节跳动短视频推荐系统全链路实践
linux·云原生·架构·kubernetes·音视频·glusterfs·elk stack
Likeadust11 小时前
新版视频直播点播平台EasyDSS用视频破局,获客转化双提升
大数据·音视频
涛涛讲AI1 天前
一段音频多段字幕,让音频能够流畅自然对应字幕 AI生成视频,扣子生成剪映视频草稿
人工智能·音视频·语音识别
lzptouch1 天前
数据预处理(音频/图像/视频/文字)及多模态统一大模型输入方案
人工智能·音视频
casdfxx1 天前
捡到h3开发板,做了个视频小车(二),御游追风plus做遥控器
音视频
mortimer1 天前
FFmpeg 音画同步实践记录:从切片、变速到拼接,彻底搞定时间轴
ffmpeg
给大佬递杯卡布奇诺1 天前
FFmpeg 基本API avcodec_send_packet函数内部调用流程分析
c++·ffmpeg·音视频