android capture video frame
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());
}
Buda Gavril
Updated on July 16, 2022Comments
-
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 about 9 yearscan you provide a sample for your question, i tried a lot for this kind of sample, finally had to ask you.
-
-
Dhiraj Tayade about 12 yearsit was giving errors... now its working... i guess there is some problem with my eclipse editor. Thanks anyway
-
Paresh Mayani about 12 yearsMy comment was to ask you that "This 2 line code is your answer or a question with your problem?"
-
Dhiraj Tayade about 12 yearsIt was a question ... The above code was not working initially... but its correct its working now
-
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 about 12 yearswhat 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 about 12 yearsTo 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 over 10 yearsit required api 14. how to use in api below it.
-
Akanksha Rathore about 10 yearsIn 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 about 10 yearsyou just use this FFmpegMediaMetadataRetriever github.com/wseemann/FFmpegMediaMetadataRetriever
-
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 almost 10 years@Nepster This was a while ago and I do not have access to the detailed code anymore. My apologies.