How to build FFmpeg (ver 3.1.1) on Android Studio (ver 2.1.2)

女生的网名这么多〃 提交于 2019-11-29 18:07:19

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!