美文网首页
FFmpeg解码MP4文件为YUV文件

FFmpeg解码MP4文件为YUV文件

作者: 张俊峰0613 | 来源:发表于2018-12-22 12:18 被阅读0次

前言

前面我学了编译FFmpeg的Android库,写了一个命令行使用FFmpeg的Android Demo,C文件都在虚拟机实现,然后ndk编译成so库,再导入Android studio使用,Android代码中没有C/C++文件,很纯净的样子。但是,在虚拟机写C代码的时候,没有自动补全功能,很不方便。所以这次用Cmake编译JNI,直接在Android studio中使用代码补全功能!

1、编译Android版FFmpeg并移植到Android studio中

1.1、编译FFmpeg的Android库

Android FFmpeg JNI开发入门_编译不同ABI的so库

1.2、创建一个支持C++的Android项目

把红框的选项都勾选上


生成之后的项目目录


1.2、将so库移植到Android项目中

在libs目录下新建对应ABI的文件夹,比如armeabi-v7a,然后将编译生成的对应ABI的so库拷贝进去;
再将头文件拷贝进去,就是生成的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中实现头文件的方法;


2.3、实现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

这样就有了自动补全的功能;


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并注册编译进avformat库里面所有的复用器(muxers),
    解复用器(demuxers)和协议模块*/
    av_register_all();
    /**网络功能的全局初始化(可选的,在使用网络协议时有必要调用)*/
    avformat_network_init();
    //初始化一个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;
    ///遍历视音频流的个数
    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;
    }

    //用来保存数据缓存的对像
    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();
        //如果异步任务不为空 并且状态是 运行时  ,就把他取消这个加载任务
        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;
        }
    }
}
运行截图

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

相关文章

网友评论

      本文标题:FFmpeg解码MP4文件为YUV文件

      本文链接:https://www.haomeiwen.com/subject/icfikqtx.html