1. 環(huán)境 ubuntu 我的是13.10 ndk r9 64位
2. 下載ffmpeg http://www.ffmpeg.org/download.html 找到
FFmpeg 2.1.3 "Fourier"
2.1.3 was released on 2014-01-15. It is the latest stable FFmpeg release from the 2.1 release branch, which was cut from master on 2013-10-28. Amongst lots of other changes, it includes all changes from ffmpeg-mt, libav master of 2013-10-27, libav 9 as of 2013-10-27.
It includes the following library versions:
libavutil 52. 48.101 libavcodec 55. 39.101 libavformat 55. 19.104 libavdevice 55. 5.100 libavfilter 3. 90.100 libswscale 2. 5.101 libswresample 0. 17.104 libpostproc 52. 3.100
Download bzip2 tarball 下載
下載之后 解壓到 $NDK/sources 文件夾下面,這是因?yàn)榫幾g快捷以及方便使用
3. 找到 ffmpeg-xxx/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)'
上面幾行會(huì)導(dǎo)致libavcodec.so.<version> (e.g. libavcodec.so.55),不會(huì)被android承認(rèn) ,修改為;
- 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. 復(fù)制下面的代碼到文本中 并命名為 build_android.sh,注意這個(gè)文件應(yīng)該保存到 ffmpeg-xxx/configure 同一個(gè)文件夾下
- #!/bin/bash
- NDK=$HOME/Desktop/adt/android-ndk-r9
- SYSROOT=$NDK/platforms/android-9/arch-arm/
- TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-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
注意修改 NDK=$HOME/Desktop/adt/android-ndk-r9 這行
5. 執(zhí)行
- sudo chmod +x build_android.sh
-
- ./build_android.sh
6.輸出 $NDK/sources/ffmpeg-xxx/android, arm/lib 和arm/include
注意 arm/lib包含了兩個(gè) library files (e.g.: libavcodec-55.so) 和 symbolic links (e.g.: libavcodec.so) 我們需要?jiǎng)h掉 symbolic links .
7. 讓ffmpeg Libraries用在我們的工程下
為了使ffmpeg libraries 可以作為 reusable modules, 需要添加一個(gè)文件 $NDK/sources/ffmpeg-xxx/android/arm/Android.mk 使用如下內(nèi)容,
- include $(CLEAR_VARS)
- LOCAL_MODULE:= libavcodec
- LOCAL_SRC_FILES:= lib/libavcodec-55.so
- LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
- include $(PREBUILT_SHARED_LIBRARY)
-
- include $(CLEAR_VARS)
- LOCAL_MODULE:= libavformat
- LOCAL_SRC_FILES:= lib/libavformat-55.so
- LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
- include $(PREBUILT_SHARED_LIBRARY)
-
- include $(CLEAR_VARS)
- LOCAL_MODULE:= libswscale
- LOCAL_SRC_FILES:= lib/libswscale-2.so
- LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
- include $(PREBUILT_SHARED_LIBRARY)
-
- include $(CLEAR_VARS)
- LOCAL_MODULE:= libavutil
- LOCAL_SRC_FILES:= lib/libavutil-52.so
- LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
- include $(PREBUILT_SHARED_LIBRARY)
-
- include $(CLEAR_VARS)
- LOCAL_MODULE:= libavfilter
- LOCAL_SRC_FILES:= lib/libavfilter-3.so
- LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
- include $(PREBUILT_SHARED_LIBRARY)
-
- include $(CLEAR_VARS)
- LOCAL_MODULE:= libwsresample
- LOCAL_SRC_FILES:= lib/libswresample-0.so
- LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
- include $(PREBUILT_SHARED_LIBRARY)
這就是為什么我們把源碼放在source文件夾下,ndk會(huì)自動(dòng)尋找
8.使用
使用 libraries在Android project’s jni/Android.mk file,
- LOCAL_PATH := $(call my-dir)
-
- include $(CLEAR_VARS)
-
- LOCAL_MODULE := tutorial03
- LOCAL_SRC_FILES := tutorial03.c
- LOCAL_LDLIBS := -llog -ljnigraphics -lz -landroid
- LOCAL_SHARED_LIBRARIES := libavformat libavcodec libswscale libavutil
-
- include $(BUILD_SHARED_LIBRARY)
- $(call import-module,ffmpeg-2.0.1/android/arm)
這里你就會(huì)發(fā)現(xiàn) 我們用到了source中調(diào)用了 import-module 使用相對(duì)路徑去調(diào)用。