Dion
Dion

Reputation: 55

C++ FFmpeg how to continue encoding after flushing?

I write the received packets in binary files. When the recording of the first file is completed, I call flush:

avcodec_send_frame(context, NULL);

This is the signal to end the stream. But when I send a new frame to the encoder, function return AVERROR_EOF (man: the encoder has been flushed, and no new frames can be sent to it). What to do to make the encoder take the frames after flushing?

Example: when decoding, you can call:

avcodec_flush_buffers(context);

This function changes the stream, but only for decoding. Maybe analogic function for encoding?

Ideas:

1) do not call flush. But the encoder buffers frames inside and gives some packets only after flushing (using h.264 with b-frames), while some packets get into the next file.

2) Recreate codec context?

Details: use Win 7, Qt 5.10, ffmpeg 4.0.2

Upvotes: 2

Views: 3998

Answers (1)

Krumelur
Krumelur

Reputation: 32497

The correct answer is that you should create a new codec context for each file, or headache will follow. The little expense of additional headers and key frames should be small unless you are doing something very exotic.

B-frames can refer to both previous and future frames, how would you even decide such a beast?

In theory you could probably force a keyframe and hope for the best, but then there is really no point in not starting a new context, unless the hundreds of bytes or so of H264 init data is a problem.

Upvotes: 4

Related Questions