Building OpenCV with FFmpeg Using Antigravity AI
Lets build Opencv with FFmpeg and also produce FFmpeg as lib. I did it many times on this blog. Pure visual studio configuration, linux build, cmake projects, building with support of different Opencv configuration, mainly for various video IO backends like gstreamer and ffmpeg. Latest aproach described by me in past using VCPKG manager is still a valid option, but all this can handle AI agent like Gemini CLI, Codex, Antigravity. What was issue in past is no longer a challenge. It is matter of minutes, So you can start coding your Opencv app in C++ app with latest Opencv build from source and customized support faster than ever before.
This process and text describes involved setting up a custom CMake build system, handling dependency conflicts, and verifying the result with a test project.
The Goal
To create a standalone, easy-to-distribute build of OpenCV 4.8.0 linked against FFmpeg 7.0 shared libraries, targeted for Visual Studio 18 2026 on Windows.
The Process
Write cmake files to get and compile dll of opencv, 4.8 compiled with ffmpeg, produce also ffmpeg as library dll. to be further included and used in other windows projects https://github.com/opencv/opencv/tree/4.8.0
Walkthrough: OpenCV 4.8.0 + FFmpeg Build
I have set up a CMake project to download and build OpenCV 4.8.0 with FFmpeg support using prebuilt FFmpeg shared libraries.
Files Created
- CMakeLists.txt: Main build configuration.
- cmake/FFmpeg.cmake: Downloads prebuilt FFmpeg binaries (DLLs).
- cmake/BuildOpenCV.cmake: Fetches and configures OpenCV source.
- build.bat: Helper script to build with Visual Studio 18 2026.
How to Build
- Open a terminal in
C:\projects\opencv. - Run
build.bat.
The script will:
- Change to the script directory.
- Download FFmpeg.
- Download OpenCV source.
- Configure the build with VS 18 2026.
- Compile Release configuration.
- Install artifacts to
C:\projects\opencv\dist.
verification
You verify the build by checking C:\projects\opencv\dist\bin. You should see opencv_world*.dll (or individual modules) and avcodec-*.dll, avformat-*.dll, etc.
The Strategy
Instead of manually downloading dependencies and setting environment variables, I instruct antigravity to designed a CMakeLists.txt that automates everything:
Fetch FFmpeg: Download prebuilt shared binaries (DLLs) from a reliable source.
Fetch OpenCV: Download the official OpenCV source code.
Configure: Force OpenCV to use our downloaded FFmpeg and build shared libraries.
Install: Package everything (OpenCV DLLs + FFmpeg DLLs) into a single
distfolder.
Key Build Files
build.bat - The build script that runs cmake
This script initializes the environment and runs the build. A critical detail I fixed was ensuring it runs from the correct directory to allow relative paths in CMake.
@echo off
setlocal
# Critical fix: Change directory to script location to avoid path issues
cd /d "%~dp0"
set BUILD_DIR=build
set INSTALL_DIR=dist
echo Configuring...
cmake -G "Visual Studio 18 2026" -B "%BUILD_DIR%" -S . -DCMAKE_INSTALL_PREFIX="%INSTALL_DIR%"
echo Building...
cmake --build "%BUILD_DIR%" --config Release
echo Installing...
cmake --install "%BUILD_DIR%" --config Release
cmake/FFmpeg.cmake - Handling Dependencies
I used FetchContent to download prebuilt FFmpeg binaries. The key here was installing the FFmpeg DLLs directly alongside the OpenCV binaries so the final application runs without "DLL not found" errors.
FetchContent_Declare(
ffmpeg_prebuilt
URL "https://github.com/BtbN/FFmpeg-Builds/releases/.../ffmpeg-master-latest-win64-gpl-shared.zip"
)
FetchContent_MakeAvailable(ffmpeg_prebuilt)
# Important: Install FFmpeg DLLs to the bin folder
file(GLOB FFMPEG_DLLS "${ffmpeg_prebuilt_SOURCE_DIR}/bin/*.dll")
install(FILES ${FFMPEG_DLLS} DESTINATION bin COMPONENT libs)
cmake/BuildOpenCV.cmake - Resolving Conflicts
During the build, I encountered two major issues:
vcpkg Conflict: The system had
vcpkginstalled, and its Protobuf library conflicted with OpenCV's internal Protobuf.IPP Errors: The Intel Performance Primitives (IPP) module in OpenCV 4.12.0 caused build failures.
I solved both by disabling the conflicting/unnecessary modules in our CMake configuration:
# Fix for vcpkg/protobuf conflict
set(WITH_PROTOBUF OFF CACHE BOOL "Disable Protobuf" FORCE)
set(BUILD_opencv_dnn OFF CACHE BOOL "Disable DNN" FORCE)
# Fix for IPP build failures
set(WITH_IPP OFF CACHE BOOL "Disable IPP" FORCE)
set(WITH_OPENCL OFF CACHE BOOL "Disable OpenCL" FORCE)
# Point OpenCV to our FFmpeg
set(ENV{FFMPEG_DIR} "${FFMPEG_ROOT}")
set(FFMPEG_DIR "${FFMPEG_ROOT}" CACHE PATH "Path to FFmpeg" FORCE)
3. Verification
I created a separate test project (OpenCV_FFmpeg_Test) to confirm the build works. Works
The Test: A simple C++ program that prints
CV_VERSIONandavcodec_version().The Link: I had to point
find_package(OpenCV)specifically to thedist/libfolder because the rootdist/OpenCVConfig.cmakewas a dispatcher that failed with our custom folder structure.
cmake_minimum_required(VERSION 3.24)project(OpenCV_FFmpeg_Test)
set(CMAKE_CXX_STANDARD 17)
# Point to our build artifact directoryset(OPENCV_DIST_DIR "c:/projects/opencv/dist")
# 1. Find OpenCV# We tell CMake where to look for OpenCVConfig.cmake# Pointing to the 'lib' folder because the root dispatcher might failfind_package(OpenCV REQUIRED PATHS "${OPENCV_DIST_DIR}/lib" NO_DEFAULT_PATH)
message(STATUS "Found OpenCV: ${OpenCV_VERSION}")message(STATUS "OpenCV Include Dirs: ${OpenCV_INCLUDE_DIRS}")
# 2. Setup FFmpeg (Manual setup since we just copied DLLs/headers)set(FFMPEG_INCLUDE_DIR "${OPENCV_DIST_DIR}/include/ffmpeg")set(FFMPEG_LIB_DIR "${OPENCV_DIST_DIR}/lib/ffmpeg")
# Helper to find FFmpeg libs in our dist folderfile(GLOB FFMPEG_LIBS "${FFMPEG_LIB_DIR}/*.lib")
add_executable(version_test main.cpp)
# Header includestarget_include_directories(version_test PRIVATE ${OpenCV_INCLUDE_DIRS} ${FFMPEG_INCLUDE_DIR})
# Linkingtarget_link_libraries(version_test PRIVATE ${OpenCV_LIBS} ${FFMPEG_LIBS})
# Copy DLLs to output directory for runningadd_custom_command(TARGET version_test POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory "${OPENCV_DIST_DIR}/bin" "$<TARGET_FILE_DIR:version_test>" COMMENT "Copying DLLs to executable directory...")Test, project ready to work with Opencv, FFmpeg libraries in fastest way that any tutorial I have ever descibed in past years. Including VCPKG was more time consuming than this aproach. Conclusion
By using a scripted CMake approach, we created a reproducible build process that:
Is independent of system-wide installations.
Automatically handles FFmpeg dependencies.
Produces a clean
distfolder ready for distribution.
The Antigravity agent helped identify the build errors (protobuf conflicts, IPP issues) and rapidly iterated on the CMake scripts to produce a working solution.