Skip to content

Commit

Permalink
libav: Fix fps reporting for avi contaners
Browse files Browse the repository at this point in the history
AVI container were aways reporting 600fps for any encoded video.  This turns out
to be because of a limitation in libav where the framerate is set from the
video stream timebase instead of using the framerate field in the stream
structure:
https://github.com/FFmpeg/FFmpeg/blob/3141dbb7adf1e2bd5b9ff700312d7732c958b8df/libavformat/avienc.c#L527

Fix this by using 1/framerate as the timebase for video streams in avi
containers.

Signed-off-by: Naushir Patuck <[email protected]>
  • Loading branch information
naushir authored and davidplowman committed Oct 18, 2022
1 parent 05daa79 commit b89cc36
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion encoder/libav_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ void LibAvEncoder::initVideoCodec(VideoOptions const *options, StreamInfo const
if (!stream_[Video])
throw std::runtime_error("libav: cannot allocate stream for vidout output context");

stream_[Video]->time_base = codec_ctx_[Video]->time_base;
// The avi stream context seems to need the video stream time_base set to
// 1/framerate to report the correct framerate in the container file.
//
// This seems to be a limitation/bug in ffmpeg:
// https://github.com/FFmpeg/FFmpeg/blob/3141dbb7adf1e2bd5b9ff700312d7732c958b8df/libavformat/avienc.c#L527
if (!strncmp(out_fmt_ctx_->oformat->name, "avi", 3))
stream_[Video]->time_base = { 1000, (int)(options->framerate * 1000) };
else
stream_[Video]->time_base = codec_ctx_[Video]->time_base;

stream_[Video]->avg_frame_rate = stream_[Video]->r_frame_rate = codec_ctx_[Video]->framerate;
avcodec_parameters_from_context(stream_[Video]->codecpar, codec_ctx_[Video]);
}
Expand Down

0 comments on commit b89cc36

Please sign in to comment.