From f84d149459b73928c864eb5aebf764cf58105c73 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期三, 07 五月 2025 16:11:24 +0800
Subject: [PATCH] 命令生成器修改注释、代码优化。
---
pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 172 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java
new file mode 100644
index 0000000..1258861
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/util/VideoUtils.java
@@ -0,0 +1,172 @@
+package com.dy.pipIrrWebFile.util;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.jcodec.api.FrameGrab;
+import org.jcodec.common.model.Picture;
+import org.jcodec.scale.AWTUtil;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.*;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2025/1/13 11:22
+ * @Description
+ */
+
+/**
+ * 瑙嗛鎿嶄綔宸ュ叿绫�
+ */
+@Slf4j
+public class VideoUtils {
+
+ /*** 鍥剧墖鏍煎紡*/
+ private static final String FILE_EXT = "jpg";
+
+ /*** 甯ф暟*/
+ private static final int THUMB_FRAME = 5;
+
+ /*** 鍥剧墖鏍煎紡*/
+ private String fileExt ;
+
+ /*** 甯ф暟*/
+ private Integer thumbFrame ;
+
+ public VideoUtils(){
+ this.fileExt = FILE_EXT;
+ this.thumbFrame = THUMB_FRAME;
+ }
+
+
+ public VideoUtils(String fileExt, int thumbFrame){
+ this.fileExt = fileExt;
+ this.thumbFrame = thumbFrame;
+ if(this.fileExt == null || this.fileExt.trim().equals("")){
+ this.fileExt = FILE_EXT;
+ }
+ if(this.thumbFrame == null || this.thumbFrame.intValue() <= 0){
+ this.thumbFrame = THUMB_FRAME;
+ }
+ }
+
+ /**
+ * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+ * @param videoFilePath 婧愯棰戞枃浠惰矾寰�
+ * @param frameFilePath 鎴彇甯х殑鍥剧墖瀛樻斁璺緞
+ */
+ public void fetchFrame(String videoFilePath, String frameFilePath) throws Exception {
+ File videoFile = new File(videoFilePath);
+ File frameFile = new File(frameFilePath);
+ getThumbnail(videoFile, frameFile);
+ }
+
+
+ /**
+ * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+ * @param videoFilePath 婧愯棰戞枃浠惰矾寰�
+ */
+ public BufferedImage fetchFrame(String videoFilePath) throws Exception {
+ File videoFile = new File(videoFilePath);
+ return getThumbnail(videoFile);
+ }
+
+ /**
+ * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+ *
+ * @param videoFile 婧愯棰戞枃浠�
+ * @param targetFile 鎴彇甯х殑鍥剧墖
+ */
+ public void fetchFrame(MultipartFile videoFile, File targetFile) throws Exception {
+ File file = new File(videoFile.getName());
+ FileUtils.copyInputStreamToFile(videoFile.getInputStream(), file);
+ getThumbnail(file, targetFile);
+ }
+
+ /**
+ * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+ *
+ * @param videoFile 婧愯棰戞枃浠�
+ */
+ public BufferedImage fetchFrame(MultipartFile videoFile) throws Exception {
+ File file = new File(videoFile.getName());
+ FileUtils.copyInputStreamToFile(videoFile.getInputStream(), file);
+ return getThumbnail(file);
+ }
+
+ /**
+ * 鑾峰彇鎸囧畾瑙嗛鐨勫抚骞朵繚瀛樹负鍥剧墖鑷虫寚瀹氱洰褰�
+ *
+ * @param videoFile 婧愯棰戞枃浠�
+
+ public File fetchFrame2(MultipartFile videoFile) {
+ String originalFilename = videoFile.getOriginalFilename();
+ File file = new File(originalFilename);
+ File targetFile = null;
+ try {
+ FileUtils.copyInputStreamToFile(videoFile.getInputStream(), file);
+
+ int i = originalFilename.lastIndexOf(".");
+ String imageName;
+
+ if (i > 0) {
+ imageName = originalFilename.substring(0, i);
+ } else {
+ imageName = UUID.randomUUID().toString().replace("-", "");
+ }
+ imageName = imageName + ".jpg";
+ targetFile = new File(imageName);
+ getThumbnail(file, targetFile);
+ } catch (Exception e) {
+ log.error("鑾峰彇瑙嗛鎸囧畾甯у紓甯革細", e);
+ } finally {
+ if (file.exists()) {
+ file.delete();
+ }
+ }
+ log.debug("瑙嗛鏂囦欢 - 甯ф埅鍙� - 澶勭悊缁撴潫");
+ return targetFile;
+ }
+ */
+
+ /**
+ * 鑾峰彇绗竴甯х缉鐣ュ浘
+ *
+ * @param videoFile 瑙嗛璺緞
+ * @param targetFile 缂╃暐鍥剧洰鏍囪矾寰�
+ */
+ public void getThumbnail(File videoFile, File targetFile) throws Exception {
+ // 鏍规嵁鎵╁睍鍚嶅垱寤轰竴涓柊鏂囦欢璺緞
+ Picture picture = FrameGrab.getFrameFromFile(videoFile, thumbFrame);
+ BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture);
+ ImageIO.write(bufferedImage, fileExt, targetFile);
+ }
+
+
+ /**
+ * 鑾峰彇绗竴甯х缉鐣ュ浘
+ *
+ * @param videoFile 瑙嗛璺緞
+ */
+ public BufferedImage getThumbnail(File videoFile) throws Exception {
+ // 鏍规嵁鎵╁睍鍚嶅垱寤轰竴涓柊鏂囦欢璺緞
+ Picture picture = FrameGrab.getFrameFromFile(videoFile, thumbFrame);
+ BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture);
+ return bufferedImage;
+ }
+
+
+ public static void main(String[] args) {
+ VideoUtils util = new VideoUtils();
+ try {
+ long startTime = System.currentTimeMillis();
+ util.getThumbnail(new File("D:\\mp4\\test.mp4"), new File("D:\\mp4\\test.jpg"));
+ System.out.println("鎴彇鍥剧墖鑰楁椂锛�" + (System.currentTimeMillis() - startTime));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}
--
Gitblit v1.8.0