使用FFmpeg从RSTP流中抓图花屏问题

使用FFmpeg从RSTP流中抓图花屏问题

场景:随着设备的数量的增加,视频流的质量逐渐降低,掉帧非常严重。在使用FFmpeg抓图花屏率也越来越高,经过网上查阅资料,基本上都是修改FFmpeg框架底层源码之类的。翻阅国外资料后,可以通过设置参数来过滤坏掉的帧

grabber.setVideoOption("skip_frame", "nokey");

使用FFmpeg

<dependency>
  <groupId>org.bytedeco</groupId>
  <artifactId>ffmpeg-platform</artifactId>
  <version>6.0-1.5.9</version>
  <exclusions>
      <exclusion>
          <artifactId>javacpp</artifactId>
          <groupId>org.bytedeco</groupId>
      </exclusion>
      <exclusion>
          <artifactId>javacpp-platform</artifactId>
          <groupId>org.bytedeco</groupId>
      </exclusion>
  </exclusions>
</dependency>

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

抓图工具类

public class VideoUtils {
    private static final Logger log = LoggerFactory.getLogger(VideoUtils.class);

    private static String rtspTransportType = "tcp";
    /**
     *       * 视频帧率
     *
     */
    private static int frameRate = 144;
    /**
     * 视频宽度
     */
    private static int frameWidth =  1920;
    /**
     * 视频高度
     */
    private static int frameHeight = 1080;

    
	/**
     * 解析视频地址并截图
     * @param path rstp 流地址
     * @param picPath 图片存放地址
     * @throws Exception
     */
    public static void getVideoImagePathByRSTP(String path, String picPath) throws Exception {
        //创建rstp流对象
        FFmpegFrameGrabber grabber = createGrabber(path);
        try {
            //开启流获取
            grabber.start();
            Frame frame = null;
            while ((frame = grabber.grabFrame()) == null){
                frame = grabber.grabFrame();
            }
            //转换图像
            Java2DFrameConverter converter = new Java2DFrameConverter();
            BufferedImage srcImage = converter.getBufferedImage(frame);
            if (srcImage!=null) {
                //创建文件
                File file = new File(picPath);
                //输出文件
                ImageIO.write(srcImage, "jpg", file);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            grabber.stop();
            grabber.close();
        }


    }


    /**
     * 构造视频抓取器
     *
     * @param rtsp 拉流地址
     * @return
     */
    private static FFmpegFrameGrabber createGrabber(String rtsp) {
        // 获取视频源
        try {
            FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(rtsp);
            //设置传输方式 TCP | UDP
            grabber.setOption("rtsp_transport", rtspTransportType);
            //设置帧率
            grabber.setFrameRate(frameRate);
            grabber.setOption("stimeout", "2000000000");
            // 设置缓冲区大小为1MB
            grabber.setOption("buffer_size", "1024000");
            // 过滤坏掉的帧
            grabber.setVideoOption("skip_frame", "nokey");
            //设置获取的视频宽度
            grabber.setImageWidth(frameWidth);
//            //设置获取的视频高度
            grabber.setImageHeight(frameHeight);
            // 设置采集器构造超时时间(单位微秒,1秒=1000000微秒)
          //  grabber.setOption("stimeout", "2000000000");
            //设置视频bit率
         //   grabber.setVideoBitrate(2000000);
            return grabber;
        } catch (FrameGrabber.Exception e) {
            log.error("创建解析rtsp FFmpegFrameGrabber 失败");
            log.error("create rtsp FFmpegFrameGrabber exception: ", e);
            return null;
        }
    }


    public static void main(String[] args) throws IOException {
        String rstp = "rtsp://10.15.10.94:5554/RealTime/100001013/1/mainstream";
        try {
        //参数1 rtsp 地址自行获取  参数2  截取图片存放地址
            VideoUtils.getVideoImagePathByRSTP(rstp, "/Users/wuhuaming/springboot/MyPractices/springboot-test/src/main/resources/static/1000.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }


}
0%