|
|
@@ -1,31 +1,24 @@
|
|
|
package cn.reghao.dfs.store.task.processor;
|
|
|
|
|
|
import cn.reghao.dfs.store.db.repository.VideoRepository;
|
|
|
+import cn.reghao.dfs.store.service.PutObjectService;
|
|
|
import cn.reghao.jutil.media.model.AudioProps;
|
|
|
import cn.reghao.jutil.media.model.MediaProps;
|
|
|
import cn.reghao.jutil.media.model.VideoProps;
|
|
|
-import cn.reghao.dfs.store.model.vo.ObjectProp;
|
|
|
import cn.reghao.dfs.store.model.vo.ObjectResult;
|
|
|
-import cn.reghao.dfs.store.service.FileStoreService;
|
|
|
import cn.reghao.dfs.store.util.FileType;
|
|
|
import cn.reghao.dfs.store.service.ObjectNameService;
|
|
|
-import cn.reghao.dfs.store.service.PutObjectService;
|
|
|
-import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
import cn.reghao.dfs.store.model.po.VideoFile;
|
|
|
import cn.reghao.jutil.media.FFmpegWrapper;
|
|
|
import cn.reghao.jutil.media.MediaQuality;
|
|
|
import cn.reghao.jutil.media.MediaResolution;
|
|
|
-import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
-import java.util.UUID;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -35,30 +28,43 @@ import java.util.concurrent.ExecutorService;
|
|
|
@Service
|
|
|
public class VideoFileProcessor {
|
|
|
private final VideoRepository videoRepository;
|
|
|
- private final PutObjectService putObjectService;
|
|
|
private final ObjectNameService objectNameService;
|
|
|
- private final FileStoreService fileStoreService;
|
|
|
- private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("converter-pool", 10);
|
|
|
+ private PutObjectService putObjectService;
|
|
|
private final Set<String> audioCodecs = Set.of("aac", "mp3");
|
|
|
private final Set<String> videoCodecs = Set.of("h264");
|
|
|
|
|
|
- public VideoFileProcessor(VideoRepository videoRepository, PutObjectService putObjectService,
|
|
|
- ObjectNameService objectNameService, FileStoreService fileStoreService) {
|
|
|
+ public VideoFileProcessor(VideoRepository videoRepository, ObjectNameService objectNameService,
|
|
|
+ PutObjectService putObjectService) {
|
|
|
this.videoRepository = videoRepository;
|
|
|
- this.putObjectService = putObjectService;
|
|
|
this.objectNameService = objectNameService;
|
|
|
- this.fileStoreService = fileStoreService;
|
|
|
+ this.putObjectService = putObjectService;
|
|
|
}
|
|
|
|
|
|
public UploadFileRet process(ObjectResult objectResult) {
|
|
|
String objectName = objectResult.getObjectName();
|
|
|
- String videoFileId = objectResult.getObjectId();
|
|
|
+ String objectId = objectResult.getObjectId();
|
|
|
+ String videoFileId = objectId;
|
|
|
+ String objectUrl = objectNameService.getObjectUrl(objectName);
|
|
|
boolean duplicate = objectResult.isDuplicate();
|
|
|
if (duplicate) {
|
|
|
String dupObjectId = objectResult.getDupObjectId();
|
|
|
- VideoFile videoFile = videoRepository.findVideoFile(dupObjectId);
|
|
|
- VideoFile videoFile1 = new VideoFile(videoFileId, videoFile);
|
|
|
- videoRepository.saveVideoFiles(List.of(videoFile1));
|
|
|
+ List<VideoFile> videoFiles = videoRepository.findVideoFiles(dupObjectId);
|
|
|
+ VideoFile videoFile = videoFiles.get(0);
|
|
|
+ VideoFile videoFile1 = new VideoFile(videoFileId, objectId, objectUrl, videoFile);
|
|
|
+ List<VideoFile> list = new ArrayList<>();
|
|
|
+ list.add(videoFile1);
|
|
|
+
|
|
|
+ if (videoFiles.size() > 1) {
|
|
|
+ for (int i = 1; i < videoFiles.size(); i++) {
|
|
|
+ VideoFile videoFile2 = videoFiles.get(i);
|
|
|
+ ObjectResult objectResult1 = putObjectService.copyObject1(videoFile2.getObjectId());
|
|
|
+ String objectId1 = objectResult1.getObjectId();
|
|
|
+ String objectUrl1 = objectNameService.getObjectUrl(objectResult1.getObjectName());
|
|
|
+ list.add(new VideoFile(videoFileId, objectId1, objectUrl1, videoFile2));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ videoRepository.saveVideoFiles(list);
|
|
|
return new UploadFileRet(videoFileId, null);
|
|
|
}
|
|
|
|
|
|
@@ -75,11 +81,15 @@ public class VideoFileProcessor {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- String videoCodec = mediaProps.getVideoProps().getCodecName();
|
|
|
+ String videoCodec = videoProps.getCodecName();
|
|
|
+ long vbitRate = videoProps.getBitRate();
|
|
|
+
|
|
|
String audioCodec = null;
|
|
|
+ long abitRate = 0;
|
|
|
AudioProps audioProps1 = mediaProps.getAudioProps();
|
|
|
if (audioProps1 != null) {
|
|
|
audioCodec = audioProps1.getCodecName();
|
|
|
+ abitRate = audioProps1.getBitRate();
|
|
|
}
|
|
|
|
|
|
/*if (videoCodecs.contains(videoCodec)) {
|
|
|
@@ -94,58 +104,13 @@ public class VideoFileProcessor {
|
|
|
}*/
|
|
|
int width = videoProps.getCodedWidth().intValue();
|
|
|
int height = videoProps.getCodedHeight().intValue();
|
|
|
- boolean horizontal = width>height;
|
|
|
int duration = videoProps.getDuration().intValue();
|
|
|
- String originalUrl = objectNameService.getObjectUrl(objectName);
|
|
|
- long bitRate = videoProps.getBitRate();
|
|
|
MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
|
|
|
String quality = mediaResolution.getQualityStr();
|
|
|
String urlType = FileType.getVideoUrlType(absolutePath);
|
|
|
- VideoFile videoFile = new VideoFile(videoFileId, videoFileId, videoCodec, audioCodec, urlType, originalUrl,
|
|
|
- bitRate, quality, width, height, duration);
|
|
|
+ VideoFile videoFile = new VideoFile(videoFileId, objectId, videoCodec, vbitRate, audioCodec, abitRate,
|
|
|
+ urlType, objectUrl, quality, width, height, duration);
|
|
|
videoRepository.saveVideoFiles(List.of(videoFile));
|
|
|
return new UploadFileRet(videoFileId, null);
|
|
|
-
|
|
|
- //log.info("添加视频格式转码任务");
|
|
|
- //threadPool.submit(new ConvertTask());
|
|
|
- }
|
|
|
-
|
|
|
- private VideoFile getConvertedVideoFile(String videoFileId, File file, String originalObjectName,
|
|
|
- int width, int height, String suffix) {
|
|
|
- String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
- String absolutePath = fileStoreService.genFilePath(contentId, file.length(), suffix);
|
|
|
- try {
|
|
|
- File savedFile = new File(absolutePath);
|
|
|
- if (savedFile.exists()) {
|
|
|
- throw new IOException(absolutePath + " exist");
|
|
|
- }
|
|
|
-
|
|
|
- String format = suffix.replace(".", "");
|
|
|
- int ret = FFmpegWrapper.formatCovert(file.getAbsolutePath(), absolutePath + suffix, format);
|
|
|
- if (ret != 0) {
|
|
|
- throw new Exception("视频转码失败");
|
|
|
- }
|
|
|
- String sha256sum = DigestUtil.sha256sum(absolutePath);
|
|
|
-
|
|
|
- ObjectProp objectProp = objectNameService.getObjectProp(originalObjectName, suffix);
|
|
|
- String originalFilename = "";
|
|
|
- ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, originalFilename, sha256sum);
|
|
|
-
|
|
|
- String objectId = objectResult.getObjectId();
|
|
|
- String videoCodec = "h264";
|
|
|
- String audioCodec = "aac";
|
|
|
- String url = objectNameService.getObjectUrl(objectResult.getObjectName());
|
|
|
- String urlType = FileType.getVideoUrlType(absolutePath);
|
|
|
- long bitRate = 0;
|
|
|
- int duration = 0;
|
|
|
- MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
|
|
|
- String quality = mediaResolution.getQualityStr();
|
|
|
- return new VideoFile(videoFileId, objectId, videoCodec, audioCodec, urlType, url, bitRate,
|
|
|
- quality, width, height, duration);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
}
|
|
|
}
|