You've already forked VptPassiveAdapter
hwaccel
This commit is contained in:
@@ -34,6 +34,7 @@ disconnectAction:
|
|||||||
command: ""
|
command: ""
|
||||||
preview:
|
preview:
|
||||||
enabled: true
|
enabled: true
|
||||||
|
hwaccel: ""
|
||||||
resolutions:
|
resolutions:
|
||||||
- 720
|
- 720
|
||||||
- 1080
|
- 1080
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ type DisconnectActionConfig struct {
|
|||||||
type PreviewConfig struct {
|
type PreviewConfig struct {
|
||||||
Enabled bool `mapstructure:"enabled"`
|
Enabled bool `mapstructure:"enabled"`
|
||||||
Resolutions []int `mapstructure:"resolutions"`
|
Resolutions []int `mapstructure:"resolutions"`
|
||||||
|
HwAccel string `mapstructure:"hwaccel"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MainConfig struct {
|
type MainConfig struct {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"ZhenTuLocalPassiveAdapter/config"
|
||||||
"ZhenTuLocalPassiveAdapter/dto"
|
"ZhenTuLocalPassiveAdapter/dto"
|
||||||
"ZhenTuLocalPassiveAdapter/logger"
|
"ZhenTuLocalPassiveAdapter/logger"
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -474,6 +475,26 @@ func CompressVideo(ctx context.Context, inputFile, outputFile string, height int
|
|||||||
subCtx, span := tracer.Start(ctx, "CompressVideo")
|
subCtx, span := tracer.Start(ctx, "CompressVideo")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
span.SetAttributes(attribute.Int("height", height))
|
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)
|
scaleFilter := fmt.Sprintf("scale=-2:%d", height)
|
||||||
ffmpegCmd := []string{
|
ffmpegCmd := []string{
|
||||||
FfmpegExec,
|
FfmpegExec,
|
||||||
@@ -489,7 +510,74 @@ func CompressVideo(ctx context.Context, inputFile, outputFile string, height int
|
|||||||
"-f", "mp4",
|
"-f", "mp4",
|
||||||
outputFile,
|
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) {
|
func GetVideoCodec(ctx context.Context, filePath string) (string, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user