Quick Start

This guide will help you get started with the basics of ytdlp-nodejs.

Basic Setup

First, import and initialize the library:

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();

Downloading a Video

Download a video using the fluent builder API:

1const result = await ytdlp
2  .download('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
3  .filter('mergevideo')
4  .quality('1080p')
5  .type('mp4')
6  .on('progress', (progress) => {
7    console.log(`Downloading: ${progress.percentage_str}`);
8    console.log(`Speed: ${progress.speed_str}`);
9    console.log(`ETA: ${progress.eta_str}`);
10  })
11  .run();
12
13console.log('Downloaded files:', result.filePaths);

Or use downloadAsync with callback-style progress:

1await ytdlp.downloadAsync('https://www.youtube.com/watch?v=dQw4w9WgXcQ', {
2  format: { filter: 'mergevideo', quality: '1080p', type: 'mp4' },
3  onProgress: (progress) => console.log(`${progress.percentage_str}`),
4});

Getting Video Info

Fetch metadata without downloading:

1const info = await ytdlp.getInfoAsync(
2  'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
3);
4
5console.log('Title:', info.title);
6console.log('Duration:', info.duration);
7console.log('Views:', info.view_count);

Streaming

Stream video directly to a file or another output stream:

1import { createWriteStream } from 'fs';
2
3const ytdlpStream = ytdlp.stream('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
4const fileStream = createWriteStream('video.mp4');
5
6// Using async pipe
7await ytdlpStream.pipeAsync(fileStream);
8console.log('Stream finished!');

Downloading Audio Only

Download and convert to MP3:

1await ytdlp.downloadAudio('https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'mp3');