package video import ( "fmt" "net/url" "regexp" "strings" ) // EmbedInfo holds parsed video embed information. type EmbedInfo struct { Provider string VideoID string URL string } var ( youtubeRegexps = []*regexp.Regexp{ regexp.MustCompile(`(?:youtube\.com/watch\?v=|youtu\.be/|youtube\.com/embed/)([a-zA-Z0-9_-]{11})`), } vimeoRegexp = regexp.MustCompile(`vimeo\.com/(\d+)`) loomRegexp = regexp.MustCompile(`loom\.com/share/([a-zA-Z0-9]+)`) ) // ParseEmbedURL parses a video URL and returns embed information. func ParseEmbedURL(rawURL string) (*EmbedInfo, error) { parsed, err := url.Parse(rawURL) if err != nil { return nil, fmt.Errorf("invalid URL: %w", err) } host := strings.ToLower(parsed.Host) if strings.Contains(host, "youtube.com") || strings.Contains(host, "youtu.be") { for _, re := range youtubeRegexps { if matches := re.FindStringSubmatch(rawURL); len(matches) > 1 { return &EmbedInfo{Provider: "youtube", VideoID: matches[1], URL: rawURL}, nil } } } if strings.Contains(host, "vimeo.com") { if matches := vimeoRegexp.FindStringSubmatch(rawURL); len(matches) > 1 { return &EmbedInfo{Provider: "vimeo", VideoID: matches[1], URL: rawURL}, nil } } if strings.Contains(host, "loom.com") { if matches := loomRegexp.FindStringSubmatch(rawURL); len(matches) > 1 { return &EmbedInfo{Provider: "loom", VideoID: matches[1], URL: rawURL}, nil } } return nil, fmt.Errorf("unsupported video provider for URL: %s", rawURL) } // EmbedIframeURL returns the iframe embed URL for a video provider and ID. func EmbedIframeURL(provider, videoID string) string { switch provider { case "youtube": return "https://www.youtube.com/embed/" + videoID case "vimeo": return "https://player.vimeo.com/video/" + videoID case "loom": return "https://www.loom.com/embed/" + videoID default: return "" } }