StreamRecorder/media/parser.go

89 lines
1.6 KiB
Go

package media
import (
"strconv"
"strings"
)
func ParseAttribute(attribute string) string {
parts := strings.Split(attribute, "=")
if len(parts) >= 2 {
return parts[1]
}
return ""
}
func StripQuotes(input string) string {
return strings.Trim(input, "\"")
}
func ResolutionToPixels(resolution string) int {
parts := strings.Split(resolution, "x")
if len(parts) != 2 {
return 0
}
width, err := strconv.Atoi(parts[0])
if err != nil {
return 0
}
height, err := strconv.Atoi(parts[1])
if err != nil {
return 0
}
return width * height
}
func parseMediaAttributes(mediaInfo string) map[string]string {
attributes := make(map[string]string)
// Remove the #EXT-X-MEDIA: prefix
content := strings.TrimPrefix(mediaInfo, ExtXMedia+":")
// Split by comma, but respect quoted values
parts := splitRespectingQuotes(content, ',')
for _, part := range parts {
if kv := strings.SplitN(strings.TrimSpace(part), "=", 2); len(kv) == 2 {
attributes[kv[0]] = kv[1]
}
}
return attributes
}
func splitRespectingQuotes(input string, delimiter rune) []string {
var result []string
var current strings.Builder
inQuotes := false
for _, char := range input {
switch char {
case '"':
inQuotes = !inQuotes
current.WriteRune(char)
case delimiter:
if inQuotes {
current.WriteRune(char)
} else {
result = append(result, current.String())
current.Reset()
}
default:
current.WriteRune(char)
}
}
if current.Len() > 0 {
result = append(result, current.String())
}
return result
}
func ValidateURL(url string) bool {
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
}