Search code examples
javaopencvvideo-capturecodec

Java setting VideoCapture.set(CAP_PROP_FOURCC, codec value)


I want to set a Codec for VideoCapture.

I found THIS LIST of codecs. Now since VideoCapture.set(CAP_PROP_FOURCC, double value) expects a double for the codec value and I haven't found a way in JavaCV to convert the codecs to double values I would like to know how do I pass the relevant codec to VideoCapture.set(CAP_PROP_FOURCC, double value)?


Solution

  • Better late then never...

    I came around the same problem when I realized I would get MUCH better performance when fetching data from the camera as an mpeg stream (~10FPS with default YUY2, ~30FPS with mpeg). The trick with setting FOURCC is, to first determine the "code" needed for the respective FourCC codec and THEN setting the property "Videoio.CAP_PROP_FOURCC" to the determined codec. For any given codec, the associated "code" can be retrieved via "VideoWriter" class.

        int fourcc = VideoWriter.fourcc('M', 'J', 'P', 'G');
        videoCapture.set(Videoio.CAP_PROP_FOURCC, fourcc);
        videoCapture.set(Videoio.CAP_PROP_FRAME_WIDTH, CAP_FRAME_WIDTH);
        videoCapture.set(Videoio.CAP_PROP_FRAME_HEIGHT, CAP_FRAME_HEIGHT);
    

    Keep in mind that, you have to set the FourCC BEFORE adjusting the frame size, otherwise it has no effect. The code above is for openCV 3.0. This page (although for C++) pointed me into the right direction: http://answers.opencv.org/question/6805/how-to-get-mjpeg-compression-format-from-webcam/