This commit is contained in:
2026-03-13 11:10:39 +08:00
parent 83bfe34394
commit fe09c60822
3 changed files with 93 additions and 3 deletions

View File

@@ -34,6 +34,7 @@ disconnectAction:
command: ""
preview:
enabled: true
hwaccel: ""
resolutions:
- 720
- 1080

View File

@@ -67,6 +67,7 @@ type DisconnectActionConfig struct {
type PreviewConfig struct {
Enabled bool `mapstructure:"enabled"`
Resolutions []int `mapstructure:"resolutions"`
HwAccel string `mapstructure:"hwaccel"`
}
type MainConfig struct {

View File

@@ -1,6 +1,7 @@
package util
import (
"ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/dto"
"ZhenTuLocalPassiveAdapter/logger"
"bytes"
@@ -474,6 +475,26 @@ func CompressVideo(ctx context.Context, inputFile, outputFile string, height int
subCtx, span := tracer.Start(ctx, "CompressVideo")
defer span.End()
span.SetAttributes(attribute.Int("height", height))
hwaccel := strings.ToLower(strings.TrimSpace(config.Config.Preview.HwAccel))
if hwaccel != "" && hwaccel != "none" {
span.SetAttributes(attribute.String("hwaccel", hwaccel))
ok, err := compressVideoGPU(subCtx, inputFile, outputFile, height, hwaccel)
if ok {
return true, nil
}
logger.Warn("GPU编码失败,回退到CPU编码",
zap.String("hwaccel", hwaccel),
zap.Error(err))
os.Remove(outputFile)
}
return compressVideoCPU(subCtx, inputFile, outputFile, height)
}
func compressVideoCPU(ctx context.Context, inputFile, outputFile string, height int) (bool, error) {
_, span := tracer.Start(ctx, "compressVideoCPU")
defer span.End()
scaleFilter := fmt.Sprintf("scale=-2:%d", height)
ffmpegCmd := []string{
FfmpegExec,
@@ -489,7 +510,74 @@ func CompressVideo(ctx context.Context, inputFile, outputFile string, height int
"-f", "mp4",
outputFile,
}
return handleFfmpegProcess(subCtx, ffmpegCmd)
return handleFfmpegProcess(ctx, ffmpegCmd)
}
func compressVideoGPU(ctx context.Context, inputFile, outputFile string, height int, hwaccel string) (bool, error) {
_, span := tracer.Start(ctx, "compressVideoGPU")
defer span.End()
span.SetAttributes(attribute.String("hwaccel", hwaccel))
var ffmpegCmd []string
switch hwaccel {
case "nvenc":
scaleFilter := fmt.Sprintf("scale_cuda=-2:%d", height)
ffmpegCmd = []string{
FfmpegExec,
"-hide_banner",
"-y",
"-hwaccel", "cuda",
"-hwaccel_output_format", "cuda",
"-i", inputFile,
"-vf", scaleFilter,
"-c:v", "h264_nvenc",
"-preset", "p4",
"-cq", "28",
"-c:a", "aac",
"-b:a", "128k",
"-f", "mp4",
outputFile,
}
case "amf":
scaleFilter := fmt.Sprintf("scale=-2:%d", height)
ffmpegCmd = []string{
FfmpegExec,
"-hide_banner",
"-y",
"-i", inputFile,
"-vf", scaleFilter,
"-c:v", "h264_amf",
"-quality", "balanced",
"-rc", "cqp",
"-qp_i", "28",
"-qp_p", "28",
"-c:a", "aac",
"-b:a", "128k",
"-f", "mp4",
outputFile,
}
case "qsv":
scaleFilter := fmt.Sprintf("scale=-2:%d", height)
ffmpegCmd = []string{
FfmpegExec,
"-hide_banner",
"-y",
"-hwaccel", "qsv",
"-i", inputFile,
"-vf", scaleFilter,
"-c:v", "h264_qsv",
"-preset", "faster",
"-global_quality", "28",
"-c:a", "aac",
"-b:a", "128k",
"-f", "mp4",
outputFile,
}
default:
return false, fmt.Errorf("不支持的硬件加速类型: %s", hwaccel)
}
return handleFfmpegProcess(ctx, ffmpegCmd)
}
func GetVideoCodec(ctx context.Context, filePath string) (string, error) {