YouTube Video Downloader (ytdl-core)

4.11.5 · active · verified Sun Apr 19

ytdl-core is a pure JavaScript library designed for downloading YouTube videos directly within Node.js environments, offering a streaming interface for efficient processing. Its current stable version is 4.11.5, with releases typically occurring as needed to address breaking changes from YouTube's API or to introduce minor features and bug fixes. The project shows an active maintenance cycle, frequently releasing patches (e.g., 4.11.2 to 4.11.5 in a few months) to adapt to YouTube's evolving infrastructure, particularly around video parsing and signature deciphering. A key differentiator is its focus on a lean, Node.js-friendly streaming API, allowing developers to pipe video streams directly to file systems or other processing utilities without intermediate storage. It ships with TypeScript types, facilitating its use in modern TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to download a YouTube video using `ytdl-core` and pipe it directly to a file. It also shows a commented-out example of how to retrieve video metadata using `getInfo`.

import * as fs from 'fs';
import ytdl from 'ytdl-core';

const videoUrl = 'http://www.youtube.com/watch?v=aqz-KE-bpKQ'; // Example video ID
const outputFileName = process.env.VIDEO_OUTPUT_PATH ?? 'video.mp4';

console.log(`Downloading video from ${videoUrl} to ${outputFileName}...`);

ytdl(videoUrl, { quality: 'highestaudio' })
  .pipe(fs.createWriteStream(outputFileName))
  .on('finish', () => {
    console.log('Download complete!');
  })
  .on('error', (err) => {
    console.error('Error during download:', err.message);
    // Optionally delete partially downloaded file on error
    if (fs.existsSync(outputFileName)) {
      fs.unlinkSync(outputFileName);
    }
  });

// Example of fetching video information
// ytdl.getInfo(videoUrl).then(info => {
//   console.log('Title:', info.videoDetails.title);
//   console.log('Formats:', info.formats.length);
// }).catch(err => console.error('Error fetching info:', err.message));

view raw JSON →