how to get frame rate of video in android os?

∥☆過路亽.° 提交于 2019-12-17 19:32:48

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/42204944/how-to-get-frame-rate-of-video-in-android-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!