Building OpenCV with FFmpeg Using Antigravity AI

In this article, I will explore how to built OpenCV with FFmpeg support, and FFmpeg libraries on Windows using the Antigravity AI IDE agent for latest Visual Studio 18 2026.

I chose to use prebuild FFmpeg library to speed up the whole process, as I am not looking for specific configuration of FFmpeg right now. 

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. 

Antigravity to configure CMAKE Opencv build

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

I always starting with Gemini Pro thinking model to develop a plan. Than switch to Flash in Antigravity. My initial Ask was:

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

The result was this plan

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

  1. Open a terminal in C:\projects\opencv.
  2. Run build.bat.

The script will:

  1. Change to the script directory.
  2. Download FFmpeg.
  3. Download OpenCV source.
  4. Configure the build with VS 18 2026.
  5. Compile Release configuration.
  6. 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-*.dllavformat-*.dll, etc.


As plan looks reasonable to me, I also keep Donwload FFmpeg from prebuild binaries. I switch to Flash model to execute the plan. I always running agent in mode that every command agains my conputer needs to be approved. 

The Strategy

Instead of manually downloading dependencies and setting environment variables, I instruct antigravity to designed a CMakeLists.txt that automates everything:

  1. Fetch FFmpeg: Download prebuilt shared binaries (DLLs) from a reliable source.

  2. Fetch OpenCV: Download the official OpenCV source code.

  3. Configure: Force OpenCV to use our downloaded FFmpeg and build shared libraries.

  4. Install: Package everything (OpenCV DLLs + FFmpeg DLLs) into a single dist folder.

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:

  1. vcpkg Conflict: The system had vcpkg installed, and its Protobuf library conflicted with OpenCV's internal Protobuf.

  2. 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_VERSION and avcodec_version().

  • The Link: I had to point find_package(OpenCV) specifically to the dist/lib folder because the root dist/OpenCVConfig.cmake was a dispatcher that failed with our custom folder structure.

No, I do not come up with CMAKE syntax how  
# CMakeLists.txt for Test Project
cmake_minimum_required(VERSION 3.24)
project(OpenCV_FFmpeg_Test)

set(CMAKE_CXX_STANDARD 17)

# Point to our build artifact directory
set(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 fail
find_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 folder
file(GLOB FFMPEG_LIBS "${FFMPEG_LIB_DIR}/*.lib")

add_executable(version_test main.cpp)

# Header includes
target_include_directories(version_test PRIVATE
    ${OpenCV_INCLUDE_DIRS}
    ${FFMPEG_INCLUDE_DIR}
)

# Linking
target_link_libraries(version_test PRIVATE
    ${OpenCV_LIBS}
    ${FFMPEG_LIBS}
)

# Copy DLLs to output directory for running
add_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

These models learned over repos, blogs, web of great work of Opencv comunity. Our real work of hours and hours of configuration, testing, try and fail. Now the Models steal and learn this all. It will make our work much easier and faster. If there was somethink what model learns from my blog. I will be super proud. 

I cover how easy is to build Opencv from source with FFmpeg today. And configure project. In Minutes, Minutes!!

By using a scripted CMake approach, we created a reproducible build process that:

  1. Is independent of system-wide installations.

  2. Automatically handles FFmpeg dependencies.

  3. Produces a clean dist folder 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.

Next Post Previous Post
No Comment
Add Comment
comment url