diff --git a/stream.go b/stream.go index 100b5bd1..d52c3d40 100644 --- a/stream.go +++ b/stream.go @@ -357,12 +357,16 @@ func (s *Stream) transcodeArgs(startAt float64) []string { // encoder selection CV := "libx264" - // no need to transcode h264 streams for max quality + // Check whether hwaccel should be used if os.Getenv("VAAPI") == "1" { CV = "h264_vaapi" extra := "-hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi" 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 args = append(args, []string{ @@ -378,7 +382,11 @@ func (s *Stream) transcodeArgs(startAt float64) []string { // VAAPI format = "format=nv12|vaapi,hwupload" 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 format = "format=nv12" 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 if s.quality == "max" { - args = append(args, []string{ - "-vf", format, - }...) + if CV == "h264_nvenc" { + // 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 { args = append(args, []string{ "-vf", fmt.Sprintf("%s,%s", format, scale), - "-maxrate", fmt.Sprintf("%d", s.bitrate), - "-bufsize", fmt.Sprintf("%d", s.bitrate*2), - }...) + "-maxrate", fmt.Sprintf("%d", s.bitrate), + "-bufsize", fmt.Sprintf("%d", s.bitrate*2), + }...) } // Output specs @@ -413,6 +428,15 @@ func (s *Stream) transcodeArgs(startAt float64) []string { "-low_power", "1", "-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" { args = append(args, []string{ "-preset", "faster",