android capture video frame

23,359

Solution 1

The following works for me:

public static Bitmap getVideoFrame(FileDescriptor FD) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(FD);
            return retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (RuntimeException ex) {
            ex.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException ex) {
            }
        }
        return null;
    }

Also works if you use a path instead of a filedescriptor.

Solution 2

Try this, I've used it and its working

public static Bitmap getVideoFrame(Context context, Uri uri) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setDataSource(uri.toString(),new HashMap<String, String>());
        return retriever.getFrameAtTime();
    } catch (IllegalArgumentException ex) {
        ex.printStackTrace();
    } catch (RuntimeException ex) {
        ex.printStackTrace();
    } finally {
        try {
            retriever.release();
        } catch (RuntimeException ex) {
        }
    }
    return null;
}

In place of uri you can directly pass your url .

Solution 3

I used this code and that is working for me. you can try this one.

if (Build.VERSION.SDK_INT >= 14) {
                ffmpegMetaDataRetriever.setDataSource(
                        videoFile.getAbsolutePath(),
                        new HashMap<String, String>());
            } else {
                ffmpegMetaDataRetriever.setDataSource(videoFile
                        .getAbsolutePath());
            }
Share:
23,359
Buda Gavril
Author by

Buda Gavril

Updated on July 16, 2022

Comments

  • Buda Gavril
    Buda Gavril almost 2 years

    I need to get a frame of a video file (it may be on sdcard, cache dir or app dir). I have package android.media in my application and inside I have class MediaMetadataRetriever. To get first frame into a bitmap, I use code:

    public static Bitmap getVideoFrame(Context context, Uri videoUri) {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
            retriever.setDataSource(context, videoUri);
            return retriever.captureFrame();
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException();
        } catch (RuntimeException ex) {
            throw new RuntimeException();
        } finally {
            retriever.release();
        }
    }
    

    But this it's not working. It throws an exception (java.lang.RuntimeException: setDataSource failed: status = 0x80000000) when I set data source. Do you know how to make this code to work? Or Do you have any similar (simple) solution without using ffmpeg or other external libraries? videoUri is a valid uri (media player can play video from that URI)

    • ManishSB
      ManishSB about 9 years
      can you provide a sample for your question, i tried a lot for this kind of sample, finally had to ask you.
  • Dhiraj Tayade
    Dhiraj Tayade about 12 years
    it was giving errors... now its working... i guess there is some problem with my eclipse editor. Thanks anyway
  • Paresh Mayani
    Paresh Mayani about 12 years
    My comment was to ask you that "This 2 line code is your answer or a question with your problem?"
  • Dhiraj Tayade
    Dhiraj Tayade about 12 years
    It was a question ... The above code was not working initially... but its correct its working now
  • Ashish Dwivedi
    Ashish Dwivedi about 12 years
    @PareshMayani, how to get the list of frames of video file to show in gallery view, please have a look at my question
  • Ashish Dwivedi
    Ashish Dwivedi about 12 years
    what value should be pass as FileDescripter argument. Real thinks is that,in my application, there are video url only , and want to get the list of frames of that video file...
  • Rab Ross
    Rab Ross about 12 years
    To get a FileDescriptor object do - openFileInput(myVideoFileName).getFD(). To do this the video must be in your applications private directory. You can also change to public static Bitmap getVideoFrame(String FD) and pass your videos path.
  • Suraj
    Suraj over 10 years
    it required api 14. how to use in api below it.
  • Akanksha Rathore
    Akanksha Rathore about 10 years
    In one second a video have 25 to 30 frames. R u able to get all frames. Because by this technique i got 3-4 repeated frames every time .and some frames are missing.
  • Arul Pandian
    Arul Pandian about 10 years
    you just use this FFmpegMediaMetadataRetriever github.com/wseemann/FFmpegMediaMetadataRetriever
  • Zar E Ahmer
    Zar E Ahmer almost 10 years
    @Rab Ross kindly give detail code. how you do this . I am unable to make it working . What must be the extention and vice versa
  • Rab Ross
    Rab Ross almost 10 years
    @Nepster This was a while ago and I do not have access to the detailed code anymore. My apologies.