javacv 接收RTSP流(或avi/mp4视频文件文件),输出byte[]
2021-03-30 00:26
标签:finally extends turn 数组 err vat dep write bool javacv 接收RTSP流(或avi/mp4视频文件文件),输出byte[] 标签:finally extends turn 数组 err vat dep write bool 原文地址:https://www.cnblogs.com/zh-peng/p/13596392.html1.maven依赖
dependency>
groupId>org.bytedecogroupId>
artifactId>javacv-platformartifactId>
version>1.5.1version>
type>pomtype>
dependency>
2.线程类
1 package com.bs.thread;
2
3 import com.bs.controller.BsDataController;
4 import org.bytedeco.javacv.FFmpegFrameGrabber;
5 import org.bytedeco.javacv.Frame;
6 import org.bytedeco.javacv.FrameGrabber;
7 import org.bytedeco.javacv.Java2DFrameConverter;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.scheduling.annotation.Async;
11 import javax.imageio.ImageIO;
12 import java.awt.image.BufferedImage;
13 import java.io.ByteArrayOutputStream;
14 import java.io.IOException;
15
16 public class RtspToJpgThread extends Thread {
17
18 public String FLAG="CLOSE";
19
20 private String rtspUrl;
21
22 private String rtspTransportType="udp";
23
24 private FFmpegFrameGrabber grabber;
25
26 private final Logger log = LoggerFactory.getLogger(this.getClass());
27
28 private boolean isStart = false;
29
30 /**
31 * 视频帧率
32 */
33 private int frameRate = 30;
34 /**
35 * 视频宽度
36 */
37 private int frameWidth = 480;
38 /**
39 * 视频高度
40 */
41 private int frameHeight = 270;
42
43 public RtspToJpgThread(String rtspUrl){
44 this.rtspUrl=rtspUrl;
45 }
46
47 @Override
48 public void run() {
49 live();
50 }
51
52
53
54 /**
55 * 开启获取rtsp流,通过websocket传输数据
56 */
57 @Async
58 public void live() {
59 log.info("连接rtsp:"+rtspUrl+",开始创建grabber");
60 grabber = createGrabber(rtspUrl);
61 if (grabber != null) {
62 log.info("创建grabber成功");
63 } else {
64 log.info("创建grabber失败");
65 }
66 startCameraPush();
67 }
68
69 /**
70 * 构造视频抓取器
71 *
72 * @param rtsp 拉流地址
73 * @return
74 */
75 public FFmpegFrameGrabber createGrabber(String rtsp) {
76 // 获取视频源
77 try {
78 FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(rtsp);
79 grabber.setOption("rtsp_transport", rtspTransportType);
80 //设置帧率
81 grabber.setFrameRate(frameRate);
82 //设置获取的视频宽度
83 grabber.setImageWidth(frameWidth);
84 //设置获取的视频高度
85 grabber.setImageHeight(frameHeight);
86 //设置视频bit率
87 grabber.setVideoBitrate(2000000);
88 return grabber;
89 } catch (FrameGrabber.Exception e) {
90 log.error("创建解析rtsp FFmpegFrameGrabber 失败");
91 log.error("create rtsp FFmpegFrameGrabber exception: ", e);
92 return null;
93 }
94 }
95
96 /**
97 * 推送图片(摄像机直播)
98 */
99 public void startCameraPush() {
100 Java2DFrameConverter java2DFrameConverter = new Java2DFrameConverter();
101 while (true) {
102 if (grabber == null) {
103 log.info("重试连接rtsp:"+rtspUrl+",开始创建grabber");
104 grabber = createGrabber(rtspUrl);
105 log.info("创建grabber成功");
106 }
107 try {
108 if (grabber != null && !isStart) {
109 grabber.start();
110 isStart = true;
111 log.info("启动grabber成功");
112 }
113 if (grabber != null) {
114 Frame frame = grabber.grabImage();
115 if (null == frame) {
116 continue;
117 }
118 BufferedImage bufferedImage = java2DFrameConverter.getBufferedImage(frame);
119 byte[] bytes = imageToBytes(bufferedImage, "jpg");
120 //使用websocket发送视频帧数据
121 if("OPEN".equals(FLAG)){
122 //log.info("============ 线程"+rtspUrl+"发出数据 ============");
123 BsDataController.ws.sendAllByObject(bytes);
124 }
125
126 }
127 } catch (FrameGrabber.Exception | RuntimeException e) {
128 log.error("因为异常,grabber关闭,rtsp连接断开,尝试重新连接");
129 log.error("exception : " , e);
130 if (grabber != null) {
131 try {
132 grabber.stop();
133 } catch (FrameGrabber.Exception ex) {
134 log.error("grabber stop exception: ", ex);
135 } finally {
136 grabber = null;
137 isStart = false;
138 }
139 }
140 }
141 }
142 }
143
144 /**
145 * 图片转字节数组
146 *
147 * @param bImage 图片数据
148 * @param format 格式
149 * @return 图片字节码
150 */
151 private byte[] imageToBytes(BufferedImage bImage, String format) {
152 ByteArrayOutputStream out = new ByteArrayOutputStream();
153 try {
154 ImageIO.write(bImage, format, out);
155 } catch (IOException e) {
156 log.error("bufferImage 转 byte 数组异常");
157 log.error("bufferImage transfer byte[] exception: ", e);
158 return null;
159 }
160 return out.toByteArray();
161 }
162 }
文章标题:javacv 接收RTSP流(或avi/mp4视频文件文件),输出byte[]
文章链接:http://soscw.com/index.php/essay/69750.html