FFmpeg(二)動態(tài)庫簡單使用(文末有源碼)

文章后面有源碼地址,源碼里面有已經(jīng)生成的動態(tài)庫板甘,拿了東西別忘了點個贊給一個支持,動態(tài)庫生成步驟請看這里

ffmpeg簡單實用,初學(xué)者可以看惫叛,高手走請走開

安卓開發(fā)者萬里長城又走了一步奕锌,真是痛快

image.png

昨天生成了動態(tài)庫著觉,動態(tài)庫生成請看這里,今天就新建了一個項目惊暴,測試了一下庫的使用如下

image.png

目錄結(jié)構(gòu)如下圖:

image.png

首先新建一個c++項目饼丘,新建完了會有一個cpp的文件夾,新建項目的Mainactivity要用java不要用kotlin辽话,這個很坑的肄鸽,kotlin目前好像不行卫病,不知道是不是我沒找到方法,卡這里至少掉了十根頭發(fā)

image.png

這個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",有十來個文件把一個一個改*

image.png

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末抛腕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子媒殉,更是在濱河造成了極大的恐慌担敌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件廷蓉,死亡現(xiàn)場離奇詭異全封,居然都是意外死亡,警方通過查閱死者的電腦和手機桃犬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門刹悴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人攒暇,你說我怎么就攤上這事土匀。” “怎么了形用?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵就轧,是天一觀的道長。 經(jīng)常有香客問我田度,道長妒御,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任镇饺,我火速辦了婚禮乎莉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己梦鉴,他們只是感情好李茫,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著肥橙,像睡著了一般魄宏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上存筏,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天宠互,我揣著相機與錄音,去河邊找鬼椭坚。 笑死予跌,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的善茎。 我是一名探鬼主播券册,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼垂涯!你這毒婦竟也來了烁焙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤耕赘,失蹤者是張志新(化名)和其女友劉穎骄蝇,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體操骡,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡九火,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了册招。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片岔激。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖跨细,靈堂內(nèi)的尸體忽然破棺而出鹦倚,到底是詐尸還是另有隱情,我是刑警寧澤冀惭,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站掀鹅,受9級特大地震影響散休,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜乐尊,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一戚丸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦限府、人聲如沸夺颤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽世澜。三九已至,卻和暖如春署穗,著一層夾襖步出監(jiān)牢的瞬間寥裂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工案疲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留封恰,地道東北人。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓褐啡,卻偏偏與公主長得像诺舔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子备畦,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容