[Libav-user] Metadata manipulation

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Libav-user] Metadata manipulation

Stefan Isidorović
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


_______________________________________________
Libav-user mailing list
[hidden email]
http://ffmpeg.org/mailman/listinfo/libav-user