静音音频流

This commit is contained in:
2026-04-18 14:59:41 +08:00
parent 4067ab8d0a
commit 7d587eac46
3 changed files with 53 additions and 16 deletions

View File

@@ -32,6 +32,7 @@ disconnectAction:
enabled: false enabled: false
thresholdMinutes: 5 thresholdMinutes: 5
command: "" command: ""
addSilentAudio: true
preview: preview:
enabled: true enabled: true
hwaccel: "" hwaccel: ""

View File

@@ -78,4 +78,5 @@ type MainConfig struct {
Viid ViidConfig `mapstructure:"viid"` Viid ViidConfig `mapstructure:"viid"`
DisconnectAction DisconnectActionConfig `mapstructure:"disconnectAction"` DisconnectAction DisconnectActionConfig `mapstructure:"disconnectAction"`
Preview PreviewConfig `mapstructure:"preview"` Preview PreviewConfig `mapstructure:"preview"`
AddSilentAudio bool `mapstructure:"addSilentAudio"`
} }

View File

@@ -334,15 +334,26 @@ func QuickVideoCut(ctx context.Context, inputFile string, offset, length float64
"-y", "-y",
"-ss", strconv.FormatFloat(offset, 'f', 2, 64), "-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-i", inputFile, "-i", inputFile,
"-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100", }
"-c:v", "copy", if config.Config.AddSilentAudio {
"-c:a", "aac", ffmpegCmd = append(ffmpegCmd,
"-shortest", "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100",
"-c:v", "copy",
"-c:a", "aac",
"-shortest",
)
} else {
ffmpegCmd = append(ffmpegCmd,
"-c:v", "copy",
"-an",
)
}
ffmpegCmd = append(ffmpegCmd,
"-t", strconv.FormatFloat(length, 'f', 2, 64), "-t", strconv.FormatFloat(length, 'f', 2, 64),
"-fflags", "+genpts", "-fflags", "+genpts",
"-f", "mp4", "-f", "mp4",
outputFile, outputFile,
} )
return handleFfmpegProcess(subCtx, ffmpegCmd) return handleFfmpegProcess(subCtx, ffmpegCmd)
} }
@@ -378,15 +389,26 @@ func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, len
"-safe", "0", "-safe", "0",
"-ss", strconv.FormatFloat(offset, 'f', 2, 64), "-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-i", tmpFile, "-i", tmpFile,
"-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100", }
"-c:v", "copy", if config.Config.AddSilentAudio {
"-c:a", "aac", ffmpegCmd = append(ffmpegCmd,
"-shortest", "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100",
"-c:v", "copy",
"-c:a", "aac",
"-shortest",
)
} else {
ffmpegCmd = append(ffmpegCmd,
"-c:v", "copy",
"-an",
)
}
ffmpegCmd = append(ffmpegCmd,
"-t", strconv.FormatFloat(length, 'f', 2, 64), "-t", strconv.FormatFloat(length, 'f', 2, 64),
"-fflags", "+genpts", "-fflags", "+genpts",
"-f", "mp4", "-f", "mp4",
outputFile, outputFile,
} )
return handleFfmpegProcess(subCtx, ffmpegCmd) return handleFfmpegProcess(subCtx, ffmpegCmd)
} }
@@ -403,10 +425,13 @@ func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length flo
ffmpegCmd = append(ffmpegCmd, "-i", file.Url) ffmpegCmd = append(ffmpegCmd, "-i", file.Url)
} }
// 添加静音音频源作为额外输入
ffmpegCmd = append(ffmpegCmd, "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100")
inputCount := len(inputFiles) inputCount := len(inputFiles)
if config.Config.AddSilentAudio {
// 添加静音音频源作为额外输入
ffmpegCmd = append(ffmpegCmd, "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100")
}
filterComplex := strings.Builder{} filterComplex := strings.Builder{}
for i := 0; i < inputCount; i++ { for i := 0; i < inputCount; i++ {
filterComplex.WriteString(fmt.Sprintf("[%d:v]", i)) filterComplex.WriteString(fmt.Sprintf("[%d:v]", i))
@@ -416,9 +441,19 @@ func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length flo
ffmpegCmd = append(ffmpegCmd, ffmpegCmd = append(ffmpegCmd,
"-filter_complex", filterComplex.String(), "-filter_complex", filterComplex.String(),
"-map", "[v]", "-map", "[v]",
"-map", fmt.Sprintf("%d:a", inputCount), )
"-c:a", "aac",
"-shortest", if config.Config.AddSilentAudio {
ffmpegCmd = append(ffmpegCmd,
"-map", fmt.Sprintf("%d:a", inputCount),
"-c:a", "aac",
"-shortest",
)
} else {
ffmpegCmd = append(ffmpegCmd, "-an")
}
ffmpegCmd = append(ffmpegCmd,
"-preset:v", "fast", "-preset:v", "fast",
"-ss", strconv.FormatFloat(offset, 'f', 2, 64), "-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-t", strconv.FormatFloat(length, 'f', 2, 64), "-t", strconv.FormatFloat(length, 'f', 2, 64),