整合SpringBoot + Nginx-http-flv-module + JavaCV(FFmpeg) RTSP推流,使用VUE + Flv.js播放

目录

  • 1、安装nginx-rtmp-module
  • 2、Maven引入JavaCV包
  • 3、编写Java核心代码
  • 4、VUE安装video.js和flv.js
  • 5、编写前端测试代码
  • 6、运行效果

1、安装nginx-rtmp-module

参考这边:https://blog.csdn.net/Prinz_Corn/article/details/120746676

2、Maven引入JavaCV包

 	<dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv</artifactId>
        <version>1.5.1</version>
    </dependency>

    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>ffmpeg-platform</artifactId>
        <version>4.1.3-1.5.1</version>
    </dependency>

3、编写Java核心代码

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.*;

@Slf4j
public class MediaVideoTransfer {
    @Setter
    private String src;

    @Setter
    private String target;

    @Setter
    private String transportType;

    // 帧抓取器(FrameGrabbe)
    private FFmpegFrameGrabber grabber;

    // 帧录制器/推流器(FrameRecorder)
    private FFmpegFrameRecorder recorder;

    // 是否启动
    @Setter
    private boolean isStart = false;

    /**
     * 开启获取rtsp流
     */
    public void live() {
        log.info("连接rtsp:" + src + ",开始创建grabber");
        boolean isSuccess = createGrabber(src);
        if (isSuccess) {
            log.info("live:创建grabber成功");
        } else {
            log.info("live:创建grabber失败");
        }
        startPushStream();
    }

    /**
     * 构造视频抓取器
     *
     * @param src 拉流地址
     * @return 创建成功与否
     */
    private boolean createGrabber(String src) {
        // 获取视频源
        try {
            grabber = FFmpegFrameGrabber.createDefault(src);
            grabber.setOption("rtsp_transport", transportType);
            grabber.start();
            isStart = true;
            recorder = new FFmpegFrameRecorder(target, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
            //avcodec.AV_CODEC_ID_H264
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            //转码格式
            recorder.setFormat("flv");
            //帧率
            recorder.setFrameRate(grabber.getFrameRate());
            //采样率
            recorder.setSampleRate(grabber.getSampleRate());
            //声道
            recorder.setAudioChannels(grabber.getAudioChannels());
            return true;
        } catch (FrameGrabber.Exception e) {
            log.error("创建解析FFmpegFrameGrabber失败:", e);
            this.stop();
            this.reset();
            return false;
        }
    }

    /**
     * 推送流
     */
    private void startPushStream() {
        if (grabber == null) {
            log.info("重试连接src:" + src + ",开始创建grabber");
            boolean isSuccess = createGrabber(src);
            if (isSuccess) {
                log.info("push:创建grabber成功");
            } else {
                log.info("push:创建grabber失败");
            }
        }
        try {
            recorder.start();
            log.info("start recoder");
            Frame frame;
            while (isStart && (frame = grabber.grabFrame()) != null) {
                recorder.setTimestamp(grabber.getTimestamp());
                recorder.record(frame);
            }
            stop();
            reset();
            log.info("stop recoder");
        } catch (FrameGrabber.Exception | RuntimeException | FrameRecorder.Exception e) {
            log.error("exception", e);
            stop();
            reset();
        }
    }

    //停止
    private void stop() {
        try {
            if (recorder != null) {
                recorder.stop();
                recorder.release();
            }
            if (grabber != null) {
                grabber.stop();
            }
        } catch (Exception e) {
            log.error("stop exception:", e);
        }
    }
    //重置
    private void reset() {
        recorder = null;
        grabber = null;
        isStart = false;
    }
}

4、VUE安装video.js和flv.js

直接执行命令安装:

npm install flv.js
npm install video.js

5、编写前端测试代码

<template>
  <div>
    <video autoplay controls width="100%" height="500" id="videoElement"></video>
  </div>
</template>

<script>
  import flvjs from 'flv.js'

  export default {
    name: 'liveTes',
    props: {
      msg: String
    },
    mounted() {
      this.$nextTick(() => {
        this.createVideo()
      })
    },
    methods: {
      createVideo() {
        if (flvjs.isSupported()) {
          var videoElement = document.getElementById('videoElement');
          var flvPlayer = flvjs.createPlayer(
            {
            type: 'flv',
            url: '---' //你的拉流rtmp或者http等地址
          });
          flvPlayer.attachMediaElement(videoElement);
          flvPlayer.load();
          flvPlayer.play();
        }
      }
    }
  }
</script>

6、运行效果

  • 最后启动前后台程序,运行效果如下,由于是监控视频流就打码了。

整合SpringBoot + Nginx-http-flv-module + JavaCV(FFmpeg) RTSP推流,使用VUE + Flv.js播放_第1张图片

你可能感兴趣的:(SpringBoot,spring,rtsp,http-flv,vue,nginx)