目录
[二、视频 / 像素格式类](#二、视频 / 像素格式类)
[三、Codec / Media 类型(avcodec)](#三、Codec / Media 类型(avcodec))
[四、错误码 / 状态(调试必用)](#四、错误码 / 状态(调试必用))
[五、AVFormat / IO / Seek 相关](#五、AVFormat / IO / Seek 相关)
[六、Log / Level / DTS 之类](#六、Log / Level / DTS 之类)
[八、Qt / C++ 友好封装](#八、Qt / C++ 友好封装)
觉得有用,就请您帮忙点赞转发收藏吧,您的鼓励是我创作的动力,多谢看官。
由于能力水平有限,文中的错误或不严谨的地方在所难免,还请批评指正。
在FFmpeg库中,处理枚举类型并获取它们的名称是一个常见的需求,尤其是在调试或显示信息时。FFmpeg提供了多种函数来实现这一功能,这些函数通常位于
libavutil和libavcodec库中。以下是一些常用的函数,它们可以帮助你根据枚举获取名称。
结论先说:
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()