ytdlp-nodejs

repository·main·Indexed 42 days ago

https://github.com/iqbal-rashed/ytdlp-nodejs

A TypeScript wrapper for the yt-dlp executable providing a type-safe, fluent API for downloading, streaming, and fetching metadata from thousands of websites. Version 3.4.5 includes a fluent builder API for configuration, a command-line interface, and support for FFmpeg integration to merge video and audio streams.

Tokens
30.2K
Snippets
126
Records
160
Agent score
53%

What's inside ytdlp-nodejs

  1. Overview of ytdlp-nodejs

    main

    ytdlp-nodejs is a Node.js wrapper for the yt-dlp command-line tool. It allows developers to integrate video downloading, streaming, and metadata extraction into Node.js applications without manually managing child processes or parsing CLI output.

    Key capabilities include:

    • Downloading: Saving video/audio to disk with progress tracking.
    • Streaming: Piping data directly to Node.js streams (e.g., Express responses).
    • Metadata Extraction: Fetching typed video information, formats, and playlists.
    • Binary Management: Automatic handling of the yt-dlp executable.
    • Customization: Support for all yt-dlp features (subtitles, cookies, custom formats) and raw argument passing.
  2. Authenticate with Username, Password, and 2FA

    main

    For sites supporting direct credential entry, use the username and password options. If the site requires Two-Factor Authentication (2FA), provide the code via the twofactor option.

    // Username and Password
    await ytdlp.downloadAsync(url, {
      username: 'your_username',
      password: 'your_password',
    });
    
    // Two-Factor Authentication
    await ytdlp.downloadAsync(url, {
      username: 'your_username',
      password: 'your_password',
      twofactor: '123456', // 2FA code
    });
  3. Download video or audio using interactive commands

    main

    You can use specific commands that trigger interactive prompts to select options like quality or format. This is useful when you want to be guided through the selection process.

    • Download Video: Prompts for video quality (e.g., 4K, 1080p) and optional extra arguments.
    • Download Audio: Prompts for audio format (MP3, M4A, WAV, or Best Quality) and optional extra arguments.
    • Get Video Info: Displays formatted metadata like title, uploader, duration, and views.
    ytdlp download <url>
    ytdlp audio <url>
    ytdlp info <url>
  4. Use the ytdlp interactive CLI mode

    main

    To launch the interactive menu, run the ytdlp command without any arguments. This mode provides a guided interface for common tasks such as downloading videos/audio, retrieving video information, updating the yt-dlp binary, or downloading FFmpeg binaries.

    npx ytdlp
    # or if installed globally
    ytdlp
  5. Handle cookies for age-gated or premium content

    main

    To access content that requires authentication, you can provide cookies either by extracting them from a browser or by providing a path to a Netscape-formatted cookies file.

    From Browser

    Use .cookiesFromBrowser(browserName) in the fluent API or the cookiesFromBrowser option in downloadAsync. Supported browsers include:

    • chrome
    • firefox
    • edge

    From File

    Use .cookies(filePath) in the fluent API or the cookies option in downloadAsync to point to a Netscape-formatted .txt file.

    // From Browser (Fluent API)
    const result = await ytdlp
      .download(url)
      .cookiesFromBrowser('chrome')
      .run();
    
    // From Browser (downloadAsync)
    await ytdlp.downloadAsync(url, {
      cookiesFromBrowser: 'chrome',
    });
    
    // From File (Fluent API)
    const result = await ytdlp.download(url).cookies('/path/to/cookies.txt').run();
    
    // From File (downloadAsync)
    await ytdlp.downloadAsync(url, {
      cookies: '/path/to/cookies.txt',
    });
  6. Download specific videos from a playlist

    main

    To download a subset of a playlist instead of the whole thing, use the playlistItems option in the downloadAsync configuration object. Supported formats include:

    • Ranges: '1-5' (first 5 videos)
    • Specific indices: '1,3,5' (videos 1, 3, and 5)
    • From index to end: '5-' (from video 5 to the end)
    await ytdlp.downloadAsync(playlistUrl, {
      playlistItems: '1-5', // First 5 videos
      // playlistItems: '1,3,5', // Videos 1, 3, and 5
      // playlistItems: '5-',    // From video 5 to the end
    });
  7. Install ytdlp-nodejs via npm

    main

    Install the package using npm to get a type-safe Node.js wrapper for yt-dlp.

    Note: FFmpeg is recommended for full functionality. You can install it manually on your system or use the built-in downloadFFmpeg() method provided by the library.

    npm install ytdlp-nodejs
  8. Download subtitles alongside video

    main

    To download subtitle files separately from the video file, set writeSubs: true in the options object passed to downloadAsync. You can specify which languages to download using the subLangs array. By default, subtitles are saved as .vtt files.

    await ytdlp.downloadAsync(url, {
      writeSubs: true,
      subLangs: ['en', 'es'], // Languages: English and Spanish
    });
  9. Integrate SponsorBlock to mark or remove segments

    main

    You can use SponsorBlock to handle crowdsourced segments (like sponsors or intros) in YouTube videos. You have two modes of operation:

    1. Marking: Uses sponsorblockMark to create chapters within the video file for specific segments.
    2. Removing: Uses sponsorblockRemove to physically cut segments out of the video file.

    Note: Using the sponsorblockRemove mode requires FFmpeg to be installed on your system.

    import { YtDlp } from 'ytdlp-nodejs';
    
    const ytdlp = new YtDlp();
    
    await ytdlp.downloadAsync(url, {
      // Mark segments as chapters
      sponsorblockMark: ['sponsor', 'intro', 'outro', 'selfpromo'],
    
      // Or remove segments entirely from the downloaded file
      sponsorblockRemove: ['sponsor', 'selfpromo'],
    });
  10. Configure proxy settings

    main

    You can route your requests through a proxy using the .proxy() method in the fluent builder API or the proxy option in downloadAsync.

    // Using fluent builder API
    const result = await ytdlp
      .download(url)
      .proxy('http://user:pass@proxyhost:port')
      .run();
    
    // Using downloadAsync
    await ytdlp.downloadAsync(url, {
      proxy: 'http://user:pass@proxyhost:port',
    });
  11. Pass raw arguments to yt-dlp

    main

    If a specific yt-dlp flag is not explicitly supported by the ytdlp-nodejs API, you can pass it directly using the .addArgs() method in the fluent builder API or the rawArgs option in downloadAsync. This is useful for advanced filtering or specific command-line behaviors.

    // Using fluent builder API
    const result = await ytdlp
      .download(url)
      .format({ filter: 'mergevideo', quality: '1080p', type: 'mp4' })
      .addArgs('--match-filter', 'duration > 60')
      .run();
    
    // Using downloadAsync
    await ytdlp.downloadAsync(url, {
      rawArgs: ['--match-filter', 'duration > 60'],
    });