I have a set of videos stored in a folder on the android file system. I would like to read each frame by frame so that i can perform some OpenCv functions on them and then display them in a Bitmap. I'm not sure how to do this correctly, any help would be appreciated.
3 Answers
You can take a look at Javacv.
"JavaCV first provides wrappers to commonly used libraries by researchers in the field of computer vision: OpenCV, FFmpeg, libdc1394, PGR FlyCapture, OpenKinect, videoInput, and ARToolKitPlus"
To read each frame by frame you'd have to do something like below
FrameGrabber videoGrabber = new FFmpegFrameGrabber(videoFilePath);
try
{
videoGrabber.setFormat("video format goes here");//mp4 for example
videoGrabber.start();
} catch (com.googlecode.javacv.FrameGrabber.Exception e)
{
Log.e("javacv", "Failed to start grabber" + e);
return -1;
}
Frame vFrame = null;
do
{
try
{
vFrame = videoGrabber.grabFrame();
if(vFrame != null)
//do your magic here
} catch (com.googlecode.javacv.FrameGrabber.Exception e)
{
Log.e("javacv", "video grabFrame failed: "+ e);
}
}while(vFrame != null);
try
{
videoGrabber.stop();
}catch (com.googlecode.javacv.FrameGrabber.Exception e)
{
Log.e("javacv", "failed to stop video grabber", e);
return -1;
}
Hope that helps. Goodluck

- 273
- 1
- 10
-
plz help me.for getting the all frames from video – dipali Feb 21 '14 at 10:26
-
I get the following error on `videoGrabber.start();`: `javacv(1999): Failed to start grabberorg.bytedeco.javacv.FrameGrabber$Exception: avformat_open_input() error -2: Could not open input "/Users/pchandak/Desktop/adt-bundle-mac-x86_64-20140702/set2_half1_video_2_50_to_3_11_te.mp4". (Has setFormat() been called?)`. I have tried on Mac and windows, but no use. – Autonomous Jul 18 '14 at 01:35
-
@ParagS.Chandakkar pretty sure it has nothing to do with the OS you're running on. Like the error log says `(Has setFormat() been called?)` so has setFormat been called? – Pawan Kumar Aug 06 '14 at 08:18
-
@PawanKumar Yes. I actually found out that it can't even get address of files. So I think the error is not about video reading, it can't find the video itself. I can see the file on my android device but if I give the same path, my device doesn't read. So I am trying to solve that problem now. – Autonomous Aug 06 '14 at 16:16
I don´t know about Android but generally you would have to use VideoCapture::open
to open your video and then use VideoCapture::grab
to get the next frame. See the Documentation of OpenCV for more information on this.
Update:
It seems like camera access is not officially supported for Android at the moment, see this issue on the OpenCV Github: https://github.com/opencv/opencv/issues/11952
You can either try the unofficial branch linked in the issue: https://github.com/komakai/opencv/tree/android-ndk-camera
or use another library to read in the frames and then create an OpenCV image from the data buffer like in this question.

- 6,003
- 3
- 35
- 46
-
VideoCapture.open always gives false in android, which means it can't open any file or camera for reading. – Divij Sehgal Aug 24 '19 at 09:52
-
@DivijSehgal Could you explain your comment? Is this a bug or has the behaviour of `VideoCapture.open` changed? Or are you having problems? If the latter please ask a new question, see: [ask] – Mailerdaimon Aug 30 '19 at 06:54
-
From what I have seen now, the behaviour of ```VideoCapture.open``` has changed from what it was when you wrote this answer. While this may have worked a few years back, It is not valid now. – Divij Sehgal Aug 30 '19 at 14:16
-
Do you have any source for that? If I look at the source [code](https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap.cpp) on Github I can find no proof that the behaviour has changed. – Mailerdaimon Sep 02 '19 at 05:32
-
Ok, I found something: https://github.com/opencv/opencv/issues/11952 it seems like you are right.. it is officially not supported on Android. In the linked issue you can find a branch of OpenCV that readds support. – Mailerdaimon Sep 02 '19 at 05:36
i know it's to late but any one can use it if he need it
so you can use @Pawan Kumar code and you need to add read permession to your manifest file <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and you will get it working.

- 113
- 2
- 13