简体   繁体   English

Android NDK和FFMPEG构建

[英]Android NDK & FFMPEG build

I'm trying to build FFMPEG library for my android application using the NDK. 我正在尝试使用NDK为我的Android应用程序构建FFMPEG库。 I have downloaded the source code from it's website and I think I build it (it's my first try working with NDK and ffmpeg). 我已经从它的网站上下载了源代码,我想我是在构建它(这是我第一次尝试使用NDK和ffmpeg)。 I have created a build.sh file which I'm using to execute it from command line on Mac OS X. But I have a few questions... 我创建了一个build.sh文件,我用它在Mac OS X上从命令行执行它。但我有几个问题......

First things is as I know I need Android.mk file so I can use the builded library in my application,but I don't know how to do that for reasons which I told you above. 首先,我知道我需要Android.mk文件,所以我可以在我的应用程序中使用构建库,但我不知道如何做到这一点,原因我上面告诉你。 After I run my build.sh I get android folder with a few folders and some libraries. 在我运行build.sh我获得了带有一些文件夹和一些库的android文件夹。 So my first question is, how can I build the Android.mk file which I need...and maybe if someone can explain me why I need it. 所以我的第一个问题是,如何构建我需要的Android.mk文件......也许有人可以解释我为什么需要它。

在此输入图像描述

My second question is I need some libraries from ffmpeg to be able to use them from my app, but after the build i can't see them in my source tree. 我的第二个问题是我需要一些来自ffmpeg的库才能在我的应用程序中使用它们,但是在构建之后我无法在源代码树中看到它们。 I know that I have to enable them in my build.sh and I think I did,but I can't see them. 我知道我必须在build.sh启用它们,我想我做了,但我看不到它们。 So any kind of information about my build file will be helpful for me to understand how to configure it. 因此,有关我的构建文件的任何类型的信息将有助于我了解如何配置它。 Here is how it looks like : 这是它的样子:

    #!/bin/bash
######################################################
# Usage:
# put this script in top of FFmpeg source tree
# ./build_android
# It generates binary for following architectures:
# ARMv6 
# ARMv6+VFP 
# ARMv7+VFPv3-d16 (Tegra2) 
# ARMv7+Neon (Cortex-A8)
# Customizing:
# 1. Feel free to change ./configure parameters for more features
# 2. To adapt other ARM variants
# set $CPU and $OPTIMIZE_CFLAGS 
# call build_one
######################################################
NDK=~/Desktop/android-ndk-r5b
PLATFORM=$NDK/platforms/android-8/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86
function build_one
{
./configure \
    --target-os=darwin \
    --prefix=$PREFIX \
    --enable-cross-compile \
    --extra-libs="-lgcc" \
    --arch=arm \
    --cc=$PREBUILT/bin/arm-linux-androideabi-gcc \
    --cross-prefix=$PREBUILT/bin/arm-linux-androideabi- \
    --nm=$PREBUILT/bin/arm-linux-androideabi-nm \
    --sysroot=$PLATFORM \
    --extra-cflags=" -O3 -fpic -DANDROID -DHAVE_SYS_UIO_H=1 -Dipv6mr_interface=ipv6mr_ifindex -fasm -Wno-psabi -fno-short-enums -fno-strict-aliasing -finline-limit=300 $OPTIMIZE_CFLAGS " \
    --disable-doc \
    --disable-ffmpeg \
    --disable-ffplay \
    --disable-ffserver \
    --disable-ffprobe \
    --extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog" \
    --enable-zlib \
    --enable-version3 \
    --enable-nonfree \
    --enable-libmp3lame \
    --enable-libspeex \
    --enable-libtheora \
    --enable-libfaac \
    --enable-libvorbis \
    --enable-libaacplus \
    --prefix=$DIST_DIR \
    $ADDITIONAL_CONFIGURE_FLAG

make clean
make  -j4 install
$PREBUILT/bin/arm-linux-androideabi-ar d libavcodec/libavcodec.a inverse.o
$PREBUILT/bin/arm-linux-androideabi-ld -rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib  -soname libffmpeg.so -shared -nostdlib  -z,noexecstack -Bsymbolic --whole-archive --no-undefined -o $PREFIX/libffmpeg.so libavcodec/libavcodec.a libavformat/libavformat.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog  --warn-once  --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a
}

#arm v6
#CPU=armv6
#OPTIMIZE_CFLAGS="-marm -march=$CPU"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7vfpv3
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
PREFIX=./android/$CPU
ADDITIONAL_CONFIGURE_FLAG=
build_one

#arm v7vfp
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
#PREFIX=./android/$CPU-vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7n
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
#PREFIX=./android/$CPU 
#ADDITIONAL_CONFIGURE_FLAG=--enable-neon
#build_one

#arm v6+vfp
#CPU=armv6
#OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
#PREFIX=./android/${CPU}_vfp 
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

Thanks for any kind of useful information/suggestions/examples and etc. 感谢您提供任何有用的信息/建议/示例等。

I made a tutorial on how to build ffmpeg and x264 for android : zip with build of ffmpeg with x264 我做了一个关于如何为android构建ffmpegx264的教程: 使用x264构建ffmpeg的zip

You can also download the zip file containing the files you need to make an application on android . 您还可以下载包含在android上创建应用程序所需文件的zip文件。

If you want more detailed instruction of how to build ffmpeg and how to use it to build Android apps, please refer to the book Android NDK cookbook: http://www.packtpub.com/android-native-development-kit-cookbook/book 如果您想了解如何构建ffmpeg以及如何使用它来构建Android应用程序的更详细说明,请参阅Android NDK手册: http//www.packtpub.com/android-native-development-kit-cookbook/书

The Bonus Chapter 1 is dedicated to multimedia app development with Android NDK and ffmpeg, and it is available for free at http://www.packtpub.com/sites/default/files/downloads/Developing_Multimedia_Applications_with_NDK.pdf Bonus第1章致力于使用Android NDK和ffmpeg进行多媒体应用程序开发,可从http://www.packtpub.com/sites/default/files/downloads/Developing_Multimedia_Applications_with_NDK.pdf免费获取。

First of all, you should read the documentation for Android NDK and check out some sample apps. 首先,您应该阅读Android NDK的文档并查看一些示例应用程序。 You'll find these details in android_NDK_home/docs and android_NDK_home/samples (where android_NDK_home is the folder name for your android NDK). 你会在android_NDK_home / docs和android_NDK_home / samples中找到这些细节(其中android_NDK_home是你的android NDK的文件夹名称)。

Also, check this link on stackoverflow: Using FFmpeg with Android-NDK 另外,请检查stackoverflow上的此链接: 将FFmpeg与Android-NDK配合使用

For a stept by step tutorial, this is a pretty good one: http://www.roman10.net/how-to-build-ffmpeg-for-android/ 对于一步一步的教程,这是一个非常好的: http ://www.roman10.net/how-to-build-ffmpeg-for-android/

Also, you'll have to install/configure some elements on your Eclipse to enable support for developing applications that are built with the NDK: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/ http://wiki.eclipse.org/Sequoyah/ndk_checklist 此外,您必须在Eclipse上安装/配置一些元素,以支持开发使用NDK构建的应用程序: http//mhandroid.wordpress.com/2011/01/23/using-eclipse-for- android-cc-development / http://wiki.eclipse.org/Sequoyah/ndk_checklist

And a pretty good project that uses the ffmpeg library: https://github.com/havlenapetr/FFMpeg 一个使用ffmpeg库的相当不错的项目: https//github.com/havlenapetr/FFMpeg

ffmpeg uses autoconf as its build system. ffmpeg使用autoconf作为其构建系统。 As of the time of this writing, no one has yet put together a Gradle build package for ffmpeg on Android. 截至本文撰写时,还没有人在Android上为ffmpeg整理Gradle构建包。 As far as I can tell, Gradle doesn't yet support autoconf, which makes it pretty difficult to do so 据我所知,Gradle还不支持autoconf,这使得它很难做到

That said, after trying several of the tutorials that were out there, as well as some of the pre-built packages, we decided to roll our own Android build script wrapper for ffmpeg that includes support for OpenSSL and librtmp . 也就是说,在尝试了几个教程以及一些预构建的软件包后,我们决定为ffmpeg推出自己的Android构建脚本包装器,其中包括对OpenSSLlibrtmp的支持。

You can find it here: https://github.com/cine-io/android-ffmpeg-with-rtmp 你可以在这里找到它: https//github.com/cine-io/android-ffmpeg-with-rtmp

Prerequisite: The Android NDK must be installed 先决条件:必须安装Android NDK

Instructions: 说明:

$ git clone git@github.com:cine-io/android-ffmpeg-with-rtmp.git
$ cd android-ffmpeg-with-rtmp
$ ./build.sh

When the script is finished, the required shared libraries and ffmpeg executable will be put into the build/dist directory. 脚本完成后,所需的共享库和ffmpeg可执行文件将被放入build/dist目录中。 You can include them in your project just like any other native binary. 您可以像其他任何本机二进制文件一样将它们包含在项目中。

To build ffmpeg executable, I use the following Android.mk : 要构建ffmpeg可执行文件,我使用以下Android.mk

SYSROOT-armeabi-v7a      := $(NDK_ROOT)/platforms/android-9/arch-arm/
SYSROOT-arm64-v8a := $(NDK_ROOT)/platforms/android-21/arch-arm64/
SYSROOT-x86      := $(NDK_ROOT)/platforms/android-9/arch-x86/
SYSROOT := $(SYSROOT-$(TARGET_ARCH_ABI))

TOOLCHAIN_TARGET_ARCH-armeabi-v7a := arm
TOOLCHAIN_TARGET_ARCH-arm64-v8a := aarch64
TOOLCHAIN_TARGET_ARCH-x86 := i686
TOOLCHAIN_TARGET_ARCH := $(TOOLCHAIN_TARGET_ARCH-$(TARGET_ARCH_ABI))

TOOLCHAIN_TARGET_ABI-armeabi-v7a := androideabi
TOOLCHAIN_TARGET_ABI-arm64-v8a := android
TOOLCHAIN_TARGET_ABI-x86 := android
TOOLCHAIN_TARGET_ABI := $(TOOLCHAIN_TARGET_ABI-$(TARGET_ARCH_ABI))

TOOLCHAIN_TRIPLET := $(TOOLCHAIN_TARGET_ARCH)-linux-$(TOOLCHAIN_TARGET_ABI)-

TOOLCHAIN_NAME-$(TARGET_ARCH_ABI) := $(TOOLCHAIN_TRIPLET)$(TOOLCHAIN_VERSION)
TOOLCHAIN_NAME-x86 := x86-$(TOOLCHAIN_VERSION)
TOOLCHAIN_NAME := $(TOOLCHAIN_NAME-$(TARGET_ARCH_ABI))

TOOLCHAIN_VERSION_PREFIX := $(TOOLCHAIN_TRIPLET)$(TOOLCHAIN_VERSION)

ARCH_ARM_MODE-armeabi := arm
ARCH_ARM_MODE-armeabi-v7a := thumb
ARCH_ARM_MODE-arm64-v8a := thumb
ARCH_ARM_MODE := $(ARCH_ARM_MODE-$(TARGET_ARCH_ABI))

include $(CLEAR_VARS)
LOCAL_ARM_MODE   := $(ARCH_ARM_MODE)

FFMPEG_PATH      := $(BUILD_DIRECTORY)/ffmpeg

LOCAL_CFLAGS-armeabi-v7a := -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp
LOCAL_CFLAGS-x86 := -m32
LOCAL_CFLAGS-arm64-v8a := -march=armv8-a+simd+fp

LOCAL_CFLAGS := $(LOCAL_CFLAGS-$(TARGET_ARCH_ABI))

LOCAL_LDFLAGS-x86 := -m32 -Wl,-eh-frame-hdr -Wl,-m,elf_i386 -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now

LOCAL_LDFLAGS := $(LOCAL_LDFLAGS-$(TARGET_ARCH_ABI))

LOCAL_CFLAGS += -fPIE
LOCAL_LDFLAGS += -fPIE -pie

FFMPEG_CONFIG_OPTS := 

FFMPEG_CONFIG_OPTS += --target-os=linux
FFMPEG_CONFIG_OPTS-$(TARGET_ARCH_ABI) := --cross-prefix=$(TOOLCHAIN_PREFIX)
FFMPEG_CONFIG_OPTS-$(TARGET_ARCH_ABI) += --enable-cross-compile
FFMPEG_CONFIG_OPTS-$(TARGET_ARCH_ABI) += --sysroot=$(SYSROOT)
FFMPEG_CONFIG_OPTS += $(FFMPEG_CONFIG_OPTS-$(TARGET_ARCH_ABI))

## disable
FFMPEG_CONFIG_OPTS += --disable-doc
FFMPEG_CONFIG_OPTS += --disable-shared
FFMPEG_CONFIG_OPTS += --disable-avdevice
FFMPEG_CONFIG_OPTS += --disable-postproc
FFMPEG_CONFIG_OPTS += --disable-network
FFMPEG_CONFIG_OPTS += --disable-iconv
FFMPEG_CONFIG_OPTS += --disable-zlib
FFMPEG_CONFIG_OPTS += --disable-ffplay
FFMPEG_CONFIG_OPTS += --disable-ffprobe
FFMPEG_CONFIG_OPTS += --disable-ffserver
FFMPEG_CONFIG_OPTS += --disable-swscale-alpha
## enable
FFMPEG_CONFIG_OPTS += --enable-pthreads
FFMPEG_CONFIG_OPTS += --enable-small

FFMPEG_CONFIG_OPTS += --extra-cflags="$(LOCAL_CFLAGS)"
FFMPEG_CONFIG_OPTS += --extra-cxxflags="$(LOCAL_CXXFLAGS)"
FFMPEG_CONFIG_OPTS += --extra-ldflags="$(LOCAL_LDFLAGS)"

FFMPEG_CONFIG_OPTS += --enable-runtime-cpudetect

ARMBITS-$(TARGET_ARCH_ABI) :=
ARMBITS-arm64-v8a := 64
ARMBITS := $(ARMBITS-$(TARGET_ARCH_ABI))

FFMPEG_CONFIG_OPTS-$(TARGET_ARCH_ABI) :=

FFMPEG_CONFIG_OPTS-armeabi := --enable-armv5te
FFMPEG_CONFIG_OPTS-armeabi += --enable-inline-asm
FFMPEG_CONFIG_OPTS-armeabi += --arch=arm$(ARMBITS)

FFMPEG_CONFIG_OPTS-armeabi-v7a := $(FFMPEG_CONFIG_OPTS-armeabi)
FFMPEG_CONFIG_OPTS-armeabi-v7a += --enable-armv6
FFMPEG_CONFIG_OPTS-armeabi-v7a += --enable-armv6t2
FFMPEG_CONFIG_OPTS-armeabi-v7a += --enable-vfp
FFMPEG_CONFIG_OPTS-armeabi-v7a += --enable-neon
FFMPEG_CONFIG_OPTS-armeabi-v7a += --enable-thumb
FFMPEG_CONFIG_OPTS-armeabi-v7a += --enable-fast-unaligned

FFMPEG_CONFIG_OPTS-arm64-v8a := $(FFMPEG_CONFIG_OPTS-armeabi-v7a)

FFMPEG_CONFIG_OPTS-x86 := --enable-yasm
FFMPEG_CONFIG_OPTS-x86 += --arch=x86

FFMPEG_CONFIG_OPTS += $(FFMPEG_CONFIG_OPTS-$(TARGET_ARCH_ABI))

LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := $(abspath $(TARGET_OBJS))/ffmpeg/lib...ffmpeg...so # see http://stackoverflow.com/a/17384650/192373

clean: $(TARGET_OBJS)/ffmpeg/clean

$(TARGET_OBJS)/ffmpeg/clean: PRIVATE_ABI := $(FFMPEG_TARGET_ABI)
$(TARGET_OBJS)/ffmpeg/clean:
    $(call host-echo-build-step,$(PRIVATE_ABI),Clean) "ffmpeg [$(PRIVATE_ABI)]"
    $(hide) $(call host-rmdir,$(@D))

$(shell $(call host-mkdir,$(dir $(LOCAL_SRC_FILES))))
$(shell $(HOST_ECHO) >$(LOCAL_SRC_FILES))
.PHONY: $(LOCAL_SRC_FILES)
$(LOCAL_SRC_FILES): $(TARGET_OBJS)/ffmpeg/ffmpeg
    $(hide) $(call host-mv,$<,$@)

include $(PREBUILT_SHARED_LIBRARY)

$(TARGET_OBJS)/ffmpeg/Makefile: FFMPEG_CONFIG_OPTS := $(FFMPEG_CONFIG_OPTS)
$(TARGET_OBJS)/ffmpeg/Makefile:
    $(hide) $(call host-mkdir,$(@D))
    $(hide) cd $(@D) && $(abspath $(FFMPEG_PATH))/configure $(FFMPEG_CONFIG_OPTS)

$(TARGET_OBJS)/ffmpeg/ffmpeg: FFMPEG_CONFIG_OPTS := using toolchain:$(NDK_ROOT)/toolchains/$(TOOLCHAIN_NAME) compiler:$(TOOLCHAIN_COMPILER) triplet:$(TOOLCHAIN_TRIPLET)
$(TARGET_OBJS)/ffmpeg/ffmpeg: $(TARGET_OBJS)/ffmpeg/Makefile
    @echo $(FFMPEG_CONFIG_OPTS)
    $(hide) $(MAKE) --no-print-directory -C $(@D)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM