FFMPEG Tips (5) 如何利用 AVDictionary 配置引數
本文是我的 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 選項,這篇文章就不展開詳述了。