Linux下编译Android平台的ffmpeg库

在Linux平台下编译要方便些,我这里使用的环境是Ubutunu。

首先你需要Android-NDK 和ffmpeg源码,不同的版本里的文件结构有所差别,编译环境也有所不同。我这里使用的是以下两个版本:

  • android-ndk-r9-linux-x86.tar.bz2
  • ffmpeg-2.2.15.tar.bz2

1. 解压 android-ndk-r9-linux-x86.tar.bz2

第一步:下载 https://dl.google.com/android/ndk/android-ndk-r9-linux-x86.tar.bz2
cd ~
tar xvf android-ndk-r9-linux-x86.tar.bz2

我的解压路径为 ~/android-ndk-r9/

2. 解压ffmpeg

cd ~
tar xvf ffmpeg-2.2.15.tar.bz2
~/ffmpeg-2.2.15/

3. 在~/ffmpeg-2.2.15/ 下创建build_android.sh

#!/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=~/android-ndk-r9
PLATFORM=$NDK/platforms/android-9/arch-arm/
PREBUILT=$NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86
function build_one
{
    ./configure --target-os=linux \
        --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-shared \
        --enable-static \
        --extra-ldflags="-Wl,-rpath-link=$PLATFORM/usr/lib -L$PLATFORM/usr/lib -nostdlib -lc -lm -ldl -llog" \
        --disable-everything \
        --enable-demuxer=mov \
        --enable-demuxer=h264 \
        --disable-ffplay \
        --enable-protocol=file \
        --enable-avformat \
        --enable-avcodec \
        --enable-decoder=rawvideo \
        --enable-decoder=mjpeg \
        --enable-decoder=h263 \
        --enable-decoder=mpeg4 \
        --enable-decoder=h264 \
        --enable-parser=h264 \
        --disable-network \
        --enable-zlib \
        --enable-avfilter \
        --disable-avdevice \
        $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 libavfilter/libavfilter.a libavutil/libavutil.a libswscale/libswscale.a -lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker $PREBUILT/lib/gcc/arm-linux-androideabi/4.6/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

chmod +x build_android.sh
./build_android.sh
过一会,就能生成一系列的静态库(libavcode.a 等)和一个动态库libffmpeg.so

在QT中就直接调用libffmpeg.so就可以了。

INCLUDEPATH +=  $$PWD/ffmpeg/include
LIBS += -L$$PWD/ffmpeg/android -lffmpeg

本文中的build_android.sh参考 http://www.roman10.net/how-to-build-ffmpeg-for-android/

根据版本的差异,对build_android.sh作了相应的修改。

你可能感兴趣的:(Android,ffmpeg,Linux,C)