There are three ways to install FFmpeg on the MAC platform, including downloading and installing through the ffmpeg static library, installing through Homebrew, and downloading and compiling the source code. You can choose your favorite method to install FFmpeg on the MAC platform.
1. Download and install the ffmpeg static library
OpenFFmpeg official website, as shown in the figure below.
Unzip the downloaded compressed file and you will see an executable file. Just execute it in the terminal.
Of course, you can also configure an environment link to point to the executable file, so that the file can be executed anywhere.
Although this method is simple and fast, it is not conducive to our learning and research on ffmpeg.
2. Install via Homebrew
2.1. Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter the password according to the prompts, and the installation can be successful step by step. If it has not been installed on the computer beforexCode
, enter xcode-select --install
installation on the command line The above is the official installation method. In order to avoid the unnecessary problem of being restricted by the wall, we can also install it by setting up a mirror. CLT for Xcode
Then we need to re-execute the above command again to install.
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
If you have installed Homebrew before, it doesn't matter. You can also set up the mirror configuration. For details, see thiswebsite.
2.2. Install FFmpeg
If you have installed ffmpeg through Homebrew, please execute the brew uninstall ffmpeg
command to uninstall FFmpeg in advance.
Execute the following command
brew install ffmpeg
or
brew install ffmpeg --HEAD
Both commands can install ffmpeg. Both will detect ffmpeg's dependency packages and install them automatically. The installation will take a certain amount of time, so please wait patiently. There is a problem here, that is, starting from version 2.0, the associated installation of options for ffmpeg is no longer provided. If you need to associate options with ffmpeg, you need to use a third-party options warehouse. So if your homebrew version is after 2.0, the above two instructions cannot be associated with audio and video codecs such as , and so on. So we can rely on the third-party warehousehomebrew-ffmpeg to realize the associated installation of options and ffmpeg. homebrew
x264
fdk-aac
(1) Execute the following instructions:
brew tap homebrew-ffmpeg/ffmpeg
brew install homebrew-ffmpeg/ffmpeg/ffmpeg
(2) Execute the following command to see which options this warehouse supports.
brew options homebrew-ffmpeg/ffmpeg/ffmpeg
(3) Based on the options obtained in step 2, replace the options in the following instructions, and then execute.
brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-<option1> --with-<option2> ...
You can also execute the following command to install all options.
brew install homebrew-ffmpeg/ffmpeg/ffmpeg $(brew options homebrew-ffmpeg/ffmpeg/ffmpeg --compact)
Installing FFmpeg through Homebrew is also a very simple way, but it is also not a recommended way because we cannot view and debug the source code to learn and study FFmpeg.
3. Source code compilation and installation
3.1. Download source code
Download the source code from the official website. You can directly download the source code package, or you can clone the code repository through the git
command.
git clone git://source.ffmpeg.org/ffmpeg.git
#或者码云的链接
git clone https://gitee.com/mirrors/ffmpeg.git
cd ffmpeg
# 查看版本
git branch -a
# 选择版本
git checkout remotes/origin/release/xxx
3.2. Compile source code
Execute the following instructions in the downloaded source code 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-shared
This command is to configure compilation, where--prefix=/usr/local/ffmpeg
represents the storage location of the compiled library files. You can set the path you like.
Execute compilation and installation commands
make &&sudo make install
This compilation process may cause errors similar to libfdk_aac not found
or x264 not found
. That is because the relevant dependencies are not installed. Here we can install them through homebrew. these dependencies.
brew install fdk-aac
brew install x264
Of course, if your computer environment is not fully configured, you may encounter more errors that need to be installed during the compilation process. It doesn't matter. If there are any dependencies that need to be installed, just execute the corresponding instructions.
3.3. Configure environment variables
In the above configuration compilation instructions, we store the compiled library files in/usr/local/ffmpeg
, and execute the instructionsopen /usr/local/ffmpeg
to enter the path. You can See the compiled related header files and library files, as shown in the figure below.
Execute the following command
touch .bash_profile
open .bash_profile
In the openbash_profile
file, add the following statement.
export FFMPEG_HOME=/usr/local/ffmpeg
export PATH=$FFMPEG_HOME/bin:$PATH
At this point, the FFmpeg installation on the Mac is complete.
Although this installation method is more complicated, we can modify the debugging source code, which is very convenient for us to study the FFmpeg source code.
4. iOS platform library compilation
The above installation methods only allow us to realize the development capabilities of FFmpeg on theMac OS
platform, but as an iOS developer, we also need to compile the Library files to provide mobile platform development.
4.1. Compile FFmpeg
Download the compiled script from the address below.
https://github.com/kewlbear/FFmpeg-iOS-build-script
The content that needs to be modified is
FF_VERSION
Specify the version number of ffmpeg.CONFIGURE_FLAGS
Modify the compilation configuration of ffmpeg, remove unnecessary compilation content, and reduce the size of the package body.ARCHS
Specify the CPU architecture for compilation. For iOS development, there are real machines and simulators.X264
Compiled pathlibx264
, if necessary, compile libx264 into it.FDK_AAC
Compiled pathlibfdk-aac
, if necessary, compile libfdk-aac into it. Execute the command./build-ffmpeg.sh
in the terminal. After the compilation is completed, the following file structure will appear.
4.2. Compile libfdk-aac
Download the stable version from the official websiteFDK-AAC
https://sourceforge.net/p/opencore-amr/fdk-aac/ci/v2.0.2/tree/
Createbuild_aac.sh
script file in the same directory. The content of the file is as follows:
CONFIGURE_FLAGS="--enable-static --with-pic=yes --disable-shared"
#指定编译平台
ARCHS="arm64 armv7"
# 源码位置
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 -mios-simulator-version-min=7.0"
HOST="--host=x86_64-apple-darwin"
else
CFLAGS="$CFLAGS -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
#合并各个架构平台的库文件
if [ "$LIPO" ]
then
echo "building fat binaries..."
mkdir -p $FAT/lib
set - $ARCHS
CWD=`pwd`
cd $THIN/$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/$1/include $FAT
fi
Execute the script in the terminal, and the compiled library file will be in the folderfdk-aac-ios
.
Here, the newly created script file may have permission issues. Execute
chmod -R 777 build_aac.sh
command.
4.3. Compile libx264
下载x264
Source码
访问 http://www.videolan.org/developers/x264.html
或者
git clone git://git.videolan.org/x264.git
Createbuild_x264.sh
script file in the same directory. The content of the file is as follows:
CONFIGURE_FLAGS="--enable-static --enable-pic --disable-cli --disable-shared"
# 指定架构平台
ARCHS="arm64 armv7"
#源码位置
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 -mios-simulator-version-min=9.0"
HOST=
else
CFLAGS="$CFLAGS -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/$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/$1/include $FAT
fi
Execute the script in the terminal, and the compiled library file will be in the folderx264-iOS
.
4.4. Use
- 1. Create a project and import the compiled library files
- 2, addition dependent
libbz2.tbd
,libiconv.tbd
,libz.tbd
3. Modify in the project's Build Settings
options to add header file references, and modify to add library files. Path reference. Header Search Paths
Library Search Paths
Original text Installation and compilation of FFmpeg under Mac platform - Nuggets
★The business card at the end of the article allows you to receive free audio and video development learning materials, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmap, etc.
See below! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓