1

I wrote a simple Android application that is using MediaMetadataRetriver class to get frames. It works fine, except that I realized that it skips frames.

The video clip I am trying to decode is one shot with the phone camera. Follow relevant code snippets:

MediaMetadataRetriever mediaDataRet = new MediaMetadataRetriever();
            mediaDataRet.setDataSource(path);

            String lengthMsStr = mediaDataRet
                    .extractMetadata(mediaDataRet.METADATA_KEY_DURATION);
            final long lenMs = Long.parseLong(lengthMsStr);

            String widthStr = mediaDataRet
                    .extractMetadata(mediaDataRet.METADATA_KEY_VIDEO_WIDTH);
            int width = Integer.parseInt(widthStr);
            String heightStr = mediaDataRet
                    .extractMetadata(mediaDataRet.METADATA_KEY_VIDEO_HEIGHT);
            int height = Integer.parseInt(heightStr);

note the variable lenMs, it holds the clid duration in milliseconds. Then for every frame I do:

                int pace = 30; // 30 fps ms spacing 


                for (long i = 0; i < lenMs; i += pace) {

                    if (is_abort())
                        return;

                    Bitmap bitmap = mediaDataRet.getFrameAtTime(i * 1000); // I tried the other version of this method with OPTION_CLOSEST, with no luck.

                    if (bc == null)
                        bc = bitmap.getConfig();

                    bitmap.getPixels(pixBuffer, 0, width, 0, 0, width, height);
[...]
}

After checking visually I noticed that some frames are skipped (like short sequences). Why? And ho do I avoid this?

gotch4
  • 13,093
  • 29
  • 107
  • 170
  • Actually i'd tried your above code to get all frames from video file but i get only the first frames repeatedly.my duration of video is 127040(2:07sec) divided the video to 32 frames can view all 32 images but all were same images i'd tried a lot to fix it with getFrameAtTime(potions) but no use at all exactly i don't knoe where it goes wrong so can you help me by your full source code should i include any thing in AndroidManifest – Manoj May 21 '14 at 16:10
  • http://stackoverflow.com/a/24307619/3496570 may help – Zar E Ahmer Feb 07 '15 at 12:44

1 Answers1

3

Use:

mediaDataRet.getFrameAtTime(i * 1000, MediaMetadataRetriever.OPTION_CLOSEST);

The getFrameAtTime(n) uses OPTION_CLOSEST_SYNC which would give you key frames only.

Thomas
  • 43,637
  • 12
  • 109
  • 140
hoford
  • 4,918
  • 2
  • 19
  • 19