I have struggled to build custom opencv 4.11 with custom ffmpeg 7.1.1. Until I found this option:
OPENCV_FFMPEG_USE_FIND_PACKAGE
within ./opencv/modules/videoio/cmake/detect_ffmpeg.cmake.
Everyone wrote about pkg-config but no one wrote that it is optional and should be specified manually.