From a7968c92796c648ca330d62b0fb0a117e10b51e2 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 29 Sep 2023 10:25:00 -0700 Subject: [PATCH] stream: add UseGopSize --- config.go | 5 ++++- stream.go | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index 68ac20a6..6e9a6efb 100644 --- a/config.go +++ b/config.go @@ -39,6 +39,9 @@ type Config struct { NVENCTemporalAQ bool `json:"nvencTemporalAQ"` NVENCScale string `json:"nvencScale"` // cuda, npp - // Use transpose for streaming + // Use transpose workaround for streaming (VA-API) UseTranspose bool `json:"useTranspose"` + + // Use GOP size workaround for streaming (NVENC) + UseGopSize bool `json:"useGopSize"` } diff --git a/stream.go b/stream.go index 62f6f704..4b536861 100644 --- a/stream.go +++ b/stream.go @@ -513,17 +513,33 @@ func (s *Stream) transcode(startId int) { // Segmenting specs args = append(args, []string{ + "-start_number", fmt.Sprintf("%d", startId), "-avoid_negative_ts", "disabled", "-f", "hls", "-hls_flags", "split_by_time", "-hls_time", fmt.Sprintf("%d", s.c.ChunkSize), "-hls_segment_type", "mpegts", "-hls_segment_filename", s.getTsPath(-1), - "-force_key_frames", fmt.Sprintf("expr:gte(t,n_forced*%d)", s.c.ChunkSize), - "-start_number", fmt.Sprintf("%d", startId), - "-", }...) + // Keyframe specs + if s.c.UseGopSize && s.m.probe.FrameRate > 0 { + // Fix GOP size + args = append(args, []string{ + "-g", fmt.Sprintf("%d", s.c.ChunkSize*s.m.probe.FrameRate), + "-keyint_min", fmt.Sprintf("%d", s.c.ChunkSize*s.m.probe.FrameRate), + }...) + } else { + // Force keyframes every chunk + args = append(args, []string{ + "-force_key_frames", fmt.Sprintf("expr:gte(t,n_forced*%d)", s.c.ChunkSize), + }...) + } + + // Output to stdout + args = append(args, "-") + + // Start the process s.coder = exec.Command(s.c.FFmpeg, args...) log.Printf("%s-%s: %s", s.m.id, s.quality, strings.Join(s.coder.Args[:], " "))