Back to posts.

Compiling FFmpeg with X264 on Windows 10 using MSVC

I'm a big fan of the FFmpeg builds by Kyle Schwarz though recently the website was down and I had to compile FFmpeg manually. Although compiling FFmpg on Windows is simple, once you've figured out the details, it will still take you some time to get it right.

Therefore I'm providing these instructions that you can follow step-by-step and will build a shared library for FFmpeg with X264. Note that at the time of writing you can't link statically with X264, see the X264_API_IMPORTS define in FFmpegs source. I've tried to keep things as short as possible. Hit me up on Twitter when you have any questions. Thanks Matthias C. M. Troffaes for providing fixes that allow us to compile X264 with MSVC with MSYS2.

You can download the shell script containing all the steps from this gist

1. Install MSYS2

  • Download MSYS2, msys2-x86_64-{date}.exe
  • Install into c:/msys64
  • Edit c:/msys64/msys2_shell.cmd and remove rem from the line with rem MSYS2_PATH_TYPE=inherit
  • Open a x64 Native Tools Command Prompt for VS 2019
  • Run c:/msys64/msys2_shell.cmd
  • Use the MSYS2 shell for the next steps and enter:
pacman -Syu
pacman -S make
pacman -S diffutils
pacman -S yasm
pacman -S nasm
 
mv /usr/bin/link.exe /usr/bin/link.exe.bak

2. Get the sources for x264 and ffmpeg

mkdir tmp
cd tmp
mkdir sources
mkdir build
cd sources
 
git clone --depth 1 https://code.videolan.org/videolan/x264.git
git clone --depth 1 git://source.ffmpeg.org/ffmpeg.git

3. Fix the build scripts for x264

cd tmp/sources/x264
curl "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD" > config.guess
sed -i 's/host_os = mingw/host_os = msys/' configure

4. Compile X264

  • Open x64 Native Tools Command Prompt for VS2019
  • Run c:/msys64/msys2_shell.cmd
  • Use the MSYS2 shell for the next steps
cd tmp/build
mkdir x264
cd x264
 
CC=cl ./../../sources/x264/configure --prefix=./../../installed --enable-shared
make -j 8
make install
mv ./../../installed/lib/libx264.dll.lib ./../../installed/lib/libx264.lib

5. Compile FFmpeg

  • Open x64 Native Tools Command Prompt for VS2019
  • Run c:/msys64/msys2_shell.cmd
  • Use the MSYS2 shell for the next steps
cd tmp/build
mkdir ffmpeg
cd ffmpeg
 
export CC=cl
 
./../../sources/ffmpeg/configure \
             --prefix=./../../installed \
             --toolchain=msvc \
             --arch=x86_64 \
             --enable-yasm  \
             --enable-asm \
             --enable-shared \
             --disable-static \
             --disable-programs \
             --enable-avresample \
             --enable-libx264 \
             --enable-gpl \
             --extra-ldflags="-LIBPATH:./../../installed/lib/" \
             --extra-cflags="-I./../../installed/include/"
 
make V=1 -j 8
 
make install

..or as a oneliner

CC=cl ./../../sources/ffmpeg/configure --prefix=./../../installed --toolchain=msvc --arch=x86_64 --enable-yasm --enable-asm --enable-shared --disable-static --disable-programs --enable-avresample --enable-libx264  --enable-gpl  --extra-ldflags="-LIBPATH:./../../installed/lib" --extra-cflags="-I./../../installed/include/"