The YoutubeMp3Downloader class is the primary interface for managing YouTube to MP3 (or other audio/video formats) downloads. It manages a download queue, handles metadata extraction, and uses ffmpeg for encoding and album art embedding.
To use it, instantiate the class with YoutubeMp3DownloaderOptions and call .download(videoId, onStateChanged) to add a task to the queue. You can monitor the lifecycle of a download via the onStateChanged callback.
import { YoutubeMp3Downloader } from './services/youtube-mp3-downloader';
const downloader = new YoutubeMp3Downloader({
ffmpegPath: '/path/to/ffmpeg',
outputPath: './downloads',
youtubeVideoQuality: 'highest',
filter: 'audioonly',
format: 'mp3',
progressTimeout: 1000,
outputOptions: ['-id3v2_version', '4']
});
downloader.download('VIDEO_ID_HERE', (action) => {
switch (action.state) {
case 'added':
console.log('Task added:', action.payload);
break;
case 'getting info':
console.log('Fetching metadata...');
break;
case 'downloading':
console.log(`Progress: ${action.payload.progress.percentage}%`);
break;
case 'done':
console.log('Download complete!', action.payload);
break;
case 'error':
console.error('Download failed:', action.error);
break;
}
});