前言
笔者尝试在win10下,编译android版本的ffmpeg,前面做了不少准备,终于可以编译了
一、安装MSYS2
下载地址:MSYS2
安装完成后执行如下命令
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
?安装所需要的工具链
二、下载android NDK
下载地址:不受支持的 NDK 下载 ?|? Android NDK ?|? Android Developers
笔者没有单独下载,因为在配置Qt for android时,已经自动下载完成了
三、下载ffmpeg源码
下载地址:Download FFmpeg
下载完成后,解压到一个没有中文、空格的目录中
四、编译源码
1.编辑configure文件
在ffmpeg的文件下找到configure,用编辑器打开
(1)找到?--cross-prefix=PREFIX use PREFIX for compilation tools [$cross_prefix] 在其下面加一行
--corss-prefix-clang=PREFIX use PREFIX for compilation clang tools [$cross_prefix]
?(2)找到?CMDLINE_SET="..... corss_prefix
?在其下面添加一行:
cross_prefix_clang
(3)找到
set_default target_os
if test "$target_os" = android; then
cc_default="clang"
fi
ar_default="${cross_prefix}${ar_default}"
cc_default="${cross_prefix}${cc_default}"
cxx_default="${cross_prefix}${cxx_default}"
nm_default="${cross_prefix}${nm_default}"
pkg_config_default="${cross_prefix}${pkg_config_default}"
替换成:
set_default target_os
if test "$target_os" = android; then
cc_default="clang"
cxx_default="clang++"
fi
ar_default="${cross_prefix}${ar_default}"
cc_default="${cross_prefix_clang}${cc_default}"
cxx_default="${cross_prefix_clang}${cxx_default}"
nm_default="${cross_prefix}${nm_default}"
pkg_config_default="${cross_prefix}${pkg_config_default}"
(4) 找到
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)'
2.编写编译脚本,如下所示
#!/bin/bash
#NDK目录
NDK=E:/software/android/commandlinetools-win-6609375_latest/ndk/22.1.7171670
HOST_TAG=windows-x86_64
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/$HOST_TAG
API=24
BASEPATH=$(cd `dirname $0`; pwd)
function build_android
{
echo "Compiling FFmpeg for $CPU"
./configure \
--prefix=$PREFIX \
--disable-neon \
--disable-hwaccels \
--disable-postproc \
--enable-shared \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-avdevice \
--disable-doc \
--disable-symver \
--extra-cflags="-I$X264_INCLUDE -I$FDK_INCLUDE -I$MP3_INCLUDE -I$SYSROOT/usr/include -Os -fpic $OPTIMIZE_CFLAGS" \
--extra-ldflags="-L$FDK_LIB -L$X264_LIB -L$MP3_LIB -L$SYSROOT/usr/lib" \
--enable-nonfree \
--enable-gpl \
--enable-libfdk-aac \
--enable-libx264 \
--enable-libmp3lame \
--enable-cross-compile \
--cross-prefix=$CROSS_PREFIX \
--target-os=android \
--arch=$ARCH \
--cpu=$CPU \
--cc=$CC \
--cxx=$CXX \
--sysroot=$SYSROOT \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
echo "The Compilation of FFmpeg for $CPU is completed"
}
#armv8-a
ARCH=arm64
CPU=armv8-a
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/$HOST_TAG/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$(pwd)/install/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
#第三方库
FDK_INCLUDE=E:/software/ffmpeg/fdk-aac_android_arm64-v8a/build_arm64-v8a/install/include
FDK_LIB=E:/software/ffmpeg/fdk-aac_android_arm64-v8a/build_arm64-v8a/install/lib
X264_INCLUDE=E:/software/ffmpeg/x264_android_arm64-v8a/install/armv8-a/include
X264_LIB=E:/software/ffmpeg/x264_android_arm64-v8a/install/armv8-a/lib
MP3_INCLUDE=E:/software/ffmpeg/lame-3.100_android_arm64-v8a/install_arm64-v8a/armv8-a/include
MP3_LIB=E:/software/ffmpeg/lame-3.100_android_arm64-v8a/install_arm64-v8a/armv8-a/lib
#X265_INCLUDE=E:/software/ffmpeg/x265_git/build/android_aarch64/install/include
#X265_LIB=E:/software/ffmpeg/x265_git/build/android_aarch64/install/lib
export PKG_CONFIG_PATH=E:/software/ffmpeg/x265_git/build/android_aarch64/install/lib/pkgconfig:$PKG_CONFIG_PATH
#export PKG_CONFIG_PATH=/E/software/ffmpeg/x265_git/build/android_aarch64/install/lib/pkgconfig:/E/software/ffmpeg/x264_android_arm64-v8a/install/armv8-a/lib/pkgconfig:$PKG_CONFIG_PATH
build_android
#armv7-a
ARCH=arm
CPU=armv7-a
CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/$HOST_TAG/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-
PREFIX=$(pwd)/install/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
#build_android
#x86
ARCH=x86
CPU=x86
CC=$TOOLCHAIN/bin/i686-linux-android$API-clang
CXX=$TOOLCHAIN/bin/i686-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/$HOST_TAG/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/i686-linux-android-
PREFIX=$(pwd)/install/$CPU
OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
#build_android
#x86_64
ARCH=x86_64
CPU=x86-64
CC=$TOOLCHAIN/bin/x86_64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/x86_64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/$HOST_TAG/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/x86_64-linux-android-
PREFIX=$(pwd)/install/$CPU
OPTIMIZE_CFLAGS="-march=$CPU -msse4.2 -mpopcnt -m64 -mtune=intel"
#build_android
?读者需要自己替换对应目录
笔者使用的api是24
将完成的脚本保存为build.sh,放到源码目录
3.编译
打开mingw64,跳转到源码目录中,运行脚本
sh build.sh
笔者大约等待了近一个小时,?得到编译后的结果
已编译完成的动、静态库
后记
1.笔者只编译了arm64-v8a的版本,其他版本笔者没有尝试,感兴趣的小伙伴可以自己尝试下
2.笔者原来想把x265也编译上,但添加后,提示error "unsatisfied condition: X264_MPEG2"
查看ffbuild目录,提示ERROR: x265 not found using pkg-config
笔者尝试安装pkg-config,发现与当前的环境冲突,故没有添加x265
|