Core dumped when using opencv with ffmpeg built for CUDA support

Hi,

whenever I want to use the VideoWriter_fourcc or any other method that uses ffmpeg in the background Im running into a segmentation fault (core dumped) error.

I’m running OpenCV in a virtual python environment with OpenCV built for cuda support.

This is the test file that I tried to run.

import cv2
from sys import settrace


def trace(frame, event, arg):
    print("%s, %s:%d" % (event, frame.f_code.co_filename, frame.f_lineno))
    return trace


settrace(trace)

# Open the video file for reading
cap = cv2.VideoCapture("test.mp4")

# Get the video dimensions and frame rate
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))

# Create a VideoWriter object to save the output video
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter("output_video.mp4", fourcc, fps, (width, height))

# Read frames from the input video, process them, and write them to the output video
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Process the frame here (e.g. apply filters, draw annotations, etc.)

    # Write the processed frame to the output video
    out.write(frame)

    # Display the processed frame (optional)
    cv2.imshow("frame", frame)
    if cv2.waitKey(1) == ord("q"):
        break

# Release the resources
cap.release()
out.release()
cv2.destroyAllWindows()

When running the script with gdb to figure out why the core dumped, this is the output.

(gdb) run video_writer_test.py 
Starting program: /home/alexander/.virtualenvs/opencv_cuda/bin/python3 video_writer_test.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff3d8c338 in CvCapture_FFMPEG::open(char const*, cv::VideoCaptureParameters const&) () from /usr/local/lib/libopencv_videoio.so.407

When I configured the build of opencv, ffmpeg was successfully found on my system. I’ve built it from the 4.7.0 branch with the same version of opencv_contrib.

These were my build options:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D WITH_CUDA=ON \
        -D WITH_CUDNN=ON \
        -D OPENCV_DNN_CUDA=ON \
        -D ENABLE_FAST_MATH=1 \
        -D CUDA_FAST_MATH=1 \
        -D CUDA_ARCH_BIN=8.6 \
        -D WITH_CUBLAS=1 \
        -D WITH_FFMPEG=1 \
        -D OPENCV_EXTRA_MODULES_PATH=/home/alexander/Developer/opencv_contrib/modules \
        -D HAVE_opencv_python3=ON \
        -D PYTHON_EXECUTABLE=~/.virtualenvs/opencv_cuda/bin/python \
        -D BUILD_EXAMPLES=ON ..

ffmpeg is running fine on my system and I’ve built it from the source with support cuda.
This is my ffmpeg version:

ffmpeg version N-109993-g205117d87f Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 12 (Ubuntu 12.2.0-3ubuntu1)
configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared
libavutil      58.  3.100 / 58.  3.100
libavcodec     60.  6.101 / 60.  6.101
libavformat    60.  4.100 / 60.  4.100
libavdevice    60.  2.100 / 60.  2.100
libavfilter     9.  4.100 /  9.  4.100
libswscale      7.  2.100 /  7.  2.100
libswresample   4. 11.100 /  4. 11.100

Other methods in opencv which dont use ffmpeg work fine, for example dnn_superres which was the reason why I built opencv with cuda support, but I’d also need to use the ffmepg methods.

I hope someone has a clue what could be the problem with them not working.

I forgot to install the build (sudo make install) that I built with the WITH_FFMPEG=1 flag so I were still using an older build.

But the important part is to configure cmake with WITH_FFMPEG=1.