How to determine a video file's framerate with MediaCodec, MediaExtractor or MediaMetadataRetriever?

Florian picture Florian · Jul 16, 2014 · Viewed 9.1k times · Source

How to I extract the frame rate of a recorded video file? I know that there is MediaFormat.KEY_FRAME_RATE and that I can access MediaFormat objects through MediaExtractor. However KEY_FRAME_RATE is only available for encoders. Instead I want to find out the frame rate of an already recorded video.

Any ideas?

Answer

Léon Pelletier picture Léon Pelletier · Aug 4, 2015

When using MediaExtractor, if your video track has a stable FPS, then you know for sure your MediaExtractor is advancing by the frame duration you are searching for.

Before doing anything, just after having set up your MediaExtractor, you can do the following:

mediaExtractor.Advance();
var fps = 1000000f / (float) mediaExtractor.SampleTime;
mediaExtractor.SeekTo(0, MediaExtractorSeekTo.None);

Like I said, you can't take for grant that all your frames have the same duration. Frame presentation time are totally arbitrary, but I feel it's not common to not have a stable FPS.