From 37443ef64b5d8b088ba5ffc071a3f32c114f1bc2 Mon Sep 17 00:00:00 2001 From: xgc Date: Fri, 19 Jan 2024 12:37:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- pom.xml | 2 +- .../java/com/gc/easy/flv/util/FlvUtil.java | 73 +++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/gc/easy/flv/util/FlvUtil.java diff --git a/README.md b/README.md index 676afd2..e997d2c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ rtsp、rtmp流地址转换成flv浏览器播放 io.github.javpower rtsp-converter-flv-spring-boot-starter - 1.5.9 + 1.5.9.1 ``` ### 第二步:实现interface diff --git a/pom.xml b/pom.xml index 8752ec4..a9b9a48 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 io.github.javpower rtsp-converter-flv-spring-boot-starter - 1.5.9 + 1.5.9.1 rtsp-converter-flv-spring-boot-starter a tool about easy rtsp-converter-flv https://github.com/javpower/easy-flv diff --git a/src/main/java/com/gc/easy/flv/util/FlvUtil.java b/src/main/java/com/gc/easy/flv/util/FlvUtil.java new file mode 100644 index 0000000..59a64d6 --- /dev/null +++ b/src/main/java/com/gc/easy/flv/util/FlvUtil.java @@ -0,0 +1,73 @@ +package com.gc.easy.flv.util; + +import com.alibaba.fastjson.JSONObject; +import org.bytedeco.javacv.FFmpegFrameGrabber; +import org.bytedeco.javacv.Frame; +import org.bytedeco.javacv.FrameGrabber; +import org.bytedeco.javacv.Java2DFrameConverter; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class FlvUtil { + + private static BufferedImage getBufferedImageByFrame(String filePath) throws IOException { + FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(filePath); + return getBufferedImageByFrame(grabber); + } + + private static BufferedImage getBufferedImageByFrame(FFmpegFrameGrabber grabber) + throws FrameGrabber.Exception { + grabber.start(); + Frame frame; + frame = grabber.grabImage(); + Java2DFrameConverter converter = new Java2DFrameConverter(); + BufferedImage buffer = converter.getBufferedImage(frame); + grabber.stop(); + return buffer; + } + + public static byte[] getFlvImg(String path) throws Exception { + return bufferedImageToByteArray(getBufferedImageByFrame(path)); + } + + + /** + * 将BufferedImage转换为byte[] + * + * @param image + * @return + */ + public static byte[] bufferedImageToByteArray(BufferedImage image) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + ImageIO.write(image, "jpg", os); + return os.toByteArray(); + } + + /** + * 上传文件 + * + * @param data 文件数据 + * @param url 上传地址 + * @param fileName 文件名称 + * @return + */ + public static JSONObject postFile(byte[] data, String url, String fileName) { + RestTemplate restTemplate = new RestTemplate(); + MultiValueMap paramMap = new LinkedMultiValueMap<>(); + ByteArrayResource contentsAsResource = new ByteArrayResource(data) { + @Override + public String getFilename() { + return fileName; + } + }; + paramMap.add("file", contentsAsResource); + return restTemplate.postForObject(url, paramMap, JSONObject.class); + } +}