Skip to content

Commit b89cc36

Browse files
naushirdavidplowman
authored andcommitted
libav: Fix fps reporting for avi contaners
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 <naush@raspberrypi.com>
1 parent 05daa79 commit b89cc36

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

encoder/libav_encoder.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,16 @@ void LibAvEncoder::initVideoCodec(VideoOptions const *options, StreamInfo const
123123
if (!stream_[Video])
124124
throw std::runtime_error("libav: cannot allocate stream for vidout output context");
125125

126-
stream_[Video]->time_base = codec_ctx_[Video]->time_base;
126+
// The avi stream context seems to need the video stream time_base set to
127+
// 1/framerate to report the correct framerate in the container file.
128+
//
129+
// This seems to be a limitation/bug in ffmpeg:
130+
// https://github.com/FFmpeg/FFmpeg/blob/3141dbb7adf1e2bd5b9ff700312d7732c958b8df/libavformat/avienc.c#L527
131+
if (!strncmp(out_fmt_ctx_->oformat->name, "avi", 3))
132+
stream_[Video]->time_base = { 1000, (int)(options->framerate * 1000) };
133+
else
134+
stream_[Video]->time_base = codec_ctx_[Video]->time_base;
135+
127136
stream_[Video]->avg_frame_rate = stream_[Video]->r_frame_rate = codec_ctx_[Video]->framerate;
128137
avcodec_parameters_from_context(stream_[Video]->codecpar, codec_ctx_[Video]);
129138
}

0 commit comments

Comments
 (0)