Android Studio编译FFmpeg

jngr0128 8年前
   <h2><strong>前言</strong></h2>    <p>编译FFmpeg的Android版本,并整合到Android Studio。</p>    <p>系统:macOS Sierra 10.12。</p>    <p>NDK版本:r12b。</p>    <p>FFmpeg版本:3.1.3</p>    <h2><strong>编译FFmpeg的步骤</strong></h2>    <ul>     <li> <p>下载 NDK 。</p> </li>     <li> <p>下载 ffmpeg源码 ,解压到NDK目录下的sources目录下。如图</p> </li>    </ul>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/5824d015e7d41868babce3fa1f08b81f.jpg"></p>    <p style="text-align:center">WechatIMG9.jpeg</p>    <ul>     <li>修改ffmpeg-3.1.3目录下的configure文件 定位到3309行:如图:</li>    </ul>    <p><img src="https://simg.open-open.com/show/c9084d1cd44e8184ce83e55474ab9480.jpg"><img src="https://simg.open-open.com/show/10924a0e26232beaa45763ea70707608.jpg"></p>    <p style="text-align:center">WechatIMG11.jpeg</p>    <p>将</p>    <pre>  <code class="language-groovy">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)'</code></pre>    <p>改成</p>    <pre>  <code class="language-groovy">SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)$(LIBMAJOR)$(SLIBSUF)'  LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'  SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'  SLIB_INSTALL_LINKS='$(SLIBNAME)'</code></pre>    <p>目的是将生成.so名称规范化,用以满足android build。</p>    <ul>     <li> <p>在当前目录下新建一个build_android.sh文件,如图:</p> </li>    </ul>    <p><img src="https://simg.open-open.com/show/f989f53cb9432ed21f81a3495af79b2f.jpg"></p>    <p style="text-align:center">WechatIMG13.jpeg</p>    <p>内容为:</p>    <pre>  <code class="language-groovy">#!/bin/bash  NDK=/Users/maimingliang/Documents/NDK/android-ndk-r12b  SYSROOT=$NDK/platforms/android-15/arch-arm/  TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64  function build_one  {  ./configure \   --prefix=$PREFIX \   --enable-shared \   --disable-static \   --disable-doc \   --disable-ffmpeg \   --disable-ffplay \   --disable-ffprobe \   --disable-ffserver \   --disable-avdevice \   --disable-doc \   --disable-symver \   --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \   --target-os=linux \   --arch=arm \   --enable-cross-compile \   --sysroot=$SYSROOT \   --extra-cflags="-Os -fpic $ADDI_CFLAGS" \   --extra-ldflags="$ADDI_LDFLAGS" \   $ADDITIONAL_CONFIGURE_FLAG  make clean  make  make install  }  CPU=arm  PREFIX=$(pwd)/android/$CPU   ADDI_CFLAGS="-marm"  build_one</code></pre>    <ul>     <li> <p>打开终端并且把路径切换到build_android 所在的目录 ,执行命令</p> </li>    </ul>    <p>./build_android.sh</p>    <p>完成编译以后,进入生成的android文件夹,在arm文件夹里面新建一个Android.mk文件,内容为:</p>    <pre>  <code class="language-groovy">LOCAL_PATH:= $(call my-dir)    include $(CLEAR_VARS)  LOCAL_MODULE:= libavcodec  LOCAL_SRC_FILES:= lib/libavcodec-57.so  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include  include $(PREBUILT_SHARED_LIBRARY)    include $(CLEAR_VARS)  LOCAL_MODULE:= libavformat  LOCAL_SRC_FILES:= lib/libavformat-57.so  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include  include $(PREBUILT_SHARED_LIBRARY)    include $(CLEAR_VARS)  LOCAL_MODULE:= libswscale  LOCAL_SRC_FILES:= lib/libswscale-4.so  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include  include $(PREBUILT_SHARED_LIBRARY)    include $(CLEAR_VARS)  LOCAL_MODULE:= libavutil  LOCAL_SRC_FILES:= lib/libavutil-55.so  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include  include $(PREBUILT_SHARED_LIBRARY)    include $(CLEAR_VARS)  LOCAL_MODULE:= libavfilter  LOCAL_SRC_FILES:= lib/libavfilter-6.so  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include  include $(PREBUILT_SHARED_LIBRARY)    include $(CLEAR_VARS)  LOCAL_MODULE:= libswresample  LOCAL_SRC_FILES:= lib/libswresample-2.so  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include  include $(PREBUILT_SHARED_LIBRARY)</code></pre>    <p>至此FFmpeg的编译完成,下面整合到Android Studio 中:</p>    <h2><strong>配置Android Studio</strong></h2>    <ul>     <li>在local.properties加入ndk的路径,我这里是:</li>    </ul>    <p>ndk.dir=/Users/maimingliang/Documents/NDK/android-ndk-r12b</p>    <p>也可以在project structure 中添加。如图所示:</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/fe3aa3ac7a6dcd1d78f80123d096bf3d.jpg"></p>    <p style="text-align: center;">WechatIMG14.jpeg</p>    <ul>     <li> <p>在build.gradle 文件中添加图中标红处的内容。</p> </li>    </ul>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/3f1b23fa5fc21f23f0504bbc0434939a.jpg"></p>    <p style="text-align: center;">WechatIMG21.jpeg</p>    <ul>     <li> <p>配置javah,ndk-build ,ndk-build clean 命令</p> </li>    </ul>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/464a97b42b9670af6bc234b6a750435f.jpg"></p>    <p style="text-align:center">WechatIMG15.jpeg</p>    <p>添加javah命令:</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/9ed53bb16d8989d698e0d04c3cdff965.jpg"></p>    <p>![Uploading WechatIMG17_232544.jpeg . . .]</p>    <p>program:$JDKPath$/bin/javah</p>    <p>paramers:-classpath $Classpath$ -v -jni $FileClass$</p>    <p>working diretory : $ModuleFileDir$/src/main/jni</p>    <p>添加 ndk-build</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/fc238ad7f0da083e6579936080ea99db.jpg"></p>    <p style="text-align:center">IMG_4113.JPG</p>    <p>program:/Users/maimingliang/Documents/NDK/android-ndk-r12b/build/ndk-build //这里要修改你的ndk路径 的ndk-build</p>    <p>paramers:</p>    <p>working diretory : $ModuleFileDir$/src/main/jni</p>    <p>添加 ndk-build clean</p>    <p style="text-align: center;"><img src="https://simg.open-open.com/show/aa7bbe03c06281e25a3508a54ed60c7d.jpg"></p>    <p style="text-align:center">IMG_4114.JPG</p>    <p>program:/Users/maimingliang/Documents/NDK/android-ndk-r12b/build/ndk-build //这里要修改你的ndk路径的 ndk-build</p>    <p>paramers:clean</p>    <p>working diretory : $ProjectFileDir$/app/src/main</p>    <ul>     <li>编写 C文件</li>     <li>编写Android.mk文件</li>     <li>编写Application.mk文件。</li>     <li>运行 javah,ndk-build ndk-build clean 命令</li>    </ul>    <p><img src="https://simg.open-open.com/show/8bff4bdb53787ef44f3ce52797648bc8.jpg"></p>    <p style="text-align:center">IMG_4115.JPG</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/6856e7a17e155bbf8e4c03048fa6f38d.jpg"></p>    <p style="text-align:center">IMG_4116.JPG</p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/42c82169bb7d</p>    <p> </p>