@7sage/vidstack
Version:
UI component library for building high-quality, accessible video and audio experiences on the web.
2 lines (1 loc) • 711 B
JavaScript
const i=/(?:youtu\.be|youtube|youtube\.com|youtube-nocookie\.com)(?:\/shorts)?\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=|)((?:\w|-){11})/,u=new Map,s=new Map;function p(e){return e.match(i)?.[1]}async function h(e,n){if(u.has(e))return u.get(e);if(s.has(e))return s.get(e);const t=new Promise(async r=>{const c=["maxresdefault","sddefault","hqdefault"];for(const a of c)for(const f of[!0,!1]){const o=l(e,a,f);if((await fetch(o,{mode:"no-cors",signal:n.signal})).status<400){u.set(e,o),r(o);return}}}).catch(()=>"").finally(()=>s.delete(e));return s.set(e,t),t}function l(e,n,t){return`https://i.ytimg.com/${t?"vi_webp":"vi"}/${e}/${n}.${t?"webp":"jpg"}`}export{h as findYouTubePoster,p as resolveYouTubeVideoId};