Before installing the package, ensure you have the following installed: Download here.
const fs = require('fs'); const path = require('path'); const ytdl = require('@distube/ytdl-core'); const ffmpeg = require('fluent-ffmpeg'); /** * Downloads a YouTube video's audio track and converts it to MP3. * @param string url - The YouTube video URL. * @param string outputName - The desired name of the output MP3 file. */ async function downloadYouTubeMP3(url, outputName) try info.videoDetails.title.replace(/[^\w\s]/gi, ''); const outputPath = path.join(__dirname, `$videoTitle.mp3`); console.log(`Title found: $info.videoDetails.title`); console.log('Downloading audio stream...'); // 2. Filter for audio-only streams to save bandwidth const audioStream = ytdl(url, quality: 'highestaudio', filter: 'audioonly', ); // 3. Convert the stream to MP3 using fluent-ffmpeg ffmpeg(audioStream) .audioCodec('libmp3lame') .audioBitrate(192) // Standard high-quality audio bitrate .toFormat('mp3') .on('start', (commandLine) => console.log('Spawned FFmpeg with command: ' + commandLine); ) .on('progress', (progress) => if (progress.percent) console.log(`Processing: $Math.round(progress.percent)% done`); else console.log(`Processing... Target size: $progress.targetSize KB`); ) .on('error', (err) => console.error('An error occurred during conversion:', err.message); ) .on('end', () => console.log(`\nSuccess! File saved to: $outputPath`); ) .save(outputPath); catch (error) console.error('Download failed:', error.message); // Example Usage: const targetUrl = 'https://youtube.com'; downloadYouTubeMP3(targetUrl, 'RickAstley_NeverGonnaGiveYouUp'); Use code with caution. Step 3: Run the Script Execute the script using Node.js: node downloader.js Use code with caution. 5. Troubleshooting Common Issues youtube-mp3-downloader npm
npm install cli-progress const YoutubeMp3Downloader = require("youtube-mp3-downloader"); const cliProgress = require("cli-progress"); Before installing the package, ensure you have the
eGauge Systems will be closed starting Thursday, December 25th 2025 and will reopen on Monday, January 5th 2026. We wish everyone a happy new years and look forward to working with you in the new year!