[关闭]
@liruiyi962464 2025-06-16T02:07:24.000000Z 字数 3920 阅读 22

Jeecg-Boot 框架中 RTSP 视频流处理解决方案

朔黄


Jeecg-Boot 框架中实现 RTSP 视频流的处理,需结合后端转码、流媒体服务器分发以及前端播放。以下是详细解决方案:


一、系统架构设计

1. 架构图

  1. RTSP 源(摄像头) FFmpeg 转码 流媒体服务器(Nginx-RTMP/SRS HTTP-FLV/HLS/WebSocket-FLV Jeecg-Boot 前端播放

2. 核心组件


二、后端实现(Spring Boot)

1. 搭建转码服务

使用 FFmpeg 将 RTSP 流转码为 HTTP-FLV 或 WebSocket-FLV。

示例 1:HTTP-FLV 转码

  1. ffmpeg -i rtsp://admin:password@192.168.1.64:554/h264/ch1/main/av_stream \
  2. -c:v libx264 -preset fast -crf 23 \
  3. -c:a aac -f flv \
  4. http://localhost:8080/live/stream.flv

示例 2:WebSocket-FLV 转码

  1. ffmpeg -i rtsp://admin:password@192.168.1.64:554/h265/ch1/main/av_stream \
  2. -c:v libx265 -preset fast -crf 28 \
  3. -c:a aac -f flv \
  4. - | websockify --web /path/to/web 8081

2. 配置 Nginx-RTMP

nginx.conf 中配置 RTMP 和 HTTP-FLV:

  1. rtmp {
  2. server {
  3. listen 1935;
  4. application live {
  5. live on;
  6. exec_static ffmpeg -i rtmp://localhost/live/$name \
  7. -c copy -f flv http://localhost:8080/live/$name.flv;
  8. }
  9. }
  10. }
  11. http {
  12. server {
  13. listen 8080;
  14. location /live {
  15. flv_live on;
  16. add_header 'Access-Control-Allow-Origin' '*';
  17. }
  18. }
  19. }

3. Spring Boot 控制器

提供转码后的流地址给前端:

  1. @RestController
  2. @RequestMapping("/api/stream")
  3. public class StreamController {
  4. @GetMapping("/flv")
  5. public String getFlvStream() {
  6. return "http://localhost:8080/live/stream.flv";
  7. }
  8. @GetMapping("/ws-flv")
  9. public String getWsFlvStream() {
  10. return "ws://localhost:8081";
  11. }
  12. }

三、前端实现(Jeecg-Boot Vue)

1. 安装播放器

  1. npm install flv.js h265web.js hls.js --save

2. 创建播放器组件

H.264 视频流(HTTP-FLV)

  1. <template>
  2. <div>
  3. <video id="flvPlayer" controls autoplay></video>
  4. </div>
  5. </template>
  6. <script>
  7. import flvjs from 'flv.js';
  8. export default {
  9. mounted() {
  10. if (flvjs.isSupported()) {
  11. const video = document.getElementById('flvPlayer');
  12. const flvPlayer = flvjs.createPlayer({
  13. type: 'flv',
  14. url: 'http://localhost:8080/api/stream/flv',
  15. });
  16. flvPlayer.attachMediaElement(video);
  17. flvPlayer.load();
  18. flvPlayer.play();
  19. }
  20. },
  21. };
  22. </script>

H.265 视频流(WebSocket-FLV)

  1. <template>
  2. <div id="h265-player"></div>
  3. </template>
  4. <script>
  5. import H265Player from 'h265web.js';
  6. export default {
  7. mounted() {
  8. const player = new H265Player({
  9. container: document.getElementById('h265-player'),
  10. videoUrl: 'ws://localhost:8080/api/stream/ws-flv',
  11. width: 800,
  12. height: 450,
  13. });
  14. player.init();
  15. },
  16. };
  17. </script>

HLS 视频流

  1. <template>
  2. <div>
  3. <video id="hlsPlayer" controls autoplay></video>
  4. </div>
  5. </template>
  6. <script>
  7. import Hls from 'hls.js';
  8. export default {
  9. mounted() {
  10. const video = document.getElementById('hlsPlayer');
  11. if (Hls.isSupported()) {
  12. const hls = new Hls();
  13. hls.loadSource('http://localhost:8080/api/stream/hls.m3u8');
  14. hls.attachMedia(video);
  15. } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
  16. video.src = 'http://localhost:8080/api/stream/hls.m3u8';
  17. }
  18. },
  19. };
  20. </script>

四、关键技术实现细节

1. 协议选择

协议 延迟 兼容性 适用场景
HTTP-FLV 1-2秒 较好 实时监控
WebSocket-FLV 1-2秒 较差 H.265 视频流
HLS 3-10秒 最好 监控回放、点播

2. FFmpeg 参数优化

3. 安全性


五、常见问题与解决方案

1. 浏览器不支持 H.265

2. 延迟过高

3. 跨域问题

4. 性能问题


六、完整部署示例

1. Docker Compose 配置

  1. version: '3'
  2. services:
  3. nginx-rtmp:
  4. image: alfg/nginx-rtmp
  5. ports:
  6. - "1935:1935"
  7. - "8080:80"
  8. volumes:
  9. - ./nginx.conf:/etc/nginx/nginx.conf
  10. ffmpeg-transcoder:
  11. image: jrottenberg/ffmpeg
  12. command: >
  13. ffmpeg -i rtsp://admin:password@192.168.1.64:554/h264/ch1/main/av_stream
  14. -c:v libx264 -preset fast -crf 23
  15. -c:a aac -f flv http://nginx-rtmp:8080/live/stream.flv
  16. jeecg-boot:
  17. image: your-jeecg-boot-image
  18. ports:
  19. - "80:8080"

2. 前端路由配置

src/router/index.js 中注册播放器页面:

  1. {
  2. path: '/video-player',
  3. name: 'VideoPlayer',
  4. component: () => import('@/views/video-player/VideoPlayer.vue'),
  5. meta: { title: '视频播放器' },
  6. }

七、总结

  1. RTSP 流需转码:浏览器不支持直接播放 RTSP,需转换为 HTTP-FLV、WebSocket-FLV 或 HLS。
  2. 选择合适协议:根据延迟、兼容性需求选择协议。
  3. 前端播放器:根据编码格式选择播放器(如 H.265 用 h265web.js)。
  4. 优化性能:调整转码参数、降低分辨率或使用硬件加速。

通过以上方案,你可以在 Jeecg-Boot 框架中实现 RTSP 视频流的转码、传输和播放,满足不同场景的需求。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注