下面是ffmpeg examples中的一个例子,用于获取输入文件的metadata信息。
#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavutil/dict.h>
int main (int argc, char **argv)
{
AVFormatContext *fmt_ctx = NULL;
AVDictionaryEntry *tag = NULL;
int ret;
if (argc != 2) {
printf("usage: %s <input_file>\n"
"example program to demonstrate the use of the libavformat metadata API.\n"
"\n", argv[0]);
return 1;
}
av_register_all();
if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL)))
return ret;
while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
printf("%s=%s\n", tag->key, tag->value);
avformat_close_input(&fmt_ctx);
return 0;
}
添加一些metadata信息可以使用如下代码,如果dict为NULL则方法内部会新建一个AVDictionary ,然后将key,value设置进去。
//method 1
AVDictionary *dict=/*NULL*/fmt_ctx->metadata;
av_dict_set(&dict,"author","cwl",0);
//method 2
char *k = av_strdup("key"); // if your strings are already allocated,
char *v = av_strdup("value"); // you can avoid copying them like this
av_dict_set(&dict, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
获取某个具体的信息如下
AVDictionaryEntry *ae=av_dict_get(dict,"author",NULL,AV_DICT_IGNORE_SUFFIX);
printf("%s",ae->value);
以字符串形式返回全部的metadata信息,第三个参数表示key-value之间的分隔符,第四个参数表示元信息之间得分隔符
char *buff=NULL;
int val=av_dict_get_string(dict,&buff,'-','=');
可以通过如下代码将字符串形式的metadata信息设置到AVDictionary中
AVDictionary *ndict=NULL;
av_dict_parse_string(&ndict,buff,"-","=",