[Libav-user] Metadata manipulation

Stefan Isidorović teva.biz at gmail.com
Fri Jul 7 12:02:49 EEST 2017


Hi all,

I am trying to write function for manipulating metadata of audio file.

Idea is to read and check if exists specific field. And another which is
problematic is to save values under specific name.

I am trying to use AVFormatContext->metadata. Its not problem for reading
but it is problematic when I try to use to save new field and values.

Here is example code.

av_register_all();

AVFormatContext* ctx;
std::string path("/home/stefan/test_track.mp3");

ctx = avformat_alloc_context();

if (avformat_open_input(&ctx, path.c_str(), 0, 0) < 0)
std::cout << "error1" << std::endl;

if (avformat_find_stream_info(ctx, 0) < 0)
std::cout << "error2" << std::endl;

AVDictionary* newDict = nullptr;
AVDictionaryEntry *tag = nullptr;
while( (tag = av_dict_get(ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))){
av_dict_set(&newDict, tag->key, tag->value, 0);
}

tag = av_dict_get(ctx->metadata, "artist", tag, AV_DICT_IGNORE_SUFFIX);
std::cout << tag->key << " : " << tag->value << std::endl;

av_dict_set(&newDict, "custom", "testtest", 0);

std::cout << "test!" << std::endl;
int status = avformat_write_header(ctx, &newDict);

if(status == 0)
std::cout << "test1" << std::endl;

return 0;

Also I tried first with this;

av_register_all();

AVFormatContext* ctx;
std::string path("/home/stefan/test_track.mp3");

ctx = avformat_alloc_context();

if (avformat_open_input(&ctx, path.c_str(), 0, 0) < 0)
std::cout << "error1" << std::endl;

if (avformat_find_stream_info(ctx, 0) < 0)
std::cout << "error2" << std::endl;

AVDictionaryEntry *tag = nullptr;
tag = av_dict_get(ctx->metadata, "artist", tag, AV_DICT_IGNORE_SUFFIX);
std::cout << tag->key << " : " << tag->value << std::endl;

av_dict_set(&ctx->metadata, "TPFL", "testtest", 0);

std::cout << "test!" << std::endl;
int status = avformat_write_header(ctx, &ctx->metadata);

if(status == 0)
std::cout << "test1" << std::endl;

return 0;

But both solutions result with Segmentation Fault error.

Can someone tell me where I am making mistake? Or how to do this correct?

Best regards,
Stefan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170707/b845f333/attachment.html>


More information about the Libav-user mailing list