How to use FFmpeg on Android

Asked 2 years ago, Updated 2 years ago, 115 views

We are developing a video editing application for Android 5.0 and above, and for this purpose, we are trying to incorporate FFMPEG into Android in the following environment:

  • Android Studio 2.3.3
  • FFmpeg 3.3.2

How to build ffmpeg for Android

Using the above site as a reference, I compile FFmpeg and incorporate Android.mk into the Android project, but it is not working well.

I've been looking at various sites, but I don't know the correct shape because of the difference between Eclipse and Android Studio in the environment, and the difference in specifications related to the Android Studio version.

First mentioned

  • Android 5.0
  • Android Studio 2.3.3
  • FFmpeg 3.3.2

If anyone knows what kind of work is needed and what kind of procedures are required to incorporate FFmpeg under the above conditions, could you tell me?

Error Occurring

ffmpeg.c:11:error:undefined reference to 'avcodec_version'  
clang++.exe:error:linker command failed with exit code1 (use-v to see invocation)  

C source code

#include<string.h>
# include <jni.h>
# include <libavcodec/avcodec.h>

jstring Java_<Package name >_TopActivity_stringFromJNI(JNIInv*env,jobject thiz){
    // avcodec_register_all();
    int test = avcodec_version();

    return(*env)->NewStringUTF(env, "Hello JNI!");
}

android ffmpeg

2022-09-30 17:12

1 Answers

http://writingminds.github.io/ffmpeg-android/
Using the above site as a reference, I was able to make the library available for now.

./android-ndk-r14b
./ WritingMinds-ffmpeg-android-8809612
We placed each folder in this , and edited the contents of each file as follows:

■./ WritingMinds-ffmpeg-android-8809612/android_build.sh

#!/bin/bash

. . settings.sh

BASEDIR = $(pwd)
TOOLCHAIN_PREFIX=${BASEDIR}/toolchain-android
# Applying required patches
patch-p0-N --dry-run --silent-fontconfig/src/fcxml.c<android_donot_use_lconv.patch1>/dev/null
if [$?-eq0]; then
  patch-p0-fontconfig/src/fcxml.c<android_donot_use_lconv.patch
fi

for in "${SUPPORTED_ARCHITECTURES[@]}"
do
  rm-rf${TOOLCHAIN_PREFIX}
  # $1 = architecture
  # $2 = base directory
  # $3 = pass1 if you want to export default compiler environment variables
  ./libpng_build.sh$i$BASEDIR1||exit1
  ./expat_build.sh$i$BASEDIR1||exit1
  ./ffmpeg_build.sh $i$BASEDIR0||exit1
done

rm-rf${TOOLCHAIN_PREFIX}

■./ WritingMinds-ffmpeg-android-8809612/abi_settings.sh

#!/bin/bash

. . settings.sh

BASEDIR = $2

case $1 in
  armeabi-v7a)
    NDK_ABI = 'arm'
    NDK_TOOLCHAIN_ABI = 'arm-linux-androidabi'
    NDK_CROSS_PREFIX="${NDK_TOOLCHAIN_ABI}"
  ;;
  x86)
    NDK_ABI = 'x86'
    NDK_TOOLCHAIN_ABI='x86'
    NDK_CROSS_PREFIX="i686-linux-android"
    CFLAGS="$CFLAGS-march=i686"
  ;;
esac

TOOLCHAIN_PREFIX=${BASEDIR}/toolchain-android
if [!-d "$TOOLCHAIN_PREFIX"]; then
  ${ANDROID_NDK_ROOT_PATH}/build/tools/make-standalone-toolchain.sh --toolchain=${NDK_TOOLCHAIN_ABI}-${NDK_TOOLCHAIN_ABI_VERSION}--platform=android-${ANDROID_API_VERSION}--install-dir=${TOOLCHAIN_PREFIX}
fi
CROSS_PREFIX=${TOOLCHAIN_PREFIX}/bin/${NDK_CROSS_PREFIX}-
NDK_SYSROOT=${TOOLCHAIN_PREFIX}/sysroot

export PKG_CONFIG_LIBDIR="${TOOLCHAIN_PREFIX}/lib/pkgconfig"

if [$3==1]; then
  export CC = "${CROSS_PREFIX}gcc --sysroot=${NDK_SYSROOT}"
  export LD = "${CROSS_PREFIX}ld"
  export RANLIB = "${CROSS_PREFIX}ranlib"
  export STRIP = "${CROSS_PREFIX} strip"
  export READELF="${CROSS_PREFIX}readelf"
  export OBJDUMP="${CROSS_PREFIX}objdump"
  export ADDR2LINE="${CROSS_PREFIX}addr2line"
  export AR = "${CROSS_PREFIX}ar"
  export AS = "${CROSS_PREFIX} as"
  export CXX="${CROSS_PREFIX}g++"
  export OBJCOPY="${CROSS_PREFIX}objcopy"
  exportELFEDIT="${CROSS_PREFIX}elfedit"
  export CPP = "${CROSS_PREFIX}cpp"
  export DWP="${CROSS_PREFIX}dwp"
  exportGCONV="${CROSS_PREFIX}gconv"
  export GDP = "${CROSS_PREFIX}gdb"
  export GPROF="${CROSS_PREFIX}gprof"
  export NM = "${CROSS_PREFIX} nm"
  export SIZE="${CROSS_PREFIX}size"
  export STRINGS="${CROSS_PREFIX} strings"
fi

■./ WritingMinds-ffmpeg-android-8809612/settings.sh

#!/bin/bash

SUPPORTED_ARCHITECTURES= (armeabi-v7ax86)
ANDROID_NDK_ROOT_PATH=${ANDROID_NDK}
if [[-z"$ANDROID_NDK_ROOT_PATH"]];then
  echo "You need to set ANDROID_NDK environment variable, please check instructions"
  exit
fi
ANDROID_API_VERSION=21
NDK_TOOLCHAIN_ABI_VERSION=4.9

NUMBER_OF_CORES=$(nproc)
HOST_UNAME=$(uname-m)
TARGET_OS=linux

CFLAGS='-U_FORTIFY_SOURCE-D_FORTIFY_SOURCE=2-fno-strict-overflow-fstack-protector-all'
LDFLAGS = '-Wl, -z, relro-Wl, -z, now-pie'

FFMPEG_PKG_CONFIG="$(pwd)/ffmpeg-pkg-config"

Then register the following as environment variables
 ANDROID_NDK=/<~Path from root~>/android-ndk-r14b

/WritingMinds-ffmpeg-android-8809612 and run Android_build.sh
 ./android_build.sh

A .so library file is created under the folder below.
 ./ WritingMinds-ffmpeg-android-8809612/build

I was able to confirm at least that FFmpeg functionality is available on Android side using this .so file.
There is a new question about how to implement the function, but it worked well in terms of creating a library.
I would like to ask you a separate question about how to implement it in the future.


2022-09-30 17:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.