Core dumped when using opencv with ffmpeg built for CUDA support


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


# 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 =
    if not ret:

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

    # Write the processed frame to the output video

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

# Release the resources

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

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

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

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:

        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D WITH_CUDA=ON \
        -D WITH_CUDNN=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 \

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.