Grab frame from video on Android

2,777 views
Skip to first unread message

BabyC

unread,
Oct 5, 2011, 3:40:35 PM10/5/11
to javacv
Hi,

I'm trying to use JavaCV to grab frame from a video.

This is my code :

CvCapture capture =
cvCreateFileCapture(Environment.getExternalStorageDirectory().toString()
+ "/video.avi");
cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, 10);
IplImage image = cvQueryFrame(capture);
if (image == null)
{
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}

cvSaveImage(Environment.getExternalStorageDirectory().toString() + "/
test.jpg", image);
cvReleaseCapture(capture);

But it don't work. It create a empty test.jpg. The IplImage is null.

I tried to put the "armeabi-v7a" and "armeabi" files in the "libs/
armeabi" directory, but it the same thing.

Can you help me please ?

Samuel Audet

unread,
Oct 5, 2011, 8:35:04 PM10/5/11
to jav...@googlegroups.com
Hello,

The precompiled libraries of OpenCV that I am making available for
Android have not been compiled against FFmpeg, so we cannot use them to
capture from video files. The reason being that we can use FFmpeg
directly if need be with FFmpegFrameGrabber, for example, no need of
OpenCV for that. So, please use FFmpegFrameGrabber, thank you

Samuel

BabyC

unread,
Oct 6, 2011, 2:37:23 PM10/6/11
to javacv
Ok,

If I success to use FFmpegFrameGrabber, it would be very nice.

I followed that to create my porject :

Eclipse (Android 2.2 or newer):

- Follow the instructions on this page:
http://developer.android.com/resources/tutorials/hello-world.html
- Go to File > New > Folder, select your project as parent folder,
type "libs/armeabi" as Folder name, and click Finish.
- Copy javacpp.jar and javacv.jar in the newly created "libs"
folder.
- Extract the *.so files from javacv-android-arm.jar as well as
the ones from OpenCV-2.3.1-android-arm.zip in the newly created "libs/
armeabi" folder.
- Navigate to Project > Properties > Java Build Path > Libraries
and click "Add JARs..."
- Select both javacpp.jar and javacv.jar from the newly created
"libs" folder.


But when I put that, the app bug and stop !
FFmpegFrameGrabber i = new FFmpegFrameGrabber("/sdcard/video.avi");


Please can you give the exacts step to follow ?
Please if you can and if you have a few time, can you create me a
project with the code needed to grab frames from a video. It would be
very usefull for me.

Thanks for your help.

BabyC

unread,
Oct 6, 2011, 2:54:19 PM10/6/11
to javacv
Sorry, it run fine but the IplImage is null.

FFmpegFrameGrabber i = new FFmpegFrameGrabber("/sdcard/
video.avi");

try
{
i.start();
IplImage image = i.grab();
cvSaveImage("/sdcard/test.jpg", image);
cvReleaseImage(image);

}
catch (Throwable t)
{}

Please can you give a little example.

Thanks

Samuel Audet

unread,
Oct 6, 2011, 8:41:25 PM10/6/11
to jav...@googlegroups.com
So you also copied the library files of FFmpeg compiled for Android from
here?
http://code.google.com/p/javacv/downloads/list

BabyC

unread,
Oct 7, 2011, 12:23:55 AM10/7/11
to javacv
Of course, I tried to copy the *.so files from "ffmpeg-0.7.5-android-
arm.zip" to the armeabi folder.

Maybe it can come from the JavaCv files : I unzipped "javacv-windows-
x86_64.jar" and I have Windows 7 32 bits.

Maybe the problem is that I use *.so files and not dll.

What do I need exactly to do and if I need dll where can I find dll of
ffmpeg and opencv ?

Thanks


On Oct 7, 2:41 am, Samuel Audet <samuel.au...@gmail.com> wrote:
> So you also copied the library files of FFmpeg compiled for Android from
> here?http://code.google.com/p/javacv/downloads/list

Samuel Audet

unread,
Oct 7, 2011, 12:25:32 AM10/7/11
to jav...@googlegroups.com
Android doesn't use .dll files, you only need the .so files from /all/
the packages marked with "android"...

BabyC

unread,
Oct 7, 2011, 9:07:40 AM10/7/11
to javacv
Sorry, it's a mistake.

I used javacv-android-arm.jar and ffmpeg + Opencv Compiled for
Android.

When I run the app in debug mode, I have somme error like :

10-07 12:56:18.505: ERROR/dalvikvm(703): Could not find class
'java.awt.image.ComponentColorModel', referenced from method
com.googlecode.javacv.cpp.opencv_core$IplImage.getBufferedImage

It don't crash the app, but maybe problems come from that ?


Thank you very much for your help. I hope that I will success to grab
frames from a video.

BabyC

unread,
Oct 8, 2011, 8:40:38 AM10/8/11
to javacv
Please, Can you help me ?

Samuel Audet

unread,
Oct 10, 2011, 8:53:48 AM10/10/11
to jav...@googlegroups.com
On 2011-10-07 22:07, BabyC wrote:
> Sorry, it's a mistake.
>
> I used javacv-android-arm.jar and ffmpeg + Opencv Compiled for
> Android.
>
> When I run the app in debug mode, I have somme error like :
>
> 10-07 12:56:18.505: ERROR/dalvikvm(703): Could not find class
> 'java.awt.image.ComponentColorModel', referenced from method
> com.googlecode.javacv.cpp.opencv_core$IplImage.getBufferedImage
>
> It don't crash the app, but maybe problems come from that ?

Yes, that's kind of normal, Android does not have any AWT package...

> Thank you very much for your help. I hope that I will success to grab
> frames from a video.

Sure, sorry for not replying before. I was out for the weekend

Samuel

BabyC

unread,
Oct 10, 2011, 12:43:57 PM10/10/11
to javacv
Incredible but true (french expression that you know).

I wanted to send you my project and just before I tested it again, and
it work ! Incredible !!!

I will try once again later because it's very strange.

Thanks for you job and your help.

BabyC

unread,
Oct 10, 2011, 2:27:31 PM10/10/11
to javacv
Yes it work fine on the phone.

Just alast question in the FFmpegFrameGrabber class, there is a grab()
function.

This mean that it can grab the actual image when I call the fonction.

For example, if I call start(), I wait 2 secondes and then I call
grab(), it will grab the frame that is displayed in the video after 2
secondes ?

Thank you very much.

Samuel Audet

unread,
Oct 11, 2011, 9:34:26 AM10/11/11
to jav...@googlegroups.com

No, you have to call grab() the appropriate number of times...

Samuel

BabyC

unread,
Oct 11, 2011, 1:57:47 PM10/11/11
to javacv
> No, you have to call grab() the appropriate number of times...

This means that the first time I call grab(), it grab the first frame,
the second time I call grab(), it grab the second frame ... ?


But I would like to grab 10 frames per second. You say in another
post :

> FFmpegFrameGrabber.getFrameRate() hasn't been implemented... something
> to add to the to do list I guess :)

So how can I grab 10 frames per second ?


And finally, what does the function trigger() exactly do ?

I hope that it's possible to grab 10 frames per second on a video that
hasn't a fps of 10.

Thanks.

Samuel Audet

unread,
Oct 11, 2011, 8:33:04 PM10/11/11
to jav...@googlegroups.com

Just call grab() ten times per second, you will get ten frames every second.

trigger() is useful for cameras that support software triggering

Samuel

2011/10/12 2:57 "BabyC" <webfr...@hotmail.fr>:

BabyC

unread,
Oct 12, 2011, 5:55:25 AM10/12/11
to javacv
> Just call grab() ten times per second, you will get ten frames every second.

So if I call grab for the first time, it will brag the first frame.
If I call grab 2 hours later, it will grab the second frame ...


This is a problem for me, because I would like send frame by bluetooth
but at the good frame rate.


So, with JavaCV, is it possible to get the fps of my video ?

Thanks

Jeremy Nicola

unread,
Oct 12, 2011, 6:06:46 AM10/12/11
to jav...@googlegroups.com
Simply store the time when you grabbed your frame. Then when you want to grab a new one, call grab() (currentTime-previousTime)*frameRate number of time before. I think that is what you need.

Jeremy

BabyC

unread,
Oct 12, 2011, 8:48:55 AM10/12/11
to javacv
I'm confused.

Please could you tell me if this is true :

1. I call start().
2. Then I directly call grab(). So it will can the first frame of the
video.
3. I wait 5 seconds.
4. Now, I call grab()


The image grabbed in 4. is the seond frame of the video ? or the frame
that appears in the movie after 5 seconds ?


Jeremy Nicola

unread,
Oct 12, 2011, 9:03:01 AM10/12/11
to jav...@googlegroups.com
If we are grabbing from a video file, it will be the second frame in the video.

Jeremy

BabyC

unread,
Oct 12, 2011, 9:36:39 AM10/12/11
to javacv
Ok so I cannot measure the time as you say in order to get the fps ?

On Oct 12, 3:03 pm, Jeremy Nicola <jeremy.nic...@gmail.com> wrote:
> If we are grabbing from a video file, it will be the second frame in the video.
>
> Jeremy
>

Curtis Larson

unread,
Oct 12, 2011, 4:18:06 PM10/12/11
to jav...@googlegroups.com
You can always use av_seek_frame to seek to a specific timestamp.

mika I.

unread,
Jun 11, 2014, 6:09:31 PM6/11/14
to jav...@googlegroups.com
Hi, could you point me in the right direction in how to load a video file from our assets or raw directory instead of sdcard?

Samuel Audet

unread,
Jun 13, 2014, 9:59:45 AM6/13/14
to jav...@googlegroups.com
On 06/12/2014 07:09 AM, mika I. wrote:
> Hi, could you point me in the right direction in how to load a video
> file from our assets or raw directory instead of sdcard?

There's some clues here about how to do it properly here:
https://code.google.com/p/javacv/issues/detail?id=363
But the easiest thing is probably to write the data into a temporary
file, and load it with FFmpeg that way.

Samuel

Reply all
Reply to author
Forward
0 new messages