一、编译x264
1)x264 源码
2)脚本
#!/bin/sh
NDK=/Users/johnwu/Documents/android/android-ndk-r17c
API=21 #最低支持Android版本
HOST_PLATFORM=darwin-x86_64
cd x264
function build_one {
OUTPUT=$(pwd)/"android"/"$CPU"
echo "开始编译"
echo "CPU = $CPU "
echo "OUTPUT = $OUTPUT "
echo "CROSS_PREFIX = $CROSS_PREFIX "
echo "SYSROOT = $SYSROOT "
echo "EXTRA_CFLAGS = $EXTRA_CFLAGS "
echo "EXTRA_LDFLAGS = $EXTRA_LDFLAGS "
./configure \
--prefix=$OUTPUT \
--cross-prefix=$CROSS_PREFIX \
--sysroot=$SYSROOT \
--host=$HOST \
--disable-asm \
--disable-shared \
--enable-static \
--disable-opencl \
--enable-pic \
--disable-cli \
--extra-cflags="$EXTRA_CFLAGS" \
--extra-ldflags="$EXTRA_LDFLAGS"
make clean
make -j4
make install
echo "编译结束"
}
CPUS="armeabi-v7a x86"
if [ "$*" ]
then
CPUS="$*" #如果有输入参数 则只编译 该架构
fi
echo "编译以下 架构 $CPUS"
for CPU_TEMP in $CPUS
do
case $CPU_TEMP in
"armeabi-v7a")
CPU="armeabi-v7a"
CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/$HOST_PLATFORM/bin/arm-linux-androideabi-
SYSROOT=$NDK/platforms/android-$API/arch-arm/
EXTRA_CFLAGS="-D__ANDROID_API__=$API -isysroot $NDK/sysroot -I$NDK/sysroot/usr/include/arm-linux-androideabi -Os -fPIC -marm"
EXTRA_LDFLAGS="-marm"
HOST=arm-linux
build_one
;;
"x86")
CPU="x86"
CROSS_PREFIX=$NDK/toolchains/x86-4.9/prebuilt/$HOST_PLATFORM/bin/i686-linux-android-
SYSROOT=$NDK/platforms/android-$API/arch-x86/
EXTRA_CFLAGS="-D__ANDROID_API__=$API -isysroot $NDK/sysroot -I$NDK/sysroot/usr/include/i686-linux-android -Os -fPIC"
EXTRA_LDFLAGS=""
HOST=i686-linux
build_one
;;
esac
done
二、编译 ffmpeg + x264
准备材料:
1)ffmpeg版本 4.1
2)ndk版本 r17c
3)build 脚本
#!/bin/sh
NDK=/Users/johnwu/Documents/android/android-ndk-r17c
API=21 #最低支持Android版本
HOST_PLATFORM=darwin-x86_64
cd ffmpeg-4.1
function build_one {
OUTPUT=$(pwd)/"android"/"$CPU"
echo "开始编译"
echo "CPU = $CPU "
echo "OUTPUT = $OUTPUT "
echo "CROSS_PREFIX = $CROSS_PREFIX "
echo "ARCH = $ARCH "
echo "SYSROOT = $SYSROOT "
echo "EXTRA_CFLAGS = $EXTRA_CFLAGS "
echo "EXTRA_LDFLAGS = $EXTRA_LDFLAGS "
./configure \
--prefix=$OUTPUT \
--enable-gpl \
--enable-debug=3 \
--enable-libx264 \
--enable-shared \
--disable-asm \
--disable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-avdevice \
--disable-doc \
--disable-symver \
--cross-prefix=$CROSS_PREFIX \
--target-os=linux \
--arch=$ARCH \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="$EXTRA_CFLAGS" \
--extra-ldflags="$EXTRA_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make -j4
make install
echo "编译结束"
}
# CPUS="arm64-v8a armeabi armeabi-v7a mips mips64 x86 x86_64"
# CPUS="arm64-v8a armeabi-v7a x86 x86_64" #目前高级的ndk 要求支持这几种即可
CPUS="armeabi-v7a x86"
if [ "$*" ]
then
CPUS="$*" #如果有输入参数 则只编译 该架构
fi
echo "编译以下 架构 $CPUS"
for CPU_TEMP in $CPUS
do
case $CPU_TEMP in
"armeabi-v7a")
CPU="armeabi-v7a"
ARCH=armeabi-v7a
X264_INCLUDE=/Users/johnwu/Documents/c_project/x264/x264/android/armeabi-v7a/include
X264_LIB=/Users/johnwu/Documents/c_project/x264/x264/android/armeabi-v7a/lib
CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/$HOST_PLATFORM/bin/arm-linux-androideabi-
SYSROOT=$NDK/platforms/android-$API/arch-arm/
EXTRA_CFLAGS="-I$X264_INCLUDE -isysroot $NDK/sysroot -I$NDK/sysroot/usr/include/arm-linux-androideabi -Os -fpic -marm"
EXTRA_LDFLAGS="-L$X264_LIB -marm"
build_one
;;
"x86")
CPU="x86"
ARCH=x86
X264_INCLUDE=/Users/johnwu/Documents/c_project/x264/x264/android/x86/include
X264_LIB=/Users/johnwu/Documents/c_project/x264/x264/android/x86/lib
CROSS_PREFIX=$NDK/toolchains/x86-4.9/prebuilt/$HOST_PLATFORM/bin/i686-linux-android-
SYSROOT=$NDK/platforms/android-$API/arch-x86/
EXTRA_CFLAGS="-I$X264_INCLUDE -isysroot $NDK/sysroot -I$NDK/sysroot/usr/include/i686-linux-android -Os -fpic"
EXTRA_LDFLAGS="-L$X264_LIB"
build_one
;;
esac
done
4)ffmpeg涉及的修改:
4.1 configure脚本进行修改,为了生成的动态库命名方式可以被Android平台理解,识别
SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'
修改后:
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'
4.2 遇到的坑 按参考的文章,外加下面的坑
添加 --disable-asm 修复 Android 6.0以上遇到的问题:ffmpeg text relocations
注意:在编译ffmpeg + 264 的时候遇到 ERROR: libx264 not found
得去看 config.log, FFmpeg 项目的 config.log 中有提示: x264 undefined reference to 'stderr'
出现这个错误是因为 x264 编译失败了,但 libx264.so 还是生成了,并且看起来就像是真的编译成功了一样。参考x264编译问题处理