Building FFmpeg from Source on CentOS 7 with libx264 and libxvid Support
Preparing the Build Environment
Compiling FFmpeg on CentOS 7 requires a C compiler and several dependencies to enable specific encoding capabilities. Before beginning the compilation process, ensure that the basic development tools are available.
yum install gcc gcc-c++ make -y
Installing the NASM Assembler
The libx264 library requires the Netwide Assembler (NASM) for optimized assembly routines. Version 2.13 or higher is typically required.
cd /usr/local/src
wget https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz
tar -xzf nasm-2.15.05.tar.gz
cd nasm-2.15.05
./configure
make && make install
Compiling libx264
With the assembler ready, download and compile the x264 library. We will install it into a custom prefix directory to keep the system clean and manage library paths manually during the FFmpeg build.
cd /usr/local/src
git clone --depth 1 https://code.videolan.org/videolan/x264.git
cd x264
./configure --prefix=/usr/local/ffmpeg_deps/x264 --enable-shared --enable-pic
make && make install
Compiling libxvid
The Xvid codec provides high-quality MPEG-4 ASP compression. Download the source code and build it from the generic build directory.
cd /usr/local/src
wget http://downloads.xvid.org/downloads/xvidcore-1.3.3.tar.gz
tar -xzf xvidcore-1.3.3.tar.gz
cd xvidcore/build/generic
./configure --prefix=/usr/local/ffmpeg_deps/xvid
make && make install
Building FFmpeg
Download the FFmpeg source code (version 4.4.3 in this example). The configuration step must explicitly include the paths to the previously compiled libraries using --extra-cflags and --extra-ldflags.
cd /usr/local/src
wget http://ffmpeg.org/releases/ffmpeg-4.4.3.tar.gz
tar -xzf ffmpeg-4.4.3.tar.gz
cd ffmpeg-4.4.3
export X264_PATH=/usr/local/ffmpeg_deps/x264
export XVID_PATH=/usr/local/ffmpeg_deps/xvid
./configure \
--prefix=/usr/local/ffmpeg \
--enable-gpl \
--enable-nonfree \
--enable-libx264 \
--enable-libxvid \
--enable-shared \
--enable-pthreads \
--enable-avfilter \
--extra-cflags="-I${X264_PATH}/include -I${XVID_PATH}/include" \
--extra-ldflags="-L${X264_PATH}/lib -L${XVID_PATH}/lib"
make && make install
Configuring Runtime Libraries
After installation, the system must be informed of the location of the shared libraries. Add the library paths to the dynamic linker configuration and reload the cache.
echo "/usr/local/ffmpeg/lib" >> /etc/ld.so.conf
echo "/usr/local/ffmpeg_deps/x264/lib" >> /etc/ld.so.conf
echo "/usr/local/ffmpeg_deps/xvid/lib" >> /etc/ld.so.conf
ldconfig
Verify the installation by checking the version:
/usr/local/ffmpeg/bin/ffmpeg -version
Cross-Platform Usage Notes
Binaries compiled on CentOS 7 generally function correctly on newer distributions like Ubuntu 16.04 or CentOS 8, provided the kernel and system libraries are compatible. If encountering errors related to missing shared objects like libxcb.so.1 when running the binary on a minimal system, install the missing dependency:
yum install libxcb -y