subcraft 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Core normalized representation of a single subtitle cue.
3
+ * Timestamps are strictly stored in integer milliseconds.
4
+ */
5
+ interface SubtitleCue {
6
+ /** Unique sequential identifier (e.g. "1", "2") */
7
+ id: string;
8
+ /** Start time in integer milliseconds */
9
+ start: number;
10
+ /** End time in integer milliseconds */
11
+ end: number;
12
+ /** Raw subtitle text content (may contain newlines) */
13
+ text: string;
14
+ /** Optional WebVTT cue placement/styling settings (e.g., "align:middle line:90%") */
15
+ settings?: string;
16
+ }
17
+ /**
18
+ * Calculates the duration of a cue in milliseconds.
19
+ * Derived helper function to keep the core data model minimal and immutable.
20
+ */
21
+ declare const getCueDuration: (cue: SubtitleCue) => number;
22
+ /** Supported subtitle file formats */
23
+ type TimecodeFormat = 'srt' | 'vtt';
24
+ /**
25
+ * Detailed error reported when a cue block fails parsing syntax rules.
26
+ */
27
+ interface ParseError {
28
+ index?: number;
29
+ rawBlock: string;
30
+ message: string;
31
+ }
32
+ /**
33
+ * Resilient parser result returning successfully parsed cues
34
+ * alongside any syntax errors encountered without throwing.
35
+ */
36
+ interface ParseResult {
37
+ cues: SubtitleCue[];
38
+ errors: ParseError[];
39
+ }
40
+ /** Severity levels for lint diagnostics */
41
+ type LintSeverity = 'error' | 'warning';
42
+ /** Quality rules enforced by the linter */
43
+ type LintRule = 'out-of-order' | 'overlap' | 'zero-or-negative-duration' | 'min-duration' | 'max-duration' | 'max-cpl' | 'max-cps';
44
+ /**
45
+ * Diagnostic report produced when a cue violates a quality rule.
46
+ */
47
+ interface LintDiagnostic {
48
+ cueId: string;
49
+ rule: LintRule;
50
+ severity: LintSeverity;
51
+ message: string;
52
+ expected?: number | string;
53
+ actual?: number | string;
54
+ }
55
+ /**
56
+ * Configuration options for locale-aware subtitle validation.
57
+ */
58
+ interface LintProfile {
59
+ name: string;
60
+ locale?: string;
61
+ maxCps: number;
62
+ maxCpl: number;
63
+ minDurationMs: number;
64
+ maxDurationMs: number;
65
+ includeSpaces?: boolean;
66
+ }
67
+
68
+ /**
69
+ * Converts a formatted timecode string into integer milliseconds.
70
+ *
71
+ * Supported formats:
72
+ * - "01:23:45,678" (SRT standard)
73
+ * - "01:23:45.678" (WebVTT standard)
74
+ * - "23:45.678" (WebVTT short form without hours)
75
+ *
76
+ * @throws {Error} if timecode format is invalid
77
+ */
78
+ declare function timecodeToMs(timecode: string): number;
79
+ /**
80
+ * Converts integer milliseconds into a standard formatted timecode string.
81
+ *
82
+ * @param ms - Non-negative integer milliseconds (negative values clamped to 0)
83
+ * @param format - 'srt' (uses comma delimiter) or 'vtt' (uses dot delimiter)
84
+ *
85
+ * @example
86
+ * msToTimecode(83456, 'srt') // "00:01:23,456"
87
+ * msToTimecode(83456, 'vtt') // "00:01:23.456"
88
+ */
89
+ declare function msToTimecode(ms: number, format?: TimecodeFormat): string;
90
+ /**
91
+ * Linearly shifts timecodes by a constant millisecond offset.
92
+ * Used to correct audio-sync delay or advance.
93
+ * Clamps negative results to 0.
94
+ *
95
+ * @param cues - Array of SubtitleCue objects
96
+ * @param offsetMs - Offset in milliseconds (positive advances, negative delays)
97
+ * @returns A new immutable array of shifted cues
98
+ */
99
+ declare function shiftTimecodes(cues: readonly SubtitleCue[], offsetMs: number): SubtitleCue[];
100
+ /**
101
+ * Scales all cue timecodes by a multiplicative ratio.
102
+ * Used for frame-rate drift compensation between video standards.
103
+ * All results are strictly rounded to integer milliseconds with Math.round.
104
+ *
105
+ * Direction definition:
106
+ * targetTimestamp = Math.round(sourceTimestamp * ratio)
107
+ *
108
+ * @example
109
+ * // 24 fps to 25 fps (PAL speed-up): video is ~4% faster, timecodes compress
110
+ * const palCues = retime(filmCues, 24 / 25); // ratio = 0.96
111
+ *
112
+ * @example
113
+ * // 25 fps to 24 fps (Slow-down): video is ~4% slower, timecodes stretch
114
+ * const filmCues = retime(palCues, 25 / 24); // ratio = 1.04167
115
+ *
116
+ * @param cues - Array of SubtitleCue objects
117
+ * @param ratio - Positive scaling multiplier (e.g., 24/25 or 25/24)
118
+ * @returns A new immutable array of retimed cues
119
+ */
120
+ declare function retime(cues: readonly SubtitleCue[], ratio: number): SubtitleCue[];
121
+
122
+ /**
123
+ * Parses raw SRT subtitle text into structured SubtitleCue objects.
124
+ * Resilient parser that collects syntax errors without throwing unhandled exceptions.
125
+ *
126
+ * Handles:
127
+ * - UTF-8 Byte Order Marks (BOM `\uFEFF`)
128
+ * - Windows (CRLF), Unix (LF), and legacy Mac (CR) line endings
129
+ * - Optional or missing cue numeric index headers
130
+ * - Multi-line subtitle texts
131
+ * - Trailing and irregular blank lines
132
+ *
133
+ * @param rawText - Raw SRT file content as a string
134
+ * @returns {ParseResult} Object containing successfully parsed cues and any block errors
135
+ */
136
+ declare function parseSRT(rawText: string): ParseResult;
137
+
138
+ /**
139
+ * Serializes an array of SubtitleCue objects into standard SRT file format.
140
+ *
141
+ * Output format:
142
+ * 1
143
+ * 00:00:20,000 --> 00:00:24,000
144
+ * Subtitle text
145
+ *
146
+ * @param cues - Array of SubtitleCue objects to format
147
+ * @returns Standard formatted SRT string
148
+ */
149
+ declare function stringifySRT(cues: readonly SubtitleCue[]): string;
150
+
151
+ interface StringifyVTTOptions {
152
+ /** Optional custom header line after WEBVTT (e.g. "WEBVTT - Commentary Track") */
153
+ header?: string;
154
+ /** Whether to output cue numeric ID headers (default: false for standard clean WebVTT) */
155
+ includeIds?: boolean;
156
+ }
157
+ /**
158
+ * Parses raw WebVTT (.vtt) subtitle text into structured SubtitleCue objects.
159
+ *
160
+ * Handles:
161
+ * - UTF-8 Byte Order Marks (BOM \uFEFF)
162
+ * - WEBVTT header and metadata blocks
163
+ * - NOTE comment blocks (skipped automatically)
164
+ * - Dot-separated milliseconds (00:01:20.500) and comma-separated fallback
165
+ * - Short timecodes without hours (MM:SS.mmm)
166
+ * - Cue placement/styling settings (e.g. align:middle line:90%)
167
+ * - Optional cue identifiers
168
+ *
169
+ * @param rawText - Raw WebVTT file content as a string
170
+ * @returns {ParseResult} Successfully parsed cues and any block errors
171
+ */
172
+ declare function parseVTT(rawText: string): ParseResult;
173
+ /**
174
+ * Serializes an array of SubtitleCue objects into standard WebVTT (.vtt) file format.
175
+ *
176
+ * @param cues - Array of SubtitleCue objects to format
177
+ * @param options - Formatting options (header title, inclusion of cue IDs)
178
+ * @returns Formatted WebVTT string
179
+ */
180
+ declare function stringifyVTT(cues: readonly SubtitleCue[], options?: StringifyVTTOptions): string;
181
+ /**
182
+ * Converts raw SRT subtitle text directly into a valid WebVTT (.vtt) string.
183
+ *
184
+ * @param srtText - Raw SubRip (.srt) text content
185
+ * @param options - WebVTT formatting options
186
+ * @returns Formatted WebVTT string
187
+ */
188
+ declare function srtToVTT(srtText: string, options?: StringifyVTTOptions): string;
189
+ /**
190
+ * Converts raw WebVTT subtitle text directly into a standard SRT (.srt) string.
191
+ *
192
+ * @param vttText - Raw WebVTT (.vtt) text content
193
+ * @returns Standard SubRip (.srt) string
194
+ */
195
+ declare function vttToSRT(vttText: string): string;
196
+
197
+ /** Default quality profiles for subtitle verification */
198
+ declare const DEFAULT_PROFILES: Record<string, LintProfile>;
199
+ /**
200
+ * Strips HTML formatting tags from subtitle text before measuring character limits.
201
+ * Handles <i>, <b>, <u>, <font color="...">, etc.
202
+ */
203
+ declare function stripFormattingTags(text: string): string;
204
+ /**
205
+ * Counts user-perceived grapheme clusters using native ECMAScript Intl.Segmenter.
206
+ * Accurately measures multi-byte conjuncts and vowel modifiers in Indic scripts (Kannada, Hindi),
207
+ * as well as emojis and complex Unicode characters without UTF-16 code unit inflation.
208
+ */
209
+ declare function countGraphemes(text: string, locale?: string): number;
210
+ /**
211
+ * Inspects an array of SubtitleCues against timing, structural, and reading speed rules.
212
+ *
213
+ * Rules verified:
214
+ * - out-of-order: Cues not in ascending chronological order
215
+ * - zero-or-negative-duration: Cues where end <= start
216
+ * - overlap: Cues overlapping chronologically with preceding cues
217
+ * - min-duration: Cues flashing too fast for comprehension (< 800ms)
218
+ * - max-duration: Cues remaining on screen too long (> 7000ms)
219
+ * - max-cpl: Any line exceeding characters-per-line limit
220
+ * - max-cps: Reading speed exceeding characters-per-second limit
221
+ *
222
+ * @param cues - Array of SubtitleCue objects to inspect
223
+ * @param customProfile - Optional custom thresholds or language profile
224
+ * @returns Array of LintDiagnostic reports
225
+ */
226
+ declare function lintCues(cues: readonly SubtitleCue[], customProfile?: Partial<LintProfile>): LintDiagnostic[];
227
+
228
+ export { DEFAULT_PROFILES, type LintDiagnostic, type LintProfile, type LintRule, type LintSeverity, type ParseError, type ParseResult, type StringifyVTTOptions, type SubtitleCue, type TimecodeFormat, countGraphemes, getCueDuration, lintCues, msToTimecode, parseSRT, parseVTT, retime, shiftTimecodes, srtToVTT, stringifySRT, stringifyVTT, stripFormattingTags, timecodeToMs, vttToSRT };
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Core normalized representation of a single subtitle cue.
3
+ * Timestamps are strictly stored in integer milliseconds.
4
+ */
5
+ interface SubtitleCue {
6
+ /** Unique sequential identifier (e.g. "1", "2") */
7
+ id: string;
8
+ /** Start time in integer milliseconds */
9
+ start: number;
10
+ /** End time in integer milliseconds */
11
+ end: number;
12
+ /** Raw subtitle text content (may contain newlines) */
13
+ text: string;
14
+ /** Optional WebVTT cue placement/styling settings (e.g., "align:middle line:90%") */
15
+ settings?: string;
16
+ }
17
+ /**
18
+ * Calculates the duration of a cue in milliseconds.
19
+ * Derived helper function to keep the core data model minimal and immutable.
20
+ */
21
+ declare const getCueDuration: (cue: SubtitleCue) => number;
22
+ /** Supported subtitle file formats */
23
+ type TimecodeFormat = 'srt' | 'vtt';
24
+ /**
25
+ * Detailed error reported when a cue block fails parsing syntax rules.
26
+ */
27
+ interface ParseError {
28
+ index?: number;
29
+ rawBlock: string;
30
+ message: string;
31
+ }
32
+ /**
33
+ * Resilient parser result returning successfully parsed cues
34
+ * alongside any syntax errors encountered without throwing.
35
+ */
36
+ interface ParseResult {
37
+ cues: SubtitleCue[];
38
+ errors: ParseError[];
39
+ }
40
+ /** Severity levels for lint diagnostics */
41
+ type LintSeverity = 'error' | 'warning';
42
+ /** Quality rules enforced by the linter */
43
+ type LintRule = 'out-of-order' | 'overlap' | 'zero-or-negative-duration' | 'min-duration' | 'max-duration' | 'max-cpl' | 'max-cps';
44
+ /**
45
+ * Diagnostic report produced when a cue violates a quality rule.
46
+ */
47
+ interface LintDiagnostic {
48
+ cueId: string;
49
+ rule: LintRule;
50
+ severity: LintSeverity;
51
+ message: string;
52
+ expected?: number | string;
53
+ actual?: number | string;
54
+ }
55
+ /**
56
+ * Configuration options for locale-aware subtitle validation.
57
+ */
58
+ interface LintProfile {
59
+ name: string;
60
+ locale?: string;
61
+ maxCps: number;
62
+ maxCpl: number;
63
+ minDurationMs: number;
64
+ maxDurationMs: number;
65
+ includeSpaces?: boolean;
66
+ }
67
+
68
+ /**
69
+ * Converts a formatted timecode string into integer milliseconds.
70
+ *
71
+ * Supported formats:
72
+ * - "01:23:45,678" (SRT standard)
73
+ * - "01:23:45.678" (WebVTT standard)
74
+ * - "23:45.678" (WebVTT short form without hours)
75
+ *
76
+ * @throws {Error} if timecode format is invalid
77
+ */
78
+ declare function timecodeToMs(timecode: string): number;
79
+ /**
80
+ * Converts integer milliseconds into a standard formatted timecode string.
81
+ *
82
+ * @param ms - Non-negative integer milliseconds (negative values clamped to 0)
83
+ * @param format - 'srt' (uses comma delimiter) or 'vtt' (uses dot delimiter)
84
+ *
85
+ * @example
86
+ * msToTimecode(83456, 'srt') // "00:01:23,456"
87
+ * msToTimecode(83456, 'vtt') // "00:01:23.456"
88
+ */
89
+ declare function msToTimecode(ms: number, format?: TimecodeFormat): string;
90
+ /**
91
+ * Linearly shifts timecodes by a constant millisecond offset.
92
+ * Used to correct audio-sync delay or advance.
93
+ * Clamps negative results to 0.
94
+ *
95
+ * @param cues - Array of SubtitleCue objects
96
+ * @param offsetMs - Offset in milliseconds (positive advances, negative delays)
97
+ * @returns A new immutable array of shifted cues
98
+ */
99
+ declare function shiftTimecodes(cues: readonly SubtitleCue[], offsetMs: number): SubtitleCue[];
100
+ /**
101
+ * Scales all cue timecodes by a multiplicative ratio.
102
+ * Used for frame-rate drift compensation between video standards.
103
+ * All results are strictly rounded to integer milliseconds with Math.round.
104
+ *
105
+ * Direction definition:
106
+ * targetTimestamp = Math.round(sourceTimestamp * ratio)
107
+ *
108
+ * @example
109
+ * // 24 fps to 25 fps (PAL speed-up): video is ~4% faster, timecodes compress
110
+ * const palCues = retime(filmCues, 24 / 25); // ratio = 0.96
111
+ *
112
+ * @example
113
+ * // 25 fps to 24 fps (Slow-down): video is ~4% slower, timecodes stretch
114
+ * const filmCues = retime(palCues, 25 / 24); // ratio = 1.04167
115
+ *
116
+ * @param cues - Array of SubtitleCue objects
117
+ * @param ratio - Positive scaling multiplier (e.g., 24/25 or 25/24)
118
+ * @returns A new immutable array of retimed cues
119
+ */
120
+ declare function retime(cues: readonly SubtitleCue[], ratio: number): SubtitleCue[];
121
+
122
+ /**
123
+ * Parses raw SRT subtitle text into structured SubtitleCue objects.
124
+ * Resilient parser that collects syntax errors without throwing unhandled exceptions.
125
+ *
126
+ * Handles:
127
+ * - UTF-8 Byte Order Marks (BOM `\uFEFF`)
128
+ * - Windows (CRLF), Unix (LF), and legacy Mac (CR) line endings
129
+ * - Optional or missing cue numeric index headers
130
+ * - Multi-line subtitle texts
131
+ * - Trailing and irregular blank lines
132
+ *
133
+ * @param rawText - Raw SRT file content as a string
134
+ * @returns {ParseResult} Object containing successfully parsed cues and any block errors
135
+ */
136
+ declare function parseSRT(rawText: string): ParseResult;
137
+
138
+ /**
139
+ * Serializes an array of SubtitleCue objects into standard SRT file format.
140
+ *
141
+ * Output format:
142
+ * 1
143
+ * 00:00:20,000 --> 00:00:24,000
144
+ * Subtitle text
145
+ *
146
+ * @param cues - Array of SubtitleCue objects to format
147
+ * @returns Standard formatted SRT string
148
+ */
149
+ declare function stringifySRT(cues: readonly SubtitleCue[]): string;
150
+
151
+ interface StringifyVTTOptions {
152
+ /** Optional custom header line after WEBVTT (e.g. "WEBVTT - Commentary Track") */
153
+ header?: string;
154
+ /** Whether to output cue numeric ID headers (default: false for standard clean WebVTT) */
155
+ includeIds?: boolean;
156
+ }
157
+ /**
158
+ * Parses raw WebVTT (.vtt) subtitle text into structured SubtitleCue objects.
159
+ *
160
+ * Handles:
161
+ * - UTF-8 Byte Order Marks (BOM \uFEFF)
162
+ * - WEBVTT header and metadata blocks
163
+ * - NOTE comment blocks (skipped automatically)
164
+ * - Dot-separated milliseconds (00:01:20.500) and comma-separated fallback
165
+ * - Short timecodes without hours (MM:SS.mmm)
166
+ * - Cue placement/styling settings (e.g. align:middle line:90%)
167
+ * - Optional cue identifiers
168
+ *
169
+ * @param rawText - Raw WebVTT file content as a string
170
+ * @returns {ParseResult} Successfully parsed cues and any block errors
171
+ */
172
+ declare function parseVTT(rawText: string): ParseResult;
173
+ /**
174
+ * Serializes an array of SubtitleCue objects into standard WebVTT (.vtt) file format.
175
+ *
176
+ * @param cues - Array of SubtitleCue objects to format
177
+ * @param options - Formatting options (header title, inclusion of cue IDs)
178
+ * @returns Formatted WebVTT string
179
+ */
180
+ declare function stringifyVTT(cues: readonly SubtitleCue[], options?: StringifyVTTOptions): string;
181
+ /**
182
+ * Converts raw SRT subtitle text directly into a valid WebVTT (.vtt) string.
183
+ *
184
+ * @param srtText - Raw SubRip (.srt) text content
185
+ * @param options - WebVTT formatting options
186
+ * @returns Formatted WebVTT string
187
+ */
188
+ declare function srtToVTT(srtText: string, options?: StringifyVTTOptions): string;
189
+ /**
190
+ * Converts raw WebVTT subtitle text directly into a standard SRT (.srt) string.
191
+ *
192
+ * @param vttText - Raw WebVTT (.vtt) text content
193
+ * @returns Standard SubRip (.srt) string
194
+ */
195
+ declare function vttToSRT(vttText: string): string;
196
+
197
+ /** Default quality profiles for subtitle verification */
198
+ declare const DEFAULT_PROFILES: Record<string, LintProfile>;
199
+ /**
200
+ * Strips HTML formatting tags from subtitle text before measuring character limits.
201
+ * Handles <i>, <b>, <u>, <font color="...">, etc.
202
+ */
203
+ declare function stripFormattingTags(text: string): string;
204
+ /**
205
+ * Counts user-perceived grapheme clusters using native ECMAScript Intl.Segmenter.
206
+ * Accurately measures multi-byte conjuncts and vowel modifiers in Indic scripts (Kannada, Hindi),
207
+ * as well as emojis and complex Unicode characters without UTF-16 code unit inflation.
208
+ */
209
+ declare function countGraphemes(text: string, locale?: string): number;
210
+ /**
211
+ * Inspects an array of SubtitleCues against timing, structural, and reading speed rules.
212
+ *
213
+ * Rules verified:
214
+ * - out-of-order: Cues not in ascending chronological order
215
+ * - zero-or-negative-duration: Cues where end <= start
216
+ * - overlap: Cues overlapping chronologically with preceding cues
217
+ * - min-duration: Cues flashing too fast for comprehension (< 800ms)
218
+ * - max-duration: Cues remaining on screen too long (> 7000ms)
219
+ * - max-cpl: Any line exceeding characters-per-line limit
220
+ * - max-cps: Reading speed exceeding characters-per-second limit
221
+ *
222
+ * @param cues - Array of SubtitleCue objects to inspect
223
+ * @param customProfile - Optional custom thresholds or language profile
224
+ * @returns Array of LintDiagnostic reports
225
+ */
226
+ declare function lintCues(cues: readonly SubtitleCue[], customProfile?: Partial<LintProfile>): LintDiagnostic[];
227
+
228
+ export { DEFAULT_PROFILES, type LintDiagnostic, type LintProfile, type LintRule, type LintSeverity, type ParseError, type ParseResult, type StringifyVTTOptions, type SubtitleCue, type TimecodeFormat, countGraphemes, getCueDuration, lintCues, msToTimecode, parseSRT, parseVTT, retime, shiftTimecodes, srtToVTT, stringifySRT, stringifyVTT, stripFormattingTags, timecodeToMs, vttToSRT };