看了一下网络上的文章大多都是在windows环境下编译android的ffmpeg的
我跟着操作了一遍,发现windows下编译ffmpeg
其实就是安装了Ubuntu的linux的虚拟机
然后把资源放到虚拟机上进行编译
最后把编译好的so库再拷贝回到windows环境下使用
这里我就总结一下mac电脑编译ffmpeg的过程,因为mac就是unix系统的
这系统的源头和linux是一家的,所以环境本来就是对的
最大的好处是不需要装Ubuntu的虚拟机器 和把文件拷贝来拷贝去
下载后记得要解压
然后打开ffmpeg-3.3.8/configure
修改其中一段代码 3305行左右
#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)'
修改完后记得保存一下
https://blog.csdn.net/momo0853/article/details/73898066
下载NDK(r14b)
找到android编译的脚本
build_android.sh
拖到ffmpeg-3.3.8
文件夹里面
修改一下里面的脚本参数
export NDK_HOME=/usr/work/android-ndk-r14b
改成自己下载的ndk的路径
export NDK_HOME=/Users/xxx/Library/Android/android-ndk-r14b
TOOLCHAIN=$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
需要改一下 改成苹果电脑的
TOOLCHAIN=$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
TOOLCHAIN=$NDK_HOME/toolchains/x86-4.9/prebuilt/linux-x86_64
也改成苹果电脑的
TOOLCHAIN=$NDK_HOME/toolchains/x86-4.9/prebuilt/darwin-x86_64
其余就不需要修改了
到了最终的编译环节
1、执行configure文件生成makefile文件
chmod u+x configure(修改权限)
./configure(执行脚本)
2、执行android编译脚本
chmod u+x build_android.sh(修改权限)
./build_android.sh(执行脚本)
编译成功后的样子
build_android.sh
#!/bin/bash
export NDK_HOME=/
XXX/android/android-ndk-r14b
export PLATFORM_VERSION=android-9
function build
{
echo "start build ffmpeg for $ARCH"
./configure --target-os=linux \
--prefix=$PREFIX --arch=$ARCH \
--disable-doc \
--enable-shared \
--disable-static \
--disable-yasm \
--disable-asm \
--disable-symver \
--enable-gpl \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--cross-prefix=$CROSS_COMPILE \
--enable-cross-compile \
--sysroot=$SYSROOT \
--enable-small \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
echo "build ffmpeg for $ARCH finished"
}
#arm
ARCH=arm
CPU=arm
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CROSS_COMPILE=$TOOLCHAIN/bin/arm-linux-androideabi-
ADDI_CFLAGS="-marm"
SYSROOT=$NDK_HOME/platforms/$PLATFORM_VERSION/arch-$ARCH/
build
#x86
ARCH=x86
CPU=x86
PREFIX=$(pwd)/android/$ARCH
TOOLCHAIN=$NDK_HOME/toolchains/x86-4.9/prebuilt/darwin-x86_64
CROSS_COMPILE=$TOOLCHAIN/bin/i686-linux-android-
ADDI_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
SYSROOT=$NDK_HOME/platforms/$PLATFORM_VERSION/arch-$ARCH/
build
懒人脚本build_android.sh链接
创建一个新的项目 勾选C++支持
打开运行时编译器支持
解决找不到ndk的问题
选择 file ->Project Structure
设置自己的ndk-build14的包目录
拷贝jniLi 和include 到对应目录下
改写build.gradle
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'armeabi', "x86"
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
ios平台编译左转
修改cmakelist
全部替换
cmake_minimum_required(VERSION 3.4.1)
include_directories(src/main/cpp/include)
add_library(
native-lib
SHARED
src/main/cpp/native-lib.cpp )
add_library( avcodec-57 SHARED IMPORTED)
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavcodec-57.so)
add_library( avdevice-57 SHARED IMPORTED)
set_target_properties( avdevice-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavdevice-57.so)
add_library( avfilter-6 SHARED IMPORTED)
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavfilter-6.so)
add_library( avformat-57 SHARED IMPORTED)
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavformat-57.so)
add_library( avutil-55 SHARED IMPORTED)
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavutil-55.so)
add_library( postproc-54 SHARED IMPORTED)
set_target_properties( postproc-54
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libpostproc-54.so)
add_library( swresample-2 SHARED IMPORTED)
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswresample-2.so)
add_library( swscale-4 SHARED IMPORTED)
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswscale-4.so)
find_library(
log-lib
log )
target_link_libraries(
native-lib
${log-lib}
avcodec-57
avdevice-57
avfilter-6
avformat-57
avutil-55
swresample-2
swscale-4
postproc-54)
java 增加库依赖
static {
System.loadLibrary("native-lib");
System.loadLibrary("avutil-55");
System.loadLibrary("swresample-2");
System.loadLibrary("avcodec-57");
System.loadLibrary("avformat-57");
System.loadLibrary("swscale-4");
System.loadLibrary("postproc-54");
System.loadLibrary("avfilter-6");
System.loadLibrary("avdevice-57");
}
修改native-lib.cpp
#include <jni.h>
#include <string>
extern "C"
{
#include <libavformat/avformat.h>
}
extern "C" JNIEXPORT jstring
JNICALL
Java_playersdk_platform_leedarson_ffmpegforandroid_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
av_register_all();
AVCodec *c_temp = av_codec_next(NULL);
char *name = (char *) malloc(100000);
strcpy(name, "Abality:");
while (c_temp != NULL)
{
switch (c_temp->type)
{
case AVMEDIA_TYPE_VIDEO:
printf("[Video]:%s\n", c_temp->name);
strcat(name, "[Video]");
strcat(name, c_temp->name);
break;
case AVMEDIA_TYPE_AUDIO:
printf("[Audio]:%s\n", c_temp->name);
strcat(name, "[Audio]");
strcat(name, c_temp->name);
break;
default:
printf("[Other]:%s\n", c_temp->name);
strcat(name, "[Other]");
strcat(name, c_temp->name);
break;
}
c_temp = c_temp->next;
}
printf("[hahahaha]:%s\n", name);
return env->NewStringUTF(name);
}
运行起来
到这里带ffmpeg的项目就跑起来了