<div dir="ltr">Hi all,<div><br></div><div>I am trying to write function for manipulating metadata of audio file.</div><div><br></div><div>Idea is to read and check if exists specific field. And another which is problematic is to save values under specific name.</div><div><br></div><div>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.</div><div><br></div><div>Here is example code.</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre">    </span><font face="monospace" color="#0000ff">av_register_all();</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">     </span>AVFormatContext* ctx;</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">  </span>std::string path("/home/stefan/test_track.mp3");</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">      </span>ctx = avformat_alloc_context();</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre"> </span>if (avformat_open_input(&ctx, path.c_str(), 0, 0) < 0)</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">          </span>std::cout << "error1" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>if (avformat_find_stream_info(ctx, 0) < 0)</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">          </span>std::cout << "error2" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>AVDictionary* newDict = nullptr;</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>AVDictionaryEntry *tag = nullptr;</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">      </span>while( (tag = av_dict_get(ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))){</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">               </span>av_dict_set(&newDict, tag->key, tag->value, 0);</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">      </span>}</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>tag = av_dict_get(ctx->metadata, "artist", tag, AV_DICT_IGNORE_SUFFIX);</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">   </span>std::cout << tag->key << " : " << tag->value << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">      </span>av_dict_set(&newDict, "custom", "testtest", 0);</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre"> </span>std::cout << "test!" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>int status = avformat_write_header(ctx, &newDict);</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">  </span>if(status == 0)</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">                </span>std::cout << "test1" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">        </span>return 0;</font></div></div><div><font face="monospace" color="#0000ff"><br></font></div><div>Also I tried first with this;</div><div><br></div><div><div><font face="monospace" color="#0000ff">av_register_all();</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">     </span>AVFormatContext* ctx;</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">  </span>std::string path("/home/stefan/test_track.mp3");</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">      </span>ctx = avformat_alloc_context();</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre"> </span>if (avformat_open_input(&ctx, path.c_str(), 0, 0) < 0)</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">          </span>std::cout << "error1" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>if (avformat_find_stream_info(ctx, 0) < 0)</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">          </span>std::cout << "error2" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>AVDictionaryEntry *tag = nullptr;</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">      </span>tag = av_dict_get(ctx->metadata, "artist", tag, AV_DICT_IGNORE_SUFFIX);</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">   </span>std::cout << tag->key << " : " << tag->value << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">      </span>av_dict_set(&ctx->metadata, "TPFL", "testtest", 0);</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">  </span>std::cout << "test!" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">       </span>int status = avformat_write_header(ctx, &ctx->metadata);</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre"> </span>if(status == 0)</font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">                </span>std::cout << "test1" << std::endl;</font></div><div><font face="monospace" color="#0000ff"><br></font></div><div><font face="monospace" color="#0000ff"><span class="Apple-tab-span" style="white-space:pre">        </span>return 0;</font></div></div><div><font face="monospace" color="#0000ff"><br></font></div><div>But both solutions result with Segmentation Fault error. </div><div><br></div><div>Can someone tell me where I am making mistake? Or how to do this correct?</div><div><br></div><div>Best regards, </div><div>Stefan</div><div><font face="monospace" color="#0000ff"><br></font></div></div>