文章后面有源碼地址,源碼里面有已經(jīng)生成的動態(tài)庫板甘,拿了東西別忘了點個贊給一個支持,動態(tài)庫生成步驟請看這里
ffmpeg簡單實用,初學(xué)者可以看惫叛,高手走請走開
安卓開發(fā)者萬里長城又走了一步奕锌,真是痛快
昨天生成了動態(tài)庫著觉,動態(tài)庫生成請看這里,今天就新建了一個項目惊暴,測試了一下庫的使用如下
目錄結(jié)構(gòu)如下圖:
首先新建一個c++項目饼丘,新建完了會有一個cpp的文件夾,新建項目的Mainactivity要用java不要用kotlin辽话,這個很坑的肄鸽,kotlin目前好像不行卫病,不知道是不是我沒找到方法,卡這里至少掉了十根頭發(fā)
這個CMakeLists.txt也很坑的典徘,開始看的網(wǎng)上的Adroid.mk和Application.mk,技不如人蟀苛,看著人家一步一步搞的,自己的有問題逮诲,最后發(fā)現(xiàn)CMakeLists.txt就是替換Adroid.mk和Application.mk這兩個東西的
還有include這個文件夾帜平,開始放到cpp下面,native-lib可以引入頭文件梅鹦,放到j(luò)niLibs目錄下就無法引入了罕模,這個最后寫的絕對路徑"./../jniLibs/include/libavcodec/avcodec.h"
這樣的,然后include頭文件的.h文件很挫有錯誤的帘瞭,就改淑掌,原本#include "libavutil/rational.h"
修改為#include "../libavutil/rational.h"
,有十來個文件把一個一個改*
CMakeLists.txt配置
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#add_library( # Sets the name of the library.
# native-lib
#
# # Sets the library as a shared library.
# SHARED
#
# # Provides a relative path to your source file(s).
# src/main/cpp/native-lib.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
#target_link_libraries( # Specifies the target library.
# native-lib
#
# # Links the target library to the log library
# # included in the NDK.
# ${log-lib})
set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})
include_directories(/src/main/jniLibs/include)
add_library( avutil-55
SHARED
IMPORTED )
set_target_properties( avutil-55
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavutil-55.so)
add_library( swresample-2
SHARED
IMPORTED )
set_target_properties( swresample-2
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libswresample-2.so)
add_library( avcodec-57
SHARED
IMPORTED )
set_target_properties( avcodec-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavcodec-57.so)
add_library( avfilter-6
SHARED
IMPORTED )
set_target_properties( avfilter-6
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavfilter-6.so)
add_library( swscale-4
SHARED
IMPORTED )
set_target_properties( swscale-4
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libswscale-4.so)
add_library( avdevice-57
SHARED
IMPORTED)
set_target_properties( avdevice-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavdevice-57.so )
add_library( avformat-57
SHARED
IMPORTED )
set_target_properties( avformat-57
PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libavformat-57.so)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp)
target_link_libraries(native-lib swresample-2 avcodec-57 avfilter-6 swscale-4 avdevice-57 avformat-57 avutil-55
${log-lib})
native-lib.cpp原碼
#include <jni.h>
#include <string>
extern "C" {
#include "./../jniLibs/include/libavutil/avutil.h"
#include "./../jniLibs/include/libavfilter/avfilter.h"
#include "./../jniLibs/include/libavformat/avformat.h"
#include "./../jniLibs/include/libavcodec/avcodec.h"
jstring
Java_com_ffmpeg_myffmpeg_MainActivity1_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
jstring
Java_com_ffmpeg_myffmpeg_MainActivity1_avfilterinfo(
JNIEnv *env, jobject) {
char info[40000] = {0};
avfilter_register_all();
AVFilter *f_temp = (AVFilter *)avfilter_next(NULL);
while(f_temp != NULL) {
sprintf(info, "%s%s\n", info, f_temp->name);
f_temp = f_temp->next;
}
return env->NewStringUTF(info);
}
jstring
Java_com_ffmpeg_myffmpeg_MainActivity1_urlprotocolinfo(
JNIEnv *env, jobject) {
char info[40000] = {0};
av_register_all();
struct URLProtocol *pup = NULL;
struct URLProtocol **p_temp = &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));
}
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));
}
return env->NewStringUTF(info);
}
jstring
Java_com_ffmpeg_myffmpeg_MainActivity1_avformatinfo(
JNIEnv *env, jobject) {
char info[40000] = {0};
av_register_all();
AVInputFormat *if_temp = av_iformat_next(NULL);
AVOutputFormat *of_temp = av_oformat_next(NULL);
while (if_temp != NULL) {
sprintf(info, "%sInput: %s\n", info, if_temp->name);
if_temp = if_temp->next;
}
while (of_temp != NULL) {
sprintf(info, "%sOutput: %s\n", info, of_temp->name);
of_temp = of_temp->next;
}
return env->NewStringUTF(info);
}
jstring
Java_com_ffmpeg_myffmpeg_MainActivity1_avcodecinfo(
JNIEnv *env, jobject) {
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, "%sdecode:", info);
} else {
sprintf(info, "%sencode:", 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;
}
return env->NewStringUTF(info);
}
}
MainActivity原碼,這里開始是Mainactivity.kt,最后修改為Mainactivity.java,報錯蝶念,又修改為了MainActivity1.java的
package com.ffmpeg.myffmpeg;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity1 extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.sample_text);
textView.setText(stringFromJNI()+
urlprotocolinfo()+
avformatinfo()+
avfilterinfo());
}
public native String stringFromJNI();
public native String urlprotocolinfo();
public native String avformatinfo();
public native String avfilterinfo();
static {
System.loadLibrary("native-lib");
System.loadLibrary("avcodec-57");
System.loadLibrary("avdevice-57");
System.loadLibrary("avfilter-6");
System.loadLibrary("avformat-57");
System.loadLibrary("avutil-55");
System.loadLibrary("postproc-54");
System.loadLibrary("swresample-2");
System.loadLibrary("swscale-4");
}
}
最后上傳代碼一份: 別忘點個贊哈
https://github.com/gethub-json/myffmpeg