Quack-Ware.com Free Software & Tutorials

11Oct/117

Include FFMpeg in Android and grab a frame from a 3gp video.

Thought I would write about the recent pain I experienced in trying to get FFMpeg into Android and subsequently grab a frame from a video. Hopefully this tutorial will help out a bunch of people, so anyway here we go.

 

The first thing you are going to want to get is JavaCV (Website here) and follow the EXACT instructions under "Quick Start for OpenCV"

 

If you followed the instructions you should have 2 jar files in your build path along with a large amount of .so files in your libs/armeabi folder. Now the class you are going to be looking at is called "FFMpegFrameGrabber" (Code here). Here is some sample code to give you a general idea of how to grab a frame from your video.

//Path is the path to your .3gp or whatever file on your sdcard
FrameGrabber f = new FFMpegFrameGrabber(path);
f.start();
IplImage _image = f.grab();
//We need to convert from 3 channels to 4 channels so that we can convert
//from IplImage to Bitmap
IplImage image = IplImage.create(return_image.width() ,return_image.height(),IPL_DEPTH_8U,4);
//source, dest, code
cvCvtColor(_image,image,CV_BGR2RGBA);
int width = image.width();
int height = image.height();
Bitmap b = Bitmap.createBitmap(width,height, Config.ARGB_8888);
//Make sure you use getByteBuffer()!
b.copyPixelsFromBuffer(image.getByteBuffer());
//MAKE SURE YOU DO NOT CALL stop() BEFORE YOU CREATE A BITMAP FROM IMAGE
f.stop();

You might see an error about cvCvtColor, in which case you need to add the following in your import statements:

import static com.googlecode.javacv.cpp.opencv_imgproc.*;

Now you can display your new bitmap in whichever way you want.

Comments (7) Trackbacks (0)
  1. Hey man,

    First of all, great tutorials! , i would like to start programming for Android.
    Maybe you could make some kind of Tutorial of which applications to download, to actually start programming Android.

    It doesn’t have to be a video of course!

    Because im trying to find on the internet, but not with real success.

    Thanks

    • There are a ton of different tutorials on the internet on just downloading the applications. The main things you need to download are Eclipse, Android Eclipse Plugin, and the Android SDK. Use Google to find more information about this!

  2. Dude… you are THE MAN… was looking for some page to point me at right direction and you certainly did… :)

  3. ‘d like to add a bitmap/image mask in each frame of a video (it can be recorded or from SD), the video is live m-jpeg stream,and save the video to the SD with the image.

    Do you know how it can be done in Android?

  4. YOU ARE THE MAN, Echt.
    Du hast mir das Leben geretet.

  5. where do you specify the time when grabbing a frame from a video?? does it gives a better performance than Mediametadataretriever?? I am working with mediametadataretriever but apparently I am only getting one frame each second or so, is there any way I can improve that rate?
    Thanks!

  6. it can not resolve “IPL_DEPTH_8U” doesn’t seem to work on android… any idea?


Leave a comment

(required)

No trackbacks yet.