|
@@ -0,0 +1,65 @@
|
|
|
|
+package com.tzld.piaoquan.tencentad.service.impl;
|
|
|
|
+
|
|
|
|
+import com.tzld.piaoquan.tencentad.common.base.CommonResponse;
|
|
|
|
+import com.tzld.piaoquan.tencentad.common.enums.AdVideoStatusEnum;
|
|
|
|
+import com.tzld.piaoquan.tencentad.model.po.AdVideo;
|
|
|
|
+import com.tzld.piaoquan.tencentad.model.po.FetchKeyFramesParam;
|
|
|
|
+import com.tzld.piaoquan.tencentad.service.FfmpegUtilService;
|
|
|
|
+import com.tzld.piaoquan.tencentad.utils.FfmpegUtil;
|
|
|
|
+import com.tzld.piaoquan.tencentad.utils.OSSUploader;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.nio.file.*;
|
|
|
|
+import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@Service
|
|
|
|
+public class FfmpegUtilServiceImpl implements FfmpegUtilService {
|
|
|
|
+
|
|
|
|
+ @Value("${image_path}")
|
|
|
|
+ private String imagePath;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public CommonResponse<String> fetchKeyFrames(FetchKeyFramesParam fetchKeyFramesParam) {
|
|
|
|
+ if (fetchKeyFramesParam == null || StringUtils.isEmpty(fetchKeyFramesParam.getUrl())
|
|
|
|
+ || StringUtils.isEmpty(fetchKeyFramesParam.getTimestamp())) {
|
|
|
|
+ return CommonResponse.create(500, "参数错误");
|
|
|
|
+ }
|
|
|
|
+ String imageFolder = imagePath + "/" + "timeTarget";
|
|
|
|
+ File dir = new File(imageFolder);
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ boolean mkdir = dir.mkdir();
|
|
|
|
+ if (!mkdir) {
|
|
|
|
+ return CommonResponse.create(500, "服务异常,请重试");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ String imagePath = imageFolder + "/" + UUID.randomUUID() + ".jpg";
|
|
|
|
+ String target = "long_articles/image";
|
|
|
|
+
|
|
|
|
+ FfmpegUtil.getTargetTimeThumbnail(fetchKeyFramesParam.getUrl(), imagePath, fetchKeyFramesParam.getTimestamp());
|
|
|
|
+ String imageUrl = OSSUploader.uploadToOSS(target, imagePath);
|
|
|
|
+ Path pathToDelete = Paths.get(imagePath);
|
|
|
|
+ try {
|
|
|
|
+ // 使用 Files.walkFileTree 删除文件夹及其内容
|
|
|
|
+ Files.walkFileTree(pathToDelete, new SimpleFileVisitor<Path>() {
|
|
|
|
+ @Override
|
|
|
|
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
|
+ Files.delete(file); // 删除文件
|
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("del imageFolder error {}", e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return CommonResponse.create(imageUrl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|