public class VideoUtils {
public native static void decode(String input, String output);
//注意加载顺序
static {
System.loadLibrary(“hxg_ffmpeg”);
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”);
}
}
/* 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();
//封装格式上下文,统领全局的结构体,保存了视频文件封装格式的相关信息
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);
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(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, 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);
LOGI(“解码完成了”);
}
- 配置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)
find_library( log-lib
log )
导入路径,为了让编译时能够寻找到这个文件夹
include_directories(src/main/cpp/include)
set(my_lib_path ${CMAKE_SOURCE_DIR}/libs)
添加三方的so库
add_library(libavcodec
SHARED
IMPORTED )
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
最后
这里我特地整理了一份《Android开发核心知识点笔记》,里面就包含了自定义View相关的内容
除了这份笔记,还给大家分享 Android学习PDF+架构视频+面试文档+源码笔记,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料这几块的内容。非常适合近期有面试和想在技术道路上继续精进的朋友。
分享上面这些资源,希望可以帮助到大家提升进阶,如果你觉得还算有用的话,不妨把它们推荐给你的朋友~
喜欢本文的话,给我点个小赞、评论区留言或者转发支持一下呗~
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
y-1712803240722)]
最后
这里我特地整理了一份《Android开发核心知识点笔记》,里面就包含了自定义View相关的内容
[外链图片转存中…(img-X6X2cDko-1712803240722)]
除了这份笔记,还给大家分享 Android学习PDF+架构视频+面试文档+源码笔记,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料这几块的内容。非常适合近期有面试和想在技术道路上继续精进的朋友。
[外链图片转存中…(img-EsViUpHK-1712803240722)]
分享上面这些资源,希望可以帮助到大家提升进阶,如果你觉得还算有用的话,不妨把它们推荐给你的朋友~
喜欢本文的话,给我点个小赞、评论区留言或者转发支持一下呗~
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-17tbiERq-1712803240723)]