资讯专栏INFORMATION COLUMN

How to Build ffmpeg with NDK r9

thekingisalwaysluc / 1015人阅读

摘要:

This is a updated post for a previous post, where we built ffmpeg 0.8 with Android NDK r5 and r6. This post will give instructions of how to build ffmpeg 2.0.1 with Android NDK r9.

Download Android NDK

The latest version of Android NDK can be downloaded at Android NDK website. At the time of writing, the newest version is NDK r9. Note that the website provides both current and legacy toolchains. We only need the current toolchain to compile ffmpeg.

After download NDK, simply decompress the archive. Note that we’ll use $NDK to represent the root path of the decompressed NDK.

Download ffmpeg source code

FFMPEG source code can be downloaded from the ffmpeg website. The latest stable release is 2.0.1. Download the source code and decompress it to $NDK/sources folder. We’ll discuss about the reason for doing this later.

Update configure file

Open ffmpeg-2.0.1/configure file with a text editor, and locate the following lines.

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

This cause ffmpeg shared libraries to be compiled to libavcodec.so. (e.g. libavcodec.so.55), which is not compatible with Android build system. Therefore we’ll need to replace the above lines with the following lines.

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

Build ffmpeg

Copy the following text to a text editor and save it as build_android.sh.

#!/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

We disabled static library and enabled shared library. Note that the build script is not optimized for a particular CPU. One should refer to ffmpeg documentation for detailed information about available configure options.

Once the file is saved, make sure the script is executable by the command below,

sudo chmod +x build_android.sh

Then execute the script by the command,

./build_android.sh

Build Output

The build can take a while to finish depending on your computer speed. Once it’s done, you should be able to find a folder $NDK/sources/ffmpeg-2.0.1/android, which contains arm/lib and arm/include folders.

The arm/lib folder contains the shared libraries, while arm/include folder contains the header files for libavcodec, libavformat, libavfilter, libavutil, libswscale etc.

Note that the arm/lib folder contains both the library files (e.g.: libavcodec-55.so) and symbolic links (e.g.: libavcodec.so) to them. We can remove the symbolic links to avoid confusion.

Make ffmpeg Libraries available for Your Projects

Now we’ve compiled the ffmpeg libraries and ready to use them. Android NDK allows us to reuse a compiled module through the import-module build command.

The reason we built our ffmpeg source code under "$NDK/sources folder" is that NDK build system will search for directories under this path for external modules automatically. To declare the ffmpeg libraries as reusable modules, we’ll need to add a file named $NDK/sources/ffmpeg-2.0.1/android/arm/Android.mk with the following content,

LOCAL_PATH:= $(call my-dir)

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:= libswresample
LOCAL_SRC_FILES:= lib/libswresample-0.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

Below is an example of how we can use the libraries in a 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)

Note that we called import-module with the relative path to $NDK/sources for the build system to locate the reusable ffmpeg libraries.

For real examples to how to use the ffmpeg libraries in Android app, please refer to my github repo of android-ffmpeg-tutorial.

from http://www.roman10.net/how-to-build-ffmpeg-with-ndk-r9/comment-page-2/

======================================

special notice

me use ndkr9 + ffmpeg version 2.2(last version),,,ffmpeg compiled successfully,but if you think add to your android project,,need to notice,,,ffmpeg of last version compiled share library one more dependent.
above author"s project"s/Android.mk one more two share library
them is libavfilter libswresample

in your project java,when load these share library,,,need notice,,,the order of their,,as follows:

static{
        System.loadLibrary("avutil-52");
        System.loadLibrary("swresample-0");
        System.loadLibrary("avcodec-55");
        System.loadLibrary("avformat-55");
        System.loadLibrary("swscale-2");
        System.loadLibrary("avfilter-4");
        System.loadLibrary("ffmpeg-jni");
    }

===========================================

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/12116.html

相关文章

  • How to Build ffmpeg with NDK r9

    摘要: This is a updated post for a previous post, where we built ffmpeg 0.8 with Android NDK r5 and r6. This post will give instructions of how to build ffmpeg 2.0.1 with Android NDK r9. Download A...

    hearaway 评论0 收藏0
  • How to build ffmpeg with hardware accelerated code

    摘要:In order to build a complete ffmpeg with hardware acceleration for Intel platform (XXX lake + Atom), we need a complete Android x86 build, the cross-compilation doesnt work well with NDK or anythin...

    番茄西红柿 评论0 收藏0
  • 一步一步教你编译FFmpeg(一)

    摘要:目前正在学习,下边将分享一下编译的步骤。编译环境获取源码从官网下载最新版解压开始编译安装以及,并配置环境变量修改配置文件将修改为这样编译出来的命名才符合的使用。 目前正在学习FFmpeg,下边将分享一下编译FFmpeg 的步骤。 1 编译环境 ubuntu 14.04LTS x86_64android-ndk64-r10-linux-x86_64ffmpeg 3.1.2 2 获取源码 ...

    Labradors 评论0 收藏0
  • 最纯粹的Android直播技术实战01-FFmpeg的编译与运行

    摘要:所以在这个系列中,就会使用来还原最纯粹的直播技术,这样就可以知道现在打包好的直播技术是怎样实现的。这里,我们也是编译成静态库。 最纯粹的Android直播技术实战01-FFmpeg的编译与运行 最新实战教程,Android自动化刷量、作弊与防作弊,案例:刷友盟统计、批量注册苹果帐号 这个系列的文章将会研究最纯粹的Android直播的实现,而且不是用现在的集成SDK来达到直播的技...

    honhon 评论0 收藏0
  • 最纯粹的Android直播技术实战01-FFmpeg的编译与运行

    摘要:所以在这个系列中,就会使用来还原最纯粹的直播技术,这样就可以知道现在打包好的直播技术是怎样实现的。这里,我们也是编译成静态库。 最纯粹的Android直播技术实战01-FFmpeg的编译与运行 最新实战教程,Android自动化刷量、作弊与防作弊,案例:刷友盟统计、批量注册苹果帐号 这个系列的文章将会研究最纯粹的Android直播的实现,而且不是用现在的集成SDK来达到直播的技...

    coolpail 评论0 收藏0

发表评论

0条评论

thekingisalwaysluc

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<