Most YouTube videos at 720p exceed 100 MB. Therefore, these bots often degrade quality significantly or fail on longer content (like movies or concerts).
const Client, LocalAuth, MessageMedia = require('whatsapp-web.js'); const qrcode = require('qrcode-terminal'); const YTDlpWrap = require('yt-dlp-wrap').default; const fs = require('fs-extra'); const path = require('path'); // Initialize yt-dlp wrapper const ytDlpWrap = new YTDlpWrap(); // Initialize WhatsApp Client with local session storage const client = new Client( authStrategy: new LocalAuth(), puppeteer: args: ['--no-sandbox', '--disable-setuid-sandbox'] ); // Generate QR Code for WhatsApp Web Authentication client.on('qr', (qr) => qrcode.generate(qr, small: true ); console.log('Scan the QR code above with your WhatsApp Link Devices option.'); ); client.on('ready', () => console.log('WhatsApp Bot is online and ready!'); ); // Listen for incoming messages client.on('message', async (msg) => youtu\.be)\/.+$/; if (ytRegex.test(messageText)) const videoUrl = messageText; const outputPath = path.join(__dirname, `video_$Date.now().mp4`); await msg.reply('⏳ Processing your request. Please wait...'); try // Download the video in mp4 format using yt-dlp await ytDlpWrap.execPromise([ videoUrl, '-f', 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]', // Merge best video and audio into MP4 '-o', outputPath ]); // Verify file size before sending (WhatsApp limit is 64MB for standard media) const stats = await fs.stat(outputPath); const fileSizeInMB = stats.size / (1024 * 1024); if (fileSizeInMB > 64) await msg.reply('❌ The requested video exceeds WhatsApp\'s 64MB file size limit.'); await fs.unlink(outputPath); return; // Load file into WhatsApp Media wrapper and send const media = MessageMedia.fromFilePath(outputPath); await client.sendMessage(msg.from, media, caption: 'Here is your downloaded video!' ); // Clean up files from local server storage await fs.unlink(outputPath); catch (error) console.error('Download Error:', error); await msg.reply('❌ Failed to download the video. Please verify the link and try again later.'); if (await fs.pathExists(outputPath)) await fs.unlink(outputPath); ); client.initialize(); Use code with caution. Step 4: Run and Authenticate Execute your script using the terminal: node index.js Use code with caution. Youtube Video Downloader Whatsapp Bot
While deploying a custom media automation script offers immense utility, developers must account for several structural limitations. 1. Media Size Restrictions Most YouTube videos at 720p exceed 100 MB
Implement built-in request delays (cooldown timers) so users cannot flood the application with simultaneous tasks. Please wait