Skip to content

Events

HLS Downloader provides a rich event system through HlsDownloaderEvent for tracking download progress and handling errors in real time.

Listening to Events

Pass an onEvent callback in the HlsDownloader constructor:

ts
import { HlsDownloader } from '@hls-downloader/core'
import { HlsDownloaderEvent } from '@hls-downloader/shared'
import { WasmAdapter } from '@hls-downloader/adapters/wasm'

const downloader = new HlsDownloader({
  adapter: WasmAdapter,
  onEvent: (event, payload) => {
    switch (event) {
      case HlsDownloaderEvent.DOWNLOADING_SEGMENTS:
        console.log(`Downloading: ${payload?.completed}/${payload?.total}`)
        break
      case HlsDownloaderEvent.STICHING_SEGMENTS:
        console.log(`Stitching: ${payload?.completed}/${payload?.total}`)
        break
      case HlsDownloaderEvent.ERROR:
        console.error('Download error')
        break
    }
  },
})

Event Reference

EventPayloadDescription
FFMPEG_LOADINGFFmpeg core is being loaded (WASM only)
FFMPEG_LOADEDFFmpeg core has been loaded (WASM only)
STARTING_DOWNLOADDownload process has started
SOURCE_PARSEDHLS source has been parsed
DOWNLOADINGDownload is in progress
DOWNLOADING_SEGMENTS{ total, completed }Segment download progress
STICHING_SEGMENTS{ total, completed }Segment stitching/merging progress
READY_FOR_DOWNLOADMerged file is ready
ERRORAn error occurred

Progress Tracking

The DOWNLOADING_SEGMENTS and STICHING_SEGMENTS events provide a progress payload with total and completed fields:

ts
type ProgressPayload = {
  total: number
  completed: number
}

These are the only two events that include a payload. All other events are emitted without additional data.

Released under the MIT License.