From 9972fc23c6c0dc3a8aab28dc246065ae8d9e330b Mon Sep 17 00:00:00 2001 From: MB-Finski <64466176+MB-Finski@users.noreply.github.com> Date: Fri, 25 Nov 2022 13:59:33 +0200 Subject: [PATCH] Add support for NVIDIA GPU accelerated transcoding Add a transcoding profile for ffmpeg suitable for using with NVENC. --- stream.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/stream.go b/stream.go index 06b1633e..01492e59 100644 --- a/stream.go +++ b/stream.go @@ -288,12 +288,16 @@ func (s *Stream) transcode(startId int) { // 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{ @@ -309,7 +313,15 @@ func (s *Stream) transcode(startId int) { // 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" + if s.width >= s.height { + scale = fmt.Sprintf("scale_cuda=-2:%d", s.height) + } else { + scale = fmt.Sprintf("scale_cuda=%d:-2", s.width) + } + } else { // x264 format = "format=nv12" if s.width >= s.height {