1. 软件版本
1.1 ndk android-ndk-r15c
1.2 ffmepg ffmpeg-4.0.5.tar.bz2
1.3 x264 x264-stable.tar.gz
2. 编译x264静态库
tar -zxvf x264-stable.tar.gz
mv x264-stable x264
vi build.sh
#!/bin/bash
NDK=/home/sheng/ndk/android-ndk-r15c
configure()
{
CPU=$1
PREFIX=$(pwd)/android/$CPU
HOST=""
CROSS_PREFIX=""
SYSROOT=""
if [ "$CPU" == "armv7-a" ]
then
HOST=arm-linux
SYSROOT=$NDK/platforms/android-21/arch-arm/
CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
else
HOST=aarch64-linux
SYSROOT=$NDK/platforms/android-21/arch-arm64/
CROSS_PREFIX=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-
fi
./configure \
--prefix=$PREFIX \
--host=$HOST \
--enable-pic \
--enable-static \
--enable-neon \
--extra-cflags="-fPIE -pie" \
--extra-ldflags="-fPIE -pie" \
--cross-prefix=$CROSS_PREFIX \
--sysroot=$SYSROOT
}
build()
{
make clean
cpu=$1
echo "build $cpu"
configure $cpu
#-j<CPU核心数>
make -j4
make install
}
build arm64
build armv7-a
sudo chmod 777 build.sh
./build.sh
3. 编译ffmpeg静态库
tar -jxvf ffmpeg-4.0.5.tar.bz2
cp ../x264 ffmpeg-4.0.5/x264
vi build.sh
#!/bin/bash
NDK=/home/sheng/ndk/android-ndk-r15c
ADDI_CFLAGS="-fPIE -pie"
ADDI_LDFLAGS="-fPIE -pie"
configure()
{
CPU=$1
PREFIX=$(pwd)/android/$CPU
x264=$(pwd)/x264/android/$CPU
HOST=""
CROSS_PREFIX=""
SYSROOT=""
ARCH=""
if [ "$CPU" == "armv7-a" ]
then
ARCH="arm"
HOST=arm-linux
SYSROOT=$NDK/platforms/android-21/arch-arm/
CROSS_PREFIX=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
else
ARCH="aarch64"
HOST=aarch64-linux
SYSROOT=$NDK/platforms/android-21/arch-arm64/
CROSS_PREFIX=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-
fi
./configure \
--prefix=$PREFIX \
--enable-small \
--disable-programs \
--disable-encoders \
--disable-muxers \
--disable-filters \
--disable-decoders \
--disable-avdevice \
--disable-doc \
--disable-ffplay \
--disable-network \
--disable-doc \
--disable-symver \
--enable-neon \
--enable-libx264 \
--enable-gpl \
--enable-pic \
--enable-jni \
--enable-pthreads \
--enable-mediacodec \
--enable-encoder=aac \
--enable-encoder=gif \
--enable-encoder=libopenjpeg \
--enable-encoder=libmp3lame \
--enable-encoder=libwavpack \
--enable-encoder=libx264 \
--enable-encoder=mpeg4 \
--enable-encoder=pcm_s16le \
--enable-encoder=png \
--enable-encoder=srt \
--enable-encoder=subrip \
--enable-encoder=yuv4 \
--enable-encoder=text \
--enable-decoder=aac \
--enable-decoder=aac_latm \
--enable-decoder=libopenjpeg \
--enable-decoder=mp3 \
--enable-decoder=mpeg4_mediacodec \
--enable-decoder=pcm_s16le \
--enable-decoder=flac \
--enable-decoder=flv \
--enable-decoder=gif \
--enable-decoder=png \
--enable-decoder=srt \
--enable-decoder=xsub \
--enable-decoder=yuv4 \
--enable-decoder=vp8_mediacodec \
--enable-decoder=h264_mediacodec \
--enable-decoder=hevc_mediacodec \
--enable-hwaccel=h264_mediacodec \
--enable-hwaccel=mpeg4_mediacodec \
--enable-ffmpeg \
--enable-bsf=aac_adtstoasc \
--enable-bsf=h264_mp4toannexb \
--enable-bsf=hevc_mp4toannexb \
--enable-bsf=mpeg4_unpack_bframes \
--enable-cross-compile \
--cross-prefix=$CROSS_PREFIX \
--disable-shared \
--enable-static \
--target-os=android \
--arch=$ARCH \
--sysroot=$SYSROOT \
--extra-cflags="-I$x264/include $ADDI_CFLAGS" \
--extra-ldflags="-L$x264/lib"
}
build()
{
make clean
cpu=$1
echo "build $cpu"
configure $cpu
make -j4
make install
}
build arm64
build armv7-a
sudo chmod 777 build.sh
./build.sh
4. 在windows打开当前目录 拷文件到 android studio 工程
explorer.exe .
4.1 拷 ffmpeg include
/ffmpeg-4.0.5/android/arm64/include
\app\src\main\cpp\include
4.2 拷 ffmpeg a
/ffmpeg-4.0.5/android/arm64/lib
\app\src\main\cpp\libs
4.3 修改app 的build.gradle
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
abiFilters "armeabi-v7a" , "arm64-v8a"
}
}
4.5 修改app 的 CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
include_directories(${CMAKE_SOURCE_DIR}/include)
add_library(
native-lib
SHARED
native-lib.cpp)
add_library(static-lib
STATIC
IMPORTED
)
set_target_properties(static-lib
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libstatic-lib.a )
#add libavcodec
add_library(libavcodec
STATIC
IMPORTED
)
SET_TARGET_PROPERTIES(
libavcodec
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavcodec.a
)
#add libavfilter
add_library(libavfilter
STATIC
IMPORTED
)
SET_TARGET_PROPERTIES(
libavfilter
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavfilter.a
)
#add libavformat
add_library(libavformat
STATIC
IMPORTED
)
SET_TARGET_PROPERTIES(
libavformat
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavformat.a
)
#add libavutil
add_library(libavutil
STATIC
IMPORTED
)
SET_TARGET_PROPERTIES(
libavutil
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libavutil.a
)
#add libpostproc
add_library(libpostproc
STATIC
IMPORTED
)
SET_TARGET_PROPERTIES(
libpostproc
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libpostproc.a
)
#add libswresample
add_library(libswresample
STATIC
IMPORTED
)
SET_TARGET_PROPERTIES(
libswresample
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libswresample.a
)
#add libswscale
add_library(libswscale
STATIC
IMPORTED
)
SET_TARGET_PROPERTIES(
libswscale
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libswscale.a
)
find_library(
log-lib
log)
target_link_libraries(
native-lib
static-lib
libavcodec
libswscale
libswresample
libpostproc
libavutil
libavformat
libavfilter
${log-lib})
4.6 修改cpp 测试调用成功
#include <jni.h>
#include <string>
#include<android/log.h>
#include "static.h"
#define LOG_TAG "native-lib"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
extern "C"{
#include "include/libavutil/avutil.h"
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_jbgf_naivesotest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
//---------------------新增--------------------
int x = 0;
x = apple();
LOGI("x = %d", x);
//----------------------------------------------
return env->NewStringUTF(av_version_info());
}
extern "C"
JNIEXPORT jbyteArray JNICALL
Java_com_jbgf_naivesotest_MainActivity_bytesFromJNI(JNIEnv *env, jobject thiz) {
jbyte byteA[8];
byteA[0] = 1;
byteA[1] = 1;
byteA[2] = 1;
byteA[3] = 1;
byteA[4] = 1;
byteA[5] = 1;
byteA[6] = 1;
byteA[7] = 1;
jbyteArray jByteArray= env->NewByteArray(8);
env->SetByteArrayRegion(jByteArray,0,8,byteA);
return jByteArray;
}
extern "C"
JNIEXPORT jbyteArray JNICALL
Java_com_jbgf_naivesotest_MainActivity_bytesFromJNI2(JNIEnv *env, jobject thiz,
jbyteArray byte_frame) {
//1.获取数组和数组长度
jbyte *c_array = env->GetByteArrayElements(byte_frame,JNI_FALSE);
int arr_len = env->GetArrayLength(byte_frame);
//2. 释放java数组内存
env->ReleaseByteArrayElements(byte_frame, c_array, 0);
//3. 具体处理
for(int i=0 ; i< arr_len;i++)
{
c_array[i] = c_array[i] + 1;
LOGI("123");
// __android_log_print()
}
//4. 组装返回数据
jbyteArray jbyteArray1 = env->NewByteArray(arr_len);
env->SetByteArrayRegion(jbyteArray1, 0, arr_len, c_array);
return jbyteArray1;
}
参考《FFmpeg一键编译Android arm64位和32位共享库(armeabi-v7a arm64-v8a)》
来源:CSDN
作者:不要用昵称
链接:https://blog.csdn.net/Shengbuqi/article/details/104302768