下载地址:https://github.com/FFmpeg/FFmpeg
下载地址:http://www.videolan.org/developers/x264.html
下载地址:https://developer.android.google.cn/ndk/downloads/older_releases
解压android-ndk-r14b-linux-x86_64.zip,最后我的目录如下如图所示:
sudo apt install yasm
#/bin/bash
make distclean
#这里替换成你的NDK目录即可
export NDK=/home/maxingrong_begoit/temp/android-ndk-r14b
export SYSROOT=$NDK/platforms/android-24/arch-arm
export TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
CPU=arm
PREFIX=$(pwd)/android/$CPU
function build_32
{
./configure \
--prefix=$PREFIX \
--enable-static \
--enable-shared \
--enable-pic \
--disable-asm \
--enable-strip \
--host=arm-linux-androideabi \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
}
build_32
make -j8
make install
#!/bin/sh
make distclean
#这里替换成你的NDK目录即可
export NDK=/home/maxingrong_begoit/temp/android-ndk-r14b
export SYSROOT=$NDK/platforms/android-24/arch-arm64
export TOOLCHAIN=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64
CPU=arm64
PREFIX=$(pwd)/android/$CPU
function build_64
{
./configure \
--prefix=$PREFIX \
--enable-static \
--enable-shared \
--enable-pic \
--disable-asm \
--enable-strip \
--host=aarch64-linux-android \
--cross-prefix=$TOOLCHAIN/bin/aarch64-linux-android- \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
}
build_64
make -j8
make install
#!/bin/bash
# 清空上次的编译
make distclean
#这里替换成你的NDK目录即可
export NDK=/home/maxingrong/disk1/temp/android-ndk-r14b
# 设置你的android平台编译器的版本 这里采用Android4.0
export SYSROOT=$NDK/platforms/android-19/arch-arm/
#编译使用的toolchain
export TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
export CPU=armv7-a \
# 这个是输出的路径
export PREFIX=$(pwd)/android/$CPU
#export ADDI_CFLAGS="-marm -I/usr/local/include -I/home/maxingrong/disk1/temp/x264/android/arm/include"
#export ADDI_LDFLAGS="-L/home/maxingrong/disk1/temp/x264/android/arm/lib -L/usr/local/lib"
./configure --target-os=android \
--prefix=$PREFIX \
--arch=arm \
--enable-neon \
--enable-hwaccels \
--enable-jni \
--enable-mediacodec \
--enable-decoder=h264_mediacodec \
--cpu=armv7-a \
--disable-doc \
--enable-shared \
--enable-static \
--disable-x86asm \
--disable-symver \
--enable-gpl \
--enable-libx264 \
--enable-muxer=mp4 \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-doc \
--disable-symver \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic -marm -lm -I/home/maxingrong/disk1/temp/x264/android/arm/include $ADDI_CFLAGS" \
--extra-ldflags="-L/home/maxingrong/disk1/temp/x264/android/arm/lib $ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make -j16
make install
#!/bin/bash
make clean
#你自己的NDK路径。
export NDK=/home/maxingrong/disk1/temp/android-ndk-r14b
# 设置你的android平台编译器的版本 这里采用Android4.0
export SYSROOT=$NDK/platforms/android-24/arch-arm64
#编译使用的toolchain
export TOOLCHAIN=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64
export CPU=arm64 \
# 这个是输出的路径
export PREFIX=$(pwd)/android/$CPU
#export ADDI_CFLAGS="-marm -I/usr/local/include -I/home/maxingrong/disk1/temp/x264/android/arm/include"
#export ADDI_LDFLAGS="-L/home/maxingrong/disk1/temp/x264/android/arm/lib -L/usr/local/lib"
./configure --target-os=android \
--prefix=$PREFIX \
--arch=arm64 \
--enable-neon \
--enable-hwaccels \
--enable-jni \
--enable-mediacodec \
--enable-decoder=h264_mediacodec \
--disable-doc \
--enable-shared \
--enable-static \
--disable-x86asm \
--disable-symver \
--enable-gpl \
--enable-libx264 \
--enable-muxer=mp4 \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-doc \
--disable-symver \
--cross-prefix=$TOOLCHAIN/bin/aarch64-linux-android- \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic -I/home/maxingrong/disk1/temp/x264/android/arm64/include $ADDI_CFLAGS" \
--extra-ldflags="-L/home/maxingrong/disk1/temp/x264/android/arm64/lib $ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make -j16
make install
分割线:到此我们得到了64bit及32bit的libx264.159.so,64bit及32bit的libavcodec.so libavdevice.so libavfilter.so libavformat.so libavutil.so libpostproc.so libswresample.so libswscale.so及这些库的头文件include
接下来开始将这些库移植入Android7.1的系统源码中,以第三方库的形式编译入系统,以让系统内的Native层代码可以调用他们
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libavcodec
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libavcodec.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libavdevice
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libavdevice.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libavfilter
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libavfilter.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libavformat
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libavformat.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libavutil
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libavutil.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libpostproc
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libpostproc.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libswresample
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libswresample.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libswscale
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libswscale.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE :=libx264.159
LOCAL_MODULE_SUFFIX := .so
LOCAL_SRC_FILES := ./lib/libx264.159.so
LOCAL_MODULE_CLASS:=SHARED_LIBRARIES
#LOCAL_MULTILIB :=32
#LOCAL_MODULE_PATH := $(TARGET_OUT)/lib/
include $(BUILD_PREBUILT)
比如可以在系统源码的build/target/product/core.mk添加配置:
PRODUCT_PACKAGES += \
libavcodec \
libavdevice \
libavfilter \
libavformat \
libavutil \
libpostproc \
libswresample \
libswscale \
libx264.159 \
不出意外的话,系统编译完成后在系统镜像/system/lib64中即可找到这些库了,到此已经成功将ffmpeg和x264添加入系统了。
当你想在Android系统源码的Native环境调用这些库的时候,只需要调用程序源码中引用一下这些库的include中的头文件,并在调用程序的Android.mk中的LOCAL_SHARED_LIBRARIES里添加对他们的引用即可:
LOCAL_SHARED_LIBRARIES := \
libavcodec \
libavdevice \
libavfilter \
libavformat \
libavutil \
libpostproc \
libswresample \
libswscale \
如果嫌麻烦也可以下载我配置好的:
x264:
https://download.csdn.net/download/Xiaoma_Pedro/12252834
ffmpeg:
https://download.csdn.net/download/Xiaoma_Pedro/12252836
Android7.1 系统源码添加的/external/ffmpeg_x264
https://download.csdn.net/download/Xiaoma_Pedro/12252845