最新的android studio2.2引入了cmake能夠很好地實現ndk的編寫。這裏使用最新的方式,對於之前的android下的ndk編譯什麼的能夠參考以前的文章:Android開發學習之路–NDK、JNI之初體驗。html
進入正題,既然是ffmpeg的移植編譯,那麼就先下載ffmpeg,https://ffmpeg.org/download.html#releases。這裏下載的是3.0.3版本。
新建ffmpeg文件夾,而後新建腳本用來編譯ffmpeg,命名爲build.sh,腳本以下:java
#!/bin/bash
cd ffmpeg
export TMPDIR=/Users/jared/Desktop/jared/study/external/ffmpeg/tempdir
NDK=/Users/jared/Desktop/jared/software/sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-16/arch-arm/
TOOLCHAIN=/Users/jared/Desktop/jared/software/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64
CPU=arm
PREFIX=/Users/jared/Desktop/jared/study/external/ffmpeg/output
ADDI_CFLAGS="-marm"
function build_one
{
./configure \
--prefix=$PREFIX \ --enable-shared \ --disable-static \ --disable-doc \ --disable-ffmpeg \ --disable-ffplay \ --disable-ffprobe \ --disable-ffserver \ --disable-doc \ --disable-symver \ --enable-small \ --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
}
build_one
cd ../
注意的是這裏的路徑是對應的ndk的相關路徑。linux
而後chmod 777 build.sh,而後執行source build.sh。開始編譯ffmpeg。小憩片刻以後,編譯完成,在output目錄下就會生成對應的so文件:android
➜ ffmpeg cd output
➜ output ls
include lib
➜ output cd lib
➜ lib ls
libavcodec-57.so libavfilter-6.so libavutil-55.so libswscale-4.so
libavcodec.so libavfilter.so libavutil.so libswscale.so
libavdevice-57.so libavformat-57.so libswresample-2.so pkgconfig
libavdevice.so libavformat.so libswresample.so
➜ lib
編譯完成,這裏只是用了arm的平臺,若是須要mips,或者x86須要修改build.sh腳本的arch和路徑這裏不贅述了。
既然庫文件都已經編譯出來了那就能夠android開搞了。c++
首先須要最新的android studio2.2,而且安裝好cmake和ndk。而後新建工程,能夠開始了。
新建工程取名爲helloffmpeg,而後選中include c++ support,而後下一步直到新建完成爲止。以下圖:
而後比通常的工程多了些東西,首先是build.gradle了,git
CMake的cpp的flagsgithub
externalNativeBuild {
cmake {
cppFlags ""
}
}
CMake的路徑shell
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
而後看CMakeLists.txt文件,這裏主要講解下配置。先看下已經配置好的庫文件:ruby
# 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)
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 )
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")
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
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的庫,bash
add_library( avutil-55 SHARED IMPORTED )
這裏avutil-55表示庫的名稱,SHARED表示是共享庫,通常.so文件,還有STATIC,通常.a文件。IMPORTED表示引用的不是生成的。
接着是這裏用到的須要生產的.so文件
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp )
最後的參數是源碼的路徑,若是有更多的源碼就接下去寫上。
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的庫文件。
最後看下和java,res同級的cpp目錄下有native-lib.cpp文件,系統自動生成的。
首先是native-lib.cpp內容:
#include <jni.h>
#include <string>
extern "C"
jstring
Java_com_jared_helloffmpeg_MainActivity_stringFromJNI(
JNIEnv *env, jobject) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
這裏有一個函數Java_com_jared_helloffmpeg_MainActivity_stringFromJNI,命名方式也能夠知道,Java開頭,com_jared_helloffmpeg爲包名,MainActivity是調用的java類,最後stringFromJNI是方法名。而後返回了hello from c++字符。
看下java的代碼:
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(avformatinfo());
/** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */
public native String stringFromJNI();
// Used to load the 'native-lib' library on application startup.
static {
loadLibrary("native-lib");
}
也很好理解這裏的stringFromJNI的native方法,具體須要看jni的基本概念了,這裏就很少贅述了。好了基本上最簡單的通了,那麼接下去就是ffmpeg的使用了。
接着來修改界面顯示ffmpeg的一些基本信息。以下:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="MainBinding"></data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jared.helloffmpeg.MainActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:id="@+id/btn_protocol" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Protocol" android:textAllCaps="false" />
<Button android:id="@+id/btn_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Format" android:textAllCaps="false" />
<Button android:id="@+id/btn_codec" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Codec" android:textAllCaps="false" />
<Button android:id="@+id/btn_filter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:text="Filter" android:textAllCaps="false" />
</LinearLayout>
<ScrollView android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/tv_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" />
</ScrollView>
</LinearLayout>
</layout>
主要是實現了四個button,分別是protocol,format,codec和filter。以下圖所示:
修改jni部分的代碼,也就是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_jared_helloffmpeg_MainActivity_stringFromJNI(
JNIEnv *env, jobject) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
jstring
Java_com_jared_helloffmpeg_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_jared_helloffmpeg_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_jared_helloffmpeg_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_jared_helloffmpeg_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);
}
}
這裏要特別注意ffmpeg的頭文件已經調用的代碼要extern 「C」裏面,要否則會報錯誤的。
最後就是java調用代碼了。以下所示:
package com.jared.helloffmpeg;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.jared.helloffmpeg.databinding.MainBinding;
import static java.lang.System.loadLibrary;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.btnProtocol.setOnClickListener(this);
binding.btnCodec.setOnClickListener(this);
binding.btnFilter.setOnClickListener(this);
binding.btnFormat.setOnClickListener(this);
// Example of a call to a native method
//TextView tv = (TextView) findViewById(R.id.sample_text);
//tv.setText(avformatinfo());
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_protocol:
binding.tvInfo.setText(urlprotocolinfo());
break;
case R.id.btn_format:
binding.tvInfo.setText(avformatinfo());
break;
case R.id.btn_codec:
binding.tvInfo.setText(avcodecinfo());
break;
case R.id.btn_filter:
binding.tvInfo.setText(avfilterinfo());
break;
default:
break;
}
}
/** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */
public native String stringFromJNI();
public native String urlprotocolinfo();
public native String avformatinfo();
public native String avcodecinfo();
public native String avfilterinfo();
// Used to load the 'native-lib' library on application startup.
static {
loadLibrary("native-lib");
}
}
都準備好了,那麼就繼續運行看看結果了:
基本上Android Studio下的cmake編譯ffmpeg就到此結束了,那麼接下去就能夠在android手機上使用ffmpeg來作一些音視頻相關的東西了。