硬件环境
macOS : Sonoma 14.4 (23E214)
Xcode : Version 15.4 (15F31d)
1、编译libx264
1.1、使用编译脚本进行编译
1.1.1 下载代码 (2种方式)
A:使用git下载
git clone http://git.videolan.org/git/x264.git x264
B:x264官网下载
x264, the best H.264/AVC encoder - VideoLAN
1.1.2 下载 gas-preprocessor
GitHub - libav/gas-preprocessor: Perl script that implements a subset of the GNU as preprocessor that Apple’s as doesn’t
需要将x264编译成RAM平台的版本,需要此文件。下载 gas-pregprcessor,将其中的gas-preprocessor.pl
文件放到/usr/local/bin
文件夹下面。然后修改其权限使其可以运行chmod u+x gas-preprocessor.pl
。
1.1.3 下载 x264 build shell
GitHub - tangyi1234/x264-iOS-build-script: x264编译脚本
下载build-x264.sh 将文件build-x264.sh放在x264同一级目录里面,注意不是放在x264文件夹里面。
#!/bin/sh
CONFIGURE_FLAGS="--enable-static --enable-pic --disable-cli"
# modified by yitang, 2017/09/05
#ARCHS="arm64 armv7s x86_64 i386 armv7"
ARCHS="arm64 x86_64 i386 armv7 armv7s"
# directories
SOURCE="x264"
FAT="x264-iOS"
SCRATCH="scratch-x264"
# must be an absolute path
THIN=`pwd`/"thin-x264"
# the one included in x264 does not work; specify full path to working one
GAS_PREPROCESSOR=/usr/local/bin/gas-preprocessor.pl
COMPILE="y"
LIPO="y"
if [ "$*" ]
then
if [ "$*" = "lipo" ]
then
# skip compile
COMPILE=
else
ARCHS="$*"
if [ $# -eq 1 ]
then
# skip lipo
LIPO=
fi
fi
fi
if [ "$COMPILE" ]
then
# begin: added by yitang, 2017/09/05
if [ ! -r $GAS_PREPROCESSOR ]
then
echo 'gas-preprocessor.pl not found. Trying to install...'
(curl -L https://github.com/libav/gas-preprocessor/blob/master/gas-preprocessor.pl \
-o /usr/local/bin/gas-preprocessor.pl \
&& chmod +x /usr/local/bin/gas-preprocessor.pl) \
|| exit 1
fi
if [ ! -r $SOURCE ]
then
echo 'x264 source not found. Trying to download...'
curl ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2 | tar xj && ln -s x264-snapshot-20170904-2245 x264 || exit 1
fi
# end: added by sunminmin, 2017/09/05
CWD=`pwd`
for ARCH in $ARCHS
do
echo "building $ARCH..."
mkdir -p "$SCRATCH/$ARCH"
cd "$SCRATCH/$ARCH"
if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
then
PLATFORM="iPhoneSimulator"
CPU=
if [ "$ARCH" = "x86_64" ]
then
SIMULATOR="-mios-simulator-version-min=7.0"
HOST=
else
SIMULATOR="-mios-simulator-version-min=5.0"
HOST="--host=i386-apple-darwin"
fi
else
PLATFORM="iPhoneOS"
if [ $ARCH = "armv7s" ]
then
CPU="--cpu=swift"
else
CPU=
fi
SIMULATOR=
if [ $ARCH = "arm64" ]
then
HOST="--host=aarch64-apple-darwin"
else
HOST="--host=arm-apple-darwin"
fi
fi
XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang -Wno-error=unused-command-line-argument-hard-error-in-future -arch $ARCH"
CFLAGS="-arch $ARCH $SIMULATOR"
CXXFLAGS="$CFLAGS"
LDFLAGS="$CFLAGS"
CC=$CC $CWD/$SOURCE/configure \
$CONFIGURE_FLAGS \
$HOST \
$CPU \
--extra-cflags="$CFLAGS" \
--extra-ldflags="$LDFLAGS" \
--prefix="$THIN/$ARCH"
mkdir extras
ln -s $GAS_PREPROCESSOR extras
make -j3 install
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
# begin: added by sunminmin, 2017/09/05
echo "copy config.h to ..."
for ARCH in $ARCHS
do
cd $CWD
echo "copy $SCRATCH/$ARCH/config.h to $THIN/$ARCH/$include"
cp -rf $SCRATCH/$ARCH/config.h $THIN/$ARCH/$include || exit 1
done
echo "building success!"
# end: added by sunminmin, 2017/09/05
echo Done
1.1.4 执行脚本完成x264的编译
lib中为静态库libx264.a、include中为两个头文件
1.2、模仿openssl的操作,修改几个脚本 (以iOS为例、Android类似)
1.2.1 下载x264源码
A:在主目录下新建init-ios-x264.sh
IJK_x264_UPSTREAM=http://git.videolan.org/git/x264.git
IJK_x264_FORK=http://git.videolan.org/git/x264.git
IJK_x264_LOCAL_REPO=extra/x264
set -e
TOOLS=tools
echo "== pull x264 base =="
sh $TOOLS/pull-repo-base.sh $IJK_x264_UPSTREAM $IJK_x264_LOCAL_REPO
function pull_fork()
{
echo "== pull x264 fork $1 =="
sh $TOOLS/pull-repo-ref.sh $IJK_x264_FORK ios/x264-$1 ${IJK_x264_LOCAL_REPO}
}
pull_fork "arm64"
# 参数可选,想用哪个架构添加下面的哪行命令 (这句话很关键关系到下面的执行)
pull_fork "armv5"
pull_fork "armv7a"
pull_fork "arm64"
pull_fork "x86"
pull_fork "x86_64"
B: 执行该脚本 ./init-ios-x264.sh 这步执行完后,会在extra下面有个x264, 下面有源码
并且在iOS文件夹内有x264-arm64文件夹,如下
可以在上面脚本第四行,修改为自定义的路径
1.2.2 执行编译
A:新建 compile-x264.sh 和 do-compile-x264.sh
a:模仿 compile-openssl.sh 在iOS文件夹内新建 compile-x264.sh
FF_ALL_ARCHS_IOS6_SDK="armv7 armv7s i386"
FF_ALL_ARCHS_IOS7_SDK="armv7 armv7s arm64 i386 x86_64"
FF_ALL_ARCHS_IOS8_SDK="armv7 arm64 i386 x86_64"
FF_ALL_ARCHS=$FF_ALL_ARCHS_IOS8_SDK
#----------
UNI_BUILD_ROOT=`pwd`
UNI_TMP="$UNI_BUILD_ROOT/tmp"
UNI_TMP_LLVM_VER_FILE="$UNI_TMP/llvm.ver.txt"
FF_TARGET=$1
set -e
#----------
FF_LIBS="libx264"
#----------
echo_archs() {
echo "===================="
echo "[*] check xcode version"
echo "===================="
echo "FF_ALL_ARCHS = $FF_ALL_ARCHS"
}
do_lipo () {
LIB_FILE=$1
LIPO_FLAGS=
for ARCH in $FF_ALL_ARCHS
do
LIPO_FLAGS="$LIPO_FLAGS $UNI_BUILD_ROOT/build/x264-$ARCH/output/lib/$LIB_FILE"
done
xcrun lipo -create $LIPO_FLAGS -output $UNI_BUILD_ROOT/build/universal/lib/$LIB_FILE
xcrun lipo -info $UNI_BUILD_ROOT/build/universal/lib/$LIB_FILE
}
do_lipo_all () {
mkdir -p $UNI_BUILD_ROOT/build/universal/lib
echo "lipo archs: $FF_ALL_ARCHS"
for FF_LIB in $FF_LIBS
do
do_lipo "$FF_LIB.a";
done
cp -R $UNI_BUILD_ROOT/build/x264-armv7/output/include $UNI_BUILD_ROOT/build/universal/
}
#----------
if [ "$FF_TARGET" = "armv7" -o "$FF_TARGET" = "armv7s" -o "$FF_TARGET" = "arm64" ]; then
echo_archs
sh tools/do-compile-x264.sh $FF_TARGET
elif [ "$FF_TARGET" = "i386" -o "$FF_TARGET" = "x86_64" ]; then
echo_archs
sh tools/do-compile-x264.sh $FF_TARGET
elif [ "$FF_TARGET" = "lipo" ]; then
echo_archs
do_lipo_all
elif [ "$FF_TARGET" = "all" ]; then
echo_archs
for ARCH in $FF_ALL_ARCHS
do
sh tools/do-compile-x264.sh $ARCH
done
do_lipo_all
elif [ "$FF_TARGET" = "check" ]; then
echo_archs
elif [ "$FF_TARGET" = "clean" ]; then
echo_archs
for ARCH in $FF_ALL_ARCHS
do
cd x264_out-$ARCH && git clean -xdf && cd -
done
else
echo "Usage:"
echo " compile-x264.sh armv7|arm64|i386|x86_64"
echo " compile-x264.sh armv7s (obselete)"
echo " compile-x264.sh lipo"
echo " compile-x264.sh all"
echo " compile-x264.sh clean"
echo " compile-x264.sh check"
exit 1
fi
b:模仿 do-compile-openssl.sh iOS文件夹内的tools种新建 do-compile-x264.sh
echo "===================="
echo "[*] check host"
echo "===================="
set -e
FF_XCRUN_DEVELOPER=`xcode-select -print-path`
if [ ! -d "$FF_XCRUN_DEVELOPER" ]; then
echo "xcode path is not set correctly $FF_XCRUN_DEVELOPER does not exist (most likely because of xcode > 4.3)"
echo "run"
echo "sudo xcode-select -switch <xcode path>"
echo "for default installation:"
echo "sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer"
exit 1
fi
case $FF_XCRUN_DEVELOPER in
*\ * )
echo "Your Xcode path contains whitespaces, which is not supported."
exit 1
;;
esac
#--------------------
# include
#--------------------
# common defines
FF_ARCH=$1
if [ -z "$FF_ARCH" ]; then
echo "You must specific an architecture 'armv7, armv7s, arm64, i386, x86_64, ...'.\n"
exit 1
fi
FF_BUILD_ROOT=`pwd`
FF_TAGET_OS="darwin"
# openssl build params
export COMMON_FF_CFG_FLAGS=
OPENSSL_CFG_FLAGS=
OPENSSL_EXTRA_CFLAGS=
OPENSSL_CFG_CPU=
# i386, x86_64
OPENSSL_CFG_FLAGS_SIMULATOR=
# armv7, armv7s, arm64
OPENSSL_CFG_FLAGS_ARM=
OPENSSL_CFG_FLAGS_ARM="iphoneos-cross"
echo "build_root: $FF_BUILD_ROOT"
#--------------------
echo "===================="
echo "[*] config arch $FF_ARCH"
echo "===================="
FF_BUILD_NAME="unknown"
FF_XCRUN_PLATFORM="iPhoneOS"
FF_XCRUN_OSVERSION=
FF_GASPP_EXPORT=
FF_XCODE_BITCODE=
if [ "$FF_ARCH" = "i386" ]; then
FF_BUILD_NAME="x264-i386"
FF_XCRUN_PLATFORM="iPhoneSimulator"
FF_XCRUN_OSVERSION="-mios-simulator-version-min=6.0"
OPENSSL_CFG_FLAGS="darwin-i386-cc $OPENSSL_CFG_FLAGS"
elif [ "$FF_ARCH" = "x86_64" ]; then
FF_BUILD_NAME="x264-x86_64"
FF_XCRUN_PLATFORM="iPhoneSimulator"
FF_XCRUN_OSVERSION="-mios-simulator-version-min=7.0"
OPENSSL_CFG_FLAGS="darwin64-x86_64-cc $OPENSSL_CFG_FLAGS"
elif [ "$FF_ARCH" = "armv7" ]; then
FF_BUILD_NAME="x264-armv7"
FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
FF_XCODE_BITCODE="-fembed-bitcode"
OPENSSL_CFG_FLAGS="$OPENSSL_CFG_FLAGS_ARM $OPENSSL_CFG_FLAGS"
# OPENSSL_CFG_CPU="--cpu=cortex-a8"
elif [ "$FF_ARCH" = "armv7s" ]; then
FF_BUILD_NAME="x264-armv7s"
OPENSSL_CFG_CPU="--cpu=swift"
FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
FF_XCODE_BITCODE="-fembed-bitcode"
OPENSSL_CFG_FLAGS="$OPENSSL_CFG_FLAGS_ARM $OPENSSL_CFG_FLAGS"
elif [ "$FF_ARCH" = "arm64" ]; then
FF_BUILD_NAME="x264-arm64"
FF_XCRUN_OSVERSION="-miphoneos-version-min=7.0"
FF_XCODE_BITCODE="-fembed-bitcode"
OPENSSL_CFG_FLAGS="$OPENSSL_CFG_FLAGS_ARM $OPENSSL_CFG_FLAGS"
FF_GASPP_EXPORT="GASPP_FIX_XCODE5=1"
else
echo "unknown architecture $FF_ARCH";
exit 1
fi
echo "build_name: $FF_BUILD_NAME"
echo "platform: $FF_XCRUN_PLATFORM"
echo "osversion: $FF_XCRUN_OSVERSION"
#--------------------
echo "===================="
echo "[*] make ios toolchain $FF_BUILD_NAME"
echo "===================="
FF_BUILD_SOURCE="$FF_BUILD_ROOT/$FF_BUILD_NAME"
FF_BUILD_PREFIX="$FF_BUILD_ROOT/build/$FF_BUILD_NAME/output"
mkdir -p $FF_BUILD_PREFIX
FF_XCRUN_SDK=`echo $FF_XCRUN_PLATFORM | tr '[:upper:]' '[:lower:]'`
FF_XCRUN_SDK_PLATFORM_PATH=`xcrun -sdk $FF_XCRUN_SDK --show-sdk-platform-path`
FF_XCRUN_SDK_PATH=`xcrun -sdk $FF_XCRUN_SDK --show-sdk-path`
FF_XCRUN_CC="xcrun -sdk $FF_XCRUN_SDK clang"
export CROSS_TOP="$FF_XCRUN_SDK_PLATFORM_PATH/Developer"
export CROSS_SDK=`echo ${FF_XCRUN_SDK_PATH/#$CROSS_TOP\/SDKs\//}`
export BUILD_TOOL="$FF_XCRUN_DEVELOPER"
export CC="$FF_XCRUN_CC -arch $FF_ARCH $FF_XCRUN_OSVERSION"
echo "build_source: $FF_BUILD_SOURCE"
echo "build_prefix: $FF_BUILD_PREFIX"
echo "CROSS_TOP: $CROSS_TOP"
echo "CROSS_SDK: $CROSS_SDK"
echo "BUILD_TOOL: $BUILD_TOOL"
echo "CC: $CC"
#--------------------
echo "\n--------------------"
echo "[*] configurate x264"
echo "--------------------"
OPENSSL_CFG_FLAGS="$OPENSSL_CFG_FLAGS $FF_XCODE_BITCODE"
OPENSSL_CFG_FLAGS="$OPENSSL_CFG_FLAGS --x264dir=$FF_BUILD_PREFIX"
# xcode configuration
export DEBUG_INFORMATION_FORMAT=dwarf-with-dsym
cd $FF_BUILD_SOURCE
if [ -f "./Makefile" ]; then
echo 'reuse configure'
else
echo "config: $OPENSSL_CFG_FLAGS"
./Configure \
$OPENSSL_CFG_FLAGS
make clean
fi
#--------------------
echo "\n--------------------"
echo "[*] compile x264"
echo "--------------------"
set +e
make
sudo make install
c:执行
./compile-x264.sh arm64
执行完成后会在iOS中的build文件夹x264-arm64内output中生成lib和include文件夹
2、编译带有libx264的ffmpeg静态库
2.1 compile-ffmpeg.sh不需要修改 修改编译do-compile-ffmpeg.sh的脚本
#! /usr/bin/env bash
#
# Copyright (C) 2013-2014 Bilibili
# Copyright (C) 2013-2014 Zhang Rui <bbcallen@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script is based on projects below
# https://github.com/kolyvan/kxmovie
# https://github.com/yixia/FFmpeg-Android
# http://git.videolan.org/?p=vlc-ports/android.git;a=summary
# https://github.com/kewlbear/FFmpeg-iOS-build-script/
#--------------------
echo "===================="
echo "[*] check host"
echo "===================="
set -e
#--------------------
# include
#--------------------
# common defines
FF_ARCH=$1
FF_BUILD_OPT=$2
echo "FF_ARCH=$FF_ARCH"
echo "FF_BUILD_OPT=$FF_BUILD_OPT"
if [ -z "$FF_ARCH" ]; then
echo "You must specific an architecture 'armv7, armv7s, arm64, i386, x86_64, ...'.\n"
exit 1
fi
FF_BUILD_ROOT=`pwd`
FF_TAGET_OS="darwin"
# ffmpeg build params
export COMMON_FF_CFG_FLAGS=
source $FF_BUILD_ROOT/../config/module.sh
FFMPEG_CFG_FLAGS=
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $COMMON_FF_CFG_FLAGS"
# Optimization options (experts only):
# FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-armv5te"
# FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-armv6"
# FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-armv6t2"
# Advanced options (experts only):
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --enable-cross-compile"
# --disable-symver may indicate a bug
# FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-symver"
# Developer options (useful when working on FFmpeg itself):
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-stripping"
##
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --arch=$FF_ARCH"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --target-os=$FF_TAGET_OS"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --enable-static"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --disable-shared"
FFMPEG_EXTRA_CFLAGS=
# i386, x86_64
FFMPEG_CFG_FLAGS_SIMULATOR=
FFMPEG_CFG_FLAGS_SIMULATOR="$FFMPEG_CFG_FLAGS_SIMULATOR --disable-asm"
FFMPEG_CFG_FLAGS_SIMULATOR="$FFMPEG_CFG_FLAGS_SIMULATOR --disable-mmx"
FFMPEG_CFG_FLAGS_SIMULATOR="$FFMPEG_CFG_FLAGS_SIMULATOR --assert-level=2"
# armv7, armv7s, arm64
FFMPEG_CFG_FLAGS_ARM=
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-pic"
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-neon"
case "$FF_BUILD_OPT" in
debug)
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --disable-optimizations"
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-debug"
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --disable-small"
;;
*)
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-optimizations"
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-debug"
FFMPEG_CFG_FLAGS_ARM="$FFMPEG_CFG_FLAGS_ARM --enable-small"
;;
esac
echo "build_root: $FF_BUILD_ROOT"
#--------------------
echo "===================="
echo "[*] check gas-preprocessor"
echo "===================="
FF_TOOLS_ROOT="$FF_BUILD_ROOT/../extra"
export PATH="$FF_TOOLS_ROOT/gas-preprocessor:$PATH"
echo "gasp: $FF_TOOLS_ROOT/gas-preprocessor/gas-preprocessor.pl"
#--------------------
echo "===================="
echo "[*] config arch $FF_ARCH"
echo "===================="
FF_BUILD_NAME="unknown"
FF_XCRUN_PLATFORM="iPhoneOS"
FF_XCRUN_OSVERSION=
FF_GASPP_EXPORT=
FF_DEP_OPENSSL_INC=
FF_DEP_OPENSSL_LIB=
FF_XCODE_BITCODE=
if [ "$FF_ARCH" = "i386" ]; then
FF_BUILD_NAME="ffmpeg-i386"
FF_BUILD_NAME_X264=x264-i386
FF_XCRUN_PLATFORM="iPhoneSimulator"
FF_XCRUN_OSVERSION="-mios-simulator-version-min=6.0"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_SIMULATOR"
elif [ "$FF_ARCH" = "x86_64" ]; then
FF_BUILD_NAME="ffmpeg-x86_64"
FF_BUILD_NAME_X264=x264-x86_64
FF_XCRUN_PLATFORM="iPhoneSimulator"
FF_XCRUN_OSVERSION="-mios-simulator-version-min=7.0"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_SIMULATOR"
elif [ "$FF_ARCH" = "armv7" ]; then
FF_BUILD_NAME="ffmpeg-armv7"
FF_BUILD_NAME_X264=x264-armv7
FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
FF_XCODE_BITCODE="-fembed-bitcode"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
# FFMPEG_CFG_CPU="--cpu=cortex-a8"
elif [ "$FF_ARCH" = "armv7s" ]; then
FF_BUILD_NAME="ffmpeg-armv7s"
FF_BUILD_NAME_X264=x264-armv7s
FFMPEG_CFG_CPU="--cpu=swift"
FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
FF_XCODE_BITCODE="-fembed-bitcode"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
elif [ "$FF_ARCH" = "arm64" ]; then
FF_BUILD_NAME="ffmpeg-arm64"
FF_BUILD_NAME_X264=x264-arm64
FF_XCRUN_OSVERSION="-miphoneos-version-min=7.0"
FF_XCODE_BITCODE="-fembed-bitcode"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
FF_GASPP_EXPORT="GASPP_FIX_XCODE5=1"
else
echo "unknown architecture $FF_ARCH";
exit 1
fi
echo "build_name: $FF_BUILD_NAME"
echo "platform: $FF_XCRUN_PLATFORM"
echo "osversion: $FF_XCRUN_OSVERSION"
#--------------------
echo "===================="
echo "[*] make ios toolchain $FF_BUILD_NAME"
echo "===================="
FF_BUILD_SOURCE="$FF_BUILD_ROOT/$FF_BUILD_NAME"
FF_BUILD_PREFIX="$FF_BUILD_ROOT/build/$FF_BUILD_NAME/output"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --prefix=$FF_BUILD_PREFIX"
mkdir -p $FF_BUILD_PREFIX
echo "build_source: $FF_BUILD_SOURCE"
echo "build_prefix: $FF_BUILD_PREFIX"
#--------------------
echo "\n--------------------"
echo "[*] configurate ffmpeg"
echo "--------------------"
FF_XCRUN_SDK=`echo $FF_XCRUN_PLATFORM | tr '[:upper:]' '[:lower:]'`
FF_XCRUN_CC="xcrun -sdk $FF_XCRUN_SDK clang"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_CPU"
FFMPEG_CFLAGS=
FFMPEG_CFLAGS="$FFMPEG_CFLAGS -arch $FF_ARCH"
FFMPEG_CFLAGS="$FFMPEG_CFLAGS $FF_XCRUN_OSVERSION"
FFMPEG_CFLAGS="$FFMPEG_CFLAGS $FFMPEG_EXTRA_CFLAGS"
FFMPEG_CFLAGS="$FFMPEG_CFLAGS $FF_XCODE_BITCODE"
FFMPEG_LDFLAGS="$FFMPEG_CFLAGS"
FFMPEG_DEP_LIBS=
#--------------------
echo "\n--------------------"
echo "[*] check OpenSSL"
echo "----------------------"
FFMPEG_DEP_OPENSSL_INC=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/include
FFMPEG_DEP_OPENSSL_LIB=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/lib
#--------------------
# with openssl
#if [ -f "${FFMPEG_DEP_OPENSSL_LIB}/libssl.a" ]; then
# FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --enable-openssl"
#
# FFMPEG_CFLAGS="$FFMPEG_CFLAGS -I${FFMPEG_DEP_OPENSSL_INC}"
# FFMPEG_DEP_LIBS="$FFMPEG_CFLAGS -L${FFMPEG_DEP_OPENSSL_LIB} -lssl -lcrypto"
#fi
FFMPEG_X264_INCLUDE=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_X264/output/include
FFMPEG_X264_LIB=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_X264/output/lib
FFMPEG_CFLAGS="$FFMPEG_CFLAGS -I$FFMPEG_X264_INCLUDE"
FFMPEG_DEP_LIBS="$FFMPEG_CFLAGS -L$FFMPEG_X264_LIB"
#FFMPEG_X264_INCLUDE=/usr/local/include
#FFMPEG_X264_LIB=/usr/local/lib
#FFMPEG_CFLAGS="$FFMPEG_CFLAGS -I${FFMPEG_X264_INCLUDE}"
#FFMPEG_DEP_LIBS="$FFMPEG_CFLAGS -L${FFMPEG_X264_LIB}"
echo " CCCCCCCCCCCCCCCCCCCCC $FFMPEG_DEP_LIBS"
#--------------------
echo "\n--------------------"
echo "[*] configure"
echo "----------------------"
if [ ! -d $FF_BUILD_SOURCE ]; then
echo ""
echo "!! ERROR"
echo "!! Can not find FFmpeg directory for $FF_BUILD_NAME"
echo "!! Run 'sh init-ios.sh' first"
echo ""
exit 1
fi
# xcode configuration
export DEBUG_INFORMATION_FORMAT=dwarf-with-dsym
cd $FF_BUILD_SOURCE
if [ -f "./config.h" ]; then
echo 'reuse configure'
else
echo "config: $FFMPEG_CFG_FLAGS $FF_XCRUN_CC"
./configure \
$FFMPEG_CFG_FLAGS \
--cc="$FF_XCRUN_CC" \
$FFMPEG_CFG_CPU \
--extra-cflags="$FFMPEG_CFLAGS" \
--extra-cxxflags="$FFMPEG_CFLAGS" \
--extra-ldflags="$FFMPEG_LDFLAGS $FFMPEG_DEP_LIBS"
make clean
fi
#--------------------
echo "\n--------------------"
echo "[*] compile ffmpeg"
echo "--------------------"
cp config.* $FF_BUILD_PREFIX
make -j3 $FF_GASPP_EXPORT
make install
mkdir -p $FF_BUILD_PREFIX/include/libffmpeg
cp -f config.h $FF_BUILD_PREFIX/include/libffmpeg/config.h
主要修改代码为
#--------------------
echo "\n--------------------"
echo "[*] check OpenSSL"
echo "----------------------"
FFMPEG_DEP_OPENSSL_INC=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/include
FFMPEG_DEP_OPENSSL_LIB=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_OPENSSL/output/lib
#--------------------
# with openssl
#if [ -f "${FFMPEG_DEP_OPENSSL_LIB}/libssl.a" ]; then
# FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS --enable-openssl"
#
# FFMPEG_CFLAGS="$FFMPEG_CFLAGS -I${FFMPEG_DEP_OPENSSL_INC}"
# FFMPEG_DEP_LIBS="$FFMPEG_CFLAGS -L${FFMPEG_DEP_OPENSSL_LIB} -lssl -lcrypto"
#fi
# (1)使用第一种方式编译的libx264,需要将头文件和静态库放到相应位置
#FFMPEG_X264_INCLUDE=/usr/local/include
#FFMPEG_X264_LIB=/usr/local/lib
#FFMPEG_CFLAGS="$FFMPEG_CFLAGS -I${FFMPEG_X264_INCLUDE}"
#FFMPEG_DEP_LIBS="$FFMPEG_CFLAGS -L${FFMPEG_X264_LIB}"
# (2) 使用第二种方式编译的libx264
FFMPEG_X264_INCLUDE=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_X264/output/include
FFMPEG_X264_LIB=$FF_BUILD_ROOT/build/$FF_BUILD_NAME_X264/output/lib
FFMPEG_CFLAGS="$FFMPEG_CFLAGS -I$FFMPEG_X264_INCLUDE"
FFMPEG_DEP_LIBS="$FFMPEG_CFLAGS -L$FFMPEG_X264_LIB"
配置文件(ijkplayer-ios/config/module.sh)中添加libx264
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-gpl"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-libx264"
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --enable-encoder=libx264"
2.2 执行ffmpeg的正常编译
# 回到iOS目录下执行
./compile-ffmpeg.sh arm64
2.3 可能出现的错误
ERROR: libx264 not found
原因基本上都是libx264库与ffmpeg不匹配,双方是否都是开启了bitcode。
if [ "$FF_ARCH" = "i386" ]; then
FF_BUILD_NAME="ffmpeg-i386"
FF_BUILD_NAME_X264=x264-i386
FF_XCRUN_PLATFORM="iPhoneSimulator"
FF_XCRUN_OSVERSION="-mios-simulator-version-min=6.0"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_SIMULATOR"
elif [ "$FF_ARCH" = "x86_64" ]; then
FF_BUILD_NAME="ffmpeg-x86_64"
FF_BUILD_NAME_X264=x264-x86_64
FF_XCRUN_PLATFORM="iPhoneSimulator"
FF_XCRUN_OSVERSION="-mios-simulator-version-min=7.0"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_SIMULATOR"
elif [ "$FF_ARCH" = "armv7" ]; then
FF_BUILD_NAME="ffmpeg-armv7"
FF_BUILD_NAME_X264=x264-armv7
FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
FF_XCODE_BITCODE="-fembed-bitcode"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
# FFMPEG_CFG_CPU="--cpu=cortex-a8"
elif [ "$FF_ARCH" = "armv7s" ]; then
FF_BUILD_NAME="ffmpeg-armv7s"
FF_BUILD_NAME_X264=x264-armv7s
FFMPEG_CFG_CPU="--cpu=swift"
FF_XCRUN_OSVERSION="-miphoneos-version-min=6.0"
FF_XCODE_BITCODE="-fembed-bitcode"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
elif [ "$FF_ARCH" = "arm64" ]; then
FF_BUILD_NAME="ffmpeg-arm64"
FF_BUILD_NAME_X264=x264-arm64
FF_XCRUN_OSVERSION="-miphoneos-version-min=7.0"
FF_XCODE_BITCODE="-fembed-bitcode"
FFMPEG_CFG_FLAGS="$FFMPEG_CFG_FLAGS $FFMPEG_CFG_FLAGS_ARM"
FF_GASPP_EXPORT="GASPP_FIX_XCODE5=1"
else
echo "unknown architecture $FF_ARCH";
exit 1
fi
将相应的第18行、26行、32行的代码注释掉即可
只要ffmpeg中的和libx264中的设置都相同的,就没问题了
编译完成后将build内的静态库和头文件取出来就可以了
参考文章:
1、 IOS-FFmpeg的编译
2、ijkplayer支持x264编译
3、x264编译
4、iOS FFmpeg+libx264包的产生