22

I want to get frame rate of video, but i don't want to use FFMPEG,JAVACV lib. is that possible to get frame rate of video in android?

I read KEY_FRAME_RATE it's says that,"Specifically, MediaExtractor provides an integer value corresponding to the frame rate information of the track if specified and non-zero." but i don't know how to use it?

if you know about how to get frame rate from video then answer here.

prakash ubhadiya
  • 1,242
  • 1
  • 12
  • 24

1 Answers1

29
MediaExtractor extractor = new MediaExtractor();
int frameRate = 24; //may be default
try {
  //Adjust data source as per the requirement if file, URI, etc.
  extractor.setDataSource(...);
  int numTracks = extractor.getTrackCount();
  for (int i = 0; i < numTracks; ++i) {
    MediaFormat format = extractor.getTrackFormat(i);
    String mime = format.getString(MediaFormat.KEY_MIME);
    if (mime.startsWith("video/")) {
    if (format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
        frameRate = format.getInteger(MediaFormat.KEY_FRAME_RATE);
      }
    }
  }
} catch (IOException e) {
  e.printStackTrace();
}finally {
  //Release stuff
  extractor.release();
}

Note: Try to run the above code in worker thread.

Update 1 What is KEY_FRAME_RATE and may be optional

KEY_FRAME_RATE Added in API level 16 String KEY_FRAME_RATE A key describing the frame rate of a video format in frames/sec. The associated value is normally an integer when the value is used by the platform, but video codecs also accept float configuration values. Specifically, MediaExtractor provides an integer value corresponding to the frame rate information of the track if specified and non-zero. Otherwise, this key is not present. MediaCodec accepts both float and integer values. This represents the desired operating frame rate if the KEY_OPERATING_RATE is not present and KEY_PRIORITY is 0 (realtime). For video encoders this value corresponds to the intended frame rate, although encoders are expected to support variable frame rate based on buffer timestamp. This key is not used in the MediaCodec input/output formats, nor by MediaMuxer.

Constant Value: "frame-rate"

Update 2 Code check if for NPE if KEY_FRAME_RATE not present. See above

Anurag Singh
  • 6,140
  • 2
  • 31
  • 47
  • it throw `NullPointerException` at format.getString(MediaFormat.KEY_MIME); – prakash ubhadiya Feb 16 '17 at 13:42
  • Post the log for the same. But how it can be if it is going inside for loop? – Anurag Singh Feb 16 '17 at 13:46
  • java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference 02-16 19:19:21.741: E/fra(7900): at android.media.MediaFormat.getInteger(MediaFormat.java:591) 02-16 19:19:21.741: E/fra(7900): at com.exam.activity.VideoPlayActivity.init(VideoPlayActivity.java:101) 02-16 19:19:21.741: E/fra(7900): at com.exam.activity.VideoPlayActivity.onCreate(VideoPlayActivity.java:59) – prakash ubhadiya Feb 16 '17 at 13:48
  • What is the android OS version on which you are testing upon? Also try posting the value of format.containsKey(MediaFormat.KEY_FRAME_RATE). You can log this before String mime. And the exception is on this format.getInteger line. – Anurag Singh Feb 16 '17 at 13:51
  • android 6.0 , kernal version 3.10.72+ – prakash ubhadiya Feb 16 '17 at 13:54
  • Did you get it after I have updated my answer above? – Anurag Singh Feb 16 '17 at 14:34
  • I used `format.getInteger(MediaFormat.KEY_FRAME_RATE);` and `format.getFloat(MediaFormat.KEY_FRAME_RATE);` both option – prakash ubhadiya Feb 17 '17 at 06:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135922/discussion-between-anurag-singh-and-prakash-u). – Anurag Singh Feb 17 '17 at 06:05
  • KEY_FRAME_RATE seems to have a complicated path... especially around Lollipop. Check out [this table](https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities.html#isFormatSupported(android.media.MediaFormat)) under the `isFormatSupported(format)` method. I've had to do some clever coding to avoid issues with it. – eatcrayons Apr 05 '17 at 21:58
  • @eatcrayons Thanks for the update. Will extend this answer in time to add support for the same. What clever coding did you do? – Anurag Singh Apr 06 '17 at 04:42
  • @AnuragSingh I do a version check. If it's Lollipop, I save the KEY_FRAME_RATE from the `MediaFormat` object to a temp variable and replace it with null. Then I make my call to `isFormatSupported(format)`. Finally, I place the saved KEY_FRAME_RATE back into the `MediaFormat` object. – eatcrayons Apr 08 '17 at 04:08
  • why we can't init MediaExtractor only once and release after for example 10 files like with MediaMetadataRetriever? – user924 Mar 04 '18 at 18:14