Compile Opencv 4.7.0 with FFmpeg 5 compiled from the source in Ubuntu

In Opencv 4.7 release notes, There is mentioned FFmpeg 5.x support improved. So let's compile FFmpeg 5.x from the source first and then Opencv 4.7 from the source with custom FFmpeg in Ubuntu. 

I did it last time with Opencv 4.6, I compiled FFmpeg and failed to build Opencv with FFmpeg enabled. So, I used apt-get install for FFmpeg and then build Opencv with FFmpeg without a problem. Final App, then links FFmpeg build from source as well as Opencv libs with FFmpeg support. Article here

Today, Let's compile Opencv 4.7 with FFmpeg libs compiled from the source once again. 

Use ‘sudo’ in all commands if applicable. ;)

Prerequisites to build FFmpeg

The major source is FFmpeg Compilation Guide. If FFmpeg during the configuration complains, you have two options. Install the prerequisite or disable the option in the configuration. I installed approximately these prerequisites.

sudo apt-get update -qq && sudo apt-get -y install \
  autoconf \
  automake \
  build-essential \
  cmake \
  git-core \
  libass-dev \
  libfreetype6-dev \
  libgnutls28-dev \
  libmp3lame-dev \
  libsdl2-dev \
  libtool \
  libva-dev \
  libvdpau-dev \
  libvorbis-dev \
  libxcb1-dev \
  libxcb-shm0-dev \
  libxcb-xfixes0-dev \
  meson \
  ninja-build \
  pkg-config \
  texinfo \
  wget \
  yasm \
  zlib1g-dev \
  nasm \
  libx264-dev \
  libx265-dev \
  libnuma-dev \
  libvpx-dev \
  libfdk-aac-dev \
  libopus-dev \
  libdav1d-dev

Build FFmpeg 5 libs from the source

  • Download by wget command, I use FFmpeg 5.0.1.
  • Untar the source code
  • Enter extracted directory, no magic here
sudo wget -O ffmpeg-5.0.tar.bz2 "https://www.ffmpeg.org/releases/ffmpeg-5.0.1.tar.bz2"
sudo tar -xvf ffmpeg-5.0.tar.bz2
cd ffmpeg-5.0.1

We will get into more detail in the following section to configure the FFmpeg build. 

  1. Enable just what you installed as prerequisites. I install libx264-dev libx265-dev, libvpx-dev, and some others. 
  2. Build with static and shared options by enable-shared.

I hit some problems, building Opencv 4.7 when FFmpeg was built just as static libs. Most probably this was my major issue. If I remember correctly, It was Segmentation Fault during compiling videoio module.  

sudo ./configure  --enable-shared --enable-gpl --enable-libx264 --enable-libx265 \
--enable-libvpx --enable-zlib

The output of the configuration process will look as follows: Pay attention to static and shared correctly set to yes. 

FFmpeg build configuration for opencv 4.7
FFmpeg configuration for Opencv

Configured, so It is the right time to compile FFmpeg by make. -j4 is just optional to build in parallel. An example of the command and the expected output is below. 

sudo make -j4
FFmpeg compilation from source
The output of make FFmpeg library.

When FFmpeg compiled without any problems. The following make install will copy FFmpeg libraries to the final destination and ldconfig will create links and cache to the most recently founded shared libraries. 

sudo make install
sudo ldconfig -v

We are done with FFmpeg. So let's build OpenCV. 

Install FFmpeg
FFmpeg installation output. 

Build Opencv 4.7 with FFmpeg 5 support

This process is used all over again. Download, unzip, create a build directory, enter the build directory, and configure Opencv build with cmake. 

sudo wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
sudo unzip opencv.zip
Sudo mkdir build
Cd build
sudo cmake  ../opencv-4.7.0

 The configuration of Opencv output is wort to check for FFMPEG and AVlibs set to yes. Also, feel free to resolve any other problem that can occur in the configuration. 

Opencv compiled with FFmpeg
Opencv 4 configuration with FFmpeg

Then just build, and install by following commands and wait a couple of minutes to finish this process. 

sudo cmake --build . --config Release -- -j 4
sudo make install
Compile Opencv FFmpeg

Install FFmpeg Opencv 4
Opencv installation step

Conclusion for building Opencv 4.7 with FFmpeg 5

I am glad to overcome some problems. I really want my own custom build of FFmpeg in Opencv. I was able to compile Opencv 4.7 with FFmpeg 5 compiled from the source. 

Let me know If you hit any problems. I am sure you will. 

Next Post Previous Post
No Comment
Add Comment
comment url