Browse Source

视频长度

licailing 4 years ago
parent
commit
c753860626

+ 6 - 0
pom.xml

@@ -298,6 +298,12 @@
             <artifactId>javacv-platform</artifactId>
             <version>1.5.4</version>
         </dependency>
+
+        <dependency>
+            <groupId>ws.schild</groupId>
+            <artifactId>jave-all-deps</artifactId>
+            <version>3.1.1</version>
+        </dependency>
     </dependencies>
 
 </project>

+ 19 - 0
src/main/java/com/izouma/wenlvju/utils/VideoUtils.java

@@ -0,0 +1,19 @@
+package com.izouma.wenlvju.utils;
+
+import ws.schild.jave.MultimediaObject;
+import ws.schild.jave.info.MultimediaInfo;
+
+import java.io.File;
+
+public class VideoUtils {
+    public static Long getVideoTime(File file) {
+        try {
+            MultimediaObject instance = new MultimediaObject(file);
+            MultimediaInfo result = instance.getInfo();
+            return result.getDuration() / 1000;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return 0L;
+    }
+}

+ 12 - 7
src/main/java/com/izouma/wenlvju/web/FileUploadController.java

@@ -2,10 +2,12 @@ package com.izouma.wenlvju.web;
 
 import com.izouma.wenlvju.exception.BusinessException;
 import com.izouma.wenlvju.service.storage.StorageService;
+import com.izouma.wenlvju.utils.VideoUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.util.TempFile;
 import org.bytedeco.javacv.FFmpegFrameGrabber;
 import org.bytedeco.javacv.Frame;
 import org.bytedeco.javacv.Java2DFrameConverter;
@@ -18,10 +20,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.imageio.ImageIO;
 import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.URLConnection;
 import java.text.SimpleDateFormat;
 import java.util.Base64;
@@ -48,8 +47,8 @@ public class FileUploadController {
             } catch (Exception ignored) {
             }
             path = basePath + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
-                   + RandomStringUtils.randomAlphabetic(8)
-                   + "." + FilenameUtils.getExtension(file.getOriginalFilename());
+                    + RandomStringUtils.randomAlphabetic(8)
+                    + "." + FilenameUtils.getExtension(file.getOriginalFilename());
         }
         InputStream is;
         try {
@@ -75,7 +74,7 @@ public class FileUploadController {
             } catch (Exception ignored) {
             }
             path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
-                   + RandomStringUtils.randomAlphabetic(8) + ext;
+                    + RandomStringUtils.randomAlphabetic(8) + ext;
         }
         InputStream is;
         try {
@@ -105,6 +104,11 @@ public class FileUploadController {
                 throw new BusinessException(file.getContentType() + " 非视频文件");
             }
 
+            File destDir = TempFile.createTempDirectory("import." + FilenameUtils.getExtension(file.getOriginalFilename()));
+            file.transferTo(destDir);
+            Long videoTime = VideoUtils.getVideoTime(destDir);
+            log.info("视频长度:" + videoTime);
+
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
             byte[] buffer = new byte[1024];
@@ -115,6 +119,7 @@ public class FileUploadController {
             baos.flush();
 
             InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
+
             String videoUrl = storageService.uploadFromInputStream(is1, videoPath);
 
             InputStream is2 = new ByteArrayInputStream(baos.toByteArray());