修复windows上视频合并BUG

This commit is contained in:
Connor
2026-01-15 09:44:09 +08:00
parent e91fbcda3a
commit 6faaecf3a9

View File

@@ -138,33 +138,37 @@ func (f *FFmpeg) trimVideo(inputPath, outputPath string, startTime, endTime floa
"start", startTime, "start", startTime,
"end", endTime) "end", endTime)
// 如果startTime和endTime都为0或者endTime <= startTime直接复制整个视频 // 如果startTime和endTime都为0或者endTime <= startTime复制整个视频
// 使用重新编码而非-c copy以确保输出文件完整性
if (startTime == 0 && endTime == 0) || endTime <= startTime { if (startTime == 0 && endTime == 0) || endTime <= startTime {
f.log.Infow("No valid trim range, copying entire video") f.log.Infow("No valid trim range, re-encoding entire video")
cmd := exec.Command("ffmpeg", cmd := exec.Command("ffmpeg",
"-i", inputPath, "-i", inputPath,
"-c", "copy", "-c:v", "libx264",
"-preset", "fast",
"-crf", "23",
"-c:a", "aac",
"-b:a", "128k",
"-movflags", "+faststart",
"-y", "-y",
outputPath, outputPath,
) )
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
f.log.Errorw("FFmpeg copy failed", "error", err, "output", string(output)) f.log.Errorw("FFmpeg re-encode failed", "error", err, "output", string(output))
return fmt.Errorf("ffmpeg copy failed: %w, output: %s", err, string(output)) return fmt.Errorf("ffmpeg re-encode failed: %w, output: %s", err, string(output))
} }
f.log.Infow("Video copied successfully", "output", outputPath) f.log.Infow("Video re-encoded successfully", "output", outputPath)
return nil return nil
} }
// 使用FFmpeg裁剪视频 // 使用FFmpeg裁剪视频
// -i: 输入文件
// -ss: 开始时间(秒) // -ss: 开始时间(秒)
// -to: 结束时间(秒)或使用-t指定持续时间 // -to/-t: 结束时间持续时间
// -c copy: 直接复制流,不重新编码(速度快) // 使用重新编码而非-c copy以确保输出文件完整性避免Windows环境下流信息丢失
// -avoid_negative_ts 1: 避免负时间戳问题
var cmd *exec.Cmd var cmd *exec.Cmd
if endTime > 0 { if endTime > 0 {
// 有明确的结束时间 // 有明确的结束时间
@@ -172,9 +176,13 @@ func (f *FFmpeg) trimVideo(inputPath, outputPath string, startTime, endTime floa
"-i", inputPath, "-i", inputPath,
"-ss", fmt.Sprintf("%.2f", startTime), "-ss", fmt.Sprintf("%.2f", startTime),
"-to", fmt.Sprintf("%.2f", endTime), "-to", fmt.Sprintf("%.2f", endTime),
"-c", "copy", "-c:v", "libx264",
"-avoid_negative_ts", "1", "-preset", "fast",
"-y", // 覆盖输出文件 "-crf", "23",
"-c:a", "aac",
"-b:a", "128k",
"-movflags", "+faststart",
"-y",
outputPath, outputPath,
) )
} else { } else {
@@ -182,8 +190,12 @@ func (f *FFmpeg) trimVideo(inputPath, outputPath string, startTime, endTime floa
cmd = exec.Command("ffmpeg", cmd = exec.Command("ffmpeg",
"-i", inputPath, "-i", inputPath,
"-ss", fmt.Sprintf("%.2f", startTime), "-ss", fmt.Sprintf("%.2f", startTime),
"-c", "copy", "-c:v", "libx264",
"-avoid_negative_ts", "1", "-preset", "fast",
"-crf", "23",
"-c:a", "aac",
"-b:a", "128k",
"-movflags", "+faststart",
"-y", "-y",
outputPath, outputPath,
) )