FFmpeg中根据枚举获取名称的相关函数总结,例如av_get_sample_fmt_name和avcodec_get_name等

目录

一、音频类

[二、视频 / 像素格式类](#二、视频 / 像素格式类)

[三、Codec / Media 类型(avcodec)](#三、Codec / Media 类型(avcodec))

[四、错误码 / 状态(调试必用)](#四、错误码 / 状态(调试必用))

[五、AVFormat / IO / Seek 相关](#五、AVFormat / IO / Seek 相关)

[六、Log / Level / DTS 之类](#六、Log / Level / DTS 之类)

七、完整对照速查表

[八、Qt / C++ 友好封装](#八、Qt / C++ 友好封装)

九、一个"打印流信息"的一行版

十、一句话总结


觉得有用,就请您帮忙点赞转发收藏吧,您的鼓励是我创作的动力,多谢看官。

由于能力水平有限,文中的错误或不严谨的地方在所难免,还请批评指正。

在FFmpeg库中,处理枚举类型并获取它们的名称是一个常见的需求,尤其是在调试或显示信息时。FFmpeg提供了多种函数来实现这一功能,这些函数通常位于libavutillibavcodec库中。以下是一些常用的函数,它们可以帮助你根据枚举获取名称。

结论先说:

FFmpeg 几乎所有核心 enum 都有官方 get_name函数

但名字不统一(有 av_*/ avcodec_*/ avutil_*

下面按模块整理,表格为主,实战优先


一、音频类

枚举 获取名称函数 返回内容 示例
AVSampleFormat av_get_sample_fmt_name() "s16", "fltp" av_get_sample_fmt_name(AV_SAMPLE_FMT_S16)
AVChannelLayout av_channel_layout_describe() "stereo", "5.1" av_channel_layout_describe(&ch)
AVChannel av_get_channel_name() "FL", "FR" av_get_channel_name(AV_CHAN_FRONT_LEFT)
AVChannelOrder av_channel_order_name() "native", "wave" FFmpeg ≥ 5.1
AVSampleRate(无 enum) 自己 QString::number() -- --

音频三件套(Qt 调试神器):

复制代码
qDebug("fmt=%s ch=%s rate=%d",
       av_get_sample_fmt_name(fmt),
       av_channel_layout_describe(&layout),
       rate);

二、视频 / 像素格式类

枚举 获取名称函数 返回内容 示例
AVPixelFormat av_get_pix_fmt_name() "yuv420p", "rgb24"
AVColorSpace av_color_space_name() "bt709", "bt2020" FFmpeg ≥ 6
AVColorPrimaries av_color_primaries_name() "bt709"
AVColorTransferCharacteristic av_color_transfer_name() "srgb", "pq"
AVColorRange av_color_range_name() "mpeg", "jpeg"
AVFieldOrder av_frame_get_side_data()+ enum "top_bottom" 少直接用

三、Codec / Media 类型(avcodec)

枚举 函数 返回
AVCodecID avcodec_get_name() "aac", "h264"
AVMediaType av_get_media_type_string() "audio", "video"
AVProfile avcodec_profile_name() "High", "Main"

打印流信息(替代 av_dump_format 的轻量版):

复制代码
qDebug("[%s] codec=%s profile=%s",
       av_get_media_type_string(st->codecpar->codec_type),
       avcodec_get_name(st->codecpar->codec_id),
       avcodec_profile_name(st->codecpar->codec_id, st->codecpar->profile));

四、错误码 / 状态(调试必用)

类型 函数 说明
int ret av_strerror() 转字符串(不是 enum)
AVERROR_EOF av_strerror() "End of file"
AVERROR(EAGAIN) "Resource temporarily unavailable"

标准写法:

复制代码
char err[128];
av_strerror(ret, err, sizeof err);
qDebug("ff err: %s", err);

五、AVFormat / IO / Seek 相关

枚举 函数 返回
AVFMT_*flags 无统一函数 自己 fmt_ctx->iformat->name
AVIOContextprotocol fmt_ctx->iformat->name "rtsp", "mp4"
seek flags AVSEEK_FLAG_BACKWARD手写

协议名:

复制代码
fmt_ctx->iformat->name   // "mov,mp4,m4a,3gp"

六、Log / Level / DTS 之类

枚举 函数
AVLogLevel 无(自己 switch)
AVDiscard
AVOptionType av_opt_type_name()(FFmpeg ≥ 6)

七、完整对照速查表

你看到的 enum 正确 API
AVCodecID id avcodec_get_name(id)
AVMediaType t av_get_media_type_string(t)
AVSampleFormat f av_get_sample_fmt_name(f)
AVPixelFormat p av_get_pix_fmt_name(p)
AVChannelLayout c av_channel_layout_describe()
int err av_strerror()
AVProfile p avcodec_profile_name()

八、Qt / C++ 友好封装

复制代码
inline QString ff_codec(const AVCodecParameters *p)
{
    return avcodec_get_name(p->codec_id);
}

inline QString ff_fmt(AVSampleFormat f)
{
    return av_get_sample_fmt_name(f);
}

inline QString ff_pix(AVPixelFormat f)
{
    return av_get_pix_fmt_name(f) ? : "unknown";
}

inline QString ff_err(int r)
{
    char b[256]; av_strerror(r, b, sizeof b); return b;
}

九、一个"打印流信息"的一行版

复制代码
void dump(AVStream *s)
{
    qDebug("type=%s codec=%s fmt=%s ch=%d rate=%d",
        av_get_media_type_string(s->codecpar->codec_type),
        avcodec_get_name(s->codecpar->codec_id),
        av_get_sample_fmt_name((AVSampleFormat)s->codecpar->format),
        s->codecpar->ch_layout.nb_channels,
        s->codecpar->sample_rate);
}

十、一句话总结

FFmpeg 的命名规则:

av_xxx_name()→ avutil / format

avcodec_xxx_name()→ codec

av_xxx_describe()→ channel / layout

错误一律 av_strerror()

相关推荐
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
2601_955662462 天前
短剧多人对白怎么配?2026年多角色配音工具对比
大数据·人工智能·音视频·语音识别
开发笔记-阿牛2 天前
蓝牙音乐灯拆解,又一次拆到蓝牙音乐灯芯片CK6865L
人工智能·单片机·嵌入式硬件·音视频·音频
旺仔小馒头wang2 天前
AI 智能手机发布会:赋予人类的五种新能力
人工智能·学习·音视频
纳祥科技2 天前
拆机拆到芯片级:ARC功放机里的NX7026、NX8420、NX6805
单片机·音视频·智能音箱
youandyouyou2 天前
律所内部培训共享的内网部署方案:架构、参数与三步落地
音视频
爱吃提升2 天前
文生视频核心
人工智能·音视频
2601_955662462 天前
抖音视频怎么做多语言配音?2026年AI配音工具与制作流程详解
人工智能·音视频·语音识别·视频
音视频牛哥2 天前
Android与HarmonyOS NEXT如何接入GB28181:从SIP到PS/RTP的完整链路
音视频·gb28181-2022对接·鸿蒙next gb28181·gb/t 28181-2022·gb/t 28181-2016·gb28181-2022平台
美狐美颜SDK开放平台2 天前
直播APP源码与视频美颜sdk如何配合?一套完整开发思路
android·人工智能·计算机视觉·音视频·直播美颜sdk