溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

FFMPEG Tips (5) 如何利用 AVDictionary 配置參數(shù)

發(fā)布時(shí)間:2020-07-21 15:27:13 來(lái)源:網(wǎng)絡(luò) 閱讀:22724 作者:Jhuster 欄目:開發(fā)技術(shù)

本文是我的 FFMPEG Tips 系列的第五篇文章,準(zhǔn)備介紹下 ffmpeg 提供的一個(gè)非常好用的健值對(duì)工具:AVDictionary,特別是對(duì)于沒(méi)有 map 容器的 c 代碼,可以充分利用它來(lái)配置和定義播放器的參數(shù),ffmpeg 本身也有很多 API 通過(guò)它來(lái)傳遞參數(shù)。


1.  AVDictionary 的用法簡(jiǎn)介


AVDictionary 所在的頭文件在 libavutil/dict.h,其定義如下:


struct AVDictionary {  
    int count;  
    AVDictionaryEntry *elems;  
};


其中,AVDictionaryEntry 的定義如下:


typedef struct AVDictionaryEntry {  
    char *key;  
    char *value;  
} AVDictionaryEntry;


下面就用示例的方式簡(jiǎn)單介紹下用法


(1)創(chuàng)建一個(gè)字典


AVDictionary *d = NULL;


(2) 銷毀一個(gè)字典


av_dict_free(&d);


(3)添加一對(duì) 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 參數(shù)的傳遞


ffmpeg 中很多 API 都是靠 AVDictionary 來(lái)傳遞參數(shù)的,比如常用的:


int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);


最后一個(gè)參數(shù)就是 AVDictionary,我們可以在打開碼流前指定各種參數(shù),比如:探測(cè)時(shí)間、超時(shí)時(shí)間、最大延時(shí)、支持的協(xié)議的白名單等等,例如:


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;
}


那么,我們?cè)趺粗?ffmpeg 的這個(gè) API 支持哪些可配置的參數(shù)呢 ?


我們可以查看 ffmpeg 源碼,比如 avformat_open_input 是結(jié)構(gòu)體 AVFormatContext 提供的 API,在 libavformat/options_table.h 中定義了 AVFormatContext 所有支持的 options 選項(xiàng),如下所示:


https://www.ffmpeg.org/doxygen/trunk/libavformat_2options__table_8h-source.html


同理,AVCodec 相關(guān) API 支持的 options 選項(xiàng)則可以在 libavcodec/options_table.h 文件中找到,如下所示:


https://www.ffmpeg.org/doxygen/3.1/libavcodec_2options__table_8h_source.html


3.  小結(jié)


當(dāng)然,我們也可以仿照 ffmpeg,給自己的核心結(jié)構(gòu)體對(duì)象定義這樣的 options 選項(xiàng),這篇文章就不展開詳述了。


關(guān)于如何利用 AVDictionary 配置參數(shù)就介紹到這兒了,文章中有不清楚的地方歡迎留言或者來(lái)信 lujun.hust@gmail.com 交流,關(guān)注我的新浪微博 @盧_俊 或者 微信公眾號(hào) @Jhuster 獲取最新的文章和資訊。


FFMPEG Tips (5) 如何利用 AVDictionary 配置參數(shù)

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI