附(2018-01-06): 有一個將x264及lame等庫集成進去了且基於android的ffmpeg的編譯方法,地址參見: https://github.com/writingminds/ffmpeg-androidhtml
原文轉自:http://abitno.me/compile-ffmpeg-android-ndkmysql
Android內置的編解碼器實在太少,因而咱們須要FFmpeg。Android提供了NDK,爲咱們使用FFmpeg這種C語言代碼提供了方便。linux
不過爲了用NDK編譯FFmpeg,還真的花費了很多時間,也獲得了不少人的幫助,最應該謝謝havlenapetr。我以爲我如今這些方法算是比較簡潔的了--android
下面就儘可能詳細的說一下我是怎麼在項目中使用FFmpeg的,可是基於我混亂的表達能力,有不明白的就問我。git
你得了解JNI和Android NDK的基本用法,若以爲個人文章還不錯,能夠看以前寫的JNI簡單入門和Android NDK入門程序員
首先建立一個標準的Android項目vPlayergithub
android create project -n vPlayer -t 8 -p vPlayer -k me.abitno.vplayer -a PlayerView
而後在vPlayer目錄裏sql
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設置正確。另外裏面有些參數你也能夠自行調整,我主要是爲了配置一個播放器而這樣設置的。shell
#!/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開始configureruby
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文件,並將下面的文字部分寫入文件:
固然,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-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