0

I am using the OpenCV 3.1 Library for Android in my project to process images and compile a video file on basis of several individual OpenCv::Mat.

My problem is to find the correct fourCC code. Using "MJPG" works fine on a Google Nexus 6P (Android 7.0) but not on a Nexus 4 (5.1.1) or a Galaxy A3 (6.0.1).

In the code, images is an Array containing the image data as bytearrays.

VideoWriter videoWriter = new VideoWriter(filePath, VideoWriter.fourcc('M','J','P','G'), FPS, new Size(w,h));
videoWriter.open(filePath, VideoWriter.fourcc('M','J','P','G'), FPS, new Size(w,h));

for (int i = 0; i < images.size(); i++) {
     byte[] image = images.get(i);
     Mat rgbMat = new Mat(h,w,CvType.CV_8UC3);
     rgbMat.put(0,0,image);

     videoWriter.write(rgbMat);
     rgbMat.release();

}

videoWriter.release();

As I said, I get a valid .avi file on the Nexus 6P but a corrupt file on the other phones. I know that the rest of my pipeline is working on all phones because I can save single images without a problem (not using OpenCV).

My question: Is there a fourCC code that should work on any phone, independent of the manufacturer?

I tried with no success: 'MPEG', 'MP4V', 'PIM1'

Thank you for your help!

  • 2
    Alright, so in the meantime I found a reference in the OpenCV forums: Currently, the Android Library only supports the 'MJPG' fourCC code. – Matthias Nefzger Oct 18 '16 at 06:33
  • Have the same issue using OpenCv 3.20. The .avi file created with MJPG fourcc code does not play in my Android 6 phone, nor with Windows 10 default media player. Conversely, can be seen ok on VLC media player. Perhaps some additional setting in VideoWriter would make it compatible for all? Did you work more on it? – Jordi C. May 10 '17 at 16:22

2 Answers2

1

If you use mVideoWriter inside onCameraFrame overrides it initialises every time so uses some conditions like below mentioned.

 if (mVideoWriter == null) {
            mVideoWriter = new VideoWriter();
            Log.d(TAG, "mVideoWriter good : " + mVideoWriter);
            mVideoWriter.open(file_path.toString() + "/" + System.currentTimeMillis() + ".avi", VideoWriter.fourcc('M', 'J', 'P', 'G'), 30.0, mRgba.size());
            Log.i(TAG, "onCameraFrame: recordFilePath");


        }
        if (!mVideoWriter.isOpened()) {
            Log.w(TAG, "onCameraFrame: open");
            mVideoWriter.open(path.toString() + "/" + System.currentTimeMillis() + ".avi", VideoWriter.fourcc('M', 'J', 'P', 'G'), 30.0, mRgba.size());

        }
Dev.Barai
  • 82
  • 1
  • 8
0

Problem is that VideoWriter in Android support only .avi format. Here is a example solution.

if (videoWriter.isOpened() == false) {
    Log.w("wewe", "som tu OPEN");
    videoWriter.open("/sdcard/movies/video.avi", VideoWriter.fourcc('M', 'J', 'P', 'G'), 25.0D, colorMat.size());
} else {
    videoWriter.write(colorMat);
}
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
KicoSVK
  • 71
  • 7