Ask Your Question

Desktop Java - how to load video file?

asked 2013-03-28 07:42:10 -0600

Rauluka gravatar image

I'm trying to load video file using desktop Java opencv, but it seems VideoCapture and other packages doesn't have appropriate methods. Do you have some ideas how to load video file?

edit retag flag offensive close merge delete



You could use that part of JavaCV. They have pretty good wrappers to read video files using ffmpeg

Paethon gravatar imagePaethon ( 2013-07-18 15:01:33 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2013-10-09 14:44:15 -0600

mirel7c3 gravatar image

updated 2013-10-10 02:22:44 -0600

Currently a method for loading video files is missing in the VideoCapture class. Although the VideoCapture class defines a native method of this kind, calling it leads to a unsatisfied link error:

// From VideoCapture 
// C++: VideoCapture::VideoCapture(string filename)
private static native long n_VideoCapture(java.lang.String filename);

I dug up the corresponding source files and got it to work. Here is how:

The needed c-constructor is already defined in opencv-2.4.6/modules/highgui/include/opencv2/highgui/highgui.cpp

CV_WRAP VideoCapture(const string& filename);

All we need to do is to export this constructor via JNI and add it to the Java class


add the constructor we long for:

// C++: VideoCapture::VideoCapture(const string& filename)

// javadoc: VideoCapture::VideoCapture(String filename)
public VideoCapture(String filename)
    nativeObj = n_VideoCapture(filename);


opencv-2.4.6/modules/java/generator/src/cpp/VideoCapture.cpp here we add the jni export:

//   VideoCapture::VideoCapture(const string& filename)

JNIEXPORT jlong JNICALL Java_org_opencv_highgui_VideoCapture_n_1VideoCapture__Ljava_lang_String_2
(JNIEnv* env, jclass, jstring filename);

JNIEXPORT jlong JNICALL Java_org_opencv_highgui_VideoCapture_n_1VideoCapture__Ljava_lang_String_2
(JNIEnv* env, jclass, jstring filename)
    try {
        const char* jnamestr = env->GetStringUTFChars(filename, NULL);
        string stdFileName(jnamestr);
        VideoCapture* _retval_ = new VideoCapture( jnamestr );

        return (jlong) _retval_;
    } catch(cv::Exception e) {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2() catched cv::Exception: %s", e.what());
        jclass je = env->FindClass("org/opencv/core/CvException");
        if(!je) je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, e.what());
        return 0;
    } catch (...) {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2() catched unknown exception (...)");
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__S()}");
        return 0;

Recompile and it should work.

edit flag offensive delete link more


Maybe you could add this as a pull request to the actual code. Look at this page for instructions on how to add changes to the base code :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-10-14 01:45:15 -0600 )edit

How can it be recompiled? I am using eclipse in windows 7.. thanks

naim gravatar imagenaim ( 2013-11-05 17:43:16 -0600 )edit

answered 2013-03-28 07:48:53 -0600

Simple steps to follow and see the world become more clearer for you:

  1. Click this link :
  2. Go to package org.opencv.highgui
  3. Click class VideoCapture
  4. Smile

You are welcome!

edit flag offensive delete link more



Thanks for reply:) I have found VideoCapture package, but it doesn't have method (like cvCapturefromAvi), you can only open a stream from a camera (method takies int parameter of a device, not a file name). If I misunderstood something I' sorry;)

Rauluka gravatar imageRauluka ( 2013-03-28 08:21:51 -0600 )edit

You are mixing the C - API functionality with the Java functionality, which resembles more the C++ commands. Did you actually look at the example that is posted? It states literally : Class for video capturing from video files or cameras. The class provides C++ API for capturing video from cameras or for reading video files. Here is how the class can be used:

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-28 08:38:09 -0600 )edit

oc I read this example, but there is no method that takes a filename, stream as a parameter. Method open takes only int id, and it's a device id (camera). If you managed to open a video file using this package I would be grateful for example.

Rauluka gravatar imageRauluka ( 2013-03-28 08:50:31 -0600 )edit

Did you try passing the location of your video as a string parameter? How did the actual algorithm respond to it? It is like this in C++ and since it uses that structure, maybe this could be the solution.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-28 09:06:23 -0600 )edit

You can't pass parameter of a different type in Java. Method open is not applicable for String type parameter, it won't compile. That's the problem of this package;/ I can read camera input, get frames, save them etc. But still can't open a video file;/

Rauluka gravatar imageRauluka ( 2013-03-28 09:20:26 -0600 )edit

Then probably there is no java binding defined for this functionality. You could file in a request to add it or create it yourself and submit it to the dev platform.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-28 09:27:12 -0600 )edit

It seems it's the only way to do it. Thanks for your time:)

Rauluka gravatar imageRauluka ( 2013-03-28 09:37:09 -0600 )edit

Can you please post your progress here? I also need to open a video file using Java.

4thguy gravatar image4thguy ( 2013-03-28 10:12:49 -0600 )edit

Actually, why go through all the trouble of using openCV for opening the video. I guess there are other available open source libraries which can read the video, grab a frame from it and let you use that frame for processing in openCV? The following does exactly this :

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-28 10:28:16 -0600 )edit

Thanks! That didn't occur to me. I'll look for something that is cross-platform as that is better-suited to my needs.

4thguy gravatar image4thguy ( 2013-03-28 10:31:45 -0600 )edit

Question Tools


Asked: 2013-03-28 07:42:10 -0600

Seen: 8,936 times

Last updated: Oct 10 '13