Undefined symbol when using VideoCapture in WebAssembly

I was trying to use OpenCV’s video capture on C++ side of wsam, but the emcc compiler can’t find a symbol. Minimal video.cc example as below

#include <opencv2/highgui.hpp>

int main() {
  cv::VideoCapture("./video.mp4"); 
}

I can compile with g++ (version 12.0.5):

 g++ -O3 `pkg-config --cflags --libs opencv4` -std=c++17 video.cc

But when using emcc (version 2.0.25):

emcc -O3 `pkg-config --cflags --libs opencv4` -std=c++17 video.cc

I got an error

error: undefined symbol: _ZN2cv12VideoCaptureC1ERKNSt3__212basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEi (referenced by top-level compiled C/C++ code)
warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
warning: __ZN2cv12VideoCaptureC1ERKNSt3__212basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEi may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors

Searched over the Internet, saw a few similar questions (same undefined symbols), but couldn’t find an answer.

Can anyone share some ideas about solving this issue? Really appreciate the help!

not a js user, but afaik, you have to use the javascript emulation here and the c++ version is not available (the whole videoio module is excluded from the js build)

Thanks a lot for he reply! This is my guess too. Any reason the videoio module is excluded - is there a plan brining it in?

I’m basically now trying to emcc build videoio module together with the code that uses cv::VideoCapture. It’s a painful process tbh…

i can only guess, but the videoio is not doing much on its own, but heavily relies on external libs like v4l, ffmpeg or gstreamer

i don’t think so, but have a look here

ok so I set "-DBUILD_opencv_videoio=ON" in opencv/platforms/js/build_js.py, then rebuild the js build. The build was successful.

I was hoping libopencv_videoio.a to appear but it is not in opencv/platforms/js/build_wasm/lib/*.a.

So linking to opencv/platforms/js/build_wasm/lib/*.a still has the same missing symbol.

But stepping back, what I want to have is an efficient video format to do some editing on the C++ side. I could load a video on js and then send all the frames as images to C++. Or I could load videos from file using ffmpeg. But I guess the problem would be that many of the functions in opencv’s video module take as input VideoCapture object. To really use opencv library for videos, I may have to use VideoCapture to load video in the first step. Is my assessment accurate?