NDK开发(六),深入理解kotlin协程pdf

这篇博客深入探讨了在Android NDK开发中使用Kotlin协程处理FFmpeg库的方法。通过CMakeLists.txt配置引入第三方SO库,并展示了如何在Java层与JNI层交互进行视频解码。文中详细介绍了如何打开输入视频文件、获取流信息、找到解码器、解码视频帧并保存为YUV格式。同时,还涉及到错误处理和资源释放。
摘要由CSDN通过智能技术生成

System.loadLibrary(“postproc-53”);

System.loadLibrary(“avfilter-5”);

System.loadLibrary(“avdevice-56”);

}

}

/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class com_qufu_ffmpeg_player_utils_VideoUtils */

#ifndef _Included_com_qufu_ffmpeg_player_utils_VideoUtils

#define _Included_com_qufu_ffmpeg_player_utils_VideoUtils

#ifdef __cplusplus

extern “C” {

#endif

/*

  • Class: com_qufu_ffmpeg_player_utils_VideoUtils

  • Method: decode

  • Signature: (Ljava/lang/String;Ljava/lang/String;)V

*/

JNIEXPORT void JNICALL Java_com_qufu_ffmpeg_1player_utils_VideoUtils_decode

(JNIEnv *, jclass, jstring, jstring);

#ifdef __cplusplus

}

#endif

#endif

  • JNI实现

//

// Created by yuanxx on 2019/9/27.

//

#include “com_qufu_ffmpeg_player_utils_VideoUtils.h”

#include <android/log.h>

//编码

#include “include/libavcodec/avcodec.h”

//封装格式处理

#include “include/libavformat/avformat.h”

//像素处理

#include “include/libswscale/swscale.h”

#define LOGI(FORMAT,…) __android_log_print(ANDROID_LOG_INFO,“huangxiaoguo”,FORMAT,##VA_ARGS);

#define LOGE(FORMAT,…) __android_log_print(ANDROID_LOG_ERROR,“huangxiaoguo”,FORMAT,##VA_ARGS);

JNIEXPORT void JNICALL Java_com_qufu_ffmpeg_1player_utils_VideoUtils_decode

(JNIEnv *env, jclass jcls, jstring input_jstr, jstring output_jstr){

//需要转码的视频文件(输入的视频文件)

const char* input_cstr = (*env)->GetStringUTFChars(env,input_jstr,NULL);

const char* output_cstr = (*env)->GetStringUTFChars(env,output_jstr,NULL);

//1.注册所有组件

av_register_all();

//封装格式上下文,统领全局的结构体,保

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

存了视频文件封装格式的相关信息

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(“视频时长:%d”, (pFormatCtx->duration)/1000000);

LOGI(“视频的宽高:%d,%d”,pCodecCtx->width,pCodecCtx->height);</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值