Retrieving frames from a video

I want to get the number of frames from the video. I am using the following code:

package com.vidualtest;

import java.io.File;
import java.io.FileDescriptor;

import android.app.Activity;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;

public class VidualTestActivity extends Activity {
/** Called when the activity is first created. */
File file;
ImageView img;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    img = (ImageView) findViewById(R.id.img);

    File sdcard = Environment.getExternalStorageDirectory();
    file = new File(sdcard, "myvid.mp4");

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    try {
        retriever.setDataSource(file.getAbsolutePath());

        img.setImageBitmap(retriever.getFrameAtTime(10000,MediaMetadataRetriever.OPTION_CLOSEST));


    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }


}

}

here in getFrameAtTime () i'm m going through static time like 10000,20000 etc. in milliseconds, but still I get the same frame from the video at another time. My goal is to get different frames with different time intervals.

Please give me your possible help.

thank

+3
source share
5 answers

The offset time parameter is in microseconds, not milliseconds. Therefore, multiply your time offset by 1000, and it should work correctly.

Thanks to Google for NOT indicating this in the documentation.

+25
source

10. 1 , 1000000, 1 .

.

int[] ids_of_images = new int[]{R.id.img,R.id.img2,R.id.img3,R.id.img4,R.id.img5};
looper =1000000;

 for(int i=0 ;i <10; i++)
        {
            ImageView imageView = (ImageView)findViewById(ids_of_images[i]);

            imageView.setImageBitmap(retriever.getFrameAtTime(looper,MediaMetadataRetriever.OPTION_CLOSEST));

            looper +=1000000;
        }

. . ImageView , .

+2

Microseconds, !

+1

, getFrameAt() , . , 1000 1500 ( ) , , , . , , , , . , .

0

With the jCodec library , I can get all the frames using this code to get all the video clips:

private class Decoder extends AsyncTask<File, Integer, Integer> {
    private static final String TAG = "DECODER";

    protected Integer doInBackground(File... params) {
        FileChannelWrapper ch = null;
        try {
            ch = NIOUtils.readableFileChannel(params[0]);
            FrameGrab frameGrab = new FrameGrab(ch);
            MediaInfo mi = frameGrab.getMediaInfo();
            Bitmap frame = Bitmap.createBitmap(mi.getDim().getWidth(), mi.getDim().getHeight(), Bitmap.Config.ARGB_8888);

            for (int i = 0; !flag; i++) {

                frameGrab.getFrame(frame);
                if (frame == null)
                    break;
                OutputStream os = null;
                try {
                    os = new BufferedOutputStream(new FileOutputStream(new File(params[0].getParentFile(), String.format("img%08d.jpg", i))));
                    frame.compress(CompressFormat.JPEG, 90, os);
                } finally {
                    if (os != null)
                        os.close();
                }
                publishProgress(i);

            }
        } catch (IOException e) {
            Log.e(TAG, "IO", e);
        } catch (JCodecException e) {
            Log.e(TAG, "JCodec", e);
        } finally {
            NIOUtils.closeQuietly(ch);
        }
        return 0;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progress.setText(String.valueOf(values[0]));
    }
}
0
source

All Articles