Skip to content

Instantly share code, notes, and snippets.

@yoeven
Created January 1, 2025 21:21
Show Gist options
  • Save yoeven/5a5de3f40c1d3b08f542a99b6d58e85c to your computer and use it in GitHub Desktop.
Save yoeven/5a5de3f40c1d3b08f542a99b6d58e85c to your computer and use it in GitHub Desktop.
Get Youtube Video Thumbnail JS/TS Script
// 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