android配置ffmpeg
上面是项目目录结构:
一配置 gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.jnidome.liyihang.jnidome"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
ndk {
abiFilters "armeabi", "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main {
jniLibs.srcDirs = ['libs']
jni.srcDirs = []
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
dataBinding{
enabled true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
配置cmake:
cmake_minimum_required(VERSION 3.4.1)
file(GLOB magic_src_dir "src/main/cpp/*.cpp")
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)
find_library( log-lib
log )
add_library( avutil-55
SHARED
IMPORTED )
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavutil-55.so )
add_library( swresample-2
SHARED
IMPORTED )
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libswresample-2.so )
add_library( avcodec-57
SHARED
IMPORTED )
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavcodec-57.so )
add_library( avfilter-6
SHARED
IMPORTED)
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavfilter-6.so )
add_library( swscale-4
SHARED
IMPORTED)
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libswscale-4.so )
add_library( avdevice-57
SHARED
IMPORTED)
set_target_properties( avdevice-57
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavdevice-57.so )
add_library( avformat-57
SHARED
IMPORTED)
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavformat-57.so )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
add_library( native-lib
SHARED
${magic_src_dir} )
include_directories(libs/include)
target_link_libraries( native-lib swresample-2 avcodec-57 avfilter-6 swscale-4 avdevice-57 avformat-57
${log-lib} )
java调用文件:
package com.jnidome.liyihang.jnidome;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
public String outFont;
private ViewDataBinding viewDataBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Log.e("mytool", Environment.getExternalStorageDirectory().getAbsolutePath());
// Example of a call to a native method
outFont=stringFromJNI();
mNotify();
}
private void mNotify() {
viewDataBinding.setVariable(BR.outs, this);
}
public void mClick(View view){
Toast.makeText(this,"ok"+view.getId(), Toast.LENGTH_LONG).show();
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
编写cpp文件:
//
// Created by yihang.li on 17-7-12.
//
#include "NativeFileWrite.h"
void NativeFileWrite::main() {
outputFilter();
}
void NativeFileWrite::outputFormatType() {
av_register_all();
struct URLProtocol *pup = NULL;
struct URLProtocol **p_temp = (struct URLProtocol **) &pup;
avio_enum_protocols((void **) p_temp, 0);
while ((*p_temp) != NULL) {
// sprintf(info, "%sInput: %s\n", info, avio_enum_protocols((void **) p_temp, 0));
LOGE("input===%s\n", avio_enum_protocols((void **) p_temp, 0));
}
pup = NULL;
avio_enum_protocols((void **) p_temp, 1);
while ((*p_temp) != NULL) {
// sprintf(info, "%sInput: %s\n", info, avio_enum_protocols((void **) p_temp, 1));
LOGE("input=%s\n", avio_enum_protocols((void **) p_temp, 1));
}
}
void NativeFileWrite::outputFormatInfo() {
av_register_all();
AVInputFormat *inputFormat = av_iformat_next(NULL);
AVOutputFormat *outputFormat = av_oformat_next(NULL);
while (inputFormat != NULL) {
LOGE("input===%s\n", inputFormat->name);
inputFormat = inputFormat->next;
}
while (outputFormat != NULL) {
LOGE("output===%s\n", outputFormat->name);
outputFormat = outputFormat->next;
}
}
void NativeFileWrite::outputCodecInfo() {
av_register_all();
AVCodec *avCodec = av_codec_next(NULL);
while (avCodec != NULL) {
if (avCodec->decode != NULL) {
LOGE("decode===\n");
} else {
LOGE("necode===\n");
}
switch (avCodec->type) {
case AVMEDIA_TYPE_VIDEO:
LOGE("video\n");
break;
case AVMEDIA_TYPE_AUDIO:
LOGE("audio\n");
break;
default:
LOGE("other\n");
break;
}
LOGE("code info===%s\n", avCodec->name);
avCodec = avCodec->next;
}
}
void NativeFileWrite::outputFilter() {
avfilter_register_all();
AVFilter *f_temp = (AVFilter *) avfilter_next(NULL);
while (f_temp != NULL) {
LOGE("filter===%s", f_temp->name);
f_temp = f_temp->next;
}
}