c++iosopencvopencv3.0video-codecs

OpenCV VideoWriter issue crashing on iOS - info.backendFactory.empty() in function 'open'


I'm using cv::VideoWriter from OpenCV 3.5.2 class to read a file and write it into another file, with a lower FPS rate. However, when calling

  cv::VideoWriter videoWriter = cv::VideoWriter();

  videoWriter.open(
    outputFilePath.UTF8String,
    cv::VideoWriter::fourcc('I', 'Y', 'U', 'V'),
    15,
    cv::Size(1920, 1080),
    true
  );

I get

terminating with uncaught exception of type cv::Exception: OpenCV(4.5.2) /Volumes/build-storage/build/master_iOS-mac/opencv/modules/videoio/src/cap.cpp:551: error: (-215:Assertion failed) !info.backendFactory.empty() in function 'open'

Where outputFilePath is an .avi file in my cache directory

/var/mobile/Containers/Data/Application/53C6BF23-5F44-4F89-AFD2-0AE271DC5A74/Library/Caches/ckckh35mc76y2071866jxqej6-rendered-2021-4-22_12-17-44.avi

Since I'm not able to get any more outputs from the error, I'm assuming this could be an error of unexpected codecs.

I have tried the following container/codec combinations.

I'm running my program on a physical iOS device (more precisely iPhone 6s)


Solution

  • I figured it out. For iOS, looks like you need force the Backend used for video writing. Therefore, using

    videoWriter.open(
      outputFilePath.UTF8String,
      cv::CAP_AVFOUNDATION,
      cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
      15,
      cv::Size(1920, 1080)
    );
    

    Worked for me. Relevant here is the cv::CAP_AVFOUNDATION api attribute