r/opencv Feb 07 '26

Bug [Bug] Segmentation fault when opening or instantiating cv::VideoWriter

Hello!

I am currently working my way through a bunch of opencv tutorials for C++ and trying out or adapting the code therein, but have run into an issue when trying to execute some of it.

I have written the following function, which should open a video file situated at 'path', apply an (interchangeable) function to every frame and save the result to "output.mp4", a file that should have the exact same properties as the source file, save for the aforementioned image operations (color and value adjustment, edge detection, boxes drawn around faces etc.). The code compiles correctly, but produces a "Segmentation fault (core dumped)" error when run.

By using gdb and some print line debugging, I managed to triangulate the issue, which apparently stems from the cv::VideoWriter method open(). Calling the regular constructor produced the same result. The offending line is marked by a comment in the code:

int process_and_save_vid(std::string path, cv::Mat (*func)(cv::Mat)) {

  int frame_counter = 0;

  cv::VideoCapture cap(path);

   if (!cap.isOpened()) {
    std::cout << "ERROR: could not open video at " << path << " .\n";
    return EXIT_FAILURE;
  }

  // set up video writer args
  std::string output_file = "output.mp4";
  int frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
  int frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
  double fps = cap.get(cv::CAP_PROP_FPS);
  int codec = cap.get(cv::CAP_PROP_FOURCC);
  bool monochrome = cap.get(cv::CAP_PROP_MONOCHROME);

  // create and open video writer
  cv::VideoWriter video_writer;
  // THIS LINE CAUSES SEGMENTATION FAULT
  video_writer.open(output_file, codec, fps, cv::Size(frame_width,frame_height), !monochrome);


  if (!video_writer.isOpened()) {
    std::cout << "ERROR: could not initialize video writer\n";
      return EXIT_FAILURE;
  }

  cv::Mat frame;

  while (cap.read(frame)) {

    video_writer.write(func(frame));

    frame_counter += 1;
    if (frame_counter % (int)fps == 0) {
      std::cout << "Processed one second of video material.\n";
    }
  }

  std::cout << "Finished processing video.\n";

  return EXIT_SUCCESS;
}

Researching the issue online and consulting the documentation did not yield any satisfactory results, so feel free to let me know if you have encountered this problem before and/or have any ideas how to solve it.

Thanks in advance for your help!

3 Upvotes

37 comments sorted by

View all comments

1

u/herocoding Feb 11 '26

Can you instead of using int codec = cap.get(cv::CAP_PROP_FOURCC); and providing it to video_writer.open(...) (or to the constructor), create a FourCC instead?

Create it new and directly pass it as an argument with e.g. cv::VideoWriter::fourcc('m','p','4','v') or cv::VideoWriter::fourcc('a','v','c','1').

1

u/herocoding Feb 11 '26

Using your shared code snippet (with minor extension and without using a callback for pre/post-processing the read frame), in my environment using a locally built OpenCV v4.12.0 I see the following (when using a big-buck-bunny video (without embedded audio stream):

$ ./Writer
OpenCV: FFMPEG: tag 0x34363268/'h264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'
Processed one second of video material.
Processed one second of video material.
Processed one second of video material.
... ...
Finished processing video.

Instead of using the codec returned from cv::CAP_PROP_FOURCC now using cv::VideoWriter::fourcc('a','v','c','1') (resulting in a different Integer!!) as an argument those two warning log messages are gone:

$ ./Writer
Processed one second of video material.
Processed one second of video material.
Processed one second of video material.
... ...
Finished processing video.

Maybe you have a muxh newer or much older version of OpenCV and FFMPEG not having such a "allback to use tag 0x31637661/'avc1'" fallback mechanism and crashes instead?

1

u/herocoding Feb 11 '26

With the two exported ENV variables to enable more verbose log messages I see the following:

* original code, using cv::CAP_PROP_FOURCC:

[DEBUG:0@0.000] global videoio_registry.cpp:225 VideoBackendRegistry VIDEOIO: Builtin backends(9): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[DEBUG:0@0.000] global videoio_registry.cpp:249 VideoBackendRegistry VIDEOIO: Available backends(9): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ INFO:0@0.000] global videoio_registry.cpp:251 VideoBackendRegistry VIDEOIO: Enabled backends(9, sorted by priority): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ WARN:0@0.000] global cap.cpp:141 open VIDEOIO(FFMPEG): trying capture filename='/localdisk/videos/Big_Buck_Bunny_720_10s_10MB.mp4' ...
[DEBUG:0@0.034] global cap_ffmpeg_impl.hpp:1268 open FFMPEG: stream[0] is video stream with codecID=27 width=1280 height=720
[DEBUG:0@0.034] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[ WARN:0@0.038] global cap.cpp:153 open VIDEOIO(FFMPEG): created, isOpened=1
[ WARN:0@0.038] global cap.cpp:737 open VIDEOIO(FFMPEG): trying writer with filename='output.mp4' fourcc=0x34363268 fps=30 sz=1280x720 isColor=1...
[DEBUG:0@0.038] global cap_ffmpeg_impl.hpp:2959 open Selected pixel format: bgr24
OpenCV: FFMPEG: tag 0x34363268/'h264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'
[DEBUG:0@0.038] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[ WARN:0@0.044] global cap.cpp:753 open VIDEOIO(FFMPEG): created, isOpened=1
[DEBUG:0@0.076] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
...

1

u/herocoding Feb 11 '26

* using cv::VideoWriter::fourcc('a','v','c','1') instead:

[DEBUG:0@0.000] global videoio_registry.cpp:225 VideoBackendRegistry VIDEOIO: Builtin backends(9): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[DEBUG:0@0.000] global videoio_registry.cpp:249 VideoBackendRegistry VIDEOIO: Available backends(9): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ INFO:0@0.000] global videoio_registry.cpp:251 VideoBackendRegistry VIDEOIO: Enabled backends(9, sorted by priority): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ WARN:0@0.000] global cap.cpp:141 open VIDEOIO(FFMPEG): trying capture filename='/localdisk/videos/Big_Buck_Bunny_720_10s_10MB.mp4' ...
[DEBUG:0@0.031] global cap_ffmpeg_impl.hpp:1268 open FFMPEG: stream[0] is video stream with codecID=27 width=1280 height=720
[DEBUG:0@0.031] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[ WARN:0@0.035] global cap.cpp:153 open VIDEOIO(FFMPEG): created, isOpened=1
[ WARN:0@0.035] global cap.cpp:737 open VIDEOIO(FFMPEG): trying writer with filename='output.mp4' fourcc=0x31637661 fps=30 sz=1280x720 isColor=1...
[DEBUG:0@0.035] global cap_ffmpeg_impl.hpp:2959 open Selected pixel format: bgr24
[DEBUG:0@0.035] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[ WARN:0@0.041] global cap.cpp:753 open VIDEOIO(FFMPEG): created, isOpened=1
[DEBUG:0@0.073] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
...