Skip to content

Commit

Permalink
Add virtual source to ffmpeg (for testing)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 3, 2024
1 parent 07f51e6 commit 8a7ab63
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
59 changes: 59 additions & 0 deletions internal/ffmpeg/virtual/virtual.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8a7ab63

Please sign in to comment.