Created
January 1, 2025 21:21
-
-
Save yoeven/5a5de3f40c1d3b08f542a99b6d58e85c to your computer and use it in GitHub Desktop.
Get Youtube Video Thumbnail JS/TS Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on info from here https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api | |
const formatsToTry = ["maxresdefault", "hqdefault", "0", "default"]; | |
const getYTThumbnail = async (video_id: string) => { | |
const baseURL = `https://img.youtube.com/vi/${video_id}/`; | |
for (let index = 0; index < formatsToTry.length; index++) { | |
const endText = formatsToTry[index]; | |
const fullURL = baseURL + `${endText}.jpg`; | |
const resp = await fetch(fullURL, { | |
method: "HEAD", | |
}); | |
if (resp.ok) { | |
return fullURL; | |
} | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment