Merge pull request #3 from MB-Finski/patch-1

Add support for NVIDIA GPU accelerated transcoding
monorepo
Varun Patil 2022-11-29 13:52:08 -08:00 committed by GitHub
commit 64767d7a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 9 deletions

View File

@ -357,12 +357,16 @@ func (s *Stream) transcodeArgs(startAt float64) []string {
// encoder selection // encoder selection
CV := "libx264" CV := "libx264"
// no need to transcode h264 streams for max quality // Check whether hwaccel should be used
if os.Getenv("VAAPI") == "1" { if os.Getenv("VAAPI") == "1" {
CV = "h264_vaapi" CV = "h264_vaapi"
extra := "-hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi" extra := "-hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi"
args = append(args, strings.Split(extra, " ")...) args = append(args, strings.Split(extra, " ")...)
} } else if os.Getenv("NVENC") == "1" {
CV = "h264_nvenc"
extra := "-hwaccel cuda -hwaccel_output_format cuda"
args = append(args, strings.Split(extra, " ")...)
}
// Input specs // Input specs
args = append(args, []string{ args = append(args, []string{
@ -378,7 +382,11 @@ func (s *Stream) transcodeArgs(startAt float64) []string {
// VAAPI // VAAPI
format = "format=nv12|vaapi,hwupload" format = "format=nv12|vaapi,hwupload"
scale = fmt.Sprintf("scale_vaapi=w=%d:h=%d:force_original_aspect_ratio=decrease", s.width, s.height) scale = fmt.Sprintf("scale_vaapi=w=%d:h=%d:force_original_aspect_ratio=decrease", s.width, s.height)
} else { } else if CV == "h264_nvenc" {
// NVENC
format = "format=nv12|cuda,hwupload"
scale = fmt.Sprintf("scale_cuda=w=%d:h=%d:force_original_aspect_ratio=decrease:passthrough=0", s.width, s.height)
} else {
// x264 // x264
format = "format=nv12" format = "format=nv12"
if s.width >= s.height { if s.width >= s.height {
@ -390,15 +398,22 @@ func (s *Stream) transcodeArgs(startAt float64) []string {
// do not scale or set bitrate for full quality // do not scale or set bitrate for full quality
if s.quality == "max" { if s.quality == "max" {
args = append(args, []string{ if CV == "h264_nvenc" {
"-vf", format, // Due to a bug(?) in NVENC, passthrough=0 must be set
}...) args = append(args, []string{
"-vf", fmt.Sprintf("%s,%s", format, "scale_cuda=passthrough=0"),
}...)
} else {
args = append(args, []string{
"-vf", format,
}...)
}
} else { } else {
args = append(args, []string{ args = append(args, []string{
"-vf", fmt.Sprintf("%s,%s", format, scale), "-vf", fmt.Sprintf("%s,%s", format, scale),
"-maxrate", fmt.Sprintf("%d", s.bitrate), "-maxrate", fmt.Sprintf("%d", s.bitrate),
"-bufsize", fmt.Sprintf("%d", s.bitrate*2), "-bufsize", fmt.Sprintf("%d", s.bitrate*2),
}...) }...)
} }
// Output specs // Output specs
@ -413,6 +428,15 @@ func (s *Stream) transcodeArgs(startAt float64) []string {
"-low_power", "1", "-low_power", "1",
"-global_quality", "25", "-global_quality", "25",
}...) }...)
} else if CV == "h264_nvenc" {
args = append(args, []string{
"-preset", "p6",
"-tune", "ll",
"-temporal-aq", "1",
"-rc", "vbr",
"-rc-lookahead","30",
"-cq", "24",
}...)
} else if CV == "libx264" { } else if CV == "libx264" {
args = append(args, []string{ args = append(args, []string{
"-preset", "faster", "-preset", "faster",