I have a static library written in C++ that I have compiled for iOS and can use in a iOS test application but I cannot use opencv for videoio.
When I try to use either
cv::VideoCapture videoReader(filePathThatExistInMySandboxDocumentsFolder.mp4);
or
cv::VideoWriter videoWriter(filePathToNotExistInMySandboxDocumentsFolder.mp4,
cv::VideoWriter::fourcc('H', '2', '6', '4'), 30, cv::Size(1920, 1080), true);
I get the following exception
[ WARN:[email protected]] global cap.cpp:196 open VIDEOIO(AVFOUNDATION): raised unknown C++ exception!
Looking at cap.cpp line 196 I can see that the backend is found and that the exception originated from line 150 createCapture
but after this I start to get lost.
- I have built OpenCV myself with this option
cmake -DCMAKE_TOOLCHAIN_FILE=../platforms/ios/cmake/Toolchains/Toolchain-iPhoneOS_Xcode.cmake -DIOS_ARCH=arm64 -DCMAKE_INSTALL_PREFIX=$OCV_INSTALL -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DBUILD_opencv_objc=OFF -DBUILD_opencv_apps=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF -DWITH_OPENEXR=OFF -DWITH_CAP_IOS=OFF ..
WITH_CAP_IOS ON or OFF gives the same exception My static library is linked with these opencv libraries
libopencv_core.dylib
libopencv_video.dylib
libopencv_videoio.dylib
libopencv_imgproc.dylib
libopencv_imgcodecs.dylib
libopencv_features2d.dylib
- I have given the application permission to use the camera in case there is a dependency to the camera.
- Writing images to disk works
- The same code works without issues on MacOS also using AVFoundation as the backend
- In Xcode AVFoundation.framework is added to Build Phases -> Link Binary with Libraries
- The same code runs without problems on MacOS with opencv built for that platform
and I have run out of ideas of what to check next to be able to read and write videos on iOS using OpenCV?
Updates since original post:
Some helpful things that might be useful to others are to build OpenCV with -DCMAKE_BUILD_TYPE=Debug
and -DOPENCV_VIDEOIO_DEBUG=ON
the later outputs useful information about videoio.
I can write MJPEG files by forcing backend to CAP_OPENCV_MJPEG
instead of CAP_AVFOUNDATION
cv::VideoWriter videoWriter(filePathToNotExistInMySandboxDocumentsFolder.mp4, cv::CAP_OPENCV_MJPEG,
cv::VideoWriter::fourcc('H', '2', '6', '4'), 30, cv::Size(1920, 1080), true);
My conclusion is that, despite having added AVFoundation to my Xcode project, OpenCV linked in my C++ lib cannot find AVFoundation. So the question seems to be how to make AVFoundation visible to the C++ library?