JavaCV 视频流抽帧
pom文件引入依赖:
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.7</version>
</dependency>
</dependencies>
@Slf4j
public class OpencvUtil {
public static BufferedImage FrameToBufferedImage(org.bytedeco.javacv.Frame frame) {
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
// bufferedImage=rotateClockwise90(bufferedImage);
return bufferedImage;
}
/**
* 处理图片,将图片旋转90度。
*/
public static BufferedImage rotateClockwise90(BufferedImage bi) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bufferedImage.setRGB(j, i, bi.getRGB(i, j));
return bufferedImage;
}
/**
* @param url url
* @param filePath 文件路径
* @param fileName 文件名
* @param minioProperties 迷你属性
* @return {@link String }
* @description 切割框架
* @author yz
* @date 2022/12/08
**/
public static String cutFrame(String url, String fileName) {
FFmpegFrameGrabber grabber;
try {
grabber = FFmpegFrameGrabber.createDefault(url);
// 使用tcp的方式,不然会丢包很严重
grabber.setOption("rtsp_transport", "tcp");
// 图片大小
grabber.setImageWidth(1280);
grabber.setImageHeight(720);
grabber.start();
//截帧
File outPut = new File(fileName);
org.bytedeco.javacv.Frame frame = grabber.grabImage();
if (frame != null) {
ImageIO.write(OpencvUtil.FrameToBufferedImage(frame), "jpeg", outPut);
}
grabber.stop();
grabber.release();
} catch (Exception e) {
System.out.println(e);
}
}
}