Code refactoring for ffmpeg device and virtual

This commit is contained in:
Alex X
2024-05-22 12:58:21 +03:00
parent c41bddbbea
commit af05083a1f
3 changed files with 59 additions and 67 deletions
+45 -42
View File
@@ -4,56 +4,59 @@ import (
"net/url"
)
func GetInput(src string) (string, error) {
func GetInput(src string) string {
query, err := url.ParseQuery(src)
if err != nil {
return "", err
return ""
}
// set defaults (using Add instead of Set)
query.Add("video", "testsrc")
query.Add("size", "1920x1080")
query.Add("decimals", "2")
input := "-re"
// https://ffmpeg.org/ffmpeg-filters.html
video := query.Get("video")
input := "-re -f lavfi -i " + video
for _, video := range query["video"] {
// https://ffmpeg.org/ffmpeg-filters.html
sep := "=" // first separator
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" // crf=1 -> 12 Mbps
case "1080":
value = "1920x1080" // crf=1 -> 25 Mbps
case "2K":
value = "2560x1440" // crf=1 -> 43 Mbps
case "4K":
value = "3840x2160" // crf=1 -> 103 Mbps
case "8K":
value = "7680x4230" // https://reolink.com/blog/8k-resolution/
}
case "decimals":
if video != "testsrc" {
continue
}
default:
continue
if video == "" {
video = "testsrc=decimals=2" // default video
sep = ":"
}
input += sep + key + "=" + value
sep = ":" // next separator
input += " -f lavfi -i " + video
// set defaults (using Add instead of Set)
query.Add("size", "1920x1080")
for key, values := range query {
value := values[0]
// https://ffmpeg.org/ffmpeg-utils.html#video-size-syntax
switch key {
case "color", "rate", "duration", "sar", "decimals":
case "size":
switch value {
case "720":
value = "1280x720" // crf=1 -> 12 Mbps
case "1080":
value = "1920x1080" // crf=1 -> 25 Mbps
case "2K":
value = "2560x1440" // crf=1 -> 43 Mbps
case "4K":
value = "3840x2160" // crf=1 -> 103 Mbps
case "8K":
value = "7680x4230" // https://reolink.com/blog/8k-resolution/
}
default:
continue
}
input += sep + key + "=" + value
sep = ":" // next separator
}
if s := query.Get("format"); s != "" {
input += ",format=" + s
}
}
if s := query.Get("format"); s != "" {
input += ",format=" + s
}
return input, nil
return input
}