之前一直在linux下编译FFmpeg,最近换电脑了,尝试了下在mac下编译ffmpeg,特记录之。

一. 准备工作

1. 下载FFmpeg。(http://ffmpeg.org/download.html#releases),看了下最新的是4.1.4,我用的是3.4.6。

2. 下载mac上使用的NDK。(https://developer.android.google.cn/ndk/downloads/index.html), 我用的是android-ndk-r14b-darwin-x86_64.zip,不推荐使用ndk-bundle,有些头文件在ndk-bundle里没有,遇到过一次。

3. 修改FFmpeg3.4.6目录下的configure文件。如果不修改的话之后编译生成的版本号会加在so的后面,会导致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. 编写编译用的脚本文件。这个网上有很多,我找到了两个可以用的:

用于编译 armeabi-v7a、arm64-v8a、x86_64版本的:

#!/bin/bash

echo "进入FFmpeg编译脚本"

# NDK环境
export NDK=/Users/haiyuegao/Library/Android/android-ndk-r14b
export SYSROOT=$NDK/platforms/android-/arch-arm
export TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
PREFIX=android-build COMMON_OPTIONS="\
--prefix=android/ \
--target-os=android \
--enable-shared \
--enable-runtime-cpudetect \
--enable-small \
--disable-static \
--disable-debug \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-doc \
--disable-symver \
--disable-asm \
--disable-stripping \
--disable-armv5te \
" function build_android { echo "开始编译FFmpeg..." # armeabi
echo "开始编译FFmpeg(armeabi)"
./configure \
--libdir=${PREFIX}/libs/armeabi \
--incdir=${PREFIX}/include/armeabi \
--pkgconfigdir=${PREFIX}/pkgconfig/armeabi \
--arch=arm \
--cpu=armv6 \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$SYSROOT \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
echo "结束编译FFmpeg(armeabi)" # armeabi-v7a
echo "开始编译FFmpeg(armeabi-v7a)"
./configure \
--libdir=${PREFIX}/libs/armeabi-v7a \
--incdir=${PREFIX}/include/armeabi-v7a \
--pkgconfigdir=${PREFIX}/pkgconfig/armeabi-v7a \
--arch=arm \
--cpu=armv7-a \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$SYSROOT \
--extra-cflags="-march=armv7-a -mfloat-abi=softfp -mfpu=neon" \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
echo "结束编译FFmpeg(armeabi-v7a)" # arm64-v8a
echo "开始编译FFmpeg(arm64-v8a)"
./configure \
--libdir=${PREFIX}/libs/arm64-v8a \
--incdir=${PREFIX}/include/arm64-v8a \
--pkgconfigdir=${PREFIX}/pkgconfig/arm64-v8a \
--arch=aarch64 \
--cpu=armv8-a \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$SYSROOT \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
echo "结束编译FFmpeg(arm64-v8a)" # x86_64
echo "开始编译FFmpeg(x86_64)"
./configure \
--libdir=${PREFIX}/libs/x86_64 \
--incdir=${PREFIX}/include/x86_64 \
--pkgconfigdir=${PREFIX}/pkgconfig/x86_64 \
--arch=x86_64 \
--cpu=x86_64 \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--sysroot=$SYSROOT \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
echo "结束编译FFmpeg(x86_64)" echo "编译结束" };
build_android

macos_build_android.sh

用于编译x86版本的

#!/bin/bash

echo "进入FFmpeg编译脚本"

# NDK环境
export NDK=/Users/haiyuegao/Library/Android/android-ndk-r14b
export SYSROOT=$NDK/platforms/android-/arch-x86
export TOOLCHAIN=$NDK/toolchains/x86-4.9/prebuilt/darwin-x86_64
PREFIX=android-build COMMON_OPTIONS="\
--prefix=android/ \
--target-os=android \
--enable-shared \
--enable-runtime-cpudetect \
--enable-small \
--disable-static \
--disable-debug \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-doc \
--disable-symver \
--disable-asm \
--disable-stripping \
--disable-armv5te \
" function build_android { echo "开始编译FFmpeg..." # x86
echo "开始编译FFmpeg(x86)"
./configure \
--libdir=${PREFIX}/libs/x86 \
--incdir=${PREFIX}/include/x86 \
--pkgconfigdir=${PREFIX}/pkgconfig/x86 \
--arch=x86 \
--cpu=i686 \
--cross-prefix=$TOOLCHAIN/bin/i686-linux-android- \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32" \
--extra-ldexeflags=-pie \
${COMMON_OPTIONS}
make clean
make -j8 && make install
echo "结束编译FFmpeg(x86)" echo "编译结束" };
build_android

macos_build_android_x86.sh

需要注意的是:要根据自己电脑的实际环境修改下上面的NDK目录地址,我的NDK地址是:

5. 修改脚本的执行权限。将脚本放置到ffmpeg的根目录下,为其添加执行权限:

然后打开终端,cd到ffmpeg那级目录,分别执行以下两条命令(如果要编译x86版本,就把shell脚本的名字换成x86版本的那个):

chmod  macos_build_android.sh
./macos_build_android.sh

6. 开始编译。视电脑的配置不同编译的时间也不同,我电脑用了大约5分钟左右完成了编译。

等待编译完成,稍后ffmpeg那级目录会出现一个android-build文件夹,里面包含了我们会用到的库文件和头文件。

二. 集成FFmpeg到项目中

1. 新建一个支持C++的Android Studio 项目。

2. 拷贝ffmpeg库文件和头文件。将编译生成的libs文件夹和include文件夹分别拷贝到项目的app目录下:

3. 更新配置文件。

修改app目录下的build.gradle文件:

添加下列代码到gradle文件:

    externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
ndk {
abiFilters "armeabi-v7a","x86"
}
}
sourceSets {
main {
jniLibs.srcDirs=['libs']
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}

最终文件如下:

apply plugin: 'com.android.application'

android {
compileSdkVersion
defaultConfig {
applicationId "com.yongdaimi.android.ffapitest"
minSdkVersion
targetSdkVersion
versionCode
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
ndk {
abiFilters "armeabi-v7a","x86"
}
}
sourceSets {
main {
jniLibs.srcDirs=['libs']
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
} dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

build.gradle

修改app目录下的CMakeLists.txt文件:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.) # Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK. # 添加头文件路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/${ANDROID_ABI}) # 设置FFmpeg库所在路径
set(FF ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}) # 打印当前位置信息
message("Current souce file positon: " ${CMAKE_CURRENT_SOURCE_DIR}) # avcodec
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so) add_library( # Sets the name of the library.
native-lib # Sets the library as a shared library.
SHARED # Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp) # Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build. find_library( # Sets the name of the path variable.
log-lib # Specifies the name of the NDK library that
# you want CMake to locate.
log) # Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library.
native-lib
avcodec # Links the target library to the log library
# included in the NDK.
${log-lib})

CMakeLists.txt

最终目录结构如下:

这里一定要注意CMakeLists.txt这个文件的位置,我在Windows下创建AS项目时这个文件在app目录下,在Mac上创建AS项目时这个文件却在app/src/main/cpp目录下,个人为了保持习惯,便将这个文件仍然移动到了app目录下,若使用Mac上默认的位置,则上述CMakeLists.txt和build.gradle中声明文件的位置部分也需要修改,切记。

4. 添加测试代码。打开native-lib.cpp,修改已有的JNI方法:

#include <jni.h>
#include <string> extern "C" {
#include <libavcodec/avcodec.h>
} extern "C" JNIEXPORT jstring JNICALL
Java_com_yongdaimi_android_ffapitest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++"; hello += avcodec_configuration(); return env->NewStringUTF(hello.c_str());

由于FFmpeg是使用纯C编写的,因此在导入其头文件的时候要注意使用extern "C" 大括号包裹。

之后运行项目,如果能出现相关的配置信息就说明FFmpeg已经加载成功了。

三. 常见错误说明

1. Error computing CMake server result.

若编译出现

Error computing CMake server result.
Check for working C compiler: D:/sdk/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
Check for working C compiler: D:/sdk/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: D:/sdk/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
Check for working CXX compiler: D:/sdk/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Boost version: 1.70.
Configuring for JNI
Configuring done

则打开Build的Toggle View按钮,根据Log分析出错的详细原因,看看是native-lib.cpp的文件找不到或者是其它问题。

2. ninja error needed by missing and no known rule to make it

Error:error: '../../../../src/main/jniLibs/x86/libstblur_preview_api.so', 
needed by '../../../../build/intermediates/cmake/debug/obj/x86/libjnistblur_preview_api.so', missing and no known rule to make it

这种错误一般是缺失了某个平台的库导致的,首先查看具体缺失的是哪个平台的so库,如果该平台的库不需要,可以在app下的build.gradle中声明不加载:

     ndk {
// 声明只使用这两个版本的库,否则默认的可是4个平台
abiFilters 'armeabi-v7a','arm64-v8a'
}

或者是检查下CMakeLists.txt中FFmpeg库的路径声明是否正确:

比如我这里声明的库路径就是app/libs/具体的平台。

参考链接:

1.  FFmpeg编译For Android(Mac)

2. 在Mac中编译Android平台的FFmpeg( arm和x86 )

FFmpeg: mac下手动编译android上使用的FFmpeg(支持x86、armeabi-v7a、arm64-v8a)的更多相关文章

  1. Mac 下反编译Android APK

    准备工作:安装ApkTool.dex2jar.JD-GUI 安装ApkTool 1.下载ApkTool.大家可以从 https://ibotpeaches.github.io/Apktool/inst ...

  2. mac下反编译android apk

    所需要的工具 http://pan.baidu.com/disk/home#path=%252Fandroid%252Fdecompile%252Fapktool-all apktool用于将资源文件 ...

  3. Mac下重新编译Linux内核

    Mac下重新编译Linux内核 操作系统实验,要求添加系统调用并重新编译内核,这里记录一下编译内核的过程 0.下载VirtualBox 博主一直用parallel desk,但因为驱动等问题,在PD上 ...

  4. Ubuntu+NDK编译openssl(为了Android上使用libcurl且支持HTTPS协议)

    为了Android上使用libcurl且支持HTTPS协议,需要依赖openssl,因此先来了解一下如何编译OpenSSL1.编译ARM下的共享库(默认的)我使用的是guardianproject的o ...

  5. mac下识别国产android手机

    mac下识别国产android手机困扰了我很久,这几天总算在google帮助下找到了解决方法. 在~/.android/下找到adb_usb.ini,如果不存在则创建.通过“系统信息”查看到插入的an ...

  6. 【FAQ】Ubuntu环境下ant编译android代码问题

    在Ubuntu14.04环境下,编译android程序时候,运行ant debug的时候出现如下异常:

  7. mac下phpize编译提示Cannot find autoconf解决办法

    mac下phpize编译如下报错: /usr/bin/phpizeConfiguring for:PHP Api Version: 20121113Zend Module Api No: 201212 ...

  8. Charles在Mac、iPhone、Android上抓http/https协议的包

    1.我使用的版本是4.0.2,下载和破解网上方法很多,不做说明 2.Charles在Mac上抓http/https协议的包 2.1先把这三个都给装上,装完后会自动跳转到钥匙串中 2.2如果装完后提示证 ...

  9. Linux下手动编译shogun

    手动编译shogun,如果按照直接按照官网上的步骤进行,会踩非常多的坑,下面分享一下在下的编译过程,希望能为阁下提供些许借鉴. 1. git clone https://github.com/shog ...

随机推荐

  1. docker到底比LXC多了些什么

    看似docker主要的OS级虚拟化操作是借助LXC, AUFS只是锦上添花.那么肯定会有人好奇docker到底比LXC多了些什么.无意中发现 stackoverflow 上正好有人问这个问题, 回答者 ...

  2. linux ssh_config和sshd_config配置文件学习

    在远程管理linux系统基本上都要使用到ssh,原因很简单:telnet.FTP等传输方式是‍以明文传送用户认证信息,本质上是不安全的,存在被网络窃听的危险.SSH(Secure Shell)目前较可 ...

  3. c的链表实现

    c的链表实现 复习了 单向链表.双向链表 ,代码中注释不多,但基本从函数名字就可以知道函数的作用. 双向链表中的前后节点中的思路是按照linux内核中思路写的. 环境 GCC 7.4.0 单向链表 # ...

  4. 桂电深信服CTF之MSC真假压缩包

    真假压缩包-复现(吐槽一句,脑洞真特么大) 先从虚假的入手,一开始猜测虚假的是伪加密,后面也确实是 后面打开压缩包,是一个txt文件 一眼可以看出来是RSA,算出来明文是5(于是就在这里卡了两天,一直 ...

  5. 个性化排序算法实践(四)——GBDT+LR

    本质上GBDT+LR是一种具有stacking思想的二分类器模型,所以可以用来解决二分类问题.这个方法出自于Facebook 2014年的论文 Practical Lessons from Predi ...

  6. JS之try..catch...

    try 测试代码块的错误. catch 语句处理错误. throw 创建并跑出错误. try { //在这里运行代码 抛出错误 }catch(err){ //在这里处理错误 } 实例: <p&g ...

  7. [Usaco2006 Jan] Redundant Paths 分离的路径

    1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1132  Solv ...

  8. [BeiJing2010组队]次小生成树 Tree

    1977: [BeiJing2010组队]次小生成树 Tree Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 5168  Solved: 1668[S ...

  9. idea添加tomcat和服务添加项目

  10. 【C语言基础】编码规范

    from:程序员互动联盟 2016-12-28 1. 基本要求  1.1 程序结构清析,简单易懂,单个函数的程序行数不得超过100行.  1.2 打算干什么,要简单,直接了当,代码精简,避免垃圾程序. ...