From 72b8d277ea81eaae2c7cff1aa77adc271f13c453 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 27 Feb 2026 17:50:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(video):=20=E4=BF=AE=E5=A4=8D=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E5=89=AA=E8=BE=91=E5=8A=9F=E8=83=BD=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E9=94=99=E8=AF=AF=E5=92=8C=E9=9F=B3=E9=A2=91?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修正 SlowVideoCut 函数中传入的参数,将第二个 task.Offset 替换为 task.Length - 添加静音音频源输入以解决无音频轨道的视频处理问题 - 配置音频编解码器为 aac 格式并启用最短时长截取 - 在多个视频处理函数中统一音频处理逻辑 - 修复 concat 方式视频剪辑的音频映射问题 - 确保视频剪辑操作保留正确的音频流处理 --- util/ffmpeg.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/util/ffmpeg.go b/util/ffmpeg.go index 6971c54..fd0d560 100644 --- a/util/ffmpeg.go +++ b/util/ffmpeg.go @@ -147,7 +147,7 @@ func runFfmpegForMultipleFile2(ctx context.Context, task *dto.FfmpegTask) bool { subCtx, span := tracer.Start(ctx, "runFfmpegForMultipleFile2") defer span.End() // 多文件,方法二:使用计算资源编码 - result, err := SlowVideoCut(subCtx, task.Files, task.Offset, task.Offset, task.OutputFile) + result, err := SlowVideoCut(subCtx, task.Files, task.Offset, task.Length, task.OutputFile) if err != nil { return false } @@ -317,8 +317,10 @@ func QuickVideoCut(ctx context.Context, inputFile string, offset, length float64 "-hide_banner", "-y", "-i", inputFile, + "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100", "-c:v", "copy", - "-an", + "-c:a", "aac", + "-shortest", "-reset_timestamps", "1", "-ss", strconv.FormatFloat(offset, 'f', 2, 64), "-t", strconv.FormatFloat(length, 'f', 2, 64), @@ -360,8 +362,10 @@ func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, len "-f", "concat", "-safe", "0", "-i", tmpFile, + "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100", "-c:v", "copy", - "-an", + "-c:a", "aac", + "-shortest", "-ss", strconv.FormatFloat(offset, 'f', 2, 64), "-t", strconv.FormatFloat(length, 'f', 2, 64), "-f", "mp4", @@ -383,6 +387,9 @@ func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length flo ffmpegCmd = append(ffmpegCmd, "-i", file.Url) } + // 添加静音音频源作为额外输入 + ffmpegCmd = append(ffmpegCmd, "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100") + inputCount := len(inputFiles) filterComplex := strings.Builder{} for i := 0; i < inputCount; i++ { @@ -393,8 +400,10 @@ func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length flo ffmpegCmd = append(ffmpegCmd, "-filter_complex", filterComplex.String(), "-map", "[v]", + "-map", fmt.Sprintf("%d:a", inputCount), + "-c:a", "aac", + "-shortest", "-preset:v", "fast", - "-an", "-ss", strconv.FormatFloat(offset, 'f', 2, 64), "-t", strconv.FormatFloat(length, 'f', 2, 64), "-f", "mp4",