蜂信物联FastBee平台https://gitee.com/beecue/fastbee
阿里资料开源项目https://gitee.com/vip204888
百度低代码前端框架https://gitee.com/baidu/amis
OpenHarmony开源项目https://gitcode.com/openharmony
仓颉编程语言开放项目https://gitcode.com/Cangjie
/**
-
生成解封装上下文
-
@param url
-
@return
*/
private AVFormatContext getFormatContext(String url) {
// 解封装上下文
AVFormatContext pFormatCtx = new avformat.AVFormatContext(null);
// 打开流媒体
if (avformat_open_input(pFormatCtx, url, null, null) != 0) {
log.error(“打开媒体失败”);
return null;
}
// 读取流媒体数据,以获得流的信息
if (avformat_find_stream_info(pFormatCtx, (PointerPointer) null) < 0) {
log.error(“获得媒体流信息失败”);
return null;
}
return pFormatCtx;
}
- 流媒体解封装后有一个保存了所有流的数组,getVideoStreamIndex方法会找到视频流在数组中的位置:
/**
-
流媒体解封装后得到多个流组成的数组,该方法找到视频流咋数组中的位置
-
@param pFormatCtx
-
@return
*/
private static int getVideoStreamIndex(AVFormatContext pFormatCtx) {
int videoStream = -1;
// 解封装后有多个流,找出视频流是第几个
for (int i = 0; i < pFormatCtx.nb_streams(); i++) {
if (pFormatCtx.streams(i).codec().codec_type() == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
}
return videoStream;
}
- 解封装之后就是解码,getCodecContext方法得到解码上下文对象:
/**
-
生成解码上下文
-
@param pFormatCtx
-
@param videoStreamIndex
-
@return
*/
private AVCodecContext getCodecContext(AVFormatContext pFormatCtx, int videoStreamIndex) {
//解码器
AVCodec pCodec;
// 得到解码上下文
AVCodecContext pCodecCtx = pFormatCtx.streams(videoStreamIndex).codec();
// 根据解码上下文得到解码器
pCodec = avcodec_find_decoder(pCodecCtx.codec_id());
if (pCodec == null) {
return null;
}
// 用解码器来初始化解码上下文
if (avcodec_open2(pCodecCtx, pCodec, (AVDictionary)null) < 0) {
return null;
}
return pCodecCtx;
}
- 紧接着从视频流解码取帧解码:
/**
-
取一帧然后解码
-
@param pCodecCtx
-
@param pFormatCtx
-
@param videoStreamIndex
-
@return
*/
private AVFrame getSingleFrame(AVCodecContext pCodecCtx, AVFormatContext pFormatCtx, int videoStreamIndex) {
// 分配帧对象
AVFrame pFrame = av_frame_alloc();
// frameFinished用于检查是否有图像
int[] frameFinished = new int[1];
// 是否找到的标志
boolean exists = false;
AVPacket packet = new AVPacket();
try {
// 每一次while循环都会读取一个packet
while (av_read_frame(pFormatCtx, packet) >= 0) {
// 检查packet所属的流是不是视频流
if (packet.stream_index() == videoStreamIndex) {
// 将AVPacket解码成AVFrame
avcodec_decode_video2(pCodecCtx, pFrame, frameFinished, packet);// Decode video frame
// 如果有图像就返回
if (frameFinished != null && frameFinished[0] != 0 && !pFrame.isNull()) {
exists = true;
break;
}
}
}
} finally {
// 一定要执行释放操作
av_free_packet(packet);
}
// 找不到就返回空
return exists ? pFrame : null;
}
- 解码后的图像是YUV420P格式,咱们将其转成YUVJ420P:
/**
-
将YUV420P格式的图像转为YUVJ420P格式
-
@param pCodecCtx 解码上下文
-
@param sourceFrame 源数据
-
@return 转换后的帧极其对应的数据指针
*/
private static FrameData YUV420PToYUVJ420P(AVCodecContext pCodecCtx, AVFrame sourceFrame) {
// 分配一个帧对象,保存从YUV420P转为YUVJ420P的结果
AVFrame pFrameRGB = av_frame_alloc();
if (pFrameRGB == null) {
return null;
}
int width = pCodecCtx.width(), height = pCodecCtx.height();
// 一些参数设定
pFrameRGB.width(width);
pFrameRGB.height(height);
pFrameRGB.format(AV_PIX_FMT_YUVJ420P);
// 计算转为YUVJ420P之后的图片字节数
int numBytes = avpicture_get_size(AV_PIX_FMT_YUVJ420P, width, height);
// 分配内存
BytePointer buffer = new BytePointer(av_malloc(numBytes));
// 图片处理工具的初始化操作
SwsContext sws_ctx = sws_getContext(width, height, pCodecCtx.pix_fmt(), width, height, AV_PIX_FMT_YUVJ420P, SWS_BICUBIC, null, null, (DoublePointer) null);
// 将pFrameRGB的data指针指向刚才分配好的内存(即buffer)
avpicture_fill(new avcodec.AVPicture(pFrameRGB), buffer, AV_PIX_FMT_YUVJ420P, width, height);
// 转换图像格式,将解压出来的YUV420P的图像转换为YUVJ420P的图像
sws_scale(sws_ctx, sourceFrame.data(), sourceFrame.linesize(), 0, height, pFrameRGB.data(), pFrameRGB.linesize());
// 及时释放
sws_freeContext(sws_ctx);
// 将AVFrame和BytePointer打包到FrameData中返回,这两个对象都要做显示的释放操作
return new FrameData(pFrameRGB, buffer);
}
- 然后就是另一个很重要方法saveImg,里面是典型的编码和输出流程,咱们前面已经了解了打开媒体流解封装解码的操作,现在要看看怎么制作媒体流,包括编码、封装和输出:
/**
-
将传入的帧以图片的形式保存在指定位置
-
@param pFrame
-
@param out_file
-
@return 小于0表示失败
*/
private int saveImg(avutil.AVFrame pFrame, String out_file) {
av_log_set_level(AV_LOG_ERROR);//设置FFmpeg日志级别(默认是debug,设置成error可以屏蔽大多数不必要的控制台消息)
AVPacket pkt = null;
AVStream pAVStream = null;
int width = pFrame.width(), height = pFrame.height();
// 分配AVFormatContext对象
avformat.AVFormatContext pFormatCtx = avformat_alloc_context();
// 设置输出格式(涉及到封装和容器)
pFormatCtx.oformat(av_guess_format(“mjpeg”, null, null));
if (pFormatCtx.oformat() == null) {
log.error(“输出媒体流的封装格式设置失败”);
return -1;
}
try {
// 创建并初始化一个和该url相关的AVIOContext
avformat.AVIOContext pb = new avformat.AVIOContext();
// 打开输出文件
if (avio_open(pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
log.info(“输出文件打开失败”);
return -1;
}
// 封装之上是协议,这里将封装上下文和协议上下文关联
pFormatCtx.pb(pb);
// 构建一个新stream
pAVStream = avformat_new_stream(pFormatCtx, null);
if (pAVStream == null) {
log.error(“将新的流放入媒体文件失败”);
return -1;
}
int codec_id = pFormatCtx.oformat().video_codec();
// 设置该stream的信息
avcodec.AVCodecContext pCodecCtx = pAVStream.codec();
pCodecCtx.codec_id(codec_id);
pCodecCtx.codec_type(AVMEDIA_TYPE_VIDEO);
pCodecCtx.pix_fmt(AV_PIX_FMT_YUVJ420P);
pCodecCtx.width(width);
pCodecCtx.height(height);
pCodecCtx.time_base().num(1);
pCodecCtx.time_base().den(25);
// 打印媒体信息
av_dump_format(pFormatCtx, 0, out_file, 1);
// 查找解码器
avcodec.AVCodec pCodec = avcodec_find_encoder(codec_id);
if (pCodec == null) {
log.info(“获取解码器失败”);
return -1;
}
// 用解码器来初始化解码上下文
if (avcodec_open2(pCodecCtx, pCodec, (PointerPointer) null) < 0) {
log.error(“解码上下文初始化失败”);
return -1;
}
// 输出的Packet
pkt = new avcodec.AVPacket();
// 分配
if (av_new_packet(pkt, width * height * 3) < 0) {
return -1;
}
int[] got_picture = { 0 };
// 把流的头信息写到要输出的媒体文件中
avformat_write_header(pFormatCtx, (PointerPointer) null);
// 把帧的内容进行编码
if (avcodec_encode_video2(pCodecCtx, pkt, pFrame, got_picture)<0) {
log.error(“把帧编码为packet失败”);
return -1;
}
// 输出一帧
if ((av_write_frame(pFormatCtx, pkt)) < 0) {
log.error(“输出一帧失败”);
return -1;
}
// 写文件尾
if (av_write_trailer(pFormatCtx) < 0) {
log.error(“写文件尾失败”);
return -1;
}
return 0;
} finally {
// 资源清理
release(false, pkt, pFormatCtx.pb(), pAVStream.codec(), pFormatCtx);
}
}
- 最后是释放资源的操作,请注意释放不同对象要用到的API也不同,另外AVFormatContext的场景不同用到的API也不同(输入输出场景),用错了就会crash,另外release方法一共被调用了两次,也就说打开媒体流和输出媒体流用到的资源和对象,最终都需要释放和回收:
/**
-
释放资源,顺序是先释放数据,再释放上下文
-
@param pCodecCtx
-
@param pFormatCtx
-
@param ptrs
*/
private void release(boolean isInput, AVPacket pkt, AVIOContext pb, AVCodecContext pCodecCtx, AVFormatContext pFormatCtx, Pointer…ptrs) {
if (null!=pkt) {
av_free_packet(pkt);
}
// 解码后,这是个数组,要遍历处理
if (null!=ptrs) {
Arrays.stream(ptrs).forEach(avutil::av_free);
}
// 解码
if (null!=pCodecCtx) {
avcodec_close(pCodecCtx);
}
// 解协议
if (null!=pb) {
avio_close(pb);
}
// 解封装
if (null!=pFormatCtx) {
if (isInput) {
avformat_close_input(pFormatCtx);
} else {
avformat_free_context(pFormatCtx);
}
}
}
- 最后写个main方法,调用openMediaAndSaveImage试试,传入媒体流的地址,以及存放图片的路径:
public static void main(String[] args) throws Exception {
// CCTV13,1920*1080分辨率,不稳定,打开失败时请多试几次
String url = “http://ivi.bupt.edu.cn/hls/cctv13hd.m3u8”;
// 安徽卫视,1024*576分辨率,较为稳定
// String url = “rtmp://58.200.131.2:1935/livetv/ahtv”;
// 本地视频文件,请改为您自己的本地文件地址
// String url = “E:\temp\202107\24\test.mp4”;
// 完整图片存放路径,注意文件名是当前的年月日时分秒
String localPath = “E:\temp\202107\24\save\” + new SimpleDateFormat(“yyyyMMddHHmmss”).format(new Date()) + “.jpg”;
// 开始操作
new Stream2Image().openMediaAndSaveImage(url, localPath);
}
言尽于此,完结
无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。
- 第一,设计模式能让专业人之间交流方便,如下:
程序员A:这里我用了XXX设计模式
程序员B:那我大致了解你程序的设计思路了
- 第二,易维护
项目经理:今天客户有这样一个需求…
程序员:明白了,这里我使用了XXX设计模式,所以改起来很快
- 第三,设计模式是编程经验的总结
程序员A:B,你怎么想到要这样去构建你的代码
程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
搜集费时费力,能看到此处的都是真爱!
mpleDateFormat(“yyyyMMddHHmmss”).format(new Date()) + “.jpg”;
// 开始操作
new Stream2Image().openMediaAndSaveImage(url, localPath);
}
言尽于此,完结
无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。
- 第一,设计模式能让专业人之间交流方便,如下:
程序员A:这里我用了XXX设计模式
程序员B:那我大致了解你程序的设计思路了
- 第二,易维护
项目经理:今天客户有这样一个需求…
程序员:明白了,这里我使用了XXX设计模式,所以改起来很快
- 第三,设计模式是编程经验的总结
程序员A:B,你怎么想到要这样去构建你的代码
程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
[外链图片转存中…(img-ZlrKUdQ8-1725177033655)]
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
[外链图片转存中…(img-Zf6DPFJF-1725177033656)]
搜集费时费力,能看到此处的都是真爱!