Android音视频三-AndroidStudio整合FFmpeg项目+FFmpeg视频解码

参数

  • 编辑器 AS3.0
  • NDK最新版

整合步骤

  1. 首先AS要下载ndk-bundle,这个比较简单在设置里面就可以下载,下载完成以后新建项目时候勾选上inclue C++选项。
  2. 由于对Cmake不是特别熟悉,所以项目里面我用的是Makefile来编译整个项目。其实Cmake最后也是生成Makefile文件来编译整个项目。项目里面就需要在main目录下新建jni文件夹,如下图:
    image.png
  3. jni文件夹新建完成以后,将我们编译生成的动态库以及头文件直接拷贝到jni目录下,动态库的编译请参考动态库的编译
  4. 进行到这里第一步的工作算是完成了,接下来就是新建一个java类,在里面编写native方法。将java和c之间建立一个桥梁.内容如下

    public class Player {
        public static native void palyVedio(String input,String output);
    }
  5. 随着AS版本的不断升级,已经不用再去手动的生成头文件,直接Alt+Enter快捷键就会生成player.c的C文件。当然你也可以自己去手动的生成头文件。

  6. 接下来是非常重要的一步,就是编写Android.mk文件和Application.mk文件,mk文件内容如下
    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)

#myapp
include $(CLEAR_VARS)
LOCAL_MODULE := myffmpeg
LOCAL_SRC_FILES := player.c
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postproc swresample swscale
include $(BUILD_SHARED_LIBRARY)

Application.mk

APP_ABI := armeabi
APP_PLATFORM := android-8

7.接下来就是要到build.grale中去设置mk文件。

android {
···
    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }
}
    defaultConfig {
     ···
        ndk {
            // 设置支持的SO库架构
            abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
            moduleName "myffmpeg"
            ldLibs "log", "z", "m", "jnigraphics", "android"
        }
    }

到这里就一切准备就绪了,接下来就是测试我们整合是是否正确。点击Rebuild Project 在Gradle Console查看,如果显示的是Sucess就说明已经配置成功了。如下图:

image.png

8 接下来就是愉快的编码时间,这里就不再过多的解析,注释已经写得很清晰。有不懂的朋友我们再交流。

    #include <jni.h>
    #include <libavformat/avformat.h>
    #include <android/log.h>
    #include <libswscale/swscale.h>

    #define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO,"^_^",FORMAT,##__VA_ARGS__);
    #define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR,">_<",FORMAT,##__VA_ARGS__);
    JNIEXPORT void JNICALL
    Java_shixin_com_myffmpeg_Player_palyVedio(JNIEnv *env, jclass type, jstring input_jstr,
                                              jstring output_jstr) {
        const char *input_cstr = (*env)->GetStringUTFChars(env, input_jstr, 0);
        const char *output_cstr = (*env)->GetStringUTFChars(env, output_jstr, 0);
        //1.注册所有组件
        av_register_all();

        //封装格式上下文,统领全局的结构体,保存了视频文件封装格式的相关信息
        AVFormatContext *pFormatCtx = avformat_alloc_context();

        //2.打开输入视频文件
        if (avformat_open_input(&pFormatCtx, input_cstr, NULL, NULL) != 0)
        {
            LOGE("%s","无法打开输入视频文件");
            return;
        }

        //3.获取视频文件信息
        if (avformat_find_stream_info(pFormatCtx,NULL) < 0)
        {
            LOGE("%s","无法获取视频文件信息");
            return;
        }

        //获取视频流的索引位置
        //遍历所有类型的流(音频流、视频流、字幕流),找到视频流
        int v_stream_idx = -1;
        int i = 0;
        //number of streams
        for (; i < pFormatCtx->nb_streams; i++)
        {
            //流的类型
            if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            {
                v_stream_idx = i;
                break;
            }
        }

        if (v_stream_idx == -1)
        {
            LOGE("%s","找不到视频流\n");
            return;
        }

        //只有知道视频的编码方式,才能够根据编码方式去找到解码器
        //获取视频流中的编解码上下文
        AVCodecContext *pCodecCtx = pFormatCtx->streams[v_stream_idx]->codec;
        //4.根据编解码上下文中的编码id查找对应的解码
        AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
        //(迅雷看看,找不到解码器,临时下载一个解码器)
        if (pCodec == NULL)
        {
            LOGE("%s","找不到解码器\n");
            return;
        }

        //5.打开解码器
        if (avcodec_open2(pCodecCtx,pCodec,NULL)<0)
        {
            LOGE("%s","解码器无法打开\n");
            return;
        }

        //输出视频信息
        LOGI("视频的文件格式:%s",pFormatCtx->iformat->name);
        LOGI("视频时长:" PRId64 "\n", (pFormatCtx->duration)/1000000);
        LOGI("视频的宽高:%d,%d",pCodecCtx->width,pCodecCtx->height);
        LOGI("解码器的名称:%s",pCodec->name);

        //准备读取
        //AVPacket用于存储一帧一帧的压缩数据(H264)
        //缓冲区,开辟空间
        AVPacket *packet = (AVPacket*)av_malloc(sizeof(AVPacket));

        //AVFrame用于存储解码后的像素数据(YUV)
        //内存分配
        AVFrame *pFrame = av_frame_alloc();
        //YUV420
        AVFrame *pFrameYUV = av_frame_alloc();
        //只有指定了AVFrame的像素格式、画面大小才能真正分配内存
        //缓冲区分配内存
        uint8_t *out_buffer = (uint8_t *)av_malloc(
                (size_t) avpicture_get_size(AV_PIX_FMT_YUV420P,
                                            pCodecCtx->width, pCodecCtx->height));
        //初始化缓冲区
        avpicture_fill((AVPicture *)pFrameYUV,
                       out_buffer,
                       AV_PIX_FMT_YUV420P,
                       pCodecCtx->width,
                       pCodecCtx->height);

        //用于转码(缩放)的参数,转之前的宽高,转之后的宽高,格式等
        struct SwsContext *sws_ctx = sws_getContext(pCodecCtx->width,
                                                    pCodecCtx->height,
                                                    pCodecCtx->pix_fmt,
                                                    pCodecCtx->width,
                                                    pCodecCtx->height,
                                                    AV_PIX_FMT_YUV420P,
                                                    SWS_BICUBIC, NULL, NULL, NULL);


        int got_picture, ret;

        FILE *fp_yuv = fopen(output_cstr, "wb+");

        int frame_count = 0;

        //6.一帧一帧的读取压缩数据
        while (av_read_frame(pFormatCtx, packet) >= 0)
        {
            //只要视频压缩数据(根据流的索引位置判断)
            if (packet->stream_index == v_stream_idx)
            {
                //7.解码一帧视频压缩数据,得到视频像素数据
                ret = avcodec_decode_video2(pCodecCtx,
                                            pFrame,
                                            &got_picture,
                                            packet);
                if (ret < 0)
                {
                    LOGE("%s","解码错误");
                    return;
                }

                //为0说明解码完成,非0正在解码
                if (got_picture)
                {
                    //AVFrame转为像素格式YUV420,宽高
                    //2 6输入、输出数据
                    //3 7输入、输出画面一行的数据的大小 AVFrame 转换是一行一行转换的
                    //4 输入数据第一列要转码的位置 从0开始
                    //5 输入画面的高度
                    sws_scale(sws_ctx,
                              (const uint8_t *const *) pFrame->data,
                              pFrame->linesize, 0,
                              pCodecCtx->height,
                              pFrameYUV->data,
                              pFrameYUV->linesize);

                    //输出到YUV文件
                    //AVFrame像素帧写入文件
                    //data解码后的图像像素数据(音频采样数据)
                    //Y 亮度 UV 色度(压缩了) 人对亮度更加敏感
                    //U V 个数是Y的1/4
                    int y_size = pCodecCtx->width * pCodecCtx->height;
                    fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv);
                    fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv);
                    fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv);

                    frame_count++;
                    LOGI("解码第%d帧",frame_count);
                }
            }

            //释放资源
            av_free_packet(packet);
        }

        fclose(fp_yuv);

        (*env)->ReleaseStringUTFChars(env,input_jstr,input_cstr);
        (*env)->ReleaseStringUTFChars(env,output_jstr,output_cstr);

        av_frame_free(&pFrame);

        avcodec_close(pCodecCtx);

        avformat_free_context(pFormatCtx);
    }

接下下来就是加载动态库,同时不要忘记加上权限

public class Player {

    public static native void palyVedio(String input,String output);

    static{
        System.loadLibrary("avutil-54");
        System.loadLibrary("swresample-1");
        System.loadLibrary("avcodec-56");
        System.loadLibrary("avformat-56");
        System.loadLibrary("swscale-3");
        System.loadLibrary("postproc-53");
        System.loadLibrary("avfilter-5");
        System.loadLibrary("avdevice-56");
        System.loadLibrary("myffmpeg");
    }
}

运行时所需的权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

测试代码

        String input = new File(Environment.getExternalStorageDirectory(),"12.mp4").getAbsolutePath();
        String output = new File(Environment.getExternalStorageDirectory(),"output_yuv420p.yuv").getAbsolutePath();
        Player.palyVedio(input, output);

补充

上面解码生产的是yuv文件,需要下载yuv播放器才可以查看是否解码成功。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值