|
|
@@ -1,12 +1,16 @@
|
|
|
package cn.reghao.dfs.store.task;
|
|
|
|
|
|
-import cn.reghao.dfs.store.service.FileType;
|
|
|
+import cn.reghao.dfs.store.db.repository.MediaRepository;
|
|
|
+import cn.reghao.oss.api.constant.UploadChannel;
|
|
|
+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.dfs.store.util.media.po.AudioProps;
|
|
|
import cn.reghao.dfs.store.util.media.po.VideoProps;
|
|
|
-import cn.reghao.oss.api.dto.VideoUrlType;
|
|
|
-import cn.reghao.dfs.store.config.OssProperties;
|
|
|
-import cn.reghao.dfs.store.db.mapper.VideoFileMapper;
|
|
|
-import cn.reghao.dfs.store.db.mapper.VideoUrlMapper;
|
|
|
+import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
import cn.reghao.dfs.store.model.po.VideoFile;
|
|
|
import cn.reghao.dfs.store.model.po.VideoUrl;
|
|
|
import cn.reghao.dfs.store.util.media.FFmpegWrapper;
|
|
|
@@ -17,6 +21,8 @@ import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.UUID;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
@@ -27,15 +33,18 @@ import java.util.concurrent.ExecutorService;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class VideoFileProcessor {
|
|
|
- private final VideoFileMapper videoFileMapper;
|
|
|
- private final VideoUrlMapper videoUrlMapper;
|
|
|
- private final String endpoint;
|
|
|
+ private final MediaRepository mediaRepository;
|
|
|
+ private final PutObjectService putObjectService;
|
|
|
+ private final ObjectNameService objectNameService;
|
|
|
+ private final FileStoreService fileStoreService;
|
|
|
private final ExecutorService threadPool = ThreadPoolWrapper.threadPool("converter-pool", 10);
|
|
|
|
|
|
- public VideoFileProcessor(VideoFileMapper videoFileMapper, VideoUrlMapper videoUrlMapper, OssProperties ossProperties) {
|
|
|
- this.videoFileMapper = videoFileMapper;
|
|
|
- this.videoUrlMapper = videoUrlMapper;
|
|
|
- this.endpoint = ossProperties.getDomain();
|
|
|
+ public VideoFileProcessor(MediaRepository mediaRepository, PutObjectService putObjectService,
|
|
|
+ ObjectNameService objectNameService, FileStoreService fileStoreService) {
|
|
|
+ this.mediaRepository = mediaRepository;
|
|
|
+ this.putObjectService = putObjectService;
|
|
|
+ this.objectNameService = objectNameService;
|
|
|
+ this.fileStoreService = fileStoreService;
|
|
|
}
|
|
|
|
|
|
public void process(String objectName, String objectId, String absolutePath) {
|
|
|
@@ -56,10 +65,7 @@ public class VideoFileProcessor {
|
|
|
int height = videoProps.getCodedHeight().intValue();
|
|
|
boolean horizontal = width>height;
|
|
|
int duration = videoProps.getDuration().intValue();
|
|
|
- String url = String.format("%s/%s", endpoint, objectName);
|
|
|
-
|
|
|
VideoFile videoFile = new VideoFile(videoFileId, objectName, horizontal, duration);
|
|
|
- videoFileMapper.save(videoFile);
|
|
|
|
|
|
AudioProps audioProps = mediaProps.getAudioProps();
|
|
|
if (audioProps == null) {
|
|
|
@@ -69,22 +75,53 @@ public class VideoFileProcessor {
|
|
|
|
|
|
String audioCodec = mediaProps.getAudioProps().getCodecName();
|
|
|
String videoCodec = mediaProps.getVideoProps().getCodecName();
|
|
|
+ VideoUrl videoUrl;
|
|
|
if ("aac".equals(audioCodec) && "h264".equals(videoCodec)) {
|
|
|
MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
|
|
|
- String urlType = VideoUrlType.mp4.name();
|
|
|
- String mediaType = FileType.getMediaType(absolutePath);
|
|
|
- if (mediaType.endsWith("flv")) {
|
|
|
- urlType = VideoUrlType.flv.name();
|
|
|
- }
|
|
|
+ String urlType = FileType.getVideoUrlType(absolutePath);
|
|
|
+ String url = objectNameService.getObjectUrl(objectName);
|
|
|
+ videoUrl = new VideoUrl(videoFileId, objectName, urlType, url, mediaResolution);
|
|
|
+ } else {
|
|
|
+ videoUrl = getConvertedVideoUrl(videoFileId, new File(absolutePath), width, height);
|
|
|
+ }
|
|
|
|
|
|
- VideoUrl videoUrl = new VideoUrl(videoFileId, objectName, urlType, url, mediaResolution);
|
|
|
- videoUrlMapper.save(videoUrl);
|
|
|
- return;
|
|
|
+ if (videoUrl != null) {
|
|
|
+ mediaRepository.saveVideoFile(videoFile, videoUrl);
|
|
|
}
|
|
|
|
|
|
- String destPath = "/opt/tmp/tomcat/" + UUID.randomUUID().toString().replace("-", "");
|
|
|
- ConvertTask convertTask = new ConvertTask(videoFileId, destPath, endpoint, videoUrlMapper);
|
|
|
log.info("添加视频格式转码任务");
|
|
|
- //threadPool.submit(convertTask);
|
|
|
+ //threadPool.submit(new ConvertTask());
|
|
|
+ }
|
|
|
+
|
|
|
+ private VideoUrl getConvertedVideoUrl(String videoFileId, File file, int width, int height) {
|
|
|
+ String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ fileStoreService.genFilePath(contentId, file.length());
|
|
|
+ String absolutePath = fileStoreService.genFilePath(contentId, file.length());
|
|
|
+ try {
|
|
|
+ File savedFile = new File(absolutePath);
|
|
|
+ if (savedFile.exists()) {
|
|
|
+ throw new IOException(absolutePath + " exist");
|
|
|
+ }
|
|
|
+
|
|
|
+ int ret = FFmpegWrapper.formatCovert(file.getAbsolutePath(), absolutePath);
|
|
|
+ if (ret != 0) {
|
|
|
+ throw new Exception("视频转码失败");
|
|
|
+ }
|
|
|
+ String sha256sum = DigestUtil.sha256sum(absolutePath);
|
|
|
+
|
|
|
+ ObjectProp objectProp = objectNameService.getObjectProp(UploadChannel.video.getCode());
|
|
|
+ String originalFilename = "";
|
|
|
+ ObjectResult objectResult = putObjectService.putObject(objectProp, contentId, savedFile, originalFilename, sha256sum);
|
|
|
+
|
|
|
+ String objectId = objectResult.getUploadFileRet().getUploadId();
|
|
|
+ String url = objectResult.getUploadFileRet().getUrl();
|
|
|
+ String urlType = FileType.getVideoUrlType(absolutePath);
|
|
|
+ MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
|
|
|
+ return new VideoUrl(videoFileId, objectId, urlType, url, mediaResolution);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|