| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | private FilePathConfig filePath; |
| | | |
| | | |
| | | |
| | | /** |
| | | * 视频文件转 m3u8 |
| | | * 支持: .mp4 | .flv | .avi | .mov | .wmv | .wav |
| | |
| | | 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); |
| | |
| | | } |
| | | long end = System.currentTimeMillis(); |
| | | log.info("临时文件上传成功......耗时:{} ms", end - start); |
| | | String m3u8FilePath = localFileToM3u8(filePathName); |
| | | String m3u8FilePath = localFileToM3u8(filePathName,mainName); |
| | | log.info("视频转换已完成 !"); |
| | | return m3u8FilePath; |
| | | } |
| | |
| | | * @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()); |
| | |
| | | |
| | | 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" |
| | |
| | | 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(".")); |
| | | } |
| | | |
| | | } |