1

I am trying to read and most important to write metadata into file using ffmpeg. But I am getting sigseg.

int main(int argc, char **argv) {

    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;
}

I also tried to make a entire copy of AVDictionary and than saving it with new addition of field I want to save. But still sigseg.

What am I missing?

7
  • Which line you get SIGSEG? Commented Jul 18, 2017 at 14:17
  • write_header function... Commented Jul 18, 2017 at 14:19
  • avformat_write_header write to output context. Here you passed an input context to it. Can you show a backtrace? Commented Jul 18, 2017 at 14:21
  • Isnt that same thing? I open context, make changes and than write to? How I can check if this is open context or output? And how to convert it? Regarding backtrace, how I can activate it in ffmpeg lib? All I get is Segmentation Fault. Commented Jul 19, 2017 at 6:02
  • No, they aren't the same thing, you must have an AVFormatContext initialized with avformat_init_output. ffmpeg.org/doxygen/3.2/… Commented Jul 19, 2017 at 8:50

1 Answer 1

0

You need to use another AVFormatContext to write your output.

In your example, just add some meta data and copy the codec, so the muxing steps for ffmpeg library is

  1. create the desired output format context, avformat_alloc_output_context2
  2. add streams to the output format context, avformat_new_stream
  3. add some custom meta data and write header
  4. use av_write_frame to write the encoded data
  5. write trailer

Below is a workable example with small modification of your code.

#include <iostream>

#ifdef __cplusplus
extern "C" {
#endif

#include <libavformat/avformat.h>

#ifdef __cplusplus 
}
#endif


int main(int argc, char **argv) {

    av_register_all();
    avcodec_register_all();

    AVFormatContext* ctx;
    std::string path("./input.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;

    AVFormatContext* ofmt_ctx;
    AVOutputFormat* ofmt = av_guess_format("mp3", "./out.mp3", NULL);
    status = avformat_alloc_output_context2(&ofmt_ctx, ofmt, "mp3", "./out.mp3");
    if (status < 0) {
        std::cerr << "could not allocate output format" << std::endl;
        return 0;
    }

    int audio_stream_index = 0;
    for (unsigned i = 0; i < ctx->nb_streams; i++) {
        if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
            audio_stream_index = i;
            const AVCodec *c = avcodec_find_encoder(ctx->streams[i]->codecpar->codec_id);
            if (c) {
                AVStream *ostream = avformat_new_stream(ofmt_ctx, c);
                avcodec_parameters_copy(ostream->codecpar, ctx->streams[i]->codecpar);
                ostream->codecpar->codec_tag = 0;
            }
            break;
        }
    }

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

    av_dump_format(ofmt_ctx, 0, "./out.mp3", 1);

    if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
        avio_open(&ofmt_ctx->pb, "./out.mp3", AVIO_FLAG_WRITE);
    }

    if (avformat_init_output(ofmt_ctx, NULL) == AVSTREAM_INIT_IN_WRITE_HEADER) {
        status = avformat_write_header(ofmt_ctx, NULL);
    }

    AVPacket *pkt = av_packet_alloc();
    av_init_packet(pkt);
    pkt->data = NULL;
    pkt->size = 0;

    while (av_read_frame(ctx, pkt) == 0) {
        if (pkt->stream_index == audio_stream_index) {
            // this is optional, we are copying the stream
            av_packet_rescale_ts(pkt, ctx->streams[audio_stream_index]->time_base,
                                 ofmt_ctx->streams[audio_stream_index]->time_base);
            av_write_frame(ofmt_ctx, pkt);
        }
    }

    av_packet_free(&pkt);

    av_write_trailer(ofmt_ctx);

    avformat_close_input(&ctx);
    avformat_free_context(ofmt_ctx);
    avformat_free_context(ctx);

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

    return 0;
}

After done, you can use ffprobe output.mp3 to check the written meta data.

Input #0, mp3, from 'out.mp3':
  Metadata:
    TPFL            : testtest
    encoder         : Lavf57.75.100
  Duration: 00:00:08.75, start: 0.011995, bitrate: 128 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
    Metadata:
      encoder         : Lavf
Sign up to request clarification or add additional context in comments.

3 Comments

There is no way to do this without creating new file? No way to just modify metadata on existing one?
As far as I know, you have to create a new file to hold the modified metadata.
can the input file name be used for output as well? so that new file is not created

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.