OSXExperts.NET

How to compile a real static FFmpeg file for OSX/macOS Intel

There are several ways to compile FFmpeg. Homebrew is an easy way to compile FFmpeg files but sadly they are not static.
Static means you can copy the FFmpeg file anywere you want. It has no dependencies compared to a dynamically build like Homebrew does.

The below script will build a real static FFmpeg file. It does not cover all possible external libraries but it will give you a headstart. You can of course add more libs like Theora, Vorbis, VPX, .. yourself.
First download all source files:

- x264    git clone https://code.videolan.org/videolan/x264.git
- x265    https://bitbucket.org/multicoreware/x265/downloads/
- cmake
- enca
- expat
- lame    git clone https://github.com/rbrito/lame.git
- fribidi    https://github.com/fribidi/fribidi/releases/download/
- freetype    https://download.savannah.gnu.org/releases/freetype/
- fontconfig    https://www.freedesktop.org/software/fontconfig/release/
- libiconv
- libass     https://github.com/libass/libass/releases/download/
- nasm
- yasm     http://www.tortall.net/projects/yasm/releases/
- pkg-config   https://pkg-config.freedesktop.org/releases/

And of course FFmpeg :-)    git clone git://git.ffmpeg.org/ffmpeg.git

You also need to install Apple Xcode which contains all needed compilers.


Script for compiling FFmpeg.

#
# create Ramdisk
#

echo '♻️ ' Create Ramdisk

if df | grep Ramdisk > /dev/null ; then tput bold ; echo ; echo ⏏ Eject Ramdisk ; tput sgr0 ; fi

if df | grep Ramdisk > /dev/null ; then diskutil eject Ramdisk ; sleep 1 ; fi

DISK_ID=$(hdid -nomount ram://6000000)

newfs_hfs -v tempdisk ${DISK_ID}

diskutil mount ${DISK_ID}

sleep 1


SOURCE="/Volumes/tempdisk/sw"

COMPILED="/Volumes/tempdisk/compile"

mkdir ${SOURCE}

mkdir ${COMPILED}

export PATH=${SOURCE}/bin:$PATH

export CC=clang && export PKG_CONFIG_PATH="${SOURCE}/lib/pkgconfig"


#
# ask to copy all files to ramdisk
#

echo
echo Copy all files to ramdisk and press a key when ready
read -s


echo '♻️ ' Start compiling YASM

#
# compile YASM
#

cd ${COMPILED}

cd yasm-1.3.0

./configure --prefix=${SOURCE}

make -j 4

make install

sleep 1


echo '♻️ ' Start compiling NASM

#
# compile NASM
#

cd ${COMPILED}

cd nasm-2.15.03

./configure --prefix=${SOURCE}

make -j 4

make install

sleep 1


echo '♻️ ' Start compiling PKG

#
# compile PKG
#

cd ${COMPILED}

cd pkg-config-0.29.2

export LDFLAGS="-framework Foundation -framework Cocoa"

./configure --prefix=${SOURCE} --with-pc-path=${SOURCE}/lib/pkgconfig --with-internal-glib --disable-shared --enable-static

make -j 4

make install

unset LDFLAGS

sleep 1

echo '♻️ ' Start compiling CMAKE

#
# compile CMAKE
#

cd ${COMPILED}

cd cmake-3.18.1

./configure --prefix=${SOURCE}

make -j 4

make install

sleep 1


#
# compile Lame
#


cd ${COMPILED}
cd lame
./configure --prefix=${SOURCE} --disable-shared --enable-static
make -j 4
make install


echo '♻️ ' Start compiling X264

#
# x264
#

cd ${COMPILED}

cd x264

./configure --prefix=${SOURCE} --disable-shared --enable-static

make -j 4

make install

make install-lib-static

sleep 1


echo '♻️ ' Start compiling X265


#
# x265
#

rm -f ${SOURCE}/include/x265*.h 2>/dev/null

rm -f ${SOURCE}/lib/libx265.a 2>/dev/null

echo '♻️ ' X265 12bit

cd ${COMPILED}

cd /Volumes/tempdisk/compile/x265/source

cmake -DCMAKE_INSTALL_PREFIX:PATH=${SOURCE} -DHIGH_BIT_DEPTH=ON -DMAIN12=ON -DENABLE_SHARED=NO -DEXPORT_C_API=NO -DENABLE_CLI=OFF .

make

mv libx265.a libx265_main12.a

make clean-generated

rm CMakeCache.txt

echo '♻️ ' X265 10bit

cd ${COMPILED}

cd /Volumes/tempdisk/compile/x265/source

cmake -DCMAKE_INSTALL_PREFIX:PATH=${SOURCE} -DHIGH_BIT_DEPTH=ON -DMAIN10=ON -DENABLE_SHARED=NO -DEXPORT_C_API=NO -DENABLE_CLI=OFF .

make clean

make

mv libx265.a libx265_main10.a

make clean-generated && rm CMakeCache.txt

echo '♻️ ' X265 full

cd ${COMPILED}

cd /Volumes/tempdisk/compile/x265/source

cmake -DCMAKE_INSTALL_PREFIX:PATH=${SOURCE} -DEXTRA_LIB="x265_main10.a;x265_main12.a" -DEXTRA_LINK_FLAGS=-L. -DLINKED_10BIT=ON -DLINKED_12BIT=ON -DENABLE_SHARED=OFF -DENABLE_CLI=OFF .

make clean

make

mv libx265.a libx265_main.a

libtool -static -o libx265.a libx265_main.a libx265_main10.a libx265_main12.a 2>/dev/null

make install

sleep 1

echo '♻️ ' Start compiling ZLIB

#
# ZLIB
#


cd ${COMPILED}

cd zlib-1.2.11

./configure --prefix=${SOURCE}

make -j 4

make install

rm ${SOURCE}/lib/libz.so*
rm ${SOURCE}/lib/libz.*

echo '♻️ ' Start compiling EXPAT

#
# EXPAT
#

cd ${COMPILED}

cd expat-2.2.9

./configure --prefix=${SOURCE} --disable-shared --enable-static

make -j 4

make install

echo '♻️ ' Start compiling LIBICONV

#
# libiconv
#

cd ${COMPILED}

cd libiconv-1.16

./configure --prefix=${SOURCE} --disable-shared --enable-static

make -j 4

make install


echo '♻️ ' Start compiling ENCA


#
# ENCA
#

cd ${COMPILED}

cd enca-1.5

./configure --prefix=${SOURCE} --disable-shared --enable-static

make -j 4

make install


echo '♻️ ' Start compiling FREETYPE

#
# FREETYPE
#

cd ${COMPILED}

cd freetype-2.10.2

./configure --prefix=${SOURCE} --disable-shared --enable-static

make -j 4

make install


echo '♻️ ' Start compiling FONTCONFIG

#
# FONTCONFIG
#

cd ${COMPILED}

cd fontconfig-2.13.92

./configure --prefix=${SOURCE} --enable-iconv --disable-libxml2 --disable-shared --enable-static

make -j 4

make install


echo '♻️ ' Start compiling FRIBIDI

#
# FRIBIDI
#

cd ${COMPILED}

cd fribidi-1.0.5

./configure --prefix=${SOURCE} --disable-shared --enable-static

make -j 4

make install

echo '♻️ ' Start compiling LIBASS

#
# LIBASS
#

cd ${COMPILED}

cd libass-0.14.0

./configure --prefix=${SOURCE} --disable-fontconfig --disable-shared --enable-static

make -j 4

make install

sleep 1


echo '♻️ ' Start compiling FFMPEG


cd ${COMPILED}

cd ffmpeg

export LDFLAGS="-L${SOURCE}/lib"

export CFLAGS="-I${SOURCE}/include"

export LDFLAGS="$LDFLAGS -lass -lfontconfig -lexpat -lenca -lfribidi -lfreetype -liconv -lstdc++ -framework CoreText -framework VideoToolbox"

./configure --prefix=${SOURCE} --extra-cflags="-fno-stack-check" --arch=x86_64 --cc=/usr/bin/clang --enable-fontconfig --enable-gpl --enable-libmp3lame --enable-libass --enable-libfreetype --enable-libx264 --enable-libx265 --enable-version3 --pkg-config-flags=--static --disable-ffplay --enable-postproc --enable-nonfree --enable-runtime-cpudetect --disable-indev=qtkit --disable-indev=x11grab_xcb

make -j 4

make install