在ubuntu环境下编译 ffmpeg for android

岳杜吟
2023-12-01

参考:Cross Compiling FFmpeg 4.0 for Android

怕链接打不开,先记录一下主要步骤:

环境:ubuntu 1604版本;

Step 1: Downloading the prerequisites:

  1. Android-ndk-r15c — 因为从r16版本编译ffmpeg就会出现问题,因此我们选用r16以前的版本,: https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip
  2. FFmpeg 4.0 — 文档中用的是4.0版本,而最新的版本是4.2版本,我用同样的方法试了4.2版本编译不成功,暂时就用4.0版本吧, https://ffmpeg.org/releases/ffmpeg-4.0.tar.bz2

Step 2: Configuring FFmpeg:

For the rest of the post, I assume that path to your NDK is /path/to/ndkand path to your ffmpeg as /path/to/ffmpeg . Now, move your ffmpeg to the sources folder in NDK. Your new ffmpeg path should look like this, /path/to/ndk/sources/ffmpeg-4.0

我的目录文件如下:

gs@tk:~/04_ffmpeg/android-ndk-r15c$ 
gs@tk:~/04_ffmpeg/android-ndk-r15c$ ls
build         meta       ndk-depends  ndk-stack  platforms  python-packages  shader-tools  source.properties  sysroot
CHANGELOG.md  ndk-build  ndk-gdb      ndk-which  prebuilt   README.md        simpleperf    sources            toolchains
gs@tk:~/04_ffmpeg/android-ndk-r15c$ 
gs@tk:~/04_ffmpeg/android-ndk-r15c$ cd sources/
gs@tk:~/04_ffmpeg/android-ndk-r15c/sources$ 
gs@tk:~/04_ffmpeg/android-ndk-r15c/sources$ ls
android  cxx-stl  ffmpeg-4.0  ffmpeg-4.0.tar.bz2  ffmpeg-4.2  ffmpeg-4.2.tar.gz  third_party
gs@tk:~/04_ffmpeg/android-ndk-r15c/sources$ 
gs@tk:~/04_ffmpeg/android-ndk-r15c/sources$ 

Before we actually configure ffmpeg, FFmpeg is configured by default to have version codes (Ex: libavcodec-10.so) but android can’t recognize such files, so all you need to do is to replace the lines in /path/to/ffmpeg/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)'

with

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

 

Now in ffmpeg folder, create a new Build Script and name it as ffmpeg_android.sh (“Just a random name”). Copy paste the below commands into the file

#!/bin/bash
NDK=/path/to/ndk
SYSROOT=$NDK/platforms/android-19/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
function build_one
{
./configure \
--prefix=$PREFIX \
--enable-shared \
--disable-static \
--disable-doc \
--disable-ffplay \
--disable-ffprobe \
--disable-doc \
--disable-symver \
--enable-protocol=concat \
--enable-protocol=file \
--enable-muxer=mp4 \
--enable-demuxer=mpegts \
--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 all
make -j3
make install
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one

Please remember to replace /path/to/ndk to the NDK Path in your local machine.

Step 3: Running the script file:

Before you run the script file above, make sure that required permissions are given to the file, run the command

chmod a+x ffmpeg_android.sh

Now run the script file as a super user.

sudo ./ffmpeg_android.sh

Have a tea, let your machine do the work for the next 20 minutes.

After some minutes (Depending on your machine’s architecture), you will find this path /path/to/ffmpeg/android/arm

There you can see some folders like “Include”, “lib”..etc .

 

编译

在这基础上实现一个最小demo。

参考:https://github.com/saitestop/FFmpegPlayer-Android

将上面的代码导入到工程,然后将编译好的lib文件和include文件替换,然后运行正常。

 

---------

或者参考:编译FFmpeg4.1.3并移植到Android app中使用(最详细的FFmpeg-Android编译教程)

 

 类似资料: