From 14821e28286d773ad5ff2c13510e39c5eb117daf Mon Sep 17 00:00:00 2001 From: kongzy <kongzy> Date: 星期五, 05 七月 2024 13:46:32 +0800 Subject: [PATCH] update --- exam-common/src/main/java/com/gkhy/exam/common/utils/M3u8Utils.java | 58 +++++++++++++++++----------------------------------------- 1 files changed, 17 insertions(+), 41 deletions(-) diff --git a/exam-common/src/main/java/com/gkhy/exam/common/utils/M3u8Utils.java b/exam-common/src/main/java/com/gkhy/exam/common/utils/M3u8Utils.java index 480c611..ef3f455 100644 --- a/exam-common/src/main/java/com/gkhy/exam/common/utils/M3u8Utils.java +++ b/exam-common/src/main/java/com/gkhy/exam/common/utils/M3u8Utils.java @@ -1,8 +1,9 @@ package com.gkhy.exam.common.utils; -import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.lang.UUID; import com.gkhy.exam.common.config.FilePathConfig; +import com.gkhy.exam.common.exception.ApiException; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import net.bramp.ffmpeg.FFmpeg; @@ -15,8 +16,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.File; -import java.time.LocalDateTime; +import java.io.*; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -39,7 +39,6 @@ private FilePathConfig filePath; - /** * 视频文件转 m3u8 * 支持: .mp4 | .flv | .avi | .mov | .wmv | .wav @@ -58,7 +57,11 @@ if (!FileUtil.exist(tempFilePath)) { FileUtil.mkdir(tempFilePath); } - String filePathName = tempFilePath + file.getOriginalFilename(); + String originalFilename = file.getOriginalFilename(); + String mainName=UUID.fastUUID().toString().replaceAll("-",""); + String fileName = mainName + originalFilename.substring(originalFilename.lastIndexOf(".")); + + String filePathName = tempFilePath + fileName; File dest = new File(filePathName); try { file.transferTo(dest); @@ -67,7 +70,7 @@ } long end = System.currentTimeMillis(); log.info("临时文件上传成功......耗时:{} ms", end - start); - String m3u8FilePath = localFileToM3u8(filePathName); + String m3u8FilePath = localFileToM3u8(filePathName,mainName); log.info("视频转换已完成 !"); return m3u8FilePath; } @@ -78,7 +81,7 @@ * @return : */ @SneakyThrows - public String localFileToM3u8(String filePathName) { + public synchronized String localFileToM3u8(String filePathName,String mainName) { long startTime = System.currentTimeMillis(); final FFmpegProbeResult probe = ffprobe.probe(filePathName); final List<FFmpegStream> streams = probe.getStreams().stream().filter(fFmpegStream -> fFmpegStream.codec_type != null).collect(Collectors.toList()); @@ -87,17 +90,19 @@ if (!audioStream.isPresent()) { log.error("未发现音频流"); + throw new ApiException("未发现音频流"); } if (!videoStream.isPresent()) { log.error("未发现视频流"); + throw new ApiException("未发现视频流"); } //m3u8文件 存储路径 - String filePath = generateFilePath(this.filePath.getBasePath()); - if (!FileUtil.exist(filePath)) { - FileUtil.mkdir(filePath); + String path = new File(System.getProperty("user.dir")).getAbsolutePath(); + path=path+filePath.getBasePath()+mainName; + if (!FileUtil.exist(path)) { + FileUtil.mkdir(path); } - String mainName = getFileMainName(filePathName); - String m3u8FileName = filePath + mainName + ".m3u8"; + String m3u8FileName = path+"/" + mainName + ".m3u8"; //下面这一串参数别乱动,经过调优的,1G视频大概需要10秒左右,如果是大佬随意改 //"-vsync", "2", "-c:v", "copy", "-c:a", "copy", "-tune", "fastdecode", "-hls_wrap", "0", "-hls_time", "10", "-hls_list_size", "0", "-threads", "12" @@ -132,34 +137,5 @@ return m3u8FileName; } - public static String generateFilePath(String basePath){ - String temp = basePath; - if(StringUtils.isNotBlank(basePath)){ - if(basePath.endsWith("/")){ - temp = basePath.substring(0,basePath.lastIndexOf("/")); - } - } - return temp+"/"+generateDateDir()+"/"; - } - - - /** - *@Description 根据当前时间,生成下级存储目录 - *@Return - */ - public static String generateDateDir(){ - LocalDateTime now = LocalDateTime.now(); - return DateUtil.format(now, "yyyyMMdd/HH/mm/ss"); - } - - /** - *@Description 根据文件全路径,获取文件主名称 - *@param fullPath 文件全路径(包含文件名) - *@Return - */ - public static String getFileMainName(String fullPath){ - String fileName = FileUtil.getName(fullPath); - return fileName.substring(0,fileName.lastIndexOf(".")); - } } \ No newline at end of file -- Gitblit v1.9.2