xueyiming 2 viikkoa sitten
vanhempi
commit
91070f9498

+ 5 - 0
tencent-ad-server/src/main/java/com/tzld/piaoquan/tencentad/controller/FfmpegUtilController.java

@@ -29,6 +29,11 @@ public class FfmpegUtilController {
 
     @PostMapping("/fetchKeyFrames")
     public CommonResponse<String> fetchKeyFrames(@RequestBody FetchKeyFramesParam fetchKeyFramesParam) {
+        return ffmpegUtilService.fetchKeyFrames1(fetchKeyFramesParam);
+    }
+
+    @PostMapping("/fetchKeyFrames/test")
+    public CommonResponse<String> fetchKeyFramesTest(@RequestBody FetchKeyFramesParam fetchKeyFramesParam) {
         return ffmpegUtilService.fetchKeyFrames(fetchKeyFramesParam);
     }
 }

+ 2 - 0
tencent-ad-server/src/main/java/com/tzld/piaoquan/tencentad/service/FfmpegUtilService.java

@@ -5,4 +5,6 @@ import com.tzld.piaoquan.tencentad.model.po.FetchKeyFramesParam;
 
 public interface FfmpegUtilService {
     CommonResponse<String> fetchKeyFrames(FetchKeyFramesParam fetchKeyFramesParam);
+
+    CommonResponse<String> fetchKeyFrames1(FetchKeyFramesParam fetchKeyFramesParam);
 }

+ 40 - 0
tencent-ad-server/src/main/java/com/tzld/piaoquan/tencentad/service/impl/FfmpegUtilServiceImpl.java

@@ -65,5 +65,45 @@ public class FfmpegUtilServiceImpl implements FfmpegUtilService {
         return CommonResponse.create(imageUrl);
     }
 
+    @Override
+    public CommonResponse<String> fetchKeyFrames1(FetchKeyFramesParam fetchKeyFramesParam) {
+        if (fetchKeyFramesParam == null || StringUtils.isEmpty(fetchKeyFramesParam.getUrl())
+                || StringUtils.isEmpty(fetchKeyFramesParam.getTimestamp())) {
+            return CommonResponse.create(500, "参数错误");
+        }
+        log.info("fetchKeyFramesParam={}", fetchKeyFramesParam);
+        log.info("imagePath={}", imagePath);
+        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.getTargetTimeThumbnail1(fetchKeyFramesParam.getUrl(), imagePath, fetchKeyFramesParam.getTimestamp());
+        String imageUrl = OSSUploader.uploadToOSS(target, imagePath);
+        log.info("imageUrl={}", imageUrl);
+        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);
+    }
+
 
 }

+ 30 - 0
tencent-ad-server/src/main/java/com/tzld/piaoquan/tencentad/utils/FfmpegUtil.java

@@ -7,6 +7,7 @@ import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.util.logging.Level;
 
 @Slf4j
 public class FfmpegUtil {
@@ -52,6 +53,35 @@ public class FfmpegUtil {
         System.out.println(getTargetTimeThumbnail(url, targetPath, timestamp));
     }
 
+    public static boolean getTargetTimeThumbnail1(String url, String targetPath, String timestamp) {
+        try {
+            // 手动指定 FFmpeg 可执行文件路径
+            String ffmpegPath = "/usr/bin/ffmpeg"; // 根据实际情况修改
+            ProcessBuilder processBuilder = new ProcessBuilder(ffmpegPath, "-ss", timestamp, "-i", url, "-vframes", "1", targetPath);
+            Process process = processBuilder.start();
+
+            // 获取 FFmpeg 的标准错误输出
+            try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
+                String line;
+                while ((line = br.readLine()) != null) {
+                    log.info("FFmpeg error output: " + line);
+                }
+            }
+
+            // 等待进程执行完毕
+            int exitCode = process.waitFor();
+            if (exitCode == 0) {
+                return true;
+            } else {
+                return false;
+            }
+        } catch (IOException | InterruptedException e) {
+            log.error("getTargetTimeThumbnail error", e);
+            return false;
+        }
+    }
+
+
 
     public static boolean getTargetTimeThumbnail(String url, String targetPath, String timestamp) {
         try {