vchikalkin e9146a0656 feat(youtube): refactor download logic and integrate new API
- Added optional YTDLP_PATH environment variable to configure yt-dlp path.
- Removed old youtube download utility and replaced it with a new API-based approach.
- Implemented new functions to fetch YouTube video download URLs using an external service.
- Introduced yt-dlp integration for fallback download method.
- Created a structured response for video information retrieval.
- Enhanced error handling for video duration and response validation.
2026-02-17 23:28:53 +03:00

41 lines
872 B
TypeScript

import axios from 'axios';
type DlsrvResponse = {
duration: number;
filename: string;
status: string;
url: string;
};
export async function getYoutubeDownload(url: string) {
const videoId = getYouTubeVideoId(url);
const { data } = await axios.post<DlsrvResponse>('https://embed.dlsrv.online/api/download/mp4', {
format: 'mp4',
quality: '720',
videoId,
});
return data.url;
}
export function getYouTubeVideoId(link: string) {
const url = new URL(link);
// 1. shorts
if (url.pathname.startsWith('/shorts/')) {
return url.pathname.split('/')[2] || null;
}
// 2. обычное видео: watch?v=
const vParam = url.searchParams.get('v');
if (vParam) return vParam;
// 3. короткая ссылка youtu.be
if (url.hostname.includes('youtu.be')) {
return url.pathname.slice(1) || null;
}
return null;
}