bc-dlp

2.0.0 • Public • Published

bc-dlp

Actions Status Coverage Status

A yt-dlp wrapper with a few utilities for downloading the latest release.

A simple node.js wrapper for yt-dlp for use with breadcrum.net.

  • Has a few dependencies
  • EventEmitter, Promise and Stream interface
  • Working Progress events
  • Utility functions
  • Migrated to real esm. Not backwards compatible with CJS require (use import).
  • Typescript Support Maybe typescript-in-js support someday. PRs welcome.

The interfaces may change a bit as things evolve here.

Installation

$ npm install bc-dlp

Usage

EventEmitter

Excecute yt-dlp and returns an EventEmitter. The ytDlpEvent event will expose all yt-dlp events, for example: The log message [download] Destination: output.mp4 will emit the event type download and the event data Destination: output.mp4. eventEmitter.child exposes the spawned yt-dlp process.

import { BcDLP } from 'bc-dlp'
const bcDLP = new BcDLP('yt-dlp') // If its in your process path

const eventEmitter = bcDLP
  .exec([
    'https://www.youtube.com/watch?v=aqz-KE-bpKQ',
    '-f',
    'best',
    '-o',
    'output.mp4'
  ])
  .on('progress', (progress) =>
    console.log(
      progress.percent,
      progress.totalSize,
      progress.currentSpeed,
      progress.eta
    )
  )
  .on('ytDlpEvent', (eventType, eventData) =>
    console.log(eventType, eventData)
  )
  .on('error', (error) => console.error(error))
  .on('close', () => console.log('all done'))

console.log(eventEmitter.child.pid)

Readable Stream

Excecute yt-dlp and returns an Readable Stream. The interface works just like the EventEmitter.

import { BcDLP } from 'bc-dlp'
import * as fs from 'fs'
const bcDLP = new BcDLP('yt-dlp')

const { readStream, execEventEmitter } = bcDLP.execStream([
  'https://www.youtube.com/watch?v=aqz-KE-bpKQ',
  '-f',
  'best[ext=mp4]'
])
readableStream.pipe(fs.createWriteStream('test.mp4'))
console.log(execEventEmitter.child.pid)

Promise

Excecute yt-dlp and returns an Promise.

const stdout = await bcDLP.execPromise([
  'https://www.youtube.com/watch?v=aqz-KE-bpKQ',
  '-f',
  'best',
  '-o',
  'output.mp4'
])
console.log(stdout)

Options and Cancellation

Additionally you can set the options of the spawned process and abort the process.
The abortion of the spawned process is handled by passing the signal of an AbortController.

const controller = new AbortController();
const eventEmitter = bcDLP.exec(
  [
    'https://www.youtube.com/watch?v=aqz-KE-bpKQ',
    '-f',
    'best',
    '-o',
    'output.mp4'
  ],
  { shell: true, detached: true, signal: controller.signal }
)

setTimeout(() => {
  controller.abort()
  console.log(eventEmitter.ytDlpProcess.killed) // true
}, 500)

Note: This fails crashes V8 with bcDLP.execPromise for some reason.

Metadata

Returns the yt-dlp --dump-json metadata as an object.

const metadata = await bcDLP.getVideoInfo(
  'https://www.youtube.com/watch?v=aqz-KE-bpKQ'
)
console.log(metadata.title)

Utility functions

Just a few utility functions to get informations.

const version = await bcDLP.getVersion()
const userAgent = await bcDLP.getUserAgent()
const help = await bcDLP.getHelp()
const extractors = await bcDLP.getExtractors()
const extractorDescriptions = await bcDLP.getExtractorDescriptions()

See also

This is re-write of foxesdocode/yt-dlp-wrap which is in turn a fork of youtube-dl-wrap. They were both well done modules, but some things were broken and extra flexability was needed for breadcrum.net.

License

MIT

Package Sidebar

Install

npm i bc-dlp

Weekly Downloads

35

Version

2.0.0

License

MIT

Unpacked Size

24.5 kB

Total Files

10

Last publish

Collaborators

  • bret