Examples

This page demonstrates common use cases with complete, runnable code examples.

Download a Video with Progress

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();
4
5async function downloadWithProgress() {
6  // Fluent builder API
7  const result = await ytdlp
8    .download('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
9    .filter('mergevideo')
10    .quality('1080p')
11    .type('mp4')
12    .output('./downloads')
13    .on('progress', (progress) => {
14      console.log(`Downloaded: ${progress.percentage_str}`);
15      console.log(`Speed: ${progress.speed_str}`);
16      console.log(`ETA: ${progress.eta_str}`);
17    })
18    .run();
19
20  console.log('Download complete!');
21  console.log('Files:', result.filePaths);
22}
23
24downloadWithProgress();

Get Video Information

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();
4
5async function getVideoInfo() {
6  const info = await ytdlp.getInfoAsync(
7    'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
8  );
9
10  console.log('Title:', info.title);
11  console.log('Duration:', info.duration, 'seconds');
12  console.log('Views:', info.view_count);
13  console.log('Uploader:', info.uploader);
14  console.log('Available formats:', info.formats?.length);
15}
16
17getVideoInfo();

Stream to File

1import { YtDlp } from 'ytdlp-nodejs';
2import { createWriteStream } from 'fs';
3
4const ytdlp = new YtDlp();
5
6async function streamToFile() {
7  const stream = ytdlp.stream('https://www.youtube.com/watch?v=dQw4w9WgXcQ', {
8    format: { filter: 'audioandvideo', type: 'mp4' },
9    onProgress: (p) => console.log(`${p.percentage_str}`),
10  });
11
12  const file = createWriteStream('output.mp4');
13  await stream.pipeAsync(file);
14  console.log('Stream complete!');
15}
16
17streamToFile();

Download Audio Only

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();
4
5async function downloadAudio() {
6  // Method 1: Using fluent builder API
7  const result = await ytdlp
8    .download('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
9    .filter('audioonly')
10    .type('mp3')
11    .quality(0) // 0-10, 0 is best
12    .on('progress', (p) => console.log(p.percentage_str))
13    .run();
14
15  console.log('Audio downloaded:', result.filePaths);
16
17  // Method 2: Using downloadAudio helper
18  await ytdlp.downloadAudio(
19    'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
20    'mp3',
21  );
22}
23
24downloadAudio();

Get Direct URLs

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();
4
5async function getUrls() {
6  // Get direct streaming URLs for all formats
7  const urls = await ytdlp.getDirectUrlsAsync(
8    'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
9  );
10
11  console.log(`Found ${urls.length} format URLs`);
12  console.log('First URL:', urls[0]);
13}
14
15getUrls();

List Available Formats

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();
4
5async function listFormats() {
6  const result = await ytdlp.getFormatsAsync(
7    'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
8  );
9
10  console.log(`Found ${result.formats.length} formats`);
11
12  // Filter video formats
13  const videoFormats = result.formats.filter(
14    (f) => f.vcodec !== 'none' && f.acodec === 'none',
15  );
16  console.log('Video-only formats:', videoFormats.length);
17
18  // Filter audio formats
19  const audioFormats = result.formats.filter(
20    (f) => f.acodec !== 'none' && f.vcodec === 'none',
21  );
22  console.log('Audio-only formats:', audioFormats.length);
23}
24
25listFormats();

Get Video as File Object

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();
4
5async function getVideoFile() {
6  const file = await ytdlp.getFileAsync(
7    'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
8    {
9      format: { filter: 'audioandvideo', type: 'mp4' },
10      onProgress: (p) => console.log(p.percentage_str),
11    },
12  );
13
14  console.log('File name:', file.name);
15  console.log('File size:', file.size, 'bytes');
16  console.log('MIME type:', file.type);
17
18  // Use the file object (e.g., upload to cloud storage)
19  // const buffer = await file.arrayBuffer();
20}
21
22getVideoFile();

Execute Raw Command

1import { YtDlp } from 'ytdlp-nodejs';
2
3const ytdlp = new YtDlp();
4
5async function execRawCommand() {
6  // Execute with custom arguments
7  const output = await ytdlp.execAsync(
8    'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
9    {
10      rawArgs: ['--print', 'title', '--print', 'duration'],
11    },
12  );
13  console.log(output);
14}
15
16execRawCommand();