fix: 修复 verified 变量作用域导致的编译错误

This commit is contained in:
MaDaLei 2026-04-17 20:04:57 +08:00
parent 2cef1d7993
commit 677227441f

View File

@ -192,36 +192,29 @@ public class ReportHighlightVideoService {
} }
Path merged = workDir.resolve("merged.mp4"); Path merged = workDir.resolve("merged.mp4");
Path sourceToSave; // 最终用于保存的文件会在下面赋值
int c1 = runFfmpeg(List.of( int c1 = runFfmpeg(List.of(
ffmpegBinary, "-y", "-f", "concat", "-safe", "0", "-i", listFile.toString(), ffmpegBinary, "-y", "-f", "concat", "-safe", "0", "-i", listFile.toString(),
"-c", "copy", merged.toString() "-c", "copy", merged.toString()
)); ));
if (c1 != 0) { if (c1 != 0) {
// stream copy 失败改用转码同时做尺寸 + Baseline Profile 标准化
int c2 = runFfmpeg(List.of( int c2 = runFfmpeg(List.of(
ffmpegBinary, "-y", "-f", "concat", "-safe", "0", "-i", listFile.toString(), ffmpegBinary, "-y", "-f", "concat", "-safe", "0", "-i", listFile.toString(),
"-c:v", "libx264", "-preset", "veryfast", "-crf", "23",
"-c:a", "aac", "-b:a", "96k", merged.toString()
));
if (c2 != 0) {
throw new IllegalStateException("ffmpeg 转码失败(退出码 " + c2 + "");
}
// 验证输出文件确保可播放
Path verified = workDir.resolve("verified.mp4");
int c3 = runFfmpeg(List.of(
ffmpegBinary, "-y", "-i", merged.toString(),
"-vf", "scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2", "-vf", "scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2",
"-c:v", "libx264", "-preset", "fast", "-crf", "22", "-c:v", "libx264", "-preset", "fast", "-crf", "22",
"-profile:v", "baseline", "-level", "3.0", // 最高兼容 WeChat/Android "-profile:v", "baseline", "-level", "3.0", // 最高兼容 WeChat/Android
"-c:a", "aac", "-b:a", "128k", "-c:a", "aac", "-b:a", "128k",
"-movflags", "+faststart", // MP4 streaming优化微信要求 "-movflags", "+faststart", // MP4 streaming 优化微信要求
"-t", String.valueOf(durationSec), "-t", String.valueOf(durationSec),
verified.toString() merged.toString()
)); ));
if (c3 != 0) { if (c2 != 0) {
// 转码验证失败降级为copy throw new IllegalStateException("ffmpeg 拼接失败(退出码 " + c2 + "");
verified = merged;
} }
sourceToSave = merged; // 转码后文件已满足兼容性要求
} else {
sourceToSave = merged; // stream copy 成功直接用
} }
String datePath = LocalDate.now().toString().replace("-", "/"); String datePath = LocalDate.now().toString().replace("-", "/");
@ -230,7 +223,6 @@ public class ReportHighlightVideoService {
Files.createDirectories(outDir); Files.createDirectories(outDir);
String filename = "highlight_" + reportId + "_" + UUID.randomUUID().toString().replace("-", "") + ".mp4"; String filename = "highlight_" + reportId + "_" + UUID.randomUUID().toString().replace("-", "") + ".mp4";
Path finalPath = outDir.resolve(filename); Path finalPath = outDir.resolve(filename);
Path sourceToSave = Files.exists(verified) ? verified : merged;
Files.copy(sourceToSave, finalPath, StandardCopyOption.REPLACE_EXISTING); Files.copy(sourceToSave, finalPath, StandardCopyOption.REPLACE_EXISTING);
String url = "/api/upload/image/" + datePath + "/" + filename; String url = "/api/upload/image/" + datePath + "/" + filename;