youtobe video added

This commit is contained in:
nabijonovdavronbek619@gmail.com
2025-11-15 20:46:12 +05:00
parent c8c77f3788
commit 6d5431f5ae
2 changed files with 37 additions and 8 deletions

22
lib/embedUrl.ts Normal file
View File

@@ -0,0 +1,22 @@
export function toEmbedUrl(url: string): string {
let videoId = '';
// watch?v=...
if (url.includes('watch?v=')) {
const params = new URL(url).searchParams;
videoId = params.get('v') || '';
}
// youtu.be/...
else if (url.includes('youtu.be/')) {
const pathname = new URL(url).pathname; // /videoId
videoId = pathname.split('/')[1] || '';
}
// shorts/...
else if (url.includes('youtube.com/shorts/')) {
const pathname = new URL(url).pathname; // /shorts/videoId
videoId = pathname.split('/')[2] || '';
}
return videoId ? `https://www.youtube.com/embed/${videoId}` : '';
}