本文是我的 FFMPEG Tips 系列的第五篇文章,准备介绍下 ffmpeg 提供的一个非常好用的健值对工具:AVDictionary,特别是对于没有 map 容器的 c 代码,可以充分利用它来配置和定义播放器的参数,ffmpeg 本身也有很多 API 通过它来传递参数。
1. AVDictionary 的用法简介
AVDictionary 所在的头文件在 libavutil/dict.h,其定义如下:
struct AVDictionary {
int count;
AVDictionaryEntry *elems;
};
其中,AVDictionaryEntry 的定义如下:
typedef struct AVDictionaryEntry {
char *key;
char *value;
} AVDictionaryEntry;
下面就用示例的方式简单介绍下用法
(1)创建一个字典
AVDictionary *d = NULL;
(2) 销毁一个字典
av_dict_free(&d);
(3)添加一对 key-value
av_dict_set(&d, "name", "jhuster", 0);
av_dict_set_int(&d, "age", "29", 0);
(4) 获取 key 的值
AVDictionaryEntry *t = NULL;
t = av_dict_get(d, "name", NULL, AV_DICT_IGNORE_SUFFIX);
av_log(NULL, AV_LOG_DEBUG, "name: %s", t->value);
t = av_dict_get(d, "age", NULL, AV_DICT_IGNORE_SUFFIX);
av_log(NULL, AV_LOG_DEBUG, "age: %d", (int) (*t->value));
(5) 遍历字典
AVDictionaryEntry *t = NULL;
while ((t = av_dict_get(d, "", t, AV_DICT_IGNORE_SUFFIX))) {
av_log(NULL, AV_LOG_DEBUG, "%s: %s", t->key, t->value);
}
2. ffmpeg 参数的传递
ffmpeg 中很多 API 都是靠 AVDictionary 来传递参数的,比如常用的:
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
最后一个参数就是 AVDictionary,我们可以在打开码流前指定各种参数,比如:探测时间、超时时间、最大延时、支持的协议的白名单等等,例如:
AVDictionary *options = NULL;
av_dict_set(&options, “probesize”, “4096", 0);
av_dict_set(&options, “max_delay”, “5000000”, 0);
AVFormatContext *ic = avformat_alloc_context();
if (avformat_open_input(&ic, url, NULL, &options) < 0) {
LOGE("could not open source %s", url);
return -1;
}
那么,我们怎么知道 ffmpeg 的这个 API 支持哪些可配置的参数呢 ?
我们可以查看 ffmpeg 源码,比如 avformat_open_input 是结构体 AVFormatContext 提供的 API,在 libavformat/options_table.h 中定义了 AVFormatContext 所有支持的 options 选项,如下所示:
www.imdb.com/list/ls083025713/?kckcc=KS
www.imdb.com/list/ls083025713/?mode=desktop
www.imdb.com/list/ls083025714/
www.imdb.com/list/ls083025714/?ref_=otl_j
m.imdb.com/list/ls083025714/
www.imdb.com/list/ls083025714/?ref_=otl_Pb
www.imdb.com/list/ls083025714/?ref_=otl_1
www.imdb.com/list/ls083025714/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025714/?fpfxx=Pb
www.imdb.com/list/ls083025714/?mode=desktop
www.imdb.com/list/ls083025760/
www.imdb.com/list/ls083025760/?ref_=otl_9
m.imdb.com/list/ls083025760/
www.imdb.com/list/ls083025760/?ref_=otl_rD
www.imdb.com/list/ls083025760/?ref_=otl_1
www.imdb.com/list/ls083025760/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025760/?tjznd=rD
www.imdb.com/list/ls083025760/?mode=desktop
www.imdb.com/list/ls083025763/
www.imdb.com/list/ls083025763/?ref_=otl_o
m.imdb.com/list/ls083025763/
www.imdb.com/list/ls083025763/?ref_=otl_6Y
www.imdb.com/list/ls083025763/?ref_=otl_6
www.imdb.com/list/ls083025763/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025763/?oygge=6Y
www.imdb.com/list/ls083025763/?mode=desktop
www.imdb.com/list/ls083025725/
www.imdb.com/list/ls083025725/?ref_=otl_5
m.imdb.com/list/ls083025725/
www.imdb.com/list/ls083025725/?ref_=otl_DH
www.imdb.com/list/ls083025725/?ref_=otl_5
www.imdb.com/list/ls083025725/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025725/?hvhjn=DH
www.imdb.com/list/ls083025725/?mode=desktop
www.imdb.com/list/ls083025726/
www.imdb.com/list/ls083025726/?ref_=otl_P
m.imdb.com/list/ls083025726/
www.imdb.com/list/ls083025726/?ref_=otl_57
www.imdb.com/list/ls083025726/?ref_=otl_5
www.imdb.com/list/ls083025726/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025726/?pdhfx=57
www.imdb.com/list/ls083025726/?mode=desktop
www.imdb.com/list/ls083025790/
www.imdb.com/list/ls083025790/?ref_=otl_x
m.imdb.com/list/ls083025790/
www.imdb.com/list/ls083025790/?ref_=otl_l3
www.imdb.com/list/ls083025790/?ref_=otl_6
www.imdb.com/list/ls083025790/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025790/?eqceu=l3
www.imdb.com/list/ls083025790/?mode=desktop
www.imdb.com/list/ls083025797/
www.imdb.com/list/ls083025797/?ref_=otl_q
m.imdb.com/list/ls083025797/
www.imdb.com/list/ls083025797/?ref_=otl_Sc
www.imdb.com/list/ls083025797/?ref_=otl_0
www.imdb.com/list/ls083025797/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025797/?ymqom=Sc
www.imdb.com/list/ls083025797/?mode=desktop
www.imdb.com/list/ls083025781/
www.imdb.com/list/ls083025781/?ref_=otl_5
m.imdb.com/list/ls083025781/
www.imdb.com/list/ls083025781/?ref_=otl_1Z
www.imdb.com/list/ls083025781/?ref_=otl_5
www.imdb.com/list/ls083025781/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025781/?hrhlf=1Z
www.imdb.com/list/ls083025781/?mode=desktop
www.imdb.com/list/ls083025100/
www.imdb.com/list/ls083025100/?ref_=otl_1
m.imdb.com/list/ls083025100/
www.imdb.com/list/ls083025100/?ref_=otl_7X
www.imdb.com/list/ls083025100/?ref_=otl_1
www.imdb.com/list/ls083025100/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025100/?dbrdh=7X
www.imdb.com/list/ls083025100/?mode=desktop
www.imdb.com/list/ls083025156/
www.imdb.com/list/ls083025156/?ref_=otl_X
m.imdb.com/list/ls083025156/
www.imdb.com/list/ls083025156/?ref_=otl_9h
www.imdb.com/list/ls083025156/?ref_=otl_5
www.imdb.com/list/ls083025156/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025156/?vddjl=9h
www.imdb.com/list/ls083025156/?mode=desktop
www.imdb.com/list/ls083025159/
www.imdb.com/list/ls083025159/?ref_=otl_b
m.imdb.com/list/ls083025159/
www.imdb.com/list/ls083025159/?ref_=otl_Xb
www.imdb.com/list/ls083025159/?ref_=otl_3
www.imdb.com/list/ls083025159/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025159/?hxnnx=Xb
www.imdb.com/list/ls083025159/?mode=desktop
www.imdb.com/list/ls083025172/
www.imdb.com/list/ls083025172/?ref_=otl_x
m.imdb.com/list/ls083025172/
www.imdb.com/list/ls083025172/?ref_=otl_75
www.imdb.com/list/ls083025172/?ref_=otl_3
www.imdb.com/list/ls083025172/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025172/?jzfzt=75
www.imdb.com/list/ls083025172/?mode=desktop
www.imdb.com/list/ls083025111/
www.imdb.com/list/ls083025111/?ref_=otl_6
m.imdb.com/list/ls083025111/
www.imdb.com/list/ls083025111/?ref_=otl_2Y
www.imdb.com/list/ls083025111/?ref_=otl_8
www.imdb.com/list/ls083025111/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025111/?uosui=2Y
www.imdb.com/list/ls083025111/?mode=desktop
www.imdb.com/list/ls083025137/
www.imdb.com/list/ls083025137/?ref_=otl_1
m.imdb.com/list/ls083025137/
www.imdb.com/list/ls083025137/?ref_=otl_v9
www.imdb.com/list/ls083025137/?ref_=otl_3
www.imdb.com/list/ls083025137/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025137/?vnvvd=v9
www.imdb.com/list/ls083025137/?mode=desktop
www.imdb.com/list/ls083025136/
www.imdb.com/list/ls083025136/?ref_=otl_K
m.imdb.com/list/ls083025136/
www.imdb.com/list/ls083025136/?ref_=otl_24
www.imdb.com/list/ls083025136/?ref_=otl_8
www.imdb.com/list/ls083025136/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025136/?uiygy=24
www.imdb.com/list/ls083025136/?mode=desktop
www.imdb.com/list/ls083025168/
www.imdb.com/list/ls083025168/?ref_=otl_0
m.imdb.com/list/ls083025168/
www.imdb.com/list/ls083025168/?ref_=otl_IW
www.imdb.com/list/ls083025168/?ref_=otl_2
www.imdb.com/list/ls083025168/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025168/?qooaq=IW
www.imdb.com/list/ls083025168/?mode=desktop
www.imdb.com/list/ls083025126/
www.imdb.com/list/ls083025126/?ref_=otl_5
m.imdb.com/list/ls083025126/
www.imdb.com/list/ls083025126/?ref_=otl_RL
www.imdb.com/list/ls083025126/?ref_=otl_5
www.imdb.com/list/ls083025126/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025126/?xxhxr=RL
www.imdb.com/list/ls083025126/?mode=desktop
www.imdb.com/list/ls083025143/
www.imdb.com/list/ls083025143/?ref_=otl_1
m.imdb.com/list/ls083025143/
www.imdb.com/list/ls083025143/?ref_=otl_3P
www.imdb.com/list/ls083025143/?ref_=otl_3
www.imdb.com/list/ls083025143/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025143/?lfbxl=3P
www.imdb.com/list/ls083025143/?mode=desktop
www.imdb.com/list/ls083025149/
www.imdb.com/list/ls083025149/?ref_=otl_4
m.imdb.com/list/ls083025149/
www.imdb.com/list/ls083025149/?ref_=otl_uU
www.imdb.com/list/ls083025149/?ref_=otl_2
www.imdb.com/list/ls083025149/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025149/?kksaw=uU
www.imdb.com/list/ls083025149/?mode=desktop
www.imdb.com/list/ls083025180/
www.imdb.com/list/ls083025180/?ref_=otl_M
m.imdb.com/list/ls083025180/
www.imdb.com/list/ls083025180/?ref_=otl_KI
www.imdb.com/list/ls083025180/?ref_=otl_8
www.imdb.com/list/ls083025180/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083025180/?oociq=KI
www.imdb.com/list/ls083025180/?mode=desktop
www.imdb.com/list/ls083069447/
www.imdb.com/list/ls083069447/?ref_=otl_1
m.imdb.com/list/ls083069447/
www.imdb.com/list/ls083069447/?ref_=otl_97
www.imdb.com/list/ls083069447/?ref_=otl_3
www.imdb.com/list/ls083069447/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069447/?fdxxd=97
www.imdb.com/list/ls083069447/?mode=desktop
www.imdb.com/list/ls083069444/
www.imdb.com/list/ls083069444/?ref_=otl_b
m.imdb.com/list/ls083069444/
www.imdb.com/list/ls083069444/?ref_=otl_77
www.imdb.com/list/ls083069444/?ref_=otl_9
www.imdb.com/list/ls083069444/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069444/?pfbnt=77
www.imdb.com/list/ls083069444/?mode=desktop
www.imdb.com/list/ls083069489/
www.imdb.com/list/ls083069489/?ref_=otl_2
m.imdb.com/list/ls083069489/
www.imdb.com/list/ls083069489/?ref_=otl_AK
www.imdb.com/list/ls083069489/?ref_=otl_0
www.imdb.com/list/ls083069489/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069489/?suswu=AK
www.imdb.com/list/ls083069489/?mode=desktop
www.imdb.com/list/ls083069903/
www.imdb.com/list/ls083069903/?ref_=otl_4
m.imdb.com/list/ls083069903/
www.imdb.com/list/ls083069903/?ref_=otl_0U
www.imdb.com/list/ls083069903/?ref_=otl_2
www.imdb.com/list/ls083069903/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069903/?wswqe=0U
www.imdb.com/list/ls083069903/?mode=desktop
www.imdb.com/list/ls083069954/
www.imdb.com/list/ls083069954/?ref_=otl_b
m.imdb.com/list/ls083069954/
www.imdb.com/list/ls083069954/?ref_=otl_N7
www.imdb.com/list/ls083069954/?ref_=otl_7
www.imdb.com/list/ls083069954/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069954/?vvzjb=N7
www.imdb.com/list/ls083069954/?mode=desktop
www.imdb.com/list/ls083069979/
www.imdb.com/list/ls083069979/?ref_=otl_a
m.imdb.com/list/ls083069979/
www.imdb.com/list/ls083069979/?ref_=otl_4M
www.imdb.com/list/ls083069979/?ref_=otl_4
www.imdb.com/list/ls083069979/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069979/?egkom=4M
www.imdb.com/list/ls083069979/?mode=desktop
www.imdb.com/list/ls083069935/
www.imdb.com/list/ls083069935/?ref_=otl_3
m.imdb.com/list/ls083069935/
www.imdb.com/list/ls083069935/?ref_=otl_3D
www.imdb.com/list/ls083069935/?ref_=otl_3
www.imdb.com/list/ls083069935/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069935/?zvbrb=3D
www.imdb.com/list/ls083069935/?mode=desktop
www.imdb.com/list/ls083069932/
www.imdb.com/list/ls083069932/?ref_=otl_j
m.imdb.com/list/ls083069932/
www.imdb.com/list/ls083069932/?ref_=otl_XJ
www.imdb.com/list/ls083069932/?ref_=otl_7
www.imdb.com/list/ls083069932/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069932/?rtdpz=XJ
www.imdb.com/list/ls083069932/?mode=desktop
www.imdb.com/list/ls083069962/
www.imdb.com/list/ls083069962/?ref_=otl_i
m.imdb.com/list/ls083069962/
www.imdb.com/list/ls083069962/?ref_=otl_60
www.imdb.com/list/ls083069962/?ref_=otl_2
www.imdb.com/list/ls083069962/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069962/?yqgii=60
www.imdb.com/list/ls083069962/?mode=desktop
www.imdb.com/list/ls083069922/
www.imdb.com/list/ls083069922/?ref_=otl_U
m.imdb.com/list/ls083069922/
www.imdb.com/list/ls083069922/?ref_=otl_A2
www.imdb.com/list/ls083069922/?ref_=otl_8
www.imdb.com/list/ls083069922/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069922/?umgim=A2
www.imdb.com/list/ls083069922/?mode=desktop
www.imdb.com/list/ls083069944/
www.imdb.com/list/ls083069944/?ref_=otl_2
m.imdb.com/list/ls083069944/
www.imdb.com/list/ls083069944/?ref_=otl_24
www.imdb.com/list/ls083069944/?ref_=otl_2
www.imdb.com/list/ls083069944/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069944/?sgwce=24
www.imdb.com/list/ls083069944/?mode=desktop
www.imdb.com/list/ls083069991/
www.imdb.com/list/ls083069991/?ref_=otl_p
m.imdb.com/list/ls083069991/
www.imdb.com/list/ls083069991/?ref_=otl_tL
www.imdb.com/list/ls083069991/?ref_=otl_5
www.imdb.com/list/ls083069991/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069991/?vzbvt=tL
www.imdb.com/list/ls083069991/?mode=desktop
www.imdb.com/list/ls083069986/
www.imdb.com/list/ls083069986/?ref_=otl_V
m.imdb.com/list/ls083069986/
www.imdb.com/list/ls083069986/?ref_=otl_Vf
www.imdb.com/list/ls083069986/?ref_=otl_9
www.imdb.com/list/ls083069986/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069986/?fvnnn=Vf
www.imdb.com/list/ls083069986/?mode=desktop
www.imdb.com/list/ls083069989/
www.imdb.com/list/ls083069989/?ref_=otl_7
m.imdb.com/list/ls083069989/
www.imdb.com/list/ls083069989/?ref_=otl_hN
www.imdb.com/list/ls083069989/?ref_=otl_1
www.imdb.com/list/ls083069989/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069989/?rvxnd=hN
www.imdb.com/list/ls083069989/?mode=desktop
www.imdb.com/list/ls083069853/
www.imdb.com/list/ls083069853/?ref_=otl_v
m.imdb.com/list/ls083069853/
www.imdb.com/list/ls083069853/?ref_=otl_rB
www.imdb.com/list/ls083069853/?ref_=otl_9
www.imdb.com/list/ls083069853/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069853/?phrbl=rB
www.imdb.com/list/ls083069853/?mode=desktop
www.imdb.com/list/ls083069877/
www.imdb.com/list/ls083069877/?ref_=otl_j
m.imdb.com/list/ls083069877/
www.imdb.com/list/ls083069877/?ref_=otl_t5
www.imdb.com/list/ls083069877/?ref_=otl_7
www.imdb.com/list/ls083069877/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069877/?tztlv=t5
www.imdb.com/list/ls083069877/?mode=desktop
www.imdb.com/list/ls083069812/
www.imdb.com/list/ls083069812/?ref_=otl_Z
m.imdb.com/list/ls083069812/
www.imdb.com/list/ls083069812/?ref_=otl_j5
www.imdb.com/list/ls083069812/?ref_=otl_1
www.imdb.com/list/ls083069812/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069812/?tddln=j5
www.imdb.com/list/ls083069812/?mode=desktop
www.imdb.com/list/ls083069837/
www.imdb.com/list/ls083069837/?ref_=otl_4
m.imdb.com/list/ls083069837/
www.imdb.com/list/ls083069837/?ref_=otl_4S
www.imdb.com/list/ls083069837/?ref_=otl_6
www.imdb.com/list/ls083069837/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069837/?gegca=4S
www.imdb.com/list/ls083069837/?mode=desktop
www.imdb.com/list/ls083069861/
www.imdb.com/list/ls083069861/?ref_=otl_g
m.imdb.com/list/ls083069861/
www.imdb.com/list/ls083069861/?ref_=otl_48
www.imdb.com/list/ls083069861/?ref_=otl_4
www.imdb.com/list/ls083069861/?mode=desktop&ref_=m_ft_dsk
www.imdb.com/list/ls083069861/?wiaec=48
www.imdb.com/list/ls083069861/?mode=desktop
www.imdb.com/list/ls083069869/
www.imdb.com/list/ls083069869/?ref_=otl_T
m.imdb.com/list/ls083069869/
www.imdb.com/list/ls083069869/?ref_=otl_77
www.imdb.com/list/ls083069869/?ref_=otl_7
www.imdb.com/list/ls083069869/?mode=desktop&ref_=m_ft_dsk
同理,AVCodec 相关 API 支持的 options 选项则可以在 libavcodec/options_table.h 文件中找到,如下所示:
3. 小结
当然,我们也可以仿照 ffmpeg,给自己的核心结构体对象定义这样的 options 选项,这篇文章就不展开详述了。