package com.zd.forward.utils;

import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import java.awt.image.BufferedImage;

/**
 * @author Administrator
 */
@Slf4j
public class VideoUtils {

    private VideoUtils() {
        throw new IllegalStateException("VideoUtils class");
    }
    /**
     * 帧率
     */
    private static final int FRAME_RATE = 25;

    /**
     * 视频宽度
     */
    private static final int FRAME_WIDTH = 1920;
    /**
     * 视频高度
     */
    private static final int FRAME_HEIGHT = 1080;

    public static FFmpegFrameGrabber createGrabber(String rtsp) throws FFmpegFrameGrabber.Exception {
        FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(rtsp);
        grabber.setOption("rtsp_transport", "tcp");
        grabber.setImageHeight(FRAME_HEIGHT);
        grabber.setImageWidth(FRAME_WIDTH);
        grabber.setFrameRate(FRAME_RATE);
        return grabber;
    }

    /**
     * frame 转图片流
     */
    public static BufferedImage frameToBufferedImage(Frame frame) {
        //创建BufferedImage对象
        try (Java2DFrameConverter converter = new Java2DFrameConverter()) {
            return converter.getBufferedImage(frame);
        }
    }
}
