fix: 修复视频 Content-Type charset 异常 + ffmpeg concat 改用转码(Baseline Profile + faststart)

This commit is contained in:
MaDaLei 2026-04-17 19:55:19 +08:00
parent ce21bad400
commit 2cef1d7993
2 changed files with 26 additions and 2 deletions

View File

@ -75,6 +75,9 @@ public class FileController {
}
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) contentType = "image/jpeg";
// 去掉可能的 charset 参数避免 video Content-Type 被设为 "video/mp4;charset=UTF-8"
int semi = contentType.indexOf(';');
if (semi >= 0) contentType = contentType.substring(0, semi).trim();
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.body(new FileSystemResource(file));
@ -91,6 +94,9 @@ public class FileController {
}
String contentType = Files.probeContentType(file.toPath());
if (contentType == null) contentType = "image/jpeg";
// 去掉可能的 charset 参数避免 video Content-Type 被设为 "video/mp4;charset=UTF-8"
int semi = contentType.indexOf(';');
if (semi >= 0) contentType = contentType.substring(0, semi).trim();
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.body(new FileSystemResource(file));

View File

@ -203,7 +203,24 @@ public class ReportHighlightVideoService {
"-c:a", "aac", "-b:a", "96k", merged.toString()
));
if (c2 != 0) {
throw new IllegalStateException("ffmpeg 拼接失败(退出码 " + c2 + "");
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",
"-c:v", "libx264", "-preset", "fast", "-crf", "22",
"-profile:v", "baseline", "-level", "3.0", // 最高兼容 WeChat/Android
"-c:a", "aac", "-b:a", "128k",
"-movflags", "+faststart", // MP4 streaming优化微信要求
"-t", String.valueOf(durationSec),
verified.toString()
));
if (c3 != 0) {
// 转码验证失败降级为copy
verified = merged;
}
}
@ -213,7 +230,8 @@ public class ReportHighlightVideoService {
Files.createDirectories(outDir);
String filename = "highlight_" + reportId + "_" + UUID.randomUUID().toString().replace("-", "") + ".mp4";
Path finalPath = outDir.resolve(filename);
Files.copy(merged, finalPath, StandardCopyOption.REPLACE_EXISTING);
Path sourceToSave = Files.exists(verified) ? verified : merged;
Files.copy(sourceToSave, finalPath, StandardCopyOption.REPLACE_EXISTING);
String url = "/api/upload/image/" + datePath + "/" + filename;