FFmpeg解碼MP4文件為YUV文件

前言

前面我學(xué)了編譯FFmpeg的Android庫,寫了一個(gè)命令行使用FFmpeg的Android Demo,C文件都在虛擬機(jī)實(shí)現(xiàn),然后ndk編譯成so庫,再導(dǎo)入Android studio使用,Android代碼中沒有C/C++文件,很純凈的樣子。但是,在虛擬機(jī)寫C代碼的時(shí)候,沒有自動(dòng)補(bǔ)全功能,很不方便。所以這次用Cmake編譯JNI,直接在Android studio中使用代碼補(bǔ)全功能!

1、編譯Android版FFmpeg并移植到Android studio中

1.1、編譯FFmpeg的Android庫

Android FFmpeg JNI開發(fā)入門_編譯不同ABI的so庫

1.2、創(chuàng)建一個(gè)支持C++的Android項(xiàng)目

把紅框的選項(xiàng)都勾選上


生成之后的項(xiàng)目目錄


1.2、將so庫移植到Android項(xiàng)目中

在libs目錄下新建對應(yīng)ABI的文件夾,比如armeabi-v7a,然后將編譯生成的對應(yīng)ABI的so庫拷貝進(jìn)去;
再將頭文件拷貝進(jìn)去,就是生成的include目錄;



然后修改app的build.gradle文件如下;


再修改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)


include_directories(libs/include)
set(DIR ../../../../libs)

add_library( avcodec-57
        SHARED
        IMPORTED )
set_target_properties( avcodec-57
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavcodec-57.so )

add_library( avdevice-57
        SHARED
        IMPORTED)
set_target_properties( avdevice-57
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavdevice-57.so )

add_library( avfilter-6
        SHARED
        IMPORTED)
set_target_properties( avfilter-6
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavfilter-6.so )

add_library( avformat-57
        SHARED
        IMPORTED)
set_target_properties( avformat-57
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavformat-57.so )

add_library( avutil-55
        SHARED
        IMPORTED )
set_target_properties( avutil-55
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libavutil-55.so )

add_library( postproc-54
        SHARED
        IMPORTED )
set_target_properties( postproc-54
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libpostproc-54.so )

add_library( swresample-2
        SHARED
        IMPORTED )
set_target_properties( swresample-2
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libswresample-2.so )

add_library( swscale-4
        SHARED
        IMPORTED)
set_target_properties( swscale-4
        PROPERTIES IMPORTED_LOCATION
        ${DIR}/armeabi-v7a/libswscale-4.so )

# 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
        avutil-55
        avcodec-57
        avformat-57
        avdevice-57
        swresample-2
        swscale-4
        postproc-54
        avfilter-6
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

2、寫代碼

2.1、布局文件

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    </LinearLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:text=""
        android:id="@+id/editText1" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:text=""
        android:id="@+id/editText2" />

    <Button
        android:text="開始解碼為YUV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button" />


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ScrollView>

</LinearLayout>

2.2、生成jni頭文件

新建FFmpeg.java文件,加載so庫,定義native方法;

public class FFmpeg {
    static {
        System.loadLibrary("avutil-55");
        System.loadLibrary("avcodec-57");
        System.loadLibrary("avformat-57");
        System.loadLibrary("avdevice-57");
        System.loadLibrary("swresample-2");
        System.loadLibrary("swscale-4");
        System.loadLibrary("postproc-54");
        System.loadLibrary("avfilter-6");
    }
    
    public static native int decode(String inputurl, String outputurl);
}

生成頭文件;
命令行:javah 包名.類名

javah com.example.zjf.ffmpegdecoder.FFmpeg

將生成的頭文件拖到cpp目錄下,然后在native-lib.cpp中實(shí)現(xiàn)頭文件的方法;


2.3、實(shí)現(xiàn)jni方法

添加用到的頭文件;

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

#ifdef __cplusplus
extern "C" {
#include <libavutil/log.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>

#include "com_example_zjf_ffmpegdecoder_FFmpeg.h"
#include <android/log.h>
#define LOGE(format, ...)  __android_log_print(ANDROID_LOG_ERROR, "(>_<)", format, ##__VA_ARGS__)
#define LOGI(format, ...)  __android_log_print(ANDROID_LOG_INFO,  "(^_^)", format, ##__VA_ARGS__)
#endif
/*
 * Class:     com_example_zjf_ffmpegdecoder_FFmpeg
 * Method:    decode
 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_example_zjf_ffmpegdecoder_FFmpeg_decode
        (JNIEnv *evn, jclass clazz, jstring inputurl, jstring outputurl){

}

#ifdef __cplusplus
}
#endif

這樣就有了自動(dòng)補(bǔ)全的功能;


jni代碼;

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

#ifdef __cplusplus
extern "C" {
#include <libavutil/log.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>

#include "com_example_zjf_ffmpegdecoder_FFmpeg.h"
#include <android/log.h>
#define LOGE(format, ...)  __android_log_print(ANDROID_LOG_ERROR, "(>_<)", format, ##__VA_ARGS__)
#define LOGI(format, ...)  __android_log_print(ANDROID_LOG_INFO,  "(^_^)", format, ##__VA_ARGS__)
#endif
/*
 * Class:     com_example_zjf_ffmpegdecoder_FFmpeg
 * Method:    decode
 * Signature: (Ljava/lang/String;Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_example_zjf_ffmpegdecoder_FFmpeg_decode
        (JNIEnv *env, jclass clazz, jstring input_jstr, jstring output_jstr){
    AVFormatContext *pFormatCtx;
    int i,videoindex;
    AVCodecContext *pCodecCtx;
    AVCodec *pCodec;
    AVFrame *pFrame,*pFrameYUV;
    uint8_t *out_buffer;
    AVPacket *pPacket;
    int y_size;
    int ret, got_picture;
    struct SwsContext *img_convert_ctx;
    FILE *fp_yuv;
    int frame_cnt;
    clock_t time_start, time_finish;
    double  time_duration = 0.0;
    char input_str[500]={0};
    char output_str[500]={0};
    char info[1000]={0};
    sprintf(input_str,"%s",env->GetStringUTFChars(input_jstr, NULL));
    sprintf(output_str,"%s",env->GetStringUTFChars(output_jstr, NULL));

    /*初始化avformat并注冊編譯進(jìn)avformat庫里面所有的復(fù)用器(muxers),
    解復(fù)用器(demuxers)和協(xié)議模塊*/
    av_register_all();
    /**網(wǎng)絡(luò)功能的全局初始化(可選的,在使用網(wǎng)絡(luò)協(xié)議時(shí)有必要調(diào)用)*/
    avformat_network_init();
    //初始化一個(gè)AVFormatContext
    pFormatCtx = avformat_alloc_context();

    //打開輸入的視頻文件
    if (avformat_open_input(&pFormatCtx,input_str,NULL,NULL) != 0){
        LOGE("Couldn't open input stream.\n");
        return -1;
    }

    //獲取視頻文件信息
    if (avformat_find_stream_info(pFormatCtx,NULL) < 0){
        LOGE("Couldn't find stream information.\n");
        return -1;
    }

    videoindex = -1;
    ///遍歷視音頻流的個(gè)數(shù)
    for (int i = 0; i < pFormatCtx -> nb_streams; i++) {
        if (pFormatCtx->streams[i]/*視音頻流*/->codec->codec_type == AVMEDIA_TYPE_VIDEO){
            videoindex = i;
            break;
        }
    }

    if(videoindex==-1){
        LOGE("Couldn't find a video stream.\n");
        return -1;
    }

    //指向AVCodecContext的指針
    pCodecCtx = pFormatCtx->streams[videoindex]->codec;
    //指向AVCodec的指針.查找解碼器
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (pCodec == NULL)
    {
        LOGE("Couldn't find Codec.\n");
        return -1;
    }

    //打開解碼器
    if (avcodec_open2(pCodecCtx,pCodec,NULL) < 0){
        LOGE("Couldn't open codec.\n");
        return -1;
    }

    //用來保存數(shù)據(jù)緩存的對像
    pFrame = av_frame_alloc();
    pFrameYUV = av_frame_alloc();

    out_buffer=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P,  pCodecCtx->width, pCodecCtx->height,1));
    av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize,out_buffer,
                         AV_PIX_FMT_YUV420P,pCodecCtx->width, pCodecCtx->height,1);

    pPacket = (AVPacket *)av_malloc(sizeof(AVPacket));

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

    sprintf(info,   "[Input     ]%s\n", input_str);
    sprintf(info, "%s[Output    ]%s\n",info,output_str);
    sprintf(info, "%s[Format    ]%s\n",info, pFormatCtx->iformat->name);
    sprintf(info, "%s[Codec     ]%s\n",info, pCodecCtx->codec->name);
    sprintf(info, "%s[Resolution]%dx%d\n",info, pCodecCtx->width,pCodecCtx->height);

    fp_yuv=fopen(output_str,"wb+");
    if(fp_yuv==NULL){
        printf("Cannot open output file.\n");
        return -1;
    }

    frame_cnt = 0;
    time_start = clock();

    while(av_read_frame(pFormatCtx, pPacket) >= 0){
        if(pPacket->stream_index == videoindex){
            ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, pPacket);
            if(ret < 0){
                LOGE("Decode Error.\n");
                return -1;
            }
            if(got_picture){
                sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                          pFrameYUV->data, pFrameYUV->linesize);

                y_size=pCodecCtx->width*pCodecCtx->height;
                fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y
                fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U
                fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V
                //Output info
                char pictype_str[10]={0};
                switch(pFrame->pict_type){
                    case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
                    case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
                    case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
                    default:sprintf(pictype_str,"Other");break;
                }
                LOGI("Frame Index: %5d. Type:%s",frame_cnt,pictype_str);
                frame_cnt++;
            }
        }
        av_free_packet(pPacket);
    }

    while (1) {
        ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, pPacket);
        if (ret < 0)
            break;
        if (!got_picture)
            break;
        sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height,
                  pFrameYUV->data, pFrameYUV->linesize);
        int y_size=pCodecCtx->width*pCodecCtx->height;
        fwrite(pFrameYUV->data[0],1,y_size,fp_yuv);    //Y
        fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv);  //U
        fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv);  //V
        //Output info
        char pictype_str[10]={0};
        switch(pFrame->pict_type){
            case AV_PICTURE_TYPE_I:sprintf(pictype_str,"I");break;
            case AV_PICTURE_TYPE_P:sprintf(pictype_str,"P");break;
            case AV_PICTURE_TYPE_B:sprintf(pictype_str,"B");break;
            default:sprintf(pictype_str,"Other");break;
        }
        LOGI("Frame Index: %5d. Type:%s",frame_cnt,pictype_str);
        frame_cnt++;
    }

    time_finish = clock();
    time_duration=(double)(time_finish - time_start);

    sprintf(info, "%s[Time      ]%fms\n",info,time_duration);
    sprintf(info, "%s[Count     ]%d\n",info,frame_cnt);

    sws_freeContext(img_convert_ctx);

    fclose(fp_yuv);

    av_frame_free(&pFrameYUV);
    av_frame_free(&pFrame);
    avcodec_close(pCodecCtx);
    avformat_close_input(&pFormatCtx);
    return 0;
}

#ifdef __cplusplus
}
#endif

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button button;
    private EditText editText1,editText2;
    private DecodeAsyncTask myTask;
    String folderurl;
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        folderurl = Environment.getExternalStorageDirectory().getPath();

        myTask = new DecodeAsyncTask();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String inputurl=folderurl+"/"+editText1.getText().toString();
                final String outputurl=folderurl+"/"+editText2.getText().toString();
                Log.d(TAG,"" + inputurl + "         " + outputurl);
                myTask.execute(inputurl,outputurl);
            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        //如果異步任務(wù)不為空 并且狀態(tài)是 運(yùn)行時(shí)  ,就把他取消這個(gè)加載任務(wù)
        if(myTask !=null && myTask.getStatus() == AsyncTask.Status.RUNNING){
            myTask.cancel(true);
        }
    }
    private class DecodeAsyncTask extends AsyncTask<String,Void,Void>{

        @Override
        protected Void doInBackground(String... strings) {
            FFmpeg.decode(strings[0],strings[1]);
            return null;
        }
    }
}
運(yùn)行截圖

源碼地址:https://github.com/Xiaoben336/FFmpegDecoder

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

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

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