Compile ffmpeg for Android

转载地址:https://yesimroy.gitbooks.io/android-note/content/compile_ffmpeg_for_android.html

Before Continuing this page, if u want to enable x264, please complete building x264 using android toolchain first. Check this.

Please put x264 folder at same level of hierarchy with ffmpeg fodler

╭─yesimroy@RoyMBPR  ~/Documents/
╰─$ ls
x264 ffmpeg-3.0

This tutorial is for generating android jni needed ffmpeg shared libraries for platform (armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, mips).

1. Download and Unzip ffmpeg

Download ffmpeg from the link, please check lib version first. It shows the description about the version of libries it has.

For ex, FFmpeg 3.0 "Einstein" includes the following library versions:


libavutil      55. 17.103
libavcodec     57. 24.102
libavformat    57. 25.100
libavdevice    57.  0.101
libavfilter     6. 31.100
libavresample   3.  0.  0
libswscale      4.  0.100
libswresample   2.  0.101
libpostproc    54.  0.100

2. Edit output shared library file name

For the output format like this libavcodec-55.so, which is accepted in Android. You need to edit ffmpeg/configure as follow:

Warning: Cause of Markdown double dollar sign ($) issue, the dollar sign $$ bellow should be correct into the right $$

Find these codes:

 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)'

And modify into these ones:

SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB)"$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)'
SLIB_INSTALL_LINKS='$(SLIBNAME)'

3. Download build scripts of ffmpeg & x264 for android ndk from my repository

Link to my ffmpeg + x264 build scripts repository

4. Copy build scripts into ffmpeg folder

Copy the scripts inside build-scripts-of-ffmpeg-x264-for-android-ndk-master/ffmpeg to your downloaded ffmpeg src code folder. Ex.

Here take ffmpeg-3.0 as an example.

cp build-scripts-of-ffmpeg-x264-for-android-ndk-master/ffmpeg/* ffmpeg-3.0/

Open text editor, check each build script, whether the ndk path and toolchain path are correct or not.

#check the paths bellow, make sure any words is correct.
#for ex. if you want to build shared libraries for arm64, the lowest platform level is 21. for 32 bits, usually i choose android-18. (as you want)
NDK=/Users/yesimroy/Library/Android/sdk/ndk-bundle
PLATFORM=$NDK/platforms/android-21/arch-arm64/
PREBUILT=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64

5. Run the build-all script for generating all platform shared library

Run the build-all script and wait for it, you can check the console logs in case there is an error.

cd ffmpeg-3.0

./build_android_all.sh

6. After compiling success

After compiling success, you will see an folder called android including all platform libraries inside your ffmpeg folder.

╭─yesimroy@MBPR  ~/Documents/ffmpeg-3.0
╰─$ ls android
arm64-v8a  armeabi  armeabi-v7a  x86  x86_64  mips

References

  1. https://github.com/hrydgard/ppsspp-ffmpeg
  2. http://blog.youkuaiyun.com/myxuan475/article/details/48625823
  3. http://stackoverflow.com/questions/4119359/build-ffmpeg-on-osx
  4. http://blog.youkuaiyun.com/jinrall/article/details/50556328
  5. https://dl.dropboxusercontent.com/u/22605641/ffmpeg_android/main.html
  6. https://trac.ffmpeg.org/attachment/ticket/4928/build_ffmpeg.sh
  7. https://github.com/hrydgard/ppsspp-ffmpeg/tree/master/android
  8. http://vinsol.com/blog/2014/07/30/cross-compiling-ffmpeg-with-x264-for-android/
  9. http://codex.wiki/post/174761-227
  10. http://stackoverflow.com/questions/27421134/system-loadlibrary-couldnt-find-native-library-in-my-case
  11. https://github.com/ongakuer/javacpp-presets/blob/custom_ffmpeg/ffmpeg/cppbuild.sh
  12. http://m.blog.youkuaiyun.com/article/details?id=50500444
  13. http://www.pcpop.com/doc/2/2541/2541831.shtml
在WSL(Ubuntu)中编译ffmpeg for Android的步骤如下: 1. 安装必要的依赖项 在Ubuntu中打开终端,执行以下命令安装必要的依赖项: ``` sudo apt-get update sudo apt-get install build-essential git autoconf automake libtool pkg-config zlib1g-dev cmake mercurial texinfo ``` 2. 下载NDK 下载并解压Android NDK,可以从官网下载最新版:https://developer.android.com/ndk/downloads/ 假设你将NDK解压到了 /opt/android-ndk-r21d 目录下。 3. 安装交叉编译工具链 执行以下命令安装交叉编译工具链: ``` cd /opt/android-ndk-r21d/build/tools/ ./make_standalone_toolchain.py --arch arm64 --api 21 --stl=libc++ --install-dir /opt/android-toolchain-arm64 ``` 这里选择了 arm64 架构,API级别为 21,STL库为 libc++,安装目录为 /opt/android-toolchain-arm64。 4. 下载ffmpeg源码 执行以下命令下载ffmpeg源码: ``` git clone https://git.ffmpeg.org/ffmpeg.git cd ffmpeg ``` 5. 配置交叉编译环境 执行以下命令配置交叉编译环境: ``` export PATH=$PATH:/opt/android-toolchain-arm64/bin export CC=aarch64-linux-android21-clang export CXX=aarch64-linux-android21-clang++ export AR=aarch64-linux-android-ar export AS=aarch64-linux-android-as export LD=aarch64-linux-android-ld export RANLIB=aarch64-linux-android-ranlib export STRIP=aarch64-linux-android-strip ``` 6. 配置ffmpeg编译选项 执行以下命令配置ffmpeg编译选项: ``` ./configure \ --target-os=android \ --prefix=/opt/ffmpeg-android/arm64-v8a \ --enable-cross-compile \ --enable-pic \ --enable-jni \ --enable-mediacodec \ --enable-decoder=h264_mediacodec \ --enable-decoder=hevc_mediacodec \ --disable-static \ --enable-shared \ --disable-doc \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffprobe \ --disable-postproc \ --disable-avdevice \ --disable-symver \ --disable-debug \ --disable-network \ --disable-everything \ --arch=aarch64 \ --cpu=armv8-a \ --cc=aarch64-linux-android21-clang \ --cxx=aarch64-linux-android21-clang++ \ --cross-prefix=aarch64-linux-android- \ --sysroot=/opt/android-toolchain-arm64/sysroot ``` 这里开启了 mediacodec 硬解码支持,只支持 H.264 和 HEVC 硬解码,其他配置选项根据需要自行调整。 7. 编译ffmpeg 执行以下命令编译ffmpeg: ``` make -j4 sudo make install ``` 这里使用了 4 核进行编译,可以根据实际情况进行调整。 8. 完成 编译完成后,ffmpeg库文件将会被安装到 /opt/ffmpeg-android/arm64-v8a 目录下。可以将这个目录打包成一个.aar库文件,用于Android Studio项目中的使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值