Paste a YouTube share/watch/short/embed URL. The tool extracts the 11-character video ID and creates common thumbnail URLs.
function getYouTubeId( input ) {
let url;
try {
url = new URL( input );
} catch ( error ) {
return "";
}
const host =
url.hostname.replace(
/^www\./,
""
);
if ( host === "youtu.be" ) {
return url.pathname
.split( "/" )
.filter( Boolean )[0] || "";
}
if (
host === "youtube.com" ||
host === "m.youtube.com"
) {
if (
url.pathname === "/watch"
) {
return url.searchParams.get(
"v"
) || "";
}
const parts =
url.pathname
.split( "/" )
.filter( Boolean );
if (
[ "shorts", "embed", "v" ]
.includes( parts[0] )
) {
return parts[1] || "";
}
}
return "";
}
const base =
"https://img.youtube.com/vi/" +
videoId +
"/";
const thumbnails = {
default:
base + "default.jpg",
medium:
base + "mqdefault.jpg",
high:
base + "hqdefault.jpg",
standard:
base + "sddefault.jpg",
max:
base + "maxresdefault.jpg"
};
maxresdefault.jpg is not available for every YouTube video. If the image does not exist, use one of the lower-resolution variants.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.