apc-llc / moviemaker-cpp

Encode video frame by frame in C++ using FFmpeg
MIT License
88 stars 14 forks source link

PTS not incremented correctly #9

Open flm8620 opened 2 years ago

flm8620 commented 2 years ago

In the line

yuvpic->pts = iframe; The iframe is not incremented. It is only incremented when the encoder finished encoding one frame

    int got_output;
    int ret = avcodec_encode_video2(c, &pkt, yuvpic, &got_output);
    if (got_output)
    {
        fflush(stdout);

        // We set the packet PTS and DTS taking in the account our FPS (second argument),
        // and the time base that our selected format uses (third argument).
        av_packet_rescale_ts(&pkt, (AVRational){ 1, frameRate }, stream->time_base);

        pkt.stream_index = stream->index;
        printf("Writing frame %d (size = %d)\n", iframe++, pkt.size);

        // Write the encoded frame to the mp4 file.
        av_interleaved_write_frame(fc, &pkt);
        av_packet_unref(&pkt);
    }

The current code works because the libvpx-vp9 encoder won't complain about this. AVCodec* codec = avcodec_find_encoder_by_name("libvpx-vp9"); But if I switch to libx264 encoder, it will complain something like:

[libx264 @ 0x564948d1d560] non-strictly-monotonic PTS
dmikushin commented 2 years ago

Hi @flm8620 thank you for this report, would you like to go ahead and make a pull request? :pray: