{"library":"node-webvtt","title":"Node WebVTT Parser, Compiler, and Segmenter","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install node-webvtt"],"cli":null},"imports":["import webvtt from 'node-webvtt';","import webvtt from 'node-webvtt';\nconst parsed = webvtt.parse(input, { strict: true });","import webvtt from 'node-webvtt';\nconst playlist = webvtt.hls.hlsSegmentPlaylist(input, segmentDuration);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import webvtt from 'node-webvtt';\n\nconst webvttInput = `WEBVTT\nKind: captions\nLanguage: en\n\n00:00:00.000 --> 00:00:01.000\nHello world!\n\n00:00:02.500 --> 00:00:04.000 align:start line:0%\nThis is a subtitle example.\n\n00:00:05.000 --> 00:00:06.000\nFoo bar.\n`;\n\nconst segmentDuration = 2; // segments will be 2 seconds long\n\ntry {\n  // 1. Parse the WebVTT input with metadata option\n  const parsed = webvtt.parse(webvttInput, { strict: false, meta: true });\n  console.log('--- Parsed WebVTT (Non-strict with Meta) ---');\n  console.log(JSON.stringify(parsed, null, 2));\n\n  // 2. Compile the parsed object back to WebVTT string\n  // Note: For compilation, the input typically matches the parsed output structure.\n  const compiledInput = {\n    valid: true,\n    meta: parsed.meta,\n    cues: parsed.cues.filter(cue => cue.start < cue.end) // Filter out malformed cues if strict:false was used\n  };\n  const compiled = webvtt.compile(compiledInput);\n  console.log('\\n--- Compiled WebVTT ---');\n  console.log(compiled);\n\n  // 3. Segment the WebVTT for HLS and generate a playlist\n  const segments = webvtt.hls.hlsSegment(webvttInput, segmentDuration);\n  console.log('\\n--- HLS Segments ---');\n  segments.forEach((seg, i) => console.log(`Segment ${i}: ${seg.filename}\\n${seg.content}`));\n\n  const playlist = webvtt.hls.hlsSegmentPlaylist(webvttInput, segmentDuration);\n  console.log('\\n--- HLS Playlist (M3U8) ---');\n  console.log(playlist);\n\n} catch (error) {\n  console.error('An error occurred during WebVTT processing:', error.message);\n}","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}