Skip to content

Commit a92ed9f

Browse files
committed
Fix bug with FFmpeg > 3.2 flushing frames
Final frames stored in buffer need to be flushed/drained so that they can be written to the video file. Credit to Peter for the update.
1 parent 628eb37 commit a92ed9f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/FFmpegWriter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,23 @@ void FFmpegWriter::flush_encoders()
590590
// Encode video packet (latest version of FFmpeg)
591591
error_code = avcodec_send_frame(video_codec, NULL);
592592
got_packet = 0;
593+
while (error_code >= 0) {
594+
error_code = avcodec_receive_packet(video_codec, &pkt);
595+
if (error_code == AVERROR(EAGAIN)|| error_code == AVERROR_EOF) {
596+
got_packet = 0;
597+
// Write packet
598+
avcodec_flush_buffers(video_codec);
599+
break;
600+
}
601+
if (pkt.pts != AV_NOPTS_VALUE)
602+
pkt.pts = av_rescale_q(pkt.pts, video_codec->time_base, video_st->time_base);
603+
if (pkt.dts != AV_NOPTS_VALUE)
604+
pkt.dts = av_rescale_q(pkt.dts, video_codec->time_base, video_st->time_base);
605+
if (pkt.duration > 0)
606+
pkt.duration = av_rescale_q(pkt.duration, video_codec->time_base, video_st->time_base);
607+
pkt.stream_index = video_st->index;
608+
error_code = av_interleaved_write_frame(oc, &pkt);
609+
}
593610
}
594611
#else
595612

0 commit comments

Comments
 (0)