安卓上使用FFMPEG解碼視頻并處理

【嵌牛導(dǎo)讀】:安卓上OPENCV是經(jīng)過(guò)剪切的,部分功能如VideoCapture不能使用。本文使用首先將FFMPEG封裝呈.so庫(kù),然后使用JNI調(diào)用C語(yǔ)言實(shí)現(xiàn)了視頻的解碼處理。

【嵌牛鼻子】:FFMPEG? android JNI? 視頻解碼

【嵌牛提問(wèn)】:安卓上使用FFMPEG進(jìn)行視頻解碼

【嵌牛正文】:

JNI調(diào)用NATIVE的FFMPEG

Android的opencv不支持videocapture解碼視頻,是因?yàn)锳ndroid上的opencv沒(méi)有集成ffmpeg這個(gè)解碼的庫(kù)。所以需要自己把ffmpeg的源碼打包成.so文件,在Android中使用JNI調(diào)用.so庫(kù),然后再c++中讀取手機(jī)中的視頻解碼以后轉(zhuǎn)為一個(gè)一個(gè)的MAT矩陣,以方便后期處理。下面是步驟

一. ? 將ffmpeg源碼編譯成.so庫(kù)。我使用的是別人編譯好的庫(kù)。具體編譯方法看這個(gè)網(wǎng)頁(yè)。https://www.2cto.com/kf/201804/739639.html

二.? 編寫(xiě)NATIVE函數(shù)

1.新建一個(gè)類(lèi)FFmpegDecode的類(lèi),寫(xiě)兩個(gè)native函數(shù)

2.使用Android studio上的終端。轉(zhuǎn)到app\build\intermediates\classes\debug文件夾下

3.生成java的native函數(shù)所對(duì)應(yīng)的c++函數(shù)頭文件

4.然后在app\build\intermediates\classes\debug目錄下找到生成的.h頭文件

三.安卓上配置NDK。

1修改工程下的gradle.properties

修改app下的build.gradle

sourceSets.main.jni.srcDirs= []

sourceSets.main.jniLibs.srcDirs = ['src/main/libs','src/main/jniLibs']

//禁止自帶的ndk功能task ndkBuild(type: Exec,description:'Compile JNI source with NDK') {


Properties properties = new Properties()


properties.load(project.rootProject.file('local.properties').newDataInputStream())


def ndkDir= properties.getProperty('ndk.dir')


if (org.apache.tools.ant.taskdefs.condition.Os.isFamily(org.apache.tools.ant.taskdefs.condition.Os.FAMILY_WINDOWS)) {


commandLine "$ndkDir/ndk-build.cmd",'-C',file('src/main/jni').absolutePath


} else{


commandLine "$ndkDir/ndk-build",'-C',file('src/main/jni').absolutePath


}

}

tasks.withType(JavaCompile) {

??? compileTask

-> compileTask.dependsOn ndkBuild

}

task ndkClean(type: Exec,description:'Clean NDK Binaries') {


Properties properties = new Properties()


properties.load(project.rootProject.file('local.properties').newDataInputStream())


def ndkDir= properties.getProperty('ndk.dir')


if (org.apache.tools.ant.taskdefs.condition.Os.isFamily(org.apache.tools.ant.taskdefs.condition.Os.FAMILY_WINDOWS)) {


commandLine "$ndkDir/ndk-build.cmd",'clean','-C',file('src/main/jni').absolutePath


} else{


commandLine "$ndkDir/ndk-build",'clean','-C',file('src/main/jni').absolutePath


}

}

defaultConfig {


multiDexEnabled true

}

clean.dependsOn 'ndkClean'

2修改工程下的local.properties

3在app/src/main下新建jni文件夾。在jni文件夾下新建兩個(gè)文件Android.mk和Application.mk。

4.將剛才生成的頭文件復(fù)制到j(luò)ni文件夾下。同時(shí)新建對(duì)應(yīng)的.cpp文件。

5.在jni文件夾下面新建一個(gè)include文件夾,把ffmpeg的源碼拷貝到該文件夾下

6.將之前編譯好的.so文件放到j(luò)ni文件夾下面

7.編寫(xiě)Android.mk文件

LOCAL_PATH := $(call my-dir)

#ffmpeg lib

include $(CLEAR_VARS)

LOCAL_MODULE := avcodec

LOCAL_SRC_FILES := libavcodec-56.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := avdevice

LOCAL_SRC_FILES := libavdevice-56.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := avfilter

LOCAL_SRC_FILES := libavfilter-5.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := avformat

LOCAL_SRC_FILES := libavformat-56.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := avutil

LOCAL_SRC_FILES := libavutil-54.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := postproc

LOCAL_SRC_FILES := libpostproc-53.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := swresample

LOCAL_SRC_FILES := libswresample-1.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := swscale

LOCAL_SRC_FILES := libswscale-3.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := yuv

LOCAL_SRC_FILES := libyuv.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := opencv_java

LOCAL_SRC_FILES := libopencv_java.so

include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

#OPENCV_CAMERA_MODULES:=on

#OPENCV_INSTALL_MODULES:=off

include ..\..\..\..\native\jni\OpenCV.mk

LOCAL_MODULE???? :=ffmdecode

LOCAL_SRC_FILES?:=com_tinymonster_ffmpegstudy1_FFmpegDecode.cpp

LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/ffmpeg

LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/libyuv

LOCAL_LDLIBS???? += -llog -ldl

LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postprocswresample swscale yuv

include $(BUILD_SHARED_LIBRARY)

8.編寫(xiě)Application文件

APP_STL := gnustl_static

APP_CPPFLAGS := -frtti -fexceptions

APP_ABI := armeabi

9.將opencv的native庫(kù)放到工程目錄下。

10.點(diǎn)擊屏幕右側(cè)的gradle中的ndkbuild。

11.然后會(huì)看到j(luò)ni下面生成了libs和obj兩個(gè)文件夾。這兩個(gè)文件夾下面生成的是對(duì)應(yīng)的.so庫(kù)

12.編寫(xiě)c代碼,讀取手機(jī)視頻,解碼視頻,并轉(zhuǎn)為OPENCV的MAT。

#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"ccj",FORMAT,##__VA_ARGS__);

#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,"ccj",FORMAT,##__VA_ARGS__);

using namespace cv;

JNIEXPORT jint JNICALLJava_com_tinymonster_ffmpegstudy1_FFmpegDecode_DecodeFile

? (JNIEnv* env, jclassobj, jstring input_){

LOGE("%s","1");

const

char *filename= env->GetStringUTFChars(input_, 0);

AVCodec*pCodec; //

解碼器指針

AVCodecContext* pCodecCtx; //ffmpeg解碼類(lèi)的類(lèi)成員

AVFrame* pAvFrame; //多媒體幀,保存解碼后的數(shù)據(jù)幀

AVFormatContext* pFormatCtx; //保存視頻流的信息

av_register_all(); //注冊(cè)庫(kù)中所有可用的文件格式和編碼器

pFormatCtx= avformat_alloc_context();

if(avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) { //檢查文件頭部

LOGE("%s","Can'tfind the stream!");

}

if(avformat_find_stream_info(pFormatCtx,NULL) < 0) { //查找流信息

LOGE("%s","Can'tfind the stream information !");

}

intvideoindex= -1;

for(int i=0; i< pFormatCtx->nb_streams; ++i) //遍歷各個(gè)流,找到第一個(gè)視頻流,并記錄該流的編碼信息

{

if (pFormatCtx->streams[i]->codec->codec_type== AVMEDIA_TYPE_VIDEO) {

videoindex= I;

break;

}

??????????? }


if(videoindex==

-1) {

LOGE("%s","Don'tfind a video stream !");

return 1;

}

pCodecCtx= pFormatCtx->streams[videoindex]->codec; //得到一個(gè)指向視頻流的上下文指針

pCodec= avcodec_find_decoder(pCodecCtx->codec_id); //到該格式的解碼器

if (pCodec== NULL) {

LOGE("%s","Cant'tfind the decoder !");

return 2;

}

if(avcodec_open2(pCodecCtx,pCodec,NULL) < 0) { //打開(kāi)解碼器

LOGE("%s","Can't open the decoder !");

return 3;

}

pAvFrame= avcodec_alloc_frame(); //分配幀存儲(chǔ)空間


AVFrame* pFrameBGR= avcodec_alloc_frame(); //存儲(chǔ)解碼后轉(zhuǎn)換的RGB數(shù)據(jù)

??????? //

保存BGR,opencv中是按BGR來(lái)保存的

int size=avpicture_get_size(AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);

uint8_t*out_buffer= (uint8_t*)av_malloc(size);

avpicture_fill((AVPicture*)pFrameBGR, out_buffer, AV_PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);

AVPacket* packet= (AVPacket*)malloc(sizeof(AVPacket));

LOGI("視頻的文件格式:%s",pFormatCtx->iformat->name);

LOGI("視頻時(shí)長(zhǎng):%d", (pFormatCtx->duration)/1000000);

LOGI("視頻的寬高:%d,%d",pCodecCtx->width,pCodecCtx->height);

LOGI("解碼器的名稱(chēng):%s",pCodec->name);

struct SwsContext*img_convert_ctx;

img_convert_ctx= sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);

//opencv

cv::Mat pCvMat;

pCvMat.create(cv::Size(pCodecCtx->width, pCodecCtx->height), CV_8UC3);

int ret;

int got_picture;

//讀取每一幀

int frame_count= 0;

while (av_read_frame(pFormatCtx, packet) >= 0)

??????????????????? {

if(packet->stream_index==videoindex)

??????????????????????? {

ret= avcodec_decode_video2(pCodecCtx, pAvFrame, &got_picture, packet);

if(ret< 0)

??????????????????????????? {

printf("Decode Error.(解碼錯(cuò)誤)\n");

return 4;

}

LOGI("解碼第%d幀",frame_count);

if (got_picture)

????????????????? ??????????{

//YUV to RGB

sws_scale(img_convert_ctx, (const uint8_t* const*)pAvFrame->data, pAvFrame->linesize, 0, pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize);

memcpy(pCvMat.data, out_buffer, size);//拷貝

frame_count++;

LOGI("解碼第%d幀",frame_count);

}

??????????????????????? }

av_free_packet(packet);

}

av_free(out_buffer);

av_free(pFrameBGR);

av_free(pAvFrame);

avcodec_close(pCodecCtx);

avformat_close_input(&pFormatCtx);

sws_freeContext(img_convert_ctx);

retur 0;


}

13.這樣就可以在JAVA代碼中調(diào)用C++代碼處理視頻了???????????????????



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容