0

I try to build and use FFmpeg Library on Android Studio.

My Environments

Windows 7 64bit, Cygwin64, Android Studtio 2.1.2, FFmpeg 3.1.1, Android NDK r12b

Ref. Page

http://www.roman10.net/2013/08/18/how-to-build-ffmpeg-with-ndk-r9/ I follow this page.

I succeed FFmpeg library build using ./build_android.sh on Cygwin

so I try to use FFmpeg Library's on Android Studio.

Setting Android Studio for use FFmpeg

my project_path/jni/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := ffmpeg_trim
LOCAL_SRC_FILES := ffmpeg_trim.c

LOCAL_LDLIBS := -lz -ljnigraphics

LOCAL_SHARED_LIBRARIES := libavformat libavcodec libavutil libswscale libswresample

LOCAL_EXPORT_C_INCLUDES := ($LOCAL_PATH)/include

include $(BUILD_SHARED_LIBRARY)

$(call import-module,ffmpeg-3.1.1/android/arm)

my jni/Application.mk

APP_ABI := armeabi-v7a

my project_path/jni/ffmpeg_trim.c

#include "my_package_util_TrimUtil.h"
#include <libavformat/avformat.h>

JNIEXPORT void JNICALL
Java_my_package_videoeditor_util_TrimUtil_registerAll(JNIEnv *env, jobject obj)
{
    av_register_all();
}

my project_path/jni/ffmpeg_trim.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class my_package_videoeditor_util_TrimUtil */

#ifndef _Included_my_package_videoeditor_util_TrimUtil
#define _Included_my_package_videoeditor_util_TrimUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     my_package_videoeditor_util_TrimUtil
 * Method:    registerAll
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_my_package_util_TrimUtil_registerAll
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

my project_path/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "my.package.videoeditor"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        mp4parser {
            applicationIdSuffix ".mp4parser"
        }
        ffmpeg {
            applicationIdSuffix ".ffmpeg"
            ndk {
                moduleName "ffmpeg_trim"
                cFlags "-std=c99 -I${project.buildDir}/../src/ffmpeg/jni/include"
            }

            sourceSets.main {
                jni.srcDirs = []
                jniLibs.srcDir 'src/ffmpeg/libs'
            }
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.googlecode.mp4parser:isoparser:1.1.21'
}

my project_path/jni folder

enter image description here

I build my Project.

But, failed and print this message.

enter image description here

I can't solve my problem. How to build and use FFmpeg Library on Android Studio.

Don't tell me other libraries using FFmpeg.

Community
  • 1
  • 1
Jake Park
  • 40
  • 1
  • 8
  • how you build ndk? i got errors like Android NDK: C:/Users/eheuristic/Downloads/lab/android-ffmpeg-tutorial01/app/src/main//jni/Android.mk: Cannot find module with tag 'ffmpeg-3.1.1/android/arm' in import path @Jake Park – Jaydeep Devda Jul 19 '16 at 06:47
  • http://stackoverflow.com/questions/37935468/how-to-configure-ffmpeg-library-with-ndk-r12-using-windows-7-64-bit-operating-sy/38258962#38258962 this link is help for you @JaydeepPatel – Jake Park Jul 19 '16 at 07:03

1 Answers1

1

I've never built ffmpeg in Android Studio itself, I've used the native build for that which install the libs and the headers in their own directories ready for use.

Here's my build script which enables gpl so I could access the yadif deinterlace filter:

#!/bin/bash

NDK=$HOME/Android/Sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-19/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
CPREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-

function build_it {
./configure \
    --prefix=$PREFIX    \
    --disable-static    \
    --enable-shared     \
    --disable-doc       \
    --disable-ffmpeg    \
    --disable-ffplay    \
    --disable-ffprobe   \
    --disable-ffserver  \
    --disable-avdevice  \
    --disable-doc       \
    --disable-symver    \
    --cross-prefix=$CPREFIX \
    --target-os=linux   \
    --arch=arm      \
    --enable-cross-compile  \
    --enable-gpl        \
    --sysroot=$SYSROOT  \
    --extra-cflags="-Os -fpic $ADDI_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    $ADDITIONAL_CONFIGURE_FLAG
    make clean
    make
    make install
}

CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"

build_it

My gradle script in Android Studio might help get you up and running:

apply plugin: 'com.android.model.application'

model {
    android {
        def ffmpeg = "src/main/android/armeabi-v7a/include"

        buildToolsVersion "23.0.1"
        compileSdkVersion 23

        defaultConfig.with {
            minSdkVersion.apiLevel = 19
        }

        ndk {
            moduleName = "ffplayer2jni"

            ldLibs.addAll("log", "android", "GLESv2", "dl", "atomic", "EGL",
                    "z", "stdc++", "OpenSLES")

            cppFlags.addAll("-std=c++11", "-fexceptions", '-I'+file(ffmpeg),
                    "-D __cplusplus=201103L", "-frtti",
                    "-D __GXX_EXPERIMENTAL_CXX0X__")
            CFlags.add('-I'+file(ffmpeg))

            stl = "gnustl_static"
            //stl = "stlport_shared"

            abiFilters.addAll("armeabi-v7a")
        }
    }

    android.buildTypes {
        release {
            minifyEnabled false
            proguardFiles.add(file('proguard-android.txt'))
        }
    }

    repositories {
        def loc = "src/main/jniLibs/armeabi-v7a/"

        libs(PrebuiltLibraries) {

            libavutil {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(loc + "libavutil.so")
                }
            }

            libavcodec {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(loc + "libavcodec.so")
                }
            }

            libavformat {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(loc + "libavformat.so")
                }
            }

            libavfilter {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(loc + "libavfilter.so")
                }
            }

            libpostproc {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(loc + "libpostproc.so")
                }
            }

            libswresample {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(loc + "libswresample.so")
                }
            }

            libswscale {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(loc + "libswscale.so")
                }
            }
        }
    }

    android.sources {
        main {
            jni {
                dependencies {
                    library "libavformat" linkage "shared"
                    library "libavcodec" linkage "shared"
                    library "libavfilter" linkage "shared"
                    library "libavutil" linkage "shared"
                    library "libswscale" linkage "shared"
                    library "libswresample" linkage "shared"
                    library "libpostproc" linkage "shared"
                }
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
}

EDIT: including the headers in a cpp project

#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS

#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif

#ifdef __cplusplus
extern "C" {
#endif

#include <libavutil/frame.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/pixfmt.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/avfiltergraph.h"
#ifdef __cplusplus
}
#endif

#include <string>
#include <list>
#include <vector>

#include "pthread.h"
#include "sched.h"
#include "unistd.h"
#include "stdio.h"
#include "time.h"
WLGfx
  • 1,169
  • 15
  • 31
  • Hello @WLGfx, thanks for your answer. I will try to it. – Jake Park Jul 20 '16 at 11:01
  • HI! How can I build ffplayer2jni.so. Have you build ffplayer2jni.so file using ndk-build of terminal? I want to see your Android.mk and Application.mk file. – Jake Park Jul 20 '16 at 15:50
  • The ffplayer2jni is the native library I build for that project. It gets built by gradle alongside the rest of the Android Studio project. All I've done is to add the ffmpeg libraries and the headers into the source folders. – WLGfx Jul 20 '16 at 17:11
  • Hi @WLGfx! THANKS! FOR! YOUR! ANSWER! U're great man! I solved my problem! – Jake Park Jul 21 '16 at 08:37
  • @WLGfx i need some tips for configuration of FFMPEG-3.4.1 with android stdio-3.0.1. – Abdul Muheet Jan 30 '18 at 07:14