Skip to content

About

Basic SRT, WebVTT, TTML and cinema subtitle XML utilities with explicit timing conversion.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

FilmProcessor Subtitle Tools

Basic SRT, WebVTT, TTML and cinema subtitle XML readers, explicit timing conversion, and a plain-text SMPTE SubtitleReel XML writer. JavaScript ES modules with no runtime dependencies. Apache-2.0 licensed.

Use

Clone this repository and import src/index.js. This package is not published to npm. SRT/WebVTT, timing conversion and XML writing work in browsers and Node.js 20+. XML reading uses the browser's native DOMParser; in Node, supply an XML DOM parser explicitly.

import { parseSRT, secondsToFrames, generateDCSubtitleXML } from './src/index.js';

const cues = parseSRT('1\n00:00:01,000 --> 00:00:02,000\nHello');
console.log(cues); // [{ start: 1, end: 2, text: 'Hello' }]
console.log(secondsToFrames(0.01, 25)); // 0.25: no automatic rounding

For XML in Node, install @xmldom/xmldom in your application and pass its constructor. It is a development dependency here for tests, not bundled code:

import { DOMParser } from '@xmldom/xmldom';
import { parseTTML } from './src/index.js';
const cues = parseTTML(
  '<tt xmlns="http://www.w3.org/ns/ttml"><body><div><p begin="1s" end="2s">Hello</p></div></body></tt>',
  { DOMParser },
);

API

  • parseSRT(text): cue times in seconds and original cue text. Invalid cue blocks throw. Blank input returns an empty array.
  • parseVTT(text): requires a WEBVTT header. Supports optional cue IDs, omitted hours and cue settings; NOTE, STYLE and REGION blocks are skipped. Settings, styling and regions are not returned or rendered.
  • parseTTML(text, { frameRate?, DOMParser? }): flat paragraph cues with explicit begin and either end or dur. Supports clock/offset, frame, subframe and tick expressions; declared frame rates and multipliers take precedence over the caller's fallback. Spans flatten to text and br becomes a newline. Frame-based times without a declared or supplied rate throw.
  • parseDCSubtitleXML(text, { frameRate?, DOMParser? }): Interop DCSubtitle uses 250 ticks/second. SMPTE SubtitleReel uses TimeCodeRate, then EditRate, then an explicit fallback. Returns plain text; styling is not retained.
  • parseSubtitleContent(text, filename, options): dispatches .srt, .vtt, .ttml, .dfxp and .xml. XML dispatch examines the root element.
  • parseTTMLTimeExpression(value, { frameRate?, subFrameRate?, tickRate? }): returns seconds or NaN for unrecognised syntax; missing required rates throw.
  • resolveTTMLTimingParameters(rootElement, fallbackFrameRate?): extracts supported TTML timing parameters from a DOM element.
  • parseTimestampToSeconds(value, frameRate?): clock parsing convenience; frame rate is never guessed.
  • secondsToFrames(seconds, frameRate) and framesToSeconds(frames, frameRate): arithmetic conversion with no quantization, epsilon or display rule. Results use JavaScript floating-point numbers.
  • formatFrameTimecode(frame, timeCodeRate): non-drop HH:MM:SS:FF from a whole frame index and positive integer time-code rate.
  • generateDCSubtitleXML(options): basic plain-text SMPTE SubtitleReel XML. See the explicit inputs below and examples/convert.js.
const xml = generateDCSubtitleXML({
  cues: [{ startFrame: 24, endFrame: 48, text: 'Hello & welcome' }],
  timeCodeRate: 24,
  subtitleId: 'urn:uuid:00000000-0000-4000-8000-000000000001',
  title: 'Example',
  language: 'en',
  issueDate: '2026-01-01T00:00:00Z',
  layout: { horizontalAlign: 'center', verticalAlign: 'bottom', verticalPosition: 10 },
});

All writer values above are caller-supplied; the example is not a recommended presentation preset. The writer uses a single reel, an integer edit rate equal to TimeCodeRate, and zero StartTime. It escapes literal text, does not interpret SRT/WebVTT markup, and refuses fractional frame indices, reversed/empty cues and invalid XML characters. Choose and apply your own rounding before calling it. It does not generate Interop XML, embed fonts, wrap MXF or author a DCP.

Supported subset and boundaries

This is a text/timing utility, not a complete subtitle renderer, TTML processor, XML Schema validator or cinema compliance validator. Readers discard styling, positioning and unsupported metadata; do not use them for lossless conversion. SRT and WebVTT text can contain markup: render it as text, never raw HTML. XML readers return { start, end, text, textFormat: 'plain' }.

TTML reading supports media timeBase and parallel, untimed containers only. Timed ancestors/spans, sequential containers, clock/SMPTE timeBase, drop-frame modes, xml:space preserve and paragraphs with both end and dur are rejected. Cinema reading rejects nonzero StartTime. Graphics subtitles are unsupported. Frame/tick fallback rates are explicit; standard subframe/tick defaults are used where applicable. This is not a general SMPTE drop-frame timecode library.

XML parsing rejects DTDs/custom entities, input over 8 Mi characters and nesting over 256 elements. An injected DOMParser must report malformed XML; the tests use xmldom with recovery disabled. The library performs no network or file I/O.

Product policies, QC severities, cue repair, frame-selection tolerances, preview style mappings, font presets, spatial positioning and application workflows are outside this package.

Tests

npm ci --ignore-scripts
npm test
node examples/convert.js

Fixtures are synthetic. Tests cover timing units, fractional-rate arithmetic, Unicode and literal text, XML rejection, unsupported timing, and a writer/reader round trip. See LICENSE and NOTICE for attribution.

Licence history

Version 0.2.0 adopts Apache License 2.0 for this project. Earlier published versions through 0.1.0 retain their MIT licence. Third-party dependencies retain their own licences. See LICENSE and NOTICE.

About

Basic SRT, WebVTT, TTML and cinema subtitle XML utilities with explicit timing conversion.

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages