const YoutubeMp3Downloader = require("youtube-mp3-downloader"); // Configure the downloader const YD = new YoutubeMp3Downloader( outputPath: "./downloads", // Where to save the MP3s youtubeVideoQuality: "highest", // Audio quality from YouTube queueParallelism: 2, // Download 2 videos at once progressTimeout: 2000, // How often to emit 'progress' (ms) allowWebm: false // Prefer opus audio (requires ffmpeg) );
const requestId = uuidv4(); console.log(`[$requestId] Starting download for $videoId`); youtube-mp3-downloader npm
| Error | Cause | Solution | |-------|-------|----------| | FFmpeg not found | FFmpeg missing or not in PATH | Install FFmpeg and verify ffmpeg -version in terminal | | Video unavailable | Deleted, private, or region-blocked video | Check the URL manually; use cookies for private videos | | No audio stream | YouTube video has no audio (e.g., a static image slideshow) | Skip or notify user | | Rate limiting | Too many requests from same IP | Implement delays (e.g., setTimeout loop) or proxy rotation | | EPIPE or stream closed | Node memory limits or unstable connection | Increase memory: node --max-old-space-size=4096 script.js | async function downloadWithRetry(videoId, retries = 3) for (let i = 0; i < retries; i++) try await new Promise((resolve, reject) => YD.download(videoId); YD.once("finished", resolve); YD.once("error", reject); ); console.log("Success!"); return; catch (err) console.log(`Attempt $i+1 failed: $err.message`); if (i === retries - 1) throw err; await new Promise(r => setTimeout(r, 2000)); // wait 2 sec const requestId = uuidv4()
ffmpeg -version mkdir youtube-mp3-converter cd youtube-mp3-converter npm init -y npm install youtube-mp3-downloader Part 3: Your First Download Script Create a file named download.js . Here is the most basic working example: console.log(`[$requestId] Starting download for $videoId`)
bar.start(100, 0); YD.download(videoId);