1、导入依赖

    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv-platform</artifactId>
        <version>1.5.9</version>
    </dependency>

    <!-- Additional dependencies required to use CUDA and cuDNN -->
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>opencv-platform-gpu</artifactId>
        <version>4.7.0-1.5.9</version>
    </dependency>

    <!-- Optional GPL builds with (almost) everything enabled -->
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>ffmpeg-platform-gpl</artifactId>
        <version>6.0-1.5.9</version>
    </dependency>

2、编写测试

package com.hm.redisdemo.demo;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class RstpDemo {
    public static void main(String[] args) throws IOException, InterruptedException {
        String rtspUrl = "rtsp://172.18.16.101:554/openUrl/5yonbiM";
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
        grabber.setOption("rtsp_transport", "tcp"); // 使用TCP传输方式,避免丢包
        grabber.setOption("buffer_size", "10240000"); // 设置缓冲区大小为1MB,提高流畅度
       // grabber.setOption("rtsp_flags", "prefer_tcp"); // 设置优先使用TCP方式传输
        grabber.start();

        Java2DFrameConverter converter = new Java2DFrameConverter();
        Thread.sleep(5000);
        //grabber.setFrameNumber(10000);
        Frame frame = grabber.grabImage();
        BufferedImage image = converter.getBufferedImage(frame);
        Thread.sleep(10000);
        ImageIO.write(image, "png", new File("screenshot15.png"));

        grabber.stop();
        grabber.release();


    }
}

执行后:

图片被保存

3、遗留问题

Q:图片每次都是有花屏的,不知道如何调优。

处理!!

A:是测试环境问题,换到直连平台的环境就好了。