npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

subcraft

v0.2.0

Published

Zero-dependency isomorphic TypeScript subtitle engine for parsing, retiming, WebVTT/SRT conversion, and Unicode grapheme-cluster quality linting (powered by Intl.Segmenter).

Readme

subcraft

Zero-dependency, isomorphic TypeScript engine for subtitle parsing, frame-rate retiming, and Unicode grapheme-cluster quality linting (powered by Intl.Segmenter).

npm version TypeScript Zero Dependencies Tests License: MIT


Highlights

  • 100% Zero Dependencies: Lightweight and fast. Runs natively in any JavaScript runtime (Browsers, Web Workers, Node.js 20+, and Edge runtimes) without polyfills.
  • Resilient Parsing: Strips UTF-8 BOM (\uFEFF), normalizes mixed line endings (CRLF, CR, LF), and captures syntax errors without crashing or throwing unhandled exceptions.
  • Deterministic Timecode Math: Lossless millisecond arithmetic with integer precision (Math.round), frame-rate drift scaling (retime), and linear audio-sync shifting (shiftTimecodes).
  • Grapheme-Aware Quality Linter: Uses the native ECMAScript Intl.Segmenter API to measure true visual characters rather than UTF-16 code units—eliminating false reading-speed warnings in Indic scripts (Kannada, Hindi) and emoji-heavy text.
  • First-Class TypeScript: Authored in strict TypeScript with comprehensive type declarations (.d.ts and .d.cts) and verified dual ESM/CJS exports via publint.

Table of Contents

  1. Installation
  2. Quick Start
  3. Core Modules
  4. The Indic / Unicode Grapheme Problem
  5. Comparison with Legacy Libraries
  6. API Reference & Types
  7. License

Installation

# Using npm
npm install subcraft

# Using pnpm
pnpm add subcraft

# Using yarn
yarn add subcraft

Quick Start

import { parseSRT, stringifySRT, lintCues, shiftTimecodes } from 'subcraft';

const rawSRT = `1
00:00:01,000 --> 00:00:04,000
Welcome to subcraft!

2
00:00:03,800 --> 00:00:06,000
Overlapping subtitle cue.`;

// 1. Parse SRT text resiliently
const { cues, errors } = parseSRT(rawSRT);
console.log(`Parsed ${cues.length} cues with ${errors.length} syntax errors.`);

// 2. Lint for quality defects (overlaps, reading speed, duration)
const diagnostics = lintCues(cues);
console.log(diagnostics);
// Output flags: Cue 2 overlaps with Cue 1 by 200ms!

// 3. Shift timings by +500ms (to fix audio desync)
const shiftedCues = shiftTimecodes(cues, 500);

// 4. Serialize back to standard SRT string
const updatedSRT = stringifySRT(shiftedCues);

Core Modules

1. SRT Parsing & Resilient Error Collection

Unlike traditional parsers that throw fatal exceptions when encountering corrupted blocks or missing line numbers, parseSRT collects syntax errors alongside successfully parsed cues:

import { parseSRT } from 'subcraft';

const { cues, errors } = parseSRT(dirtySubtitleContent);

if (errors.length > 0) {
  errors.forEach(err => {
    console.warn(`Block error around index ${err.index}: ${err.message}`);
    console.warn(`Raw block: "${err.rawBlock}"`);
  });
}
  • Automatically strips UTF-8 BOM (\uFEFF).
  • Normalizes Windows (\r\n) and Unix (\n) newlines.
  • Handles missing or non-numeric cue IDs by assigning fallback sequential numbers.

2. SRT Serialization

Formats structured SubtitleCue[] objects back into strict standard SubRip (.srt) syntax:

import { stringifySRT, SubtitleCue } from 'subcraft';

const cues: SubtitleCue[] = [
  { id: '1', start: 1000, end: 4000, text: 'Hello world' }
];

const srtString = stringifySRT(cues);

3. WebVTT Parsing, Serialization & Format Conversion

Parse standard WebVTT (.vtt) files with headers, NOTE comments, and placement settings (align:middle line:90%):

import { parseVTT, stringifyVTT, srtToVTT, vttToSRT } from 'subcraft';

// 1. Parse WebVTT
const { cues, errors } = parseVTT(vttText);

// 2. Serialize to WebVTT with custom header
const vttOutput = stringifyVTT(cues, { header: 'English SDH' });

// 3. Instant one-line bidirectional format conversion
const webVTT = srtToVTT(rawSRT);   // Convert SRT to WebVTT
const srtText = vttToSRT(rawVTT);   // Convert WebVTT to SRT

3. Timecode Conversions

Convert between standard timecode strings and integer milliseconds without floating-point drift:

import { timecodeToMs, msToTimecode } from 'subcraft';

// Convert string to milliseconds
timecodeToMs('00:01:23,456'); // Returns: 83456
timecodeToMs('01:23.456');    // WebVTT short form -> Returns: 83456

// Convert milliseconds to formatted timecode
msToTimecode(83456, 'srt');   // "00:01:23,456"
msToTimecode(83456, 'vtt');   // "00:01:23.456"

4. Audio Sync & Frame-Rate Drift Retiming

Linear Shift (shiftTimecodes)

Add or subtract a constant offset across all cues (e.g. delaying subtitles by 500ms). Negative results are safely clamped to 0ms:

import { shiftTimecodes } from 'subcraft';

// Shift all cues forward by 1.5 seconds
const delayed = shiftTimecodes(cues, 1500);

Frame-Rate Scaling (retime)

When videos are converted between frame rates (e.g., 24 fps Film to 25 fps PAL television), timecodes drift multiplicatively over time. retime applies a scaling ratio, strictly rounding each millisecond with Math.round:

import { retime } from 'subcraft';

// 24 fps to 25 fps (PAL speed-up): video runs ~4% faster, timecodes compress
const palCues = retime(filmCues, 24 / 25); // ratio: 0.96

// 25 fps to 24 fps (Slowdown): video runs slower, timecodes stretch
const filmCues = retime(palCues, 25 / 24); // ratio: 1.04167

5. Locale-Aware Quality Linter

Inspects subtitles against broadcaster standards (Netflix / BBC style guides):

import { lintCues, DEFAULT_PROFILES } from 'subcraft';

// Lint using standard Latin rules (default)
const diagnostics = lintCues(cues);

// Lint using Indic (Kannada) profile
const kannadaDiagnostics = lintCues(cues, DEFAULT_PROFILES.indicKannada);

// Custom profile overrides
const customDiagnostics = lintCues(cues, {
  maxCps: 18,
  maxCpl: 40,
  minDurationMs: 1000,
  maxDurationMs: 6000,
  includeSpaces: true,
  locale: 'kn',
});

Rules Enforced:

| Rule | Severity | Description | Default Threshold | | :--- | :---: | :--- | :--- | | out-of-order | error | Cue start timestamp is earlier than previous cue start | Ascending chronological order | | zero-or-negative-duration | error | Cue duration is zero or negative (end <= start) | end > start | | overlap | error | Cue start timestamp precedes the previous cue's end timestamp | start >= prevEnd | | min-duration | warning | Cue flashes too quickly for human cognitive reading | Min 800ms | | max-duration | warning | Cue stays on screen for too long, distracting from video | Max 7,000ms | | max-cpl | warning | Line length exceeds display boundary limits | Max 42 CPL (Latin) / 35 CPL (Indic) | | max-cps | warning | Reading speed exceeds human reading speed capacity | Max 20 CPS (Latin) / 15 CPS (Indic) |


The Indic / Unicode Grapheme Problem

In standard JavaScript, string.length counts UTF-16 code units, not visual characters.

In Indic scripts (such as Kannada, Hindi, Telugu, Tamil), conjunct consonants, viramas, and vowel modifiers (matras) are stored across multiple code units:

// Standard JavaScript string.length:
console.log('ವಿದ್ಯಾರ್ಥಿ'.length); // 10 code units!
console.log('ಬೆಂಗಳೂರು'.length);   // 8 code units!

If a subtitle validation tool calculates Characters Per Second (CPS) using string.length, it counts 10 characters for a 5-character word like "ವಿದ್ಯಾರ್ಥಿ". This doubles the reading speed calculation, triggering false-positive "Reading speed too fast!" warnings.

How subcraft Solves This:

subcraft uses the native ECMAScript standard Intl.Segmenter (UAX #29 Unicode grapheme clustering):

import { countGraphemes } from 'subcraft';

countGraphemes('ವಿದ್ಯಾರ್ಥಿ', 'kn'); // Accurately returns: 5 graphemes
countGraphemes('ಬೆಂಗಳೂರು', 'kn');   // Accurately returns: 4 graphemes

subcraft also automatically strips HTML markup (<i>, <b>, <font...>) before counting characters, ensuring that visual styling does not artificially penalize reading speed metrics.


Comparison with Legacy Libraries

| Feature | subcraft | subtitle.js (legacy) | srt-parser-2 | | :--- | :---: | :---: | :---: | | Dependencies | 0 (Zero) | Depends on Node.js stream | 0 | | Browser & Web Worker Native | ✅ 100% Native | ❌ Requires Node polyfills | ✅ | | Built-in Quality Linter | ✅ Comprehensive | ❌ None | ❌ None | | Unicode Grapheme CPS / CPL | ✅ Intl.Segmenter | ❌ None | ❌ None | | Overlap & Chronology Checks | ✅ Yes | ❌ None | ❌ None | | Frame-rate Drift Retiming | ✅ Yes (retime) | ❌ Only linear shift | ❌ None | | Resilient Error Collection | ✅ Returns { cues, errors } | ❌ Throws errors | ❌ Incomplete | | Type Definitions | ✅ Strict ESM + CJS .d.ts | ⚠️ Partial | ⚠️ Basic |


API Reference & Types

Data Models

export interface SubtitleCue {
  id: string;      // Unique identifier (e.g., "1", "2")
  start: number;   // Integer start time in milliseconds
  end: number;     // Integer end time in milliseconds
  text: string;    // Raw subtitle text payload (may contain newlines)
}

export interface ParseResult {
  cues: SubtitleCue[];
  errors: ParseError[];
}

export interface ParseError {
  index?: number;
  rawBlock: string;
  message: string;
}

export interface LintDiagnostic {
  cueId: string;
  rule: 
    | 'out-of-order'
    | 'zero-or-negative-duration'
    | 'overlap'
    | 'min-duration'
    | 'max-duration'
    | 'max-cpl'
    | 'max-cps';
  severity: 'error' | 'warning';
  message: string;
  expected?: number | string;
  actual?: number | string;
}

License

MIT © 2026 Ranjith Gowda