|
@@ -0,0 +1,111 @@
|
|
|
|
|
+package cn.reghao.dfs.store.task;
|
|
|
|
|
+
|
|
|
|
|
+import cn.reghao.dfs.store.db.repository.MediaRepository;
|
|
|
|
|
+import cn.reghao.dfs.store.model.po.VideoUrl;
|
|
|
|
|
+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.service.ObjectNameService;
|
|
|
|
|
+import cn.reghao.dfs.store.service.PutObjectService;
|
|
|
|
|
+import cn.reghao.dfs.store.util.FileType;
|
|
|
|
|
+import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
|
|
+import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
|
|
|
|
|
+import cn.reghao.jutil.media.FFmpegWrapper;
|
|
|
|
|
+import cn.reghao.jutil.media.model.AudioProps;
|
|
|
|
|
+import cn.reghao.jutil.media.model.MediaProps;
|
|
|
|
|
+import cn.reghao.oss.api.constant.UploadChannel;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author reghao
|
|
|
|
|
+ * @date 2023-07-05 09:12:17
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class AudioFileProcessor {
|
|
|
|
|
+ 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);
|
|
|
|
|
+ private final Set<String> audioCodecs = Set.of("aac", "mp3");
|
|
|
|
|
+
|
|
|
|
|
+ public AudioFileProcessor(MediaRepository mediaRepository, PutObjectService putObjectService,
|
|
|
|
|
+ ObjectNameService objectNameService, FileStoreService fileStoreService) {
|
|
|
|
|
+ this.mediaRepository = mediaRepository;
|
|
|
|
|
+ this.putObjectService = putObjectService;
|
|
|
|
|
+ this.objectNameService = objectNameService;
|
|
|
|
|
+ this.fileStoreService = fileStoreService;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void process(ObjectResult objectResult) {
|
|
|
|
|
+ String objectId = objectResult.getObjectId();
|
|
|
|
|
+ boolean duplicate = objectResult.isDuplicate();
|
|
|
|
|
+ if (duplicate) {
|
|
|
|
|
+ String dupObjectId = objectResult.getDupObjectId();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String objectName = objectResult.getObjectName();
|
|
|
|
|
+ String absolutePath = objectResult.getAbsolutePath();
|
|
|
|
|
+ MediaProps mediaProps = FFmpegWrapper.getMediaProps(absolutePath);
|
|
|
|
|
+ if (mediaProps == null) {
|
|
|
|
|
+ log.info("{} 的 FFmpeg 媒体信息为 null", objectName);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ AudioProps audioProps = mediaProps.getAudioProps();
|
|
|
|
|
+ if (audioProps == null) {
|
|
|
|
|
+ log.info("{} 的 FFmpeg 音频信息为 null", objectName);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String audioCodec = audioProps.getCodecName();
|
|
|
|
|
+ if (!audioCodecs.contains(audioCodec)) {
|
|
|
|
|
+ log.error("{} 对象的音频非 aac 编码, 暂不处理", objectName);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //log.info("添加视频格式转码任务");
|
|
|
|
|
+ //threadPool.submit(new ConvertTask());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private VideoUrl getConvertedAudioUrl(String audioFileId, File file) {
|
|
|
|
|
+ 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");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String suffix = "m4a";
|
|
|
|
|
+ int ret = FFmpegWrapper.convertAudio(file.getAbsolutePath(), absolutePath + "." + suffix);
|
|
|
|
|
+ if (ret != 0) {
|
|
|
|
|
+ throw new Exception("音频转码失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ String sha256sum = DigestUtil.sha256sum(absolutePath);
|
|
|
|
|
+
|
|
|
|
|
+ ObjectProp objectProp = objectNameService.getObjectProp(UploadChannel.audio.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);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|