// youtubeUtils.js /** * YouTube URL에서 비디오 ID를 추출합니다. */ export function extractYouTubeVideoId(url) { if (!url) return null; // youtu.be/ID 형식 (단축 URL) const shortUrlRegex = /youtu\.be\/([a-zA-Z0-9_-]{11})/; const shortMatch = url.match(shortUrlRegex); if (shortMatch) { return shortMatch[1]; } // youtube.com/watch?v=ID 형식 (일반 URL) const standardRegex = /youtube\.com\/watch\?v=([a-zA-Z0-9_-]{11})/; const standardMatch = url.match(standardRegex); if (standardMatch) { return standardMatch[1]; } // youtube.com/embed/ID 형식 (임베드 URL) const embedRegex = /youtube\.com\/embed\/([a-zA-Z0-9_-]{11})/; const embedMatch = url.match(embedRegex); if (embedMatch) { return embedMatch[1]; } // 만약 위 정규식들이 실패하면 마지막 시도 const lastAttemptRegex = /([a-zA-Z0-9_-]{11})/; const urlParts = url.split(/[\/\?&]/); for (const part of urlParts) { if (part.match(lastAttemptRegex) && part.length === 11) { return part; } } return null; } /** * YouTube 썸네일 URL을 가져옵니다 (가장 안정적인 방법). */ export function getYouTubeThumbnail(url) { const videoId = extractYouTubeVideoId(url); if (!videoId) return ''; // return `https://i.ytimg.com/vi/${videoId}/mqdefault.jpg`; // 기본 해상도 return `https://i.ytimg.com/vi/${videoId}/maxresdefault.jpg`; // 고해상도 (지원 안되는 영상 있음) }