Logo
Computer Vision & Media Build Guide

OpenCV Compilation & FFmpeg Integration

A complete guide to compiling OpenCV 4.x from source using CMake across Windows, macOS, and Linux, and seamlessly hooking it into FFmpeg to unlock powerful computer vision video filters.

Step 1: Source Code & CMake Toolchain

Download OpenCV and prepare the cross-platform CMake build system

OpenCV 4.x Source

The open-source computer vision library. We use the 4.x branch for modern compatibility and stability.

Extraction Command:
unzip opencv-4.x.zip && cd opencv-4.x

CMake (Required)

Unlike FFmpeg's configure script, OpenCV heavily relies on CMake to generate platform-specific makefiles.

Installation:
# Windows (MSYS2): pacman -S mingw-w64-x86_64-cmake
# Ubuntu/Linux: sudo apt install cmake build-essential
# macOS: brew install cmake

Step 2: Cross-Platform OpenCV Build

Crucial: You MUST enable OPENCV_GENERATE_PKGCONFIG for FFmpeg to find it later.

The PKG-CONFIG Trap

FFmpeg's configure script uses `pkg-config` to check if OpenCV exists. By default, OpenCV 4's CMake does NOT generate a .pc file. You must explicitly pass `-DOPENCV_GENERATE_PKGCONFIG=ON` during the cmake phase.

1. Windows Platform (Run inside MSYS2 MINGW64 shell)
cd opencv-4.x
mkdir build && cd build

# Windows MinGW requires specific generator
cmake -G "MinGW Makefiles" \
    -DOPENCV_GENERATE_PKGCONFIG=ON \
    -DCMAKE_INSTALL_PREFIX=/usr/local/opencv4 \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    ..

mingw32-make -j$(nproc)
mingw32-make install
2. Ubuntu / Debian Linux Platform
cd opencv-4.x
mkdir build && cd build

cmake -DOPENCV_GENERATE_PKGCONFIG=ON \
    -DCMAKE_INSTALL_PREFIX=/usr/local/opencv4 \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTS=OFF \
    ..

make -j$(nproc)
sudo make install
3. macOS Platform
cd opencv-4.x
mkdir build && cd build

cmake -DOPENCV_GENERATE_PKGCONFIG=ON \
    -DCMAKE_INSTALL_PREFIX=/usr/local/opencv4 \
    -DWITH_OPENJPEG=OFF \
    -DBUILD_TESTS=OFF \
    ..

make -j$(sysctl -n hw.logicalcpu)
make install

Step 3: Integrate into FFmpeg

Export the pkg-config path and recompile FFmpeg with --enable-libopencv

link_ffmpeg.sh
# 1. Expose OpenCV's .pc file to the system PKG_CONFIG_PATH
export PKG_CONFIG_PATH="/usr/local/opencv4/lib/pkgconfig:$PKG_CONFIG_PATH"

# 2. Verify pkg-config can find it (Should output OpenCV flags)
pkg-config --cflags --libs opencv4

# 3. Go to FFmpeg source and configure with OpenCV enabled
cd ../ffmpeg-7.0
./configure \
    --enable-libopencv \
    --enable-gpl \
    --enable-shared \
    --extra-cflags="-I/usr/local/opencv4/include/opencv4"
make -j$(nproc) && make install

Step 4: Verification & Usage Examples

Confirm the filters are loaded and run an edge detection test

1. Verify OpenCV Filters

Check if FFmpeg correctly registered the OpenCV video filters (edgedetect, facedetect, etc.).

Console Command:
./ffmpeg -filters | grep opencv
Expected Output Match:
... edgedetect V->V Detect and draw edge. (opencv)
... facedetect V->V Detect and draw faces. (opencv)
2. Run Edge Detection Test

Apply the Canny edge detector from OpenCV directly to a video stream.

CLI Execution:
./ffmpeg -i input.mp4 -vf "edgedetect=low=0.1:high=0.4" -c:a copy output_edged.mp4