There are three ways to install FFmpeg on the MAC platform, respectively through FFmpeg static library download installation, through Homebrew installation and download source code compiled installation, you can choose your favorite way to achieve the MAC platform FFmpeg installation.

1. Download and install ffMPEG static library

Open the FFmpeg official website, as shown below.

Unzip the downloaded compressed file, you can see an executable file, execute it in the terminal.

It is also possible to configure an environment link to the executable so that it can be executed anywhere.

Although this way is simple and quick, it is not conducive to our study of FFMPEG.

2. Install through Homebrew

2.1. Install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Copy the code

If xCode has not been installed on your computer before, enter xcode-select –install CLT for xCode on the command line. Then you need to run the above command again to install.

The above is the official installation method. In order to avoid unnecessary problems caused by wall climbing, we can also install it by setting a mirror.

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/$homebrew-core.git"
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install /bin/bash brew-install/install.sh
rm -rf brew-install
Copy the code

If you’ve installed Homebrew before, it doesn’t matter, you can also set up the image configuration, see this website for details.

2.2. Install FFmpeg

If ffMPEG has been installed through Homebrew, run brew Uninstall FFmpeg to uninstall FFMpeg.

Execute the following command

brew install ffmpeg
Copy the code

or

brew install ffmpeg --HEAD
Copy the code

Both commands can install FFmPEG, both will detect ffmPEG dependency packages and automatically install, installation takes some time, please wait patiently. One problem is that homebrew no longer provides associated installation of options for FFMPEG from version 2.0. If you need to associate options with FFMPEG, you need to use a third-party options repository. So if your Homebrew version is later than 2.0, the above two instructions will not be associated with audio and video codecs such as X264, FDK-AAC, etc. So we can rely on the third-party repository Homebrew-FFmpeg to implement options and FFMPEG associated installation.

(1) Execute the following commands:

brew tap homebrew-ffmpeg/ffmpeg
brew install homebrew-ffmpeg/ffmpeg/ffmpeg
Copy the code

(2) Run the following command to see which options are supported by the warehouse.

brew options homebrew-ffmpeg/ffmpeg/ffmpeg
Copy the code

(3) Replace option in the following command according to options obtained in Step 2, and then execute.

brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-<option1> --with-<option2> ...
Copy the code

You can also run the following command to install all options.

brew install homebrew-ffmpeg/ffmpeg/ffmpeg $(brew options homebrew-ffmpeg/ffmpeg/ffmpeg --compact)
Copy the code

Installing FFmpeg via Homebrew is also a pretty neat way to do it, but it’s also not recommended because you can’t look at and debug the source code to learn about FFmpeg.

3, source code compilation and installation

3.1 download the source code

Download the source code from the official website. You can download the source code package directly, or clone the repository with git instructions.

Git clone git://source.ffmpeg.org/ffmpeg.git # or links to code cloud git clone https://gitee.com/mirrors/ffmpeg.git CD ffmpeg # to check the version Git branch - a # choose version git checkout remotes/origin/release/XXXCopy the code

3.2 compile the source code

Execute the following command in the downloaded source directory

./configure --prefix=/usr/local/ffmpeg \ --enable-gpl \ --enable-version3 \ --enable-nonfree \ --enable-postproc \ --enable-libass \ --enable-libcelt \  --enable-libfdk-aac \ --enable-libfreetype \ --enable-libmp3lame \ --enable-libopencore-amrnb \ --enable-libopencore-amrwb \ --enable-libopenjpeg \ --enable-openssl \ --enable-libopus \ --enable-libspeex \ --enable-libtheora \ --enable-libvorbis \ --enable-libvpx \ --enable-libx264 \ --enable-libxvid \ --disable-static \ --enable-sharedCopy the code

–prefix=/usr/local/ffmpeg –prefix=/usr/local/ffmpeg –prefix=/usr/local/ffmpeg

Run the compile and install command

make &&sudo make install
Copy the code

This compilation process may produce errors like liBFdk_aAC not Found or X264 Not found, which are not associated with dependencies installed, which we can install via Homebrew.

brew install fdk-aac
brew install x264
Copy the code

Of course, if your computer environment configuration is not complete, the compilation process may encounter the need to install errors will be more, it does not matter, what kind of dependencies need to install, execute the corresponding instructions can be.

3.3. Configure environment variables

Open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg to open /usr/local/ffmpeg

Execute the following command

touch .bash_profile
open .bash_profile
Copy the code

In the bash_profile file that opens, add the following statement.

export FFMPEG_HOME=/usr/local/ffmpeg
export PATH=$FFMPEG_HOME/bin:$PATH
Copy the code

At this point, the FFmpeg installation on the Mac is complete.

Although this installation is more complex, but we can modify the debugging source code, it is very convenient for us to study FFmpeg source code.

4. IOS platform library compilation

The above installation methods only allow us to implement FFmpeg development capabilities on the Mac OS platform, but as iOS developers, we need to compile the iOS platform library files for mobile development.

4.1. Compile FFmpeg

Download the compiled script from the following address.

https://github.com/kewlbear/FFmpeg-iOS-build-script
Copy the code

The contents that need to be modified are

  • FF_VERSIONSpecifies the version number of ffMPEG.
  • CONFIGURE_FLAGSModify ffMPEG compilation configuration, remove unnecessary compilation content, reduce package size.
  • ARCHSSpecifies the CPU architecture for compilation. For iOS development, there are real machines and emulators.
  • X264compiledlibx264If you need to compile libx264 into it.
  • FDK_AACcompiledlibfdk-aacIf libfDK-aac needs to be compiled into.

Run the./build-ffmpeg.sh command on the terminal. After the compilation, the following file structure is displayed.

4.2 compile liBFDK-aAC

Download the stable version of FDK-AAC from the official website

https://sourceforge.net/p/opencore-amr/fdk-aac/ci/v2.0.2/tree/
Copy the code

Create the build_aac.sh script file in the same directory as the build_aac.sh script.

CONFIGURE_FLAGS="--enable-static --with-pic=yes --disable-shared"
# specify the compilation platform
ARCHS="arm64 armv7"
# source location
SOURCE="FDK - aac - 2.0.2"
FAT="fdk-aac-ios"
SCRATCH="scratch"
# must be an absolute path
THIN=`pwd` /"thin"
COMPILE="y"
LIPO="y"

if [ "$*" ]
then
    if [ "$*" = "lipo" ]
    then
        COMPILE=
    else
        ARCHS="$*"
        if [ $# -eq 1 ]
        then
            # skip lipo
            LIPO=
        fi
    fi
fi

if [ "$COMPILE" ]
then
    CWD=`pwd`
    for ARCH in $ARCHS
    do
        echo "building $ARCH..."
        mkdir -p "$SCRATCH/$ARCH"
        cd "$SCRATCH/$ARCH"
        CFLAGS="-arch $ARCH"
        if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
        then
            PLATFORM="iPhoneSimulator"
            CPU=
            if [ "$ARCH" = "x86_64" ]
            then
                CFLAGS="$CFLAGS- the mios - simulator - version - min = 7.0"
                HOST="--host=x86_64-apple-darwin"
            else
                CFLAGS="$CFLAGS- the mios - simulator - version - min = 7.0"
                HOST="--host=i386-apple-darwin"
            fi
        else
            PLATFORM="iPhoneOS"
            if [ $ARCH = arm64 ]
            then
                HOST="--host=aarch64-apple-darwin"
            else
                HOST="--host=arm-apple-darwin"
            fi
            CFLAGS="$CFLAGS -fembed-bitcode"
        fi

        XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
        CC="xcrun -sdk $XCRUN_SDK clang -Wno-error=unused-command-line-argument-hard-error-in-future"
        AS="$CWD/$SOURCE/extras/gas-preprocessor.pl $CC"
        CXXFLAGS="$CFLAGS"
        LDFLAGS="$CFLAGS"

        $CWD/$SOURCE/configure \
            $CONFIGURE_FLAGS \
            $HOST \
            $CPU \
            CC="$CC" \
            CXX="$CC" \
            CPP="$CC -E" \
            AS="$AS" \
            CFLAGS="$CFLAGS" \
            LDFLAGS="$LDFLAGS" \
            CPPFLAGS="$CFLAGS" \
            --prefix="$THIN/$ARCH" || exit 1
        make -j8 install || exit 1
        cd $CWD
    done
fi

# Merge library files for each architectural platform
if [ "$LIPO" ]
then
    echo "building fat binaries..."
    mkdir -p $FAT/lib
    set - $ARCHS
    CWD=`pwd`
    cd $THIN/The $1/lib
    for LIB in *.a
    do
        cd $CWD
        lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
    done
    
    cd $CWD
    cp -rf $THIN/The $1/include $FAT
fi
Copy the code

Execute the script on the terminal and the compiled library file is in the folder fdK-aac-ios.

In this case, the newly created script file may have permissions. Run the chmod -r 777 build_aac.sh command.

4.3. Compile libx264

Download the x264 source code

Visit http://www.videolan.org/developers/x264.html or git clone git://git.videolan.org/x264.gitCopy the code

Create the build_x264.sh script file in the same directory as the following:

CONFIGURE_FLAGS="--enable-static --enable-pic --disable-cli --disable-shared"
Specify the architecture platform
ARCHS="arm64 armv7"
# source location
SOURCE="x264-master"
FAT="x264-iOS"
SCRATCH="scratch-x264"
THIN=`pwd` /"thin-x264"

COMPILE="y"
LIPO="y"

if [ "$*" ]
then
    if [ "$*" = "lipo" ]
    then
        COMPILE=
    else
        ARCHS="$*"
        if [ $# -eq 1 ]
        then
            LIPO=
        fi
    fi
fi

if [ "$COMPILE" ]
then
    CWD=`pwd`
    for ARCH in $ARCHS
    do
        echo "building $ARCH..."
        mkdir -p "$SCRATCH/$ARCH"
        cd "$SCRATCH/$ARCH"
        CFLAGS="-arch $ARCH"
        ASFLAGS=
        if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
        then
            PLATFORM="iPhoneSimulator"
            CPU=
            if [ "$ARCH" = "x86_64" ]
            then
                CFLAGS="$CFLAGS- the mios - simulator - version - min = 9.0"
                HOST=
            else
                CFLAGS="$CFLAGS- the mios - simulator - version - min = 9.0"
                HOST="--host=i386-apple-darwin"
            fi
        else
            PLATFORM="iPhoneOS"
            if [ $ARCH = "arm64" ]
            then
                HOST="--host=aarch64-apple-darwin"
                XARCH="-arch aarch64"
            else
                HOST="--host=arm-apple-darwin"
                XARCH="-arch arm"
            fi

            CFLAGS="$CFLAGS- fembed - bitcode - mios - version - min = 9.0"
            ASFLAGS="$CFLAGS"
        fi
  
        XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
        CC="xcrun -sdk $XCRUN_SDK clang"
        if [ $PLATFORM = "iPhoneOS" ]
        then
            export AS="$CWD/$SOURCE/tools/gas-preprocessor.pl $XARCH -- $CC"
        else
            export -n AS
        fi
        CXXFLAGS="$CFLAGS"
        LDFLAGS="$CFLAGS"
        CC=$CC $CWD/$SOURCE/configure \
            $CONFIGURE_FLAGS \
            $HOST \
            --extra-cflags="$CFLAGS" \
            --extra-asflags="$ASFLAGS" \
            --extra-ldflags="$LDFLAGS" \
            --prefix="$THIN/$ARCH" || exit 1
            
        make -j8 install || exit 1
        cd $CWD
    done
fi

if [ "$LIPO" ]
then
    echo "building fat binaries..."
    mkdir -p $FAT/lib
    set - $ARCHS
    CWD=`pwd`
    cd $THIN/The $1/lib
    for LIB in *.a
    do
        cd $CWD
        lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
    done

    cd $CWD
    cp -rf $THIN/The $1/include $FAT
fi
Copy the code

Execute the script in terminal, and the compiled library file is in folder X264-ios.

4.4, use,

  • 1. Create the project and import the compiled library file

  • 2. Add dependencieslibbz2.tbd,libiconv.tbd,libz.tbd

  • 3. In engineeringBuild SettingsModify in optionsHeader Search PathsAdd header file reference, modifyLibrary Search PathsAdd a path reference to the library file.