UNPKG

yt-dlp-video

Version:

A robust video downloader built on yt-dlp for downloading and processing video content from various sources.

71 lines (60 loc) 1.79 kB
const { DownloaderHelper } = require('node-downloader-helper'); const path = require('path'); const fs = require('fs'); const os = require('os'); const getBinaryUrl = () => { const platform = os.platform(); const baseUrl = 'https://github.com/yt-dlp/yt-dlp/releases/latest/download/'; switch(platform) { case 'win32': return `${baseUrl}yt-dlp.exe`; case 'darwin': case 'linux': return `${baseUrl}yt-dlp`; default: throw new Error(`Unsupported platform: ${platform}`); } }; const getBinaryName = () => { return os.platform() === 'win32' ? 'yt-dlp.exe' : 'yt-dlp'; }; const setup = async () => { const binPath = path.join(__dirname, 'bin'); if (!fs.existsSync(binPath)) { fs.mkdirSync(binPath, { recursive: true }); } const binaryUrl = getBinaryUrl(); const binaryPath = path.join(binPath, getBinaryName()); // Always check for binary and permissions let needsDownload = true; if (fs.existsSync(binaryPath)) { if (os.platform() !== 'win32') { try { fs.accessSync(binaryPath, fs.constants.X_OK); needsDownload = false; } catch { // Binary exists but not executable, will redownload } } else { needsDownload = false; } } if (needsDownload) { const dl = new DownloaderHelper(binaryUrl, binPath); await new Promise((resolve, reject) => { dl.on('end', () => { if (os.platform() !== 'win32') { fs.chmodSync(binaryPath, '755'); } resolve(); }); dl.on('error', reject); dl.start(); }); } return binaryPath; }; setup() .then(() => console.log('yt-dlp setup completed')) .catch(console.error); module.exports = setup;