静音音频流

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
thresholdMinutes: 5
command: ""
addSilentAudio: true
preview:
enabled: true
hwaccel: ""

View File

@@ -78,4 +78,5 @@ type MainConfig struct {
Viid ViidConfig `mapstructure:"viid"`
DisconnectAction DisconnectActionConfig `mapstructure:"disconnectAction"`
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",
"-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-i", inputFile,
"-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100",
"-c:v", "copy",
"-c:a", "aac",
"-shortest",
}
if config.Config.AddSilentAudio {
ffmpegCmd = append(ffmpegCmd,
"-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),
"-fflags", "+genpts",
"-f", "mp4",
outputFile,
}
)
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
@@ -378,15 +389,26 @@ func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, len
"-safe", "0",
"-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-i", tmpFile,
"-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100",
"-c:v", "copy",
"-c:a", "aac",
"-shortest",
}
if config.Config.AddSilentAudio {
ffmpegCmd = append(ffmpegCmd,
"-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),
"-fflags", "+genpts",
"-f", "mp4",
outputFile,
}
)
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, "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100")
inputCount := len(inputFiles)
if config.Config.AddSilentAudio {
// 添加静音音频源作为额外输入
ffmpegCmd = append(ffmpegCmd, "-f", "lavfi", "-i", "anullsrc=channel_layout=mono:sample_rate=44100")
}
filterComplex := strings.Builder{}
for i := 0; i < inputCount; 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,
"-filter_complex", filterComplex.String(),
"-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",
"-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-t", strconv.FormatFloat(length, 'f', 2, 64),