1. 程式人生 > >windows下編譯最新版ffmpeg3.3-android,並通過CMake方式移植到Android studio2.3中

windows下編譯最新版ffmpeg3.3-android,並通過CMake方式移植到Android studio2.3中

windows下編譯最新版ffmpeg3.3

  1. 編輯ffmpeg資料夾下面的configure檔案,找到
SLIBNAME_WITH_MAJOR='$(SLIBNAME).$(LIBMAJOR)'
LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"'
SLIB_INSTALL_NAME='$(SLIBNAME_WITH_VERSION)'
SLIB_INSTALL_LINKS='$(SLIBNAME_WITH_MAJOR) $(SLIBNAME)'
將其修改成:
SLIBNAME_WITH_MAJOR='$(SLIBPREF)
$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF)' LIB_INSTALL_EXTRA_CMD='$$(RANLIB) "$(LIBDIR)/$(LIBNAME)"' SLIB_INSTALL_NAME='$(SLIBNAME_WITH_MAJOR)' SLIB_INSTALL_LINKS='$(SLIBNAME)'

============================================
2.新建一個build_android.sh檔案
注意:要根據環境配置前四項,且每行末尾不能有空格。 export TMPDIR,NDK,SYSROOT,TOOLCHAIN –cross-prefix一定有

#!/bin/bash
export TMPDIR="C:/Users/shuo.wang/Desktop/ff"
NDK=C:/Users/shuo.wang/AppData/Local/Android/sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-21/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
function build_one { 
./configure \
     --prefix=$PREFIX \
     --enable-shared \
--disable-static \ --disable-doc \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffprobe \ --disable-ffserver \ --disable-avdevice \ --disable-doc \ --disable-symver \ --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \ --target-os=linux \ --arch=arm \ --enable-cross-compile \ --sysroot=$SYSROOT \ --extra-cflags="-Os -fpic $ADDI_CFLAGS" \ --extra-ldflags="$ADDI_LDFLAGS" \ $ADDITIONAL_CONFIGURE_FLAG make clean make make install } CPU=arm PREFIX=$(pwd)/android/$CPU ADDI_CFLAGS="-marm" build_one

然後在MinGW中進入cd進ffmpeg目錄。
命令:
1. chmod 777 ./build_android.sh
2. ./build_android.sh 然後就開始編譯了。

注意因為 NDK和ffmpeg,版本問題。ffmpeg3.1編譯不過。
ffmpeg3.3可以。

編譯完成後這時會發現 FFmpeg下多了一個資料夾android

下面開始移植到Android studio中

需要Android studio版本2.2以上,並安裝CMake。本例中用Android studio2.3
1.新建工程取名為FFmpegAndroid,然後選中include c++ support,然後下一步直到新建完成為止。

2.然後開啟app中build.gradle。
新增如下:

   externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
//新增 start
        ndk {
            abiFilters "armeabi", "armeabi-v7a"
        }
//----------------------------------------------
sourceSets.main {
        jniLibs.srcDirs = ['libs']
        jni.srcDirs = []
    }
//新增 end

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

3.把之前編譯好的.so庫拷貝進專案libs目錄
如圖:
這裡寫圖片描述

4.重點 CMakeLists.txt配置

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)


add_library(
             native-lib
             SHARED
             src/main/cpp/native-lib.cpp )

find_library(
              log-lib
              log )


set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)

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")



include_directories(libs/include)

#target_include_directories(native-lib PRIVATE libs/include)

target_link_libraries( native-lib swresample-2 avcodec-57 avfilter-6 swscale-4 avdevice-57 avformat-57
                       ${log-lib} )
 cmake_minimum_required(VERSION 3.4.1):表示cmake的最低版本是3.4.1。

add_library():新增庫,分為兩種,一種是需要編譯為庫的程式碼,一種是已經編譯好的庫檔案。
比如上面編譯好的ffmpeg的庫

  這裡用到的需要生產的.so檔案

add_library( native-lib
             SHARED
             src/main/cpp/native-lib.cpp )

  最後的引數是原始碼的路徑,如果有更多的原始碼就接下去寫上。

add_library( avutil-55
             SHARED
             IMPORTED )

  這裡avutil-55表示庫的名稱,SHARED表示是共享庫,一般.so檔案,還有STATIC,一般.a檔案。IMPORTED表示引用的不是生成的。

set_target_properties:對於已經編譯好的so檔案需要引入,所以需要設定。

set_target_properties( avutil-55
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavutil-55.so )

  這裡avutil-55是名字,然後是PROPERTIES IMPORTED_LOCATION加上庫的路徑。

  include_directories:一般外面引入的庫檔案需要標頭檔案,所以可以通過這個來引入:

include_directories(libs/include)

  target_link_libraries:連結,把需要的so檔案連結起來,這裡native-lib需要連結ffmpeg的庫檔案。

5.修改native-lib.cpp

#include <jni.h>
#include <string>

extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>

jstring
Java_com_ws_ffmpeg_ffmpegandroid_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

jstring
Java_com_ws_ffmpeg_ffmpegandroid_MainActivity_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_ws_ffmpeg_ffmpegandroid_MainActivity_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_ws_ffmpeg_ffmpegandroid_MainActivity_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);
}

jstring
Java_com_ws_ffmpeg_ffmpegandroid_MainActivity_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);
}

}


6.呼叫native方法

package com.ws.ffmpeg.ffmpegandroid;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private Button protocol,format,codec,filter;
    private TextView tv_info;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        protocol = (Button) findViewById(R.id.btn_protocol);
        format = (Button) findViewById(R.id.btn_format);
        codec = (Button) findViewById(R.id.btn_codec);
        filter = (Button) findViewById(R.id.btn_filter);
        tv_info = (TextView) findViewById(R.id.tv_info);

        protocol.setOnClickListener(this);
        format.setOnClickListener(this);
        codec.setOnClickListener(this);
        filter.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_protocol:
                tv_info.setText(urlprotocolinfo());
                break;
            case R.id.btn_format:
                tv_info.setText(avformatinfo());
                break;
            case R.id.btn_codec:
                tv_info.setText(avcodecinfo());
                break;
            case R.id.btn_filter:
                tv_info.setText(avfilterinfo());
                break;
            default:
                break;
        }
    }

    public native String stringFromJNI();

    public native String urlprotocolinfo();
    public native String avformatinfo();
    public native String avcodecinfo();
    public native String avfilterinfo();


}

ok ! 效果如圖:
這裡寫圖片描述