ffmpeg metadata及AVDictionary分析

下面是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,"-","=",
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值