'No-op remuxing of .avi file

I want to demux and then mux .avi file without changing anything.

My program is this (redacted for brevity):

    AVFormatContext *input_format_context = NULL;
    avformat_open_input(
            &input_format_context,
            input_url,
            NULL, // fmt
            NULL // options
            );

    avformat_find_stream_info(input_format_context, NULL);

    AVFormatContext *output_format_context = NULL;
    avformat_alloc_output_context2(
            &output_format_context,
            NULL, // oformat
            NULL, // format_name
            output_url
            );

    avio_open2(
            &output_format_context->pb,
            output_url,
            AVIO_FLAG_WRITE,
            NULL, // int_cb,
            NULL // options
            );

    for (int i = 0; i < input_format_context->nb_streams; i++) {
        avformat_new_stream(output_format_context, NULL);

        AVStream *input_stream = input_format_context->streams[i];
        AVStream *output_stream = output_format_context->streams[i];

        AVCodecParameters *params = avcodec_parameters_alloc();

        avcodec_parameters_copy(params, input_stream->codecpar);

        output_stream->codecpar = params;
    }
    
    avformat_write_header(output_format_context, NULL))

    AVPacket *input_packet = NULL;
    input_packet = av_packet_alloc();
    while (!av_read_frame(
                    input_format_context,
                    input_packet
                    )) {
        av_write_frame(output_format_context, input_packet);
        av_packet_unref(input_packet);
    }

    av_write_trailer(output_format_context);

Problem: Output file is created but instead of close to 10 minute video it is a 24-second slide show consisting of around 3 frames.

It seems that the problem is (perhaps not the only one) lack of PTS on the packet. When I explicitly print it (input_packet->pts) for each packet it is -9223372036854775808. And also the following warning is printed:

[avi @ 0x562868c6c000] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly

How do I then "fix my code to set the timestamps properly"?



Solution 1:[1]

I just found a solution.

I added this:

        output_stream->time_base = input_stream->time_base;

which then, I understand, allows the video player to calculate PTS on the fly. This does not remove the warning itself, though. I understand that .avi simply does not have PTS, so it's not a bug as such. To get rid of the warning one can manually set PTS on the packets:

        input_packet->pts = calculated_ts;

I would think I should be able to also just do:

    output_format_context->oformat->flags |= AVFMT_NOTIMESTAMPS;

However, I cannot do that:

error: assignment of member ‘flags’ in read-only object

So, it looks like ffmpeg is requiring PTS even for .avi or there's a bug or I'm still doing something wrong.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dgsgsdgsgdhddhg