node-webvtt is a JavaScript library designed for comprehensive handling of WebVTT (Web Video Text Tracks) files. It provides functionalities for parsing WebVTT input into a structured JavaScript object, compiling such objects back into WebVTT format, and segmenting WebVTT content. A key differentiator is its integrated support for HLS (HTTP Live Streaming), enabling the generation of HLS playlists and segments directly from WebVTT data, which is crucial for delivering timed text tracks alongside adaptive video streams. The library is currently at version 1.9.4 (as of the last recorded release in early 2022) and maintains an active release cadence, primarily focusing on bug fixes, dependency updates, and minor feature enhancements. It offers both strict and non-strict parsing options, allowing developers to control error handling behavior when processing potentially malformed VTT files, and also supports parsing of WebVTT metadata.
npm install node-webvttVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a WebVTT string (including metadata), compiling a parsed object back to WebVTT, and then segmenting the input to generate HLS-compatible WebVTT segments and an associated M3U8 playlist. It also highlights the use of `strict: false` and `meta: true` options for parsing.
Use a `try/catch` block around `webvtt.parse()` calls or pass `{ strict: false }` as an option to tolerate malformed cues and receive errors in an `errors` array instead of an exception.Ensure WebVTT cues have distinct start and end times (`start < end`) for maximum compatibility and future-proofing, especially if `strict: true` is desired.
Upgrade `node-webvtt` to version 1.3.0 or higher to enable WebVTT metadata parsing via the `{ meta: true }` option in `webvtt.parse()`.Explicitly define the `startOffset` parameter when calling `webvtt.hls.hlsSegment()` to ensure correct timestamp mapping for your HLS workflow, especially if not using the default 0 for `LOCAL` timestamp.
Review and correct the timestamp format in the WebVTT file. Alternatively, call `webvtt.parse(input, { strict: false })` to allow parsing with errors and inspect the `result.errors` array.Ensure the WebVTT file strictly adheres to the WebVTT specification, starting with 'WEBVTT' on the first line. For files with potential header variations, consider using `{ strict: false }`.Ensure you are using `import webvtt from 'node-webvtt';` (ESM) or `const webvtt = require('node-webvtt');` (CJS) and accessing `hls` as a property of the default export: `webvtt.hls.hlsSegmentPlaylist(...)`. Verify your `node-webvtt` version supports HLS features (added in v1.2).