12

The MediaMetadataRetriever.getFrameAtTime() always returns same frames when ever call. Have a look my code

private ArrayList<Bitmap> getFrames(String path){
    try {
        ArrayList<Bitmap> bArray = new ArrayList<Bitmap>();
        bArray.clear();
        MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
        mRetriever.setDataSource(getDataSource(path));

        for (int i = 3000; i <60000; i=i+5000) {
            bArray.add(mRetriever.getFrameAtTime(i, MediaMetadataRetriever.OPTION_CLOSEST_SYNC));

        }

        return bArray;
    } catch (Exception e) {
        // TODO: handle exception

        return null;

    }
}

This method always return same frames

Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78
  • 1
    That's behaviour you can expect, as per documentation: *"This method finds a representative frame close to the given time position by considering the given option **if possible**, and returns it as a bitmap. Returns: A Bitmap containing a representative video frame, which can be null, if such a frame cannot be retrieved."* – MH. Apr 17 '12 at 10:48
  • @MH. thank for reply how can i get the frames from video file. Have a Look my [question](http://stackoverflow.com/questions/10071408/how-to-get-frames-of-video-file-in-android) – Ashish Dwivedi Apr 17 '12 at 11:13
  • 2
    After 3 yrs, m still trying to find it hard for the solution...any one knows about the solution??? – BST Kaal Aug 25 '15 at 20:23
  • Same here after 2 yrs more... – user-123 Jan 17 '18 at 21:26

3 Answers3

11

I don't know how long is your video, but the time to use in the long var as the time for getTimeAtFrame must be expressed in MICRO seconds

ex: a video of 1 second have 1000000 USeconds, if use a very short period (like you) you must very lucky for retrieve the first frame only that you video have!!!

Philippe Boissonneault
  • 3,949
  • 3
  • 26
  • 33
Morgan Mora
  • 119
  • 1
  • 3
  • 1
    I've had this problem too and I'm definitely sure that it has nothing to do with the time interval that is used. And the example from the OP is using micro seconds. – Léon Pelletier May 20 '15 at 21:23
4

You are going to have to use something like FFMPEG to fetch the frames.

You will have to use the NDK, and compile FFMPEG for Android; unfortunately it's not going to be very easy.

A couple of starting points:

http://ffmpeg.org/

ffmpeg for a android (using tutorial: "ffmpeg and Android.mk")

Good luck!

Community
  • 1
  • 1
Zambotron
  • 699
  • 5
  • 14
2

Léon Pelletier is right. The problem is that MediaMetadataRetriever.getFrameAtTime() could only extract key frames from video at second level. For example, if a video has about 4 seconds, you can get only 4 or 5 different frames. To get all video frames, please refer to MediaCodec.

DeTac
  • 61
  • 5