This example uses the combined libffmpeg library. Please refer to the previous practice The practice of compiling ffmpeg into a single library on android platform
Catalog
Configuration environment
Operating system: ubuntu 16.05
Note: the compilation of ffmpeg library uses android-ndk-r10e version, which will result in errors
In Android studio project, the version used with cmake is android-ndk-r16b
New project ffmpeg single hello
- Configure build.gradle as follows
android { ...... defaultConfig { ...... externalNativeBuild { cmake { cppFlags "" } ndk { abiFilters "armeabi-v7a" } } sourceSets { main { //Library address jniLibs.srcDirs = ['libs'] } } } ...... externalNativeBuild { cmake { path "CMakeLists.txt" } } }
- Create a new CMakeLists.txt file with the following configuration
cmake_minimum_required(VERSION 3.4.1) add_library(ffmpeg-hello SHARED src/main/cpp/ffmpeg_hello.c) find_library(log-lib log) #Get the path of the parent directory get_filename_component(PARENT_DIR ${CMAKE_SOURCE_DIR} PATH) set(LIBRARY_DIR ${PARENT_DIR}/ffmpeg-single) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") set(CMAKE_VERBOSE_MAKEFILE on) add_library(ffmpeg-single SHARED IMPORTED) set_target_properties(ffmpeg-single PROPERTIES IMPORTED_LOCATION ${LIBRARY_DIR}/libs/${ANDROID_ABI}/libffmpeg.so ) #Include header file include_directories(${LIBRARY_DIR}/libs/${ANDROID_ABI}/include) target_link_libraries(ffmpeg-hello ffmpeg-single ${log-lib})
The author considers that the subsequent ones are based on the same libffmpeg.so library, so it is not necessary to place one for each module, so a separate component ffmpeg single is separated, and the required header files and libraries are placed in the libs directory
Other single library examples rely on this library
- New class ffmpeg hello.java
package com.onzhou.ffmpeg.hello; public class FFmpegHello { static { System.loadLibrary("ffmpeg"); System.loadLibrary("ffmpeg-hello"); } public native String urlprotocolinfo(); public native String avformatinfo(); public native String avcodecinfo(); public native String avfilterinfo(); public native String configurationinfo(); }
- Create a new source file ffmpeg? Hello. C in src/main/cpp directory
#include <jni.h> #include <stdio.h> #include <time.h> #include "libavcodec/avcodec.h" #include "libavformat/avformat.h" #include "libavfilter/avfilter.h" #include "libavutil/log.h" #ifdef ANDROID #include <android/log.h> #define LOG_TAG "FFmpegHello" #define LOGE(format, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, format, ##__VA_ARGS__) #define LOGI(format, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, format, ##__VA_ARGS__) #else #define LOGE(format, ...) printf(LOG_TAG format "\n", ##__VA_ARGS__) #define LOGI(format, ...) printf(LOG_TAG format "\n", ##__VA_ARGS__) #endif JNIEXPORT jstring JNICALL Java_com_onzhou_ffmpeg_hello_FFmpegHello_urlprotocolinfo (JNIEnv * env, jobject obj){ char info[40000]={0}; av_register_all(); struct URLProtocol *pup = NULL; //input struct URLProtocol **p_temp = &pup; avio_enum_protocols((void **)p_temp, 0); while ((*p_temp) != NULL){ sprintf(info, "%s[in ][%10s]\n", info, avio_enum_protocols((void **)p_temp, 0)); } pup = NULL; //output avio_enum_protocols((void **)p_temp, 1); while ((*p_temp) != NULL){ sprintf(info, "%s[out][%10s]\n", info, avio_enum_protocols((void **)p_temp, 1)); } LOGE("%s", info); return (*env)->NewStringUTF(env, info); } JNIEXPORT jstring JNICALL Java_com_onzhou_ffmpeg_hello_FFmpegHello_avformatinfo (JNIEnv * env, jobject obj){ char info[40000] = { 0 }; av_register_all(); AVInputFormat *if_temp = av_iformat_next(NULL); AVOutputFormat *of_temp = av_oformat_next(NULL); //input while(if_temp!=NULL){ sprintf(info, "%s[in ][%10s]\n", info, if_temp->name); if_temp=if_temp->next; } //output while (of_temp != NULL){ sprintf(info, "%s[out][%10s]\n", info, of_temp->name); of_temp = of_temp->next; } LOGE("%s", info); return (*env)->NewStringUTF(env, info); } JNIEXPORT jstring JNICALL Java_com_onzhou_ffmpeg_hello_FFmpegHello_avcodecinfo (JNIEnv * env, jobject obj){ char info[40000] = { 0 }; av_register_all(); AVCodec *c_temp = av_codec_next(NULL); while(c_temp!=NULL){ if (c_temp->decode!=NULL){ sprintf(info, "%s[dec]", info); } else{ sprintf(info, "%s[enc]", info); } switch (c_temp->type){ case AVMEDIA_TYPE_VIDEO: sprintf(info, "%s[video]", info); break; case AVMEDIA_TYPE_AUDIO: sprintf(info, "%s[audio]", info); break; default: sprintf(info, "%s[other]", info); break; } sprintf(info, "%s[%10s]\n", info, c_temp->name); c_temp=c_temp->next; } LOGE("%s", info); return (*env)->NewStringUTF(env, info); } JNIEXPORT jstring JNICALL Java_com_onzhou_ffmpeg_hello_FFmpegHello_avfilterinfo (JNIEnv * env, jobject obj){ char info[40000] = { 0 }; avfilter_register_all(); AVFilter *f_temp = (AVFilter *)avfilter_next(NULL); while (f_temp != NULL){ sprintf(info, "%s[%10s]\n", info, f_temp->name); } LOGE("%s", info); return (*env)->NewStringUTF(env, info); } JNIEXPORT jstring JNICALL Java_com_onzhou_ffmpeg_hello_FFmpegHello_configurationinfo (JNIEnv * env, jobject obj){ char info[10000] = { 0 }; av_register_all(); sprintf(info, "%s\n", avcodec_configuration()); LOGE("%s", info); return (*env)->NewStringUTF(env, info); }
- Compile, package and run, and output the following information:
Project address:
https://github.com/byhook/ffmpeg4android
Reference resources:
https://blog.csdn.net/leixiaohua1020/article/details/47008825