How YouTube thumbnail URLs work (and how to always get the HD one)

2026-11-28来源 dev.toEnglish

If you've ever needed the thumbnail of a YouTube video, for a link preview, a blog embed, or a quick design reference, you might be surprised how simple it is. There's no API key and no scraping. Every video's thumbnails live at predictable URLs.

There's one gotcha, though, and most snippets online get it wrong.

The URL pattern

Every YouTube video has an 11-character ID. In https://www.youtube.com/watch?v=dQw4w9WgXcQ, it's dQw4w9WgXcQ. The thumbnails are served from i.ytimg.com:

https://i.ytimg.com/vi/<VIDEO_ID>/<SIZE>.jpg 

There are five sizes:

Name Size Always exists?
maxresdefault 1280 × 720 No, only for HD uploads
sddefault 640 × 480 Usually
hqdefault 480 × 360 Yes
mqdefault 320 × 180 Yes
default 120 × 90 Yes

WebP versions also exist at https://i.ytimg.com/vi_webp/<VIDEO_ID>/<SIZE>.webp.

Step 1: get the video ID from any link

People paste every kind of link: watch?v=, youtu.be/, /shorts/, /embed/, /live/. One regex covers them all:

export function getYouTubeId(input) { const s = String(input || '').trim(); if (/^[\w-]{11}$/.test(s)) return s; // already an ID const m = s.match( /(?:youtube(?:-nocookie)?\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/|live\/|v\/)|youtu\.be\/)([\w-]{11})/i ); return m ? m[1] : null; } getYouTubeId('https://youtu.be/dQw4w9WgXcQ'); // 'dQw4w9WgXcQ' getYouTubeId('https://www.youtube.com/shorts/aqz-KE-bpKQ'); // 'aqz-KE-bpKQ' 

Step 2: build the URLs

const SIZES = ['maxresdefault', 'sddefault', 'hqdefault', 'mqdefault', 'default']; export function youtubeThumbnails(id) { return SIZES.map((name) => `https://i.ytimg.com/vi/${id}/${name}.jpg`); } 

The gotcha: maxresdefault doesn't always exist

This is where most tutorials stop, and it's why people end up with a blurry grey box on their site.

maxresdefault only exists if the video was uploaded in HD. When it's missing, YouTube doesn't give you a nice JSON error. It still serves an image: a small 120 × 90 grey placeholder. In the browser, <img onerror> won't fire for it, so a naive "try the biggest, fall back on error" approach silently shows the placeholder.

The fix is to check the real size of the image you got back:

function imageSize(url) { return new Promise((resolve) => { const img = new Image(); img.onload = () => resolve({ w: img.naturalWidth, h: img.naturalHeight }); img.onerror = () => resolve(null); img.src = url; }); } export async function bestYouTubeThumbnail(id) { for (const name of SIZES) { const url = `https://i.ytimg.com/vi/${id}/${name}.jpg`; const size = await imageSize(url); // Anything 120px wide (except the real "default") is the placeholder if (size && (name === 'default' || size.w > 120)) return url; } return null; } 

Now you always get the largest thumbnail that actually exists.

What about Shorts?

Shorts use the same ID and the same URLs, but the images are still 16:9 with the vertical frame in the middle. If you want the original 9:16 look, crop the centre of maxresdefault (or hqdefault) to a 9:16 box.

And Vimeo?

Vimeo has no URL pattern, but its public oEmbed endpoint returns the thumbnail with no API key:

const res = await fetch( `https://vimeo.com/api/oembed.json?url=${encodeURIComponent(videoUrl)}&width=1280` ); const { thumbnail_url } = await res.json(); 

The full helper

I put all of this (YouTube, Shorts and Vimeo, browser and Node) into a tiny MIT-licensed file with no dependencies: video-thumbnail-urls on GitHub.

If you just need the image and don't want to write any code, I also built a free web version: Thumbnail Downloader. Paste a link and it shows every size that really exists, ready to download. And if you're designing thumbnails rather than grabbing them, here's a thumbnail size cheat sheet for YouTube, Shorts, Vimeo and more.

One reminder: only reuse thumbnails you own or have permission to use.

Have you hit any other YouTube image quirks? Let me know in the comments.

原文:https://www.a8q9.cn/en/article/how-youtube-thumbnail-urls-work-and-how-to-always-get-the-hd-one-9bfd73.html

相关文章