From 8a7ab63b0055eda9c19e10502dd59ce103c08e29 Mon Sep 17 00:00:00 2001 From: Alex X Date: Fri, 3 May 2024 13:53:46 +0300 Subject: [PATCH] Add virtual source to ffmpeg (for testing) --- internal/ffmpeg/ffmpeg.go | 6 +++ internal/ffmpeg/virtual/virtual.go | 59 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 internal/ffmpeg/virtual/virtual.go diff --git a/internal/ffmpeg/ffmpeg.go b/internal/ffmpeg/ffmpeg.go index 54f5abaa..e815f39b 100644 --- a/internal/ffmpeg/ffmpeg.go +++ b/internal/ffmpeg/ffmpeg.go @@ -7,6 +7,7 @@ import ( "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/ffmpeg/device" "github.com/AlexxIT/go2rtc/internal/ffmpeg/hardware" + "github.com/AlexxIT/go2rtc/internal/ffmpeg/virtual" "github.com/AlexxIT/go2rtc/internal/rtsp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/ffmpeg" @@ -193,6 +194,11 @@ func parseArgs(s string) *ffmpeg.Args { if err != nil { return nil } + } else if strings.HasPrefix(s, "virtual?") { + var err error + if args.Input, err = virtual.GetInput(s[8:]); err != nil { + return nil + } } else { args.Input = inputTemplate("file", s, query) } diff --git a/internal/ffmpeg/virtual/virtual.go b/internal/ffmpeg/virtual/virtual.go new file mode 100644 index 00000000..4a791982 --- /dev/null +++ b/internal/ffmpeg/virtual/virtual.go @@ -0,0 +1,59 @@ +package virtual + +import ( + "net/url" +) + +func GetInput(src string) (string, error) { + query, err := url.ParseQuery(src) + if err != nil { + return "", err + } + + // set defaults (using Add instead of Set) + query.Add("source", "testsrc") + query.Add("size", "1920x1080") + query.Add("decimals", "2") + + // https://ffmpeg.org/ffmpeg-filters.html + source := query.Get("source") + input := "-re -f lavfi -i " + source + + sep := "=" // first separator + for key, values := range query { + value := values[0] + + // https://ffmpeg.org/ffmpeg-utils.html#video-size-syntax + switch key { + case "color", "rate", "duration", "sar": + case "size": + switch value { + case "720": + value = "1280x720" + case "1080": + value = "1920x1080" + case "2K": + value = "2560x1440" + case "4K": + value = "3840x2160" + case "8K": + value = "7680x4230" // https://reolink.com/blog/8k-resolution/ + } + case "decimals": + if source != "testsrc" { + continue + } + default: + continue + } + + input += sep + key + "=" + value + sep = ":" // next separator + } + + if s := query.Get("format"); s != "" { + input += ",format=" + s + } + + return input, nil +}