附(2018-01-06): 有一个将x264及lame等库集成进去了且基于android的ffmpeg的编译方法,地址参见: https://github.com/writingminds/ffmpeg-android
原文转自:http://abitno.me/compile-ffmpeg-android-ndk
Android内置的编解码器实在太少,于是我们需要FFmpeg。Android提供了NDK,为我们使用FFmpeg这种C语言代码提供了方便。
不过为了用NDK编译FFmpeg,还真的花费了不少时间,也得到了很多人的帮助,最应该谢谢havlenapetr。我觉得我现在这些方法算是比较简洁的了--
下面就尽量詳細的说一下我是怎么在项目中使用FFmpeg的,但是基于我混乱的表达能力,有不明白的就问我。
你得了解JNI和Android NDK的基本用法,若觉得我的文章还不错,可以看之前写的JNI简单入门和Android NDK入门
首先创建一个标准的Android项目vPlayer
android create project -n vPlayer -t 8 -p vPlayer -k me.abitno.vplayer -a PlayerView
然后在vPlayer目录里
mkdir jni && cd jni
wget http://ffmpeg.org/releases/ffmpeg-0.6.tar.bz2
tar xf ffmpeg-0.6.tar.bz2 && mv ffmpeg-0.6 ffmpeg && cd ffmpeg
在ffmpeg下新建一个config.sh,内容如下,注意把PREBUILT和PLATFORM设置正确。另外里面有些参数你也可以自行调整,我主要是为了配置一个播放器而这样设置的。
#!/bin/bash
PREBUILT=/home/abitno/Android/android-ndk-r4/build/prebuilt/linux-x86/arm-eabi-4.4.0
PLATFORM=/home/abitno/Android/android-ndk-r4/build/platforms/android-8/arch-arm
./configure --target-os=linux
--arch=arm
--enable-version3
--enable-gpl
--enable-nonfree
--disable-stripping
--disable-ffmpeg
--disable-ffplay
--disable-ffserver
--disable-ffprobe
--disable-encoders
--disable-muxers
--disable-devices
--disable-protocols
--enable-protocol=file
--enable-avfilter
--disable-network
--disable-mpegaudio-hp
--disable-avdevice
--enable-cross-compile
--cc=$PREBUILT/bin/arm-eabi-gcc
--cross-prefix=$PREBUILT/bin/arm-eabi-
--nm=$PREBUILT/bin/arm-eabi-nm
--extra-cflags="-fPIC -DANDROID"
--disable-asm
--enable-neon
--enable-armv5te
--extra-ldflags="-Wl,-T,$PREBUILT/arm-eabi/lib/ldscripts/armelf.x -Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib $PREBUILT/lib/gcc/arm-eabi/4.4.0/crtbegin.o $PREBUILT/lib/gcc/arm-eabi/4.4.0/crtend.o -lc -lm -ldl"
运行config.sh开始configure
chmod +x config.sh
./config.sh
configure完成后,编辑刚刚生成的config.h,找到这句
#define restrict restrict
Android的GCC不支持restrict关键字,于是修改成下面这样
#define restrict
编辑libavutil/libm.h,把其中的static方法都删除。
分别修改libavcodec、libavfilter、libavformat、libavutil、libpostproc和libswscale下的Makefile,把下面两句删除
include $(SUBDIR)../subdir.mak include $(SUBDIR)../config.mak
在ffmpeg下添加一个文件av.mk,内容如下
# LOCAL_PATH is one of libavutil, libavcodec, libavformat, or libswscale
#include $(LOCAL_PATH)/../config-$(TARGET_ARCH).mak
include $(LOCAL_PATH)/../config.mak
OBJS :=
OBJS-yes :=
MMX-OBJS-yes :=
include $(LOCAL_PATH)/Makefile
# collect objects
OBJS-$(HAVE_MMX) += $(MMX-OBJS-yes)
OBJS += $(OBJS-yes)
FFNAME := lib$(NAME)
FFLIBS := $(foreach,NAME,$(FFLIBS),lib$(NAME))
FFCFLAGS = -DHAVE_AV_CONFIG_H -Wno-sign-compare -Wno-switch -Wno-pointer-sign
FFCFLAGS += -DTARGET_CONFIG="config-$(TARGET_ARCH).h"
ALL_S_FILES := $(wildcard $(LOCAL_PATH)/$(TARGET_ARCH)/*.S)
ALL_S_FILES := $(addprefix $(TARGET_ARCH)/, $(notdir $(ALL_S_FILES)))
ifneq ($(ALL_S_FILES),)
ALL_S_OBJS := $(patsubst %.S,%.o,$(ALL_S_FILES))
C_OBJS := $(filter-out $(ALL_S_OBJS),$(OBJS))
S_OBJS := $(filter $(ALL_S_OBJS),$(OBJS))
else
C_OBJS := $(OBJS)
S_OBJS :=
endif
C_FILES := $(patsubst %.o,%.c,$(C_OBJS))
S_FILES := $(patsubst %.o,%.S,$(S_OBJS))
FFFILES := $(sort $(S_FILES)) $(sort $(C_FILES))
接下来要添加一系列的Android.mk,在jni根目录下的内容如下
include $(all-subdir-makefiles)
在ffmpeg目录下,Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_LIBRARIES := libavformat libavcodec libavutil libpostproc libswscale
LOCAL_MODULE := ffmpeg
include $(BUILD_SHARED_LIBRARY)
include $(call all-makefiles-under,$(LOCAL_PATH))
libavformat/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=
$(LOCAL_PATH)
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_CFLAGS += -include "string.h" -Dipv6mr_interface=ipv6mr_ifindex
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
libavcodec/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=
$(LOCAL_PATH)
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
libavfilter、libavutil、libpostproc和libswscale下的Android.mk内容如下
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../av.mk
LOCAL_SRC_FILES := $(FFFILES)
LOCAL_C_INCLUDES :=
$(LOCAL_PATH)
$(LOCAL_PATH)/..
LOCAL_CFLAGS += $(FFCFLAGS)
LOCAL_STATIC_LIBRARIES := $(FFLIBS)
LOCAL_MODULE := $(FFNAME)
include $(BUILD_STATIC_LIBRARY)
最外层的jni/Android.mk和jni/ffmpeg/Android.mk我只是随便这样写的,你应该根据自己的需求改写。
最后运行ndk-build,经过漫长的等待就编译完成了。至于具体怎么应用可能以后会写,我变得太懒了。。。
-----------------------------------------------------------------------------------------------------------------------------------------------------------
另一篇文章:
环境:
CentOS6.2——64位
借鉴:https://vec.io/posts/how-to-build-ffmpeg-with-android-ndk
在根目录下创建work文件夹:cd / && mkdir work
a) 先把要用到的安装包下载到本地:
FFmpeg:git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
Android NDK:(我下载的版本是r8b-linux-x86_64)http://developer.android.com/tools/sdk/ndk/index.html
一定要看好下载对应本地系统的软件包版本。
创建好的目录结构是:work/ffmpeg work/android-ndk-r8b
b)配置环境变量:(可以省略)
export ANDROID_NDK_HOME=/work/android-ndk-r8b
export PATH=$ANDROID_NDK_HOME:$PATH
c) 进入ffmpeg目录,创建config.sh文件,并将下面的文字部分写入文件:
- export ANDROID_NDK=/work/android-ndk-r8b
- export TOOLCHAIN=/work/tmp/ffmpeg
- export SYSROOT=$TOOLCHAIN/sysroot/
- $ANDROID_NDK/build/tools/make-standalone-toolchain.sh --platform=android-14 --install-dir=$TOOLCHAIN
- export PATH=$TOOLCHAIN/bin:$PATH
- export CC=arm-linux-androideabi-gcc
- export LD=arm-linux-androideabi-ld
- export AR=arm-linux-androideabi-ar
- CFLAGS="-O3 -Wall -mthumb -pipe -fpic -fasm
- -finline-limit=300 -ffast-math
- -fstrict-aliasing -Werror=strict-aliasing
- -fmodulo-sched -fmodulo-sched-allow-regmoves
- -Wno-psabi -Wa,--noexecstack
- -D__ARM_ARCH_5__ -D__ARM_ARCH_5E__
- -D__ARM_ARCH_5T__ -D__ARM_ARCH_5TE__
- -DANDROID -DNDEBUG"
- EXTRA_CFLAGS="-march=armv7-a -mfpu=neon -mfloat-abi=softfp -mvectorize-with-neon-quad"
- EXTRA_LDFLAGS="-Wl,--fix-cortex-a8"
- FFMPEG_FLAGS="--prefix=/tmp/ffmpeg/build
- --target-os=linux
- --arch=arm
- --enable-cross-compile
- --cc=arm-linux-androideabi-gcc
- --cross-prefix=arm-linux-androideabi-
- --enable-shared
- --disable-symver
- --disable-doc
- --disable-ffplay
- --disable-ffmpeg
- --disable-ffprobe
- --disable-ffserver
- --disable-avdevice
- --disable-avfilter
- --disable-encoders
- --disable-muxers
- --disable-filters
- --disable-devices
- --disable-everything
- --enable-protocols
- --enable-parsers
- --enable-demuxers
- --disable-demuxer=sbg
- --enable-decoder=h264
- --enable-bsfs
- --enable-network
- --enable-swscale
- --enable-asm
- --enable-version3"
- ./configure $FFMPEG_FLAGS --extra-cflags="$CFLAGS $EXTRA_CFLAGS" --extra-ldflags="$EXTRA_LDFLAGS"
- make clean
- make -j4
- make install
当然,configure的参数可以根据自己的需要指定。
d) 执行文件:
./config.sh
这时候可能就会出现各种各样的问题了,不要慌,看看config.log文件,这时候如果有类似
arm-linux-androideabi-gcc is unable to create an executable file.
C compiler test failed.
什么编译器不能创建可执行文件,C编译器找不到什么的,这多数情况下是你指定的NDK路径有问题,再就是NDK的版本问题,不是对应你系统的开发包,我的是Linuxx86_64版本的。
/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
这种问题,这时候就要借助google大神了,万能的google,程序员的福音,好像是缺少什么库依赖,没关系,执行
yum install alibc.i686
问题似乎可以解决了,接下来继续执行
./config.sh
又报出错误:
/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/as: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
一大堆,不过不要紧,有万能的google在,好像是缺少了zlib,好,跟前面一样执行
yum install zlib.i686
可能会提示安装失败什么的,这时候就要去官网下载最新版本的zlib了:http://zlib.net/
下载后解压,进入目录后依次执行
./configure
make
make install
这时候问题看似都解决了,不过我的机器报出错了,lib文件安错位置了,configure默认安装到了/usr/local/lib里,没关系,这时候设置一个软链接就好了:
mv ./lib/libz.so.1 ./lib/libz.so.1_backup
ln -s /usr/local/lib/libz.so.1 ./lib/libz.so.1
再次进入ffmpeg目录执行:
./config.sh
如果问题没有解决,提示mysql什么的,告诉你一个狠招
yum install mysql-embedded.i686
^ o ^
这下没问题了,执行
./config.sh
……………………………………………………
…………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………
…………………………
漫长的编译过程……
最终大功告成,进入/tmp/ffmpeg/build里就看到了你想得到的头文件和库文件,而对应的ffmpeg所有的lib文件夹下也生成了对应的.so文件。
----------------------------------------------------------------------------------------------------------
方法3:
ffmpeg编译成android的单独的libffmpeg.so
替换ffmpeg-3.2.1/configure 中的:
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)'
在ffmpeg-3.2.1/目录下创建一个build_android文件,内容为:
#!/bin/bash NDK=/home/lx/Downloads/android-ndk-r13b SYSROOT=$NDK/platforms/android-19/arch-arm TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 CPU=arm ARCH=arm PREFIX=$(pwd)/android/$CPU ADDI_CFLAGS="-marm" #配置 ./configure --prefix=$PREFIX --arch=$ARCH --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- --extra-ldflags="$ADDI_LDFLAGS" --sysroot=$SYSROOT --extra-cflags="-Os -fpic $ADDI_CFLAGS" --target-os=linux --enable-cross-compile --enable-gpl --disable-shared --enable-static --disable-doc --disable-debug --enable-small --disable-programs --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver $ADDITIONAL_CONFIGURE_FLAG
#编译
make clean
make -j4
make install
#打包
$TOOLCHAIN/bin/arm-linux-androideabi-ld
-rpath-link=$SYSROOT/usr/lib
-L$SYSROOT/usr/lib
-L$PREFIX/lib
-soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o
$PREFIX/libffmpeg.so
libavcodec/libavcodec.a
libavfilter/libavfilter.a
libswresample/libswresample.a
libavformat/libavformat.a
libavutil/libavutil.a
libswscale/libswscale.a
libavdevice/libavdevice.a
libpostproc/libpostproc.a
-lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker
$TOOLCHAIN/lib/gcc/arm-linux-androideabi/4.9.x/libgcc.a
#strip
$TOOLCHAIN/bin/arm-linux-androideabi-strip $PREFIX/libffmpeg.so