Ashish Dwivedi
Ashish Dwivedi

Reputation: 8186

How to get the frame from video file in android

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

Upvotes: 12

Views: 13546

Answers (3)

DeTac
DeTac

Reputation: 61

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.

Upvotes: 2

Morgan Mora
Morgan Mora

Reputation: 119

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!!!

Upvotes: 11

Zambotron
Zambotron

Reputation: 699

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!

Upvotes: 4

Related Questions