├── go.mod ├── extractVideo └── extVideo.go └── main.go /go.mod: -------------------------------------------------------------------------------- 1 | module ytTHumbnailDownloader 2 | 3 | go 1.22.2 4 | -------------------------------------------------------------------------------- /extractVideo/extVideo.go: -------------------------------------------------------------------------------- 1 | package extractVideo 2 | 3 | import ( 4 | "net/url" 5 | "strings" 6 | ) 7 | 8 | func ExtractVideoId(videoURL string) string { 9 | parsedURL, err := url.Parse(videoURL) 10 | if err != nil { 11 | return "" 12 | } 13 | 14 | 15 | if parsedURL.Host == "youtu.be" { 16 | return strings.Trim(parsedURL.Path, "/") 17 | } 18 | 19 | if parsedURL.Host == "www.youtube.com" || parsedURL.Host == "youtube.com" { 20 | queryParams := parsedURL.Query() 21 | return queryParams.Get("v") 22 | } 23 | 24 | if strings.Contains(parsedURL.Path, "/embed/") { 25 | parts := strings.Split(parsedURL.Path, "/") 26 | return parts[len(parts)-1] 27 | } 28 | 29 | return "" 30 | } 31 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "log" 7 | "net/http" 8 | "os" 9 | "ytTHumbnailDownloader/extractVideo" 10 | ) 11 | 12 | func main() { 13 | fmt.Print("Enter the YouTube Video URL: ") 14 | var urlVideo string 15 | fmt.Scanln(&urlVideo) 16 | 17 | videoId := extractVideo.ExtractVideoId(urlVideo) 18 | if videoId == "" { 19 | log.Fatal("Invalid URL") 20 | } 21 | 22 | thumbnailURL := fmt.Sprintf("https://img.youtube.com/vi/%s/maxresdefault.jpg", videoId) 23 | 24 | res, err := http.Get(thumbnailURL) 25 | if err != nil { 26 | log.Fatal("Failed to download thumbnail") 27 | } 28 | defer res.Body.Close() 29 | 30 | fileName := videoId + ".jpg" 31 | file, err := os.Create(fileName) 32 | if err != nil { 33 | log.Fatal("Failed to create file") 34 | } 35 | defer file.Close() 36 | 37 | _, err = io.Copy(file, res.Body) 38 | if err != nil { 39 | log.Fatal("Failed to save image") 40 | } 41 | 42 | fmt.Printf("Thumbnail saved at: %s\n", fileName) 43 | } 44 | --------------------------------------------------------------------------------