下载地址:https://github.com/FFmpeg/FFmpeg
1.2 下载x264源码下载地址: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
修改x264源码根目录下的configure中的这一行,改成下图这个样,不然生成的库的名字会是libx264.so.159 (Android系统是不识别这种命名的动态库的)#/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
执行build_x264_android32.sh后,在x264源码目录的android/arm中输出编译结果:libx264.a 和 libx264.159.so#!/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
执行build_x264_android64.sh后,在x264源码目录的android/arm64中输出编译结果:libx264.a 和 libx264.159.so#!/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
执行build_ffmpeg_android32.sh后,在ffmpeg源码目录的android/arm中输出编译结果:重要的是include中的头文件及lib文件夹下的动态库。#!/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
执行build_ffmpeg_android64.sh后,在ffmpeg源码目录的android/arm中输出编译结果:重要的是include中的头文件及lib文件夹下的动态库。分割线:到此我们得到了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层代码可以调用他们
3 将编译好的ffmpeg及x264移入Android7.1系统源码中并测试调用(注意:我这里以64bit库为例) 3.1 以添加第三方库的形式将ffmpeg、x264的动态库添加入系统中 3.1.1 在Android7.1 源码目录external中创建文件夹ffmpeg_x264目录 将ffmpeg编译出来的64bit的include目录、lib目录复制进Android7.1系统源码目录的external/ffmpeg_x264中,比如下图是我放到我的Android系统源码中的截图: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)
3.1.3 在系统编译控制mk文件中添加对这些库的编译控制
比如可以在系统源码的build/target/product/core.mk添加配置:
PRODUCT_PACKAGES += \
libavcodec \
libavdevice \
libavfilter \
libavformat \
libavutil \
libpostproc \
libswresample \
libswscale \
libx264.159 \
3.1.4 对Android7.1源码进行编译
不出意外的话,系统编译完成后在系统镜像/system/lib64中即可找到这些库了,到此已经成功将ffmpeg和x264添加入系统了。
3.2 在Android7.1的系统Native层测试ffmpeg、x264库的调用当你想在Android系统源码的Native环境调用这些库的时候,只需要调用程序源码中引用一下这些库的include中的头文件,并在调用程序的Android.mk中的LOCAL_SHARED_LIBRARIES里添加对他们的引用即可:
LOCAL_SHARED_LIBRARIES := \
libavcodec \
libavdevice \
libavfilter \
libavformat \
libavutil \
libpostproc \
libswresample \
libswscale \
4 Enjoy it
如果嫌麻烦也可以下载我配置好的:
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