android uses ffmpeg library

1. Create an Android project, create a jni directory, copy the library and header files from the previous article to the jni directory, and create an Android.mk file. And create a main.c
2. The project directory is as shown in Figure



3.main .c file content
#include <string.h>
#include <jni.h>
#include <dlfcn.h>
#include <android/log.h>
#include "libavcodec/avcodec.h"


#define TAG "kx_ffmpeg"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__)
#define LOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__)

jint Java_com_kk_xx_ffmpeg_FFMPEGInterface_test(JNIEnv *env, jobject obj, jint codecID)
{
                AVCodec *codec = NULL;

                /*register all formats and codecs */
                av_register_all ();

                codec= avcodec_find_decoder(codecID);

                if(codec != NULL)
                {
                       return 0;
                }
                else
                {
                      return -1;
                }
}


4.Android.mk内容
LOCAL_PATH := $(callmy-dir)
MY_PATH := /home/kangxian/FFMPEG/ffmpeg_android/testFFMPEG/app/jni
MY_INCLUDE_PATH := /home/kangxian/FFMPEG/ffmpeg_android/testFFMPEG/app/jni/prebuilt
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef

$(warning $(MY_PATH))

include $(CLEAR_VARS)
LOCAL_MODULE := avcodec-57-prebuilt
LOCAL_SRC_FILES := $(MY_PATH)/prebuilt/libavcodec-57.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE :=avdevice-57-prebuilt
LOCAL_SRC_FILES :=$(MY_PATH)/prebuilt/libavdevice-57.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE :=avfilter-6-prebuilt
LOCAL_SRC_FILES :=$(MY_PATH)/prebuilt/libavfilter-6.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE :=avformat-57-prebuilt
LOCAL_SRC_FILES :=$(MY_PATH)/prebuilt/libavformat-57.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE :=  avutil-55-prebuilt
LOCAL_SRC_FILES :=$(MY_PATH)/prebuilt/libavutil-55.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE :=  avswresample-2-prebuilt
LOCAL_SRC_FILES :=$(MY_PATH)/prebuilt/libswresample-2.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := swscale-4-prebuilt
LOCAL_SRC_FILES :=$(MY_PATH)/prebuilt/libswscale-4.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE :=kx_ffmpeg
LOCAL_SRC_FILES := $(MY_PATH)/main.c
LOCAL_C_INCLUDES : = $(MY_INCLUDE_PATH)

LOCAL_LDLIBS := -llog -ljnigraphics -lz -landroid -L/home/kangxian/NDK/android-ndk-r10/platforms/android-9/arch-arm/usr/lib
LOCAL_SHARED_LIBRARIES:= avcodec-57 -prebuilt avdevice-57-prebuilt avfilter-6-prebuilt avformat-57-prebuilt avutil-55-prebuilt

include $(BUILD_SHARED_LIBRARY)

5. Switch to the jni directory, execute ndk-build, if there is no error, it will be generated in the libs directory 7 so libraries, as shown below:



6. The jni library available for Android has been compiled, copy the 7 libraries to the Android project and try, if the jni function returns 0, it proves that the ffmpeg library we compiled is available.
7. Create a package name and class name that are consistent with the jni function, as shown below


8. Content of FFMPEGInterface.java
package com.kk.xx.ffmpeg;

/**
 * Created by Administrator on 2017/3/14.
 */

public class FFMPEGInterface {
    static{
        System.loadLibrary("avutil-55");
        System.loadLibrary("avcodec-57");
        System.loadLibrary("swresample-2");
        System.loadLibrary("avformat-57");
        System.loadLibrary("swscale-4");
        System.loadLibrary("avfilter-6");
        System.loadLibrary("avdevice-57");
        System.loadLibrary("kx_ffmpeg");
    }

    public static native int test(int codecID);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326334760&siteId=291194637