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);</