mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-08-24 12:15:27 -07:00
Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7358507b1
|
||
|
|
b029cc73a1
|
||
|
|
60432ca2f3
|
||
|
|
9044340676
|
||
|
|
6d1a1b841a
|
||
|
|
0ac5db1c92
|
||
|
|
4635bfb264
|
||
|
|
1717d2d3f2
|
||
|
|
9f08adbfb9
|
||
|
|
6f52008e5d
|
||
|
|
c4284d1dd4
|
||
|
|
da2a212434
|
||
|
|
faab084588
|
||
|
|
c87dcd6239
|
@@ -1,5 +1,5 @@
|
||||
type: fixed
|
||||
area: subtitles
|
||||
|
||||
- Typeset ASS karaoke and animated signs no longer flood the primary overlay, subtitle sidebar, immersion history, or sentence mining with repeated glyph fragments or full-line color phases. Matching timed comments and full-line boundary events recover the complete authored line without merging ordinary repeated dialogue or separately positioned signs, and dialogue spoken while a song's animation is on screen is kept intact instead of being replaced by the lyric. Entrance and exit frames that run past the authored line timing still resolve to the clean line during lyric transitions, and dialogue spoken while a song's animation is on screen enters immersion and subtitle history without the fragment lines beside it.
|
||||
- Typeset ASS karaoke and animated signs no longer flood the primary overlay, subtitle sidebar, immersion history, or sentence mining with repeated glyph fragments or full-line color phases. Matching timed comments and full-line boundary events recover the complete authored line without merging ordinary repeated dialogue or separately positioned signs, and dialogue spoken while a song's animation is on screen is kept intact instead of being replaced by the lyric. Entrance and exit frames that run past the authored line timing still resolve to the clean line during lyric transitions, and dialogue spoken while a song's animation is on screen enters immersion and subtitle history without the fragment lines beside it. Dense visual grids (sign walls, countdown frames, scattered glyph typesetting) stay out of the published text, while multi-row CC-style dialogue blocks and wrapped lyric rows are still published. Decorative letters that lyric effects render in symbol fonts over the syllables are dropped with the animation instead of corrupting the reconstructed line or leaking as stray cues. Karaoke highlight sweeps that repaint one syllable at a time over an already-visible lyric are suppressed instead of surfacing as rolling partial copies or lone flickering syllables beside the line, and drop-shadow glyph copies offset a few pixels from their base no longer double every syllable in the reconstructed lyric. Positioned word gaps are also recovered on lines where a single fragment carries a literal space, and between wide syllable chunks whose word gap is hidden by their own width, so reconstructed translations keep their spacing instead of running words together.
|
||||
- The secondary subtitle overlay drops layered duplicate lines from animated tracks, so a short stack of repeated words collapses to its distinct lines even when the full karaoke heuristic does not apply.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
type: fixed
|
||||
area: subtitles
|
||||
|
||||
- Prevented embedded subtitle parsing from starving network playback: mounted SMB/NFS media now uses deduplicated mpv live text, while duplicate extraction requests for local media share one ffmpeg process.
|
||||
- Embedded subtitle tracks on network-mounted (SMB/NFS) media are extracted and parsed again, restoring full karaoke reconstruction, sidebar cues, and mining for releases that ship subtitles only inside the container. Extraction reads the whole file once per episode (roughly 10 seconds per GB on gigabit), its timeout now accommodates large Bluray remuxes, and duplicate extraction requests share one ffmpeg process. Only true remote URLs keep the live-text-only path.
|
||||
- Live subtitle text from per-glyph typeset karaoke no longer shows a wall of scattered letters in the overlays while extraction is still running or when no parsed cues exist (remote URLs, unreadable sources); the glyph wall and its typed-syllable fragments are suppressed while concurrent dialogue lines remain.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
type: fixed
|
||||
area: overlay
|
||||
|
||||
- Secondary subtitles now parse the selected ASS/SRT/VTT source with the primary subtitle deduplication pipeline, preventing layered animation text from appearing several times in the overlay, mined cards, and statistics. Fragmented ASS karaoke keeps spaces authored at event boundaries instead of joining every word together. Long ASS lines repeated as dialogue and positioned signs are also collapsed when they differ only in whitespace or terminal punctuation. Dense multi-row sign layouts no longer become one concatenated secondary line. Live mpv text remains the fallback for unreadable tracks and applies full-line duplicate filtering before display.
|
||||
- Secondary subtitles now parse the selected ASS/SRT/VTT source with the primary subtitle deduplication pipeline, preventing layered animation text from appearing several times in the overlay, mined cards, and statistics. Fragmented ASS karaoke keeps spaces authored at event boundaries and recovers Latin word spaces encoded only by positioned fragment gaps, including word gaps measured across wide glyphs that width normalization alone reads as ordinary letter advances. Progressive karaoke highlights, offset shadow copies, overlapping decorative glyphs, and sign textures remain suppressed, including clipped repeated-glyph mask strips without font overrides and texture payloads that switch actor or font and use nearly transparent random text. Canonical lyrics now advance when their generated entrance begins, so word-by-word opening effects appear as one sentence instead of stacked rows during the lead-in. Wrapped lyrics also remain intact when a timed token repeats at another horizontal position. Long ASS lines repeated as dialogue and positioned signs are also collapsed when they differ only in whitespace or terminal punctuation. Dense multi-row sign layouts no longer become concatenated primary or secondary lines. Live mpv text remains the fallback for unreadable tracks and applies full-line duplicate filtering before display. A failed source refresh also clears ASS-only cleanup so fallback text from other formats stays intact.
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
type: fixed
|
||||
area: stats
|
||||
|
||||
- Immersion statistics storage now applies its SQLite busy timeout before WAL setup, avoiding transient database-lock failures when worker connections overlap.
|
||||
@@ -87,7 +87,9 @@ interface SubtitleCue {
|
||||
|
||||
ASS scripts can also redraw one complete lyric for two or more long color/highlight phases. Those flush-timed phases collapse separately from short animation frames when they share text, style, actor, and layer and carry direct animation evidence, such as temporal tags or changing non-spatial overrides. Spatial command changes do not prove a phase, so separately positioned signs remain distinct.
|
||||
|
||||
**Canonical animation recovery.** Some ASS producers keep the readable lyric or sign as a timed `Comment:` and generate hundreds of `Dialogue:` frames containing repeated glyphs or changing clip regions. Others retain the complete line as brief `Dialogue:` events around the generated fragments. A complete event is promoted only when nearby dialogue from the same style and actor forms a proven animation cluster and reconstructs its entire text in source order. The generated frames are then replaced by one cue marked `source: 'canonical-ass'`. This source marker lets the live primary-subtitle path prefer the clean authored text and timing for display, sidebar history, immersion recording, and mining, while unmatched editor notes and alternative translations remain ignored.
|
||||
**Canonical animation recovery.** Some ASS producers keep the readable lyric or sign as a timed `Comment:` and generate hundreds of `Dialogue:` frames containing repeated glyphs or changing clip regions. Others retain the complete line as brief `Dialogue:` events around the generated fragments. A complete event is promoted only when nearby dialogue from the same style and actor forms a proven animation cluster and reconstructs its entire text in source order. The generated frames are then replaced by one cue marked `source: 'canonical-ass'`. This source marker lets the live primary-subtitle path prefer the clean authored text and timing for display, sidebar history, immersion recording, and mining, while unmatched editor notes and alternative translations remain ignored. Secondary selection advances to an entering canonical cue at its generated animation start when the preceding authored cue ends before the new authored span. Unrelated simultaneous cues that continue through the new span remain visible.
|
||||
|
||||
**Font texture cleanup.** A clipped repeated-glyph run or frequent changes to secondary alpha marks a texture seed. Clipped runs do not need a font override because some signs build their masks from ordinary `l` glyphs. The parser removes short clipped pieces that share a no-font seed's style and timing, or pieces that share a font seed's style, timing, and font even when the actor changes. It also removes positioned text layers with at least `E0` global alpha when they overlap a seed in the same style. Opaque authored sign text stays publishable when the texture switches fonts or actors around it.
|
||||
|
||||
#### Prefetch Service Lifecycle
|
||||
|
||||
|
||||
@@ -98,8 +98,15 @@ coming and prefetching would otherwise idle for the rest of the cue.
|
||||
## Secondary Subtitle Flow
|
||||
|
||||
- `secondary-sub-text` remains the immediate fallback, so unreadable subtitle sources, remote URLs,
|
||||
and files on network mounts still appear without waiting for file resolution. Embedded-track
|
||||
extraction is skipped for those sources to avoid competing with playback for network bandwidth.
|
||||
and still-extracting embedded tracks appear without waiting for file resolution. Embedded-track
|
||||
extraction runs for local and network-mounted files alike (demuxing reads the whole container,
|
||||
about 10 seconds per GB on gigabit, under a generous timeout); only true remote URLs skip it,
|
||||
having no on-disk container to demux.
|
||||
- The live fallback also suppresses per-glyph typesetting walls: when many simultaneous
|
||||
one-glyph lines are present (generated karaoke lettering flattened into live text), those
|
||||
lines and their short syllable companions are dropped while concurrent dialogue lines stay.
|
||||
This keeps the overlay clean while extraction is still in flight and for sources that never
|
||||
produce parsed cues.
|
||||
- Parsed secondary text and the live fallback remove exact repeated lines at any length. A
|
||||
flattened-line identity also removes long dialogue/sign repetitions that differ only in
|
||||
whitespace or terminal punctuation, while distinct simultaneous short lines remain separate.
|
||||
@@ -109,9 +116,12 @@ coming and prefetching would otherwise idle for the rest of the cue.
|
||||
- The selected source is parsed with `parseSubtitleCues()`, including metadata-aware ASS duplicate
|
||||
and animation collapse. Playback `time-pos` selects the active parsed cue after applying
|
||||
`secondary-sub-delay`.
|
||||
- Fragment reconstruction marks positioned parts that span multiple vertical rows as a grid.
|
||||
Secondary text omits those grids instead of flattening a translated table or schedule into one
|
||||
synthetic line. Reconstructed single-line karaoke remains eligible for display.
|
||||
- Fragment reconstruction marks tall multi-row positioned parts as a grid only when they read
|
||||
like tiling: a couple of texts repeated across many fragments, the same text re-shown at one
|
||||
spot over time (countdown/animation frames), or scattered single glyphs. Secondary text omits
|
||||
those grids instead of flattening a translated table or schedule into one synthetic line.
|
||||
Wrapped lyric rows, CC-style dialogue blocks, and reconstructed single-line karaoke remain
|
||||
eligible for display.
|
||||
- The resolved text is stored in `mpvClient.currentSecondarySubText` before it is broadcast. The
|
||||
overlay, mining, timing tracker, and immersion statistics therefore consume the same secondary
|
||||
text when a readable source is available.
|
||||
@@ -120,10 +130,12 @@ coming and prefetching would otherwise idle for the rest of the cue.
|
||||
- Simultaneous ASS lines are flattened in top-to-bottom positioned order, falling back to their
|
||||
authored source order when no usable position exists.
|
||||
- Fragment-only ASS karaoke is reconstructed per style before publication. Explicit spaces
|
||||
survive concatenation, while scripts that discarded their word boundaries remain compact
|
||||
instead of gaining false spaces between syllables. Short runs qualify only when overlapping
|
||||
positioned events also show changing overrides or repeated layer copies; an English or romaji
|
||||
style name alone never turns ordinary dialogue into a lyric.
|
||||
survive concatenation. Latin fragment typesetting with no literal spaces also recovers word
|
||||
boundaries represented only by materially larger horizontal `\pos` or `\move` gaps within that
|
||||
line. Unpositioned fragments stay compact instead of gaining guessed spaces between syllables.
|
||||
Short runs qualify only when overlapping positioned events also show changing overrides or
|
||||
repeated layer copies; an English or romaji style name alone never turns ordinary dialogue into
|
||||
a lyric.
|
||||
- Recovered canonical ASS text remains active for the generated animation envelope. For
|
||||
reconstructed lyric styles, the longest-lived active line wins over brief entrance and exit
|
||||
fragments from the same style.
|
||||
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
isAssTemporalCommand,
|
||||
normalizePlainSubtitleText,
|
||||
parseAssEffectField,
|
||||
removeLiveGlyphFragmentLines,
|
||||
removeAssControlDebrisLines,
|
||||
} from './ass-text';
|
||||
|
||||
test('assToPlainText drops vector drawing runs', () => {
|
||||
@@ -74,6 +76,14 @@ test('assToPlainText normalizes CRLF before converting', () => {
|
||||
assert.equal(assToPlainText('一行目\r\n二行目'), '一行目\n二行目');
|
||||
});
|
||||
|
||||
test('removeAssControlDebrisLines drops malformed spacer resets without eating dialogue', () => {
|
||||
assert.equal(
|
||||
removeAssControlDebrisLines('Visible line\n\\\n{\\fr0\n\\{\\frz287.5'),
|
||||
'Visible line',
|
||||
);
|
||||
assert.equal(removeAssControlDebrisLines('本文{\\pos(1,2)'), '本文{\\pos(1,2)');
|
||||
});
|
||||
|
||||
test('normalizePlainSubtitleText settles whitespace without decoding ASS', () => {
|
||||
// A brace reaching this layer is literal text mpv chose to show, not markup.
|
||||
assert.equal(normalizePlainSubtitleText('本文{\\pos(1,2)'), '本文{\\pos(1,2)');
|
||||
@@ -193,3 +203,18 @@ test('isAnimatedAssEffectKind covers the stock animated effects only', () => {
|
||||
assert.equal(isAnimatedAssEffectKind('other'), false);
|
||||
assert.equal(isAnimatedAssEffectKind('none'), false);
|
||||
});
|
||||
|
||||
test('removeLiveGlyphFragmentLines drops a per-glyph typesetting wall and its syllable', () => {
|
||||
const wall = [...'wansdumretoikhI'].join('\n');
|
||||
assert.equal(removeLiveGlyphFragmentLines(`${wall}\ntai`), '');
|
||||
});
|
||||
|
||||
test('removeLiveGlyphFragmentLines keeps concurrent dialogue beside a glyph wall', () => {
|
||||
const wall = [...'wansdumretoikhI'].join('\n');
|
||||
assert.equal(removeLiveGlyphFragmentLines(`${wall}\nそれよりも ノート…`), 'それよりも ノート…');
|
||||
});
|
||||
|
||||
test('removeLiveGlyphFragmentLines leaves ordinary short lines alone', () => {
|
||||
const text = 'え\nはい。\nそうだな';
|
||||
assert.equal(removeLiveGlyphFragmentLines(text), text);
|
||||
});
|
||||
|
||||
@@ -91,6 +91,41 @@ export function assToPlainText(text: string, lineBreak: AssLineBreak = '\n'): st
|
||||
return resolveWhitespaceEscapes(stripAssMarkup(text.replace(/\r\n/g, '\n')), lineBreak);
|
||||
}
|
||||
|
||||
const MALFORMED_ASS_ROTATION_RESET = /^\\?\{\\(?:fr|frx|fry|frz|fax|fay)[-+.0-9]*$/u;
|
||||
|
||||
/**
|
||||
* Drop non-rendering spacer events left as literal text by a malformed, unclosed ASS
|
||||
* rotation reset. These events otherwise become repeated `\\` or `{\\fr0` subtitle
|
||||
* lines after mpv-compatible decoding.
|
||||
*/
|
||||
export function removeAssControlDebrisLines(text: string): string {
|
||||
return text
|
||||
.split('\n')
|
||||
.filter((line) => {
|
||||
const compact = line.replace(/\s+/gu, '');
|
||||
return compact !== '\\' && !MALFORMED_ASS_ROTATION_RESET.test(compact);
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
const MIN_GLYPH_BURST_LINES = 6;
|
||||
const MAX_GLYPH_BURST_COMPANION_GLYPHS = 3;
|
||||
|
||||
/**
|
||||
* Per-glyph karaoke typesetting flattened into live text becomes a wall of
|
||||
* single-character lines plus the short syllable currently being typed. No authored
|
||||
* subtitle stacks this many one-glyph lines at once, so when the wall is present drop
|
||||
* it and its short companion fragments while keeping any concurrent dialogue line.
|
||||
*/
|
||||
export function removeLiveGlyphFragmentLines(text: string): string {
|
||||
const lines = text.split('\n');
|
||||
const singleGlyphLines = lines.filter((line) => [...line.trim()].length === 1).length;
|
||||
if (singleGlyphLines < MIN_GLYPH_BURST_LINES) return text;
|
||||
return lines
|
||||
.filter((line) => [...line.trim()].length > MAX_GLYPH_BURST_COMPANION_GLYPHS)
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
export interface NormalizePlainSubtitleTextOptions {
|
||||
/** Fold every line break into a single space. */
|
||||
collapseLineBreaks?: boolean;
|
||||
|
||||
@@ -3,7 +3,7 @@ import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
import { Database } from './sqlite';
|
||||
import { Database, type DatabaseSync } from './sqlite';
|
||||
import { getStatsExcludedWords, replaceStatsExcludedWords } from './query-lexical';
|
||||
import { finalizeSessionRecord, startSessionRecord } from './session';
|
||||
import {
|
||||
@@ -87,6 +87,29 @@ test('applyPragmas sets the SQLite tuning defaults used by immersion tracking',
|
||||
}
|
||||
});
|
||||
|
||||
test('applyPragmas installs the busy timeout before WAL negotiation', () => {
|
||||
const statements: string[] = [];
|
||||
const db: DatabaseSync = {
|
||||
exec(source) {
|
||||
statements.push(source);
|
||||
return db;
|
||||
},
|
||||
prepare() {
|
||||
throw new Error('not used');
|
||||
},
|
||||
close() {
|
||||
return db;
|
||||
},
|
||||
};
|
||||
|
||||
applyPragmas(db);
|
||||
|
||||
assert.deepEqual(statements.slice(0, 2), [
|
||||
'PRAGMA busy_timeout = 2500',
|
||||
'PRAGMA journal_mode = WAL',
|
||||
]);
|
||||
});
|
||||
|
||||
test('ensureSchema creates immersion core tables', () => {
|
||||
const dbPath = makeDbPath();
|
||||
const db = new Database(dbPath);
|
||||
|
||||
@@ -315,10 +315,12 @@ function migrateSessionEventTimestampsToText(db: DatabaseSync): void {
|
||||
}
|
||||
|
||||
export function applyPragmas(db: DatabaseSync): void {
|
||||
// Install the wait policy before WAL negotiation, which can briefly contend with
|
||||
// another connection closing or checkpointing the same database.
|
||||
db.exec('PRAGMA busy_timeout = 2500');
|
||||
db.exec('PRAGMA journal_mode = WAL');
|
||||
db.exec('PRAGMA synchronous = NORMAL');
|
||||
db.exec('PRAGMA foreign_keys = ON');
|
||||
db.exec('PRAGMA busy_timeout = 2500');
|
||||
db.exec(`PRAGMA journal_size_limit = ${WAL_JOURNAL_SIZE_LIMIT_BYTES}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,12 @@ test('parseSrtCues handles multi-line subtitle text', () => {
|
||||
assert.equal(cues[0]!.text, 'これは\nテストです');
|
||||
});
|
||||
|
||||
test('parseSrtCues preserves lines that only resemble malformed ASS controls', () => {
|
||||
const content = ['1', '00:01:00,000 --> 00:01:05,000', '\\', '{\\fr0', ''].join('\n');
|
||||
|
||||
assert.equal(parseSrtCues(content)[0]?.text, '\\\n{\\fr0');
|
||||
});
|
||||
|
||||
test('parseSrtCues strips HTML-like markup while preserving line breaks', () => {
|
||||
const content = [
|
||||
'1',
|
||||
@@ -1021,3 +1027,752 @@ test('parseSubtitleCues detects subtitle formats from remote URLs', () => {
|
||||
assert.equal(cues.length, 1);
|
||||
assert.equal(cues[0]!.text, 'URLテスト');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues skips zero-duration ASS metadata events', () => {
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
'Dialogue: 0,0:00:00.00,0:00:00.00,Default,,0,0,0,,[Script Info]',
|
||||
'Dialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,Real subtitle',
|
||||
].join('\n');
|
||||
|
||||
assert.deepEqual(parseSubtitleCues(content, 'test.ass'), [
|
||||
{ startTime: 1, endTime: 2, text: 'Real subtitle' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues drops malformed ASS spacer reset debris', () => {
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
'Dialogue: 0,0:00:01.00,0:00:02.00,Background,,0,0,0,,{\\pos(10,10)}\\h\\h\\h\\{\\fr0',
|
||||
'Dialogue: 1,0:00:01.00,0:00:02.00,Default,,0,0,0,,Visible line',
|
||||
].join('\n');
|
||||
|
||||
assert.deepEqual(parseSubtitleCues(content, 'test.ass'), [
|
||||
{ startTime: 1, endTime: 2, text: 'Visible line' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues recovers spaces encoded only by positioned Latin glyph gaps', () => {
|
||||
const glyphs = [
|
||||
['T', 100],
|
||||
['h', 118],
|
||||
['e', 136],
|
||||
['s', 164],
|
||||
['t', 178],
|
||||
['a', 194],
|
||||
['r', 210],
|
||||
['s', 227],
|
||||
['I', 255],
|
||||
['s', 275],
|
||||
['e', 293],
|
||||
['e', 311],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
glyphs.map(
|
||||
([glyph, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP English,,0,0,0,,{\\pos(${x},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'The stars I see');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues does not split narrow letters inside positioned English words', () => {
|
||||
const text = 'carryinghappiness';
|
||||
const positions = [
|
||||
323, 341, 356, 369, 383, 396, 410, 428, 456, 474, 493, 512, 526, 540, 558, 575, 590,
|
||||
];
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
[...text].map(
|
||||
(glyph, index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP English,,0,0,0,,{\\pos(${positions[index]},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'carrying happiness');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues keeps proportional-font variation inside positioned English words', () => {
|
||||
const text = 'sendsripplesacrossthestillnessofyourheart';
|
||||
const positions = [
|
||||
32, 46, 60, 78, 95, 121, 131, 144, 163, 177, 189, 203, 232, 248, 261, 274, 288, 302, 329, 346,
|
||||
363, 390, 405, 416, 423, 432, 443, 457, 471, 485, 512, 525, 551, 564, 579, 593, 622, 639, 655,
|
||||
670, 684,
|
||||
];
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
[...text].map(
|
||||
(glyph, index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,Insert English,,0,0,0,,{\\pos(${positions[index]},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(
|
||||
parseSubtitleCues(content, 'test.ass')[0]?.text,
|
||||
'sends ripples across the stillness of your heart',
|
||||
);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues keeps a short capitalized word when the following gap is larger', () => {
|
||||
const text = 'IfIgrow';
|
||||
const positions = [347, 365, 397, 430, 446, 463, 485];
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
[...text].map(
|
||||
(glyph, index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,Insert English,,0,0,0,,{\\pos(${positions[index]},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'If I grow');
|
||||
});
|
||||
|
||||
// Geometry taken from a real per-glyph ED line. The `waves within` gap crosses a wide
|
||||
// `w`, so the width-normalized ratio reads it as a common advance; only the constant
|
||||
// extra distance of the authored word space gives it away.
|
||||
test('parseSubtitleCues recovers a word gap measured across a wide glyph', () => {
|
||||
const text = 'youcanhearthesoundofthewaveswithinmyheart';
|
||||
const positions = [
|
||||
202, 223, 244, 274, 296, 317, 346, 367, 389, 408, 433, 450, 470, 499, 517, 538, 557, 578, 609,
|
||||
627, 651, 668, 688, 723, 748, 770, 792, 811, 843, 863, 875, 892, 907, 922, 957, 983, 1013, 1034,
|
||||
1055, 1075, 1090,
|
||||
];
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
[...text].map(
|
||||
(glyph, index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,ED English,,0,0,0,,{\\pos(${positions[index]},687)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(
|
||||
parseSubtitleCues(content, 'test.ass')[0]?.text,
|
||||
'you can hear the sound of the waves within my heart',
|
||||
);
|
||||
});
|
||||
|
||||
// A single short word gives too few gap samples to trust the excess rule: its narrow
|
||||
// glyphs skew the common advance low and `w e` would read as a word gap.
|
||||
test('parseSubtitleCues does not split a short single positioned word', () => {
|
||||
const text = 'Swelling';
|
||||
const positions = [592, 613, 635, 647, 655, 662, 673, 689];
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
[...text].map(
|
||||
(glyph, index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,ED English,,0,0,0,,{\\pos(${positions[index]},682)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'Swelling');
|
||||
});
|
||||
|
||||
// A capitalized word whose first letter sits before a wide glyph (`S|miles`) overruns
|
||||
// the width table; the excess rule must not split a capital from its lowercase run.
|
||||
test('parseSubtitleCues keeps a capitalized word intact under the excess rule', () => {
|
||||
const text = 'Smilesarebudding';
|
||||
const positions = [37, 63, 80, 88, 99, 113, 142, 157, 171, 201, 217, 235, 255, 269, 280, 295];
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
[...text].map(
|
||||
(glyph, index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,ED English,,0,0,0,,{\\pos(${positions[index]},682)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'Smiles are budding');
|
||||
});
|
||||
|
||||
// Mirrors a real ED: per-syllable romaji at y=34 overlaid with animated single letters
|
||||
// at y=29 rendered through `\fn` in a symbol font, where `a` draws as a sparkle. The
|
||||
// letters must neither join the reconstructed line nor survive as their own cues.
|
||||
test('parseSubtitleCues drops symbol-font glyph decoration from a reconstructed line', () => {
|
||||
const syllables = [
|
||||
['so', 479],
|
||||
['t', 505],
|
||||
['to', 529],
|
||||
['mi', 577],
|
||||
['mi', 618],
|
||||
['ni', 663],
|
||||
['a', 699],
|
||||
['te', 728],
|
||||
['ru', 764],
|
||||
['to', 810],
|
||||
] as const;
|
||||
const decoration = [
|
||||
['a', 479, '0:00:01.25'],
|
||||
['z', 577, '0:00:02.51'],
|
||||
['x', 618, '0:00:02.78'],
|
||||
['q', 505, '0:00:04.20'],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
syllables.map(
|
||||
([syllable, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:05.37,ED Romaji,,0,0,0,fx,{\\an5\\pos(${x},34)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${syllable}`,
|
||||
),
|
||||
),
|
||||
...decoration.map(
|
||||
([glyph, x, start]) =>
|
||||
`Dialogue: 0,${start},0:00:05.37,ED Romaji,,0,0,0,fx,{\\pos(${x},29)\\fnSplit splat splodge\\fs28\\t(3870,3970,\\fscx105)}${glyph}`,
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
const cues = parseSubtitleCues(content, 'test.ass');
|
||||
assert.equal(cues.length, 1);
|
||||
assert.equal(cues[0]?.text, 'sotto mimi ni ateru to');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues drops clipped repeated-glyph texture text', () => {
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
"Dialogue: 10,0:00:01.00,0:00:04.00,Default,,0,0,0,,I'm blocking them.",
|
||||
'Dialogue: 2,0:00:01.00,0:00:04.00,MarySigns,,0,0,0,,{\\pos(960,80)\\fnSerangkaian Pattern Regular\\clip(800,20,1120,140)}LLLLLLLLLLLLLLLLLLLLLLLL',
|
||||
'Dialogue: 3,0:00:01.00,0:00:04.00,MarySigns,,0,0,0,,{\\pos(960,150)\\fnSF Pro Display}Enter a message',
|
||||
].join('\n');
|
||||
|
||||
assert.deepEqual(
|
||||
parseSubtitleCues(content, 'test.ass').map((cue) => cue.text),
|
||||
["I'm blocking them.", 'Enter a message'],
|
||||
);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues drops clipped repeated-glyph texture text without a font override', () => {
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
'Dialogue: 0,0:00:01.00,0:00:04.00,FrogSigns,,0,0,0,,{\\an7\\pos(736.49,152.99)\\fscy150\\fs10\\bord3\\c&H657BC8&\\3c&H657BC8&\\blur3\\clip}lllllllllllll',
|
||||
'Dialogue: 0,0:00:01.00,0:00:04.00,FrogSigns,,0,0,0,,{\\an7\\pos(769.9,106.18)\\fscy150\\fs12\\bord3\\c&H66729F&\\3c&H66729F&\\blur5\\clip}llll',
|
||||
'Dialogue: 5,0:00:01.00,0:00:04.00,FrogSigns,,0,0,0,,{\\pos(893,311)}Read',
|
||||
].join('\n');
|
||||
|
||||
assert.deepEqual(
|
||||
parseSubtitleCues(content, 'test.ass').map((cue) => cue.text),
|
||||
['Read'],
|
||||
);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues drops per-character alpha texture text', () => {
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
"Dialogue: 10,0:00:01.00,0:00:04.00,Default,Girl,0,0,0,,So Doloris was actually Uika-chan from sumimi! That's amazing!",
|
||||
"Dialogue: 2,0:00:01.00,0:00:04.00,MarySigns,,0,0,0,,{\\pos(960,240)\\fnCinzel}Hanasakigawa Girl's School",
|
||||
'Dialogue: 3,0:00:01.00,0:00:04.00,MarySigns,,0,0,0,,{\\pos(960,300)\\fnSplit splat splodge\\clip(800,200,1120,400)}d{\\2a1}s{\\2a0}h{\\2a1}f{\\2a0}k{\\2a1}h{\\2a0}f{\\2a1}s{\\2a0}d{\\2a1}f{\\2a0}e',
|
||||
'Dialogue: 3,0:00:01.00,0:00:04.00,MarySigns,,0,0,0,,{\\pos(980,340)\\fnSplit splat splodge}f {\\2a1}a',
|
||||
'Dialogue: 4,0:00:01.00,0:00:04.00,MarySigns,,0,0,0,,{\\pos(960,360)\\fnGrain SemiBold}5{\\2a1}X{\\2a0}N{\\2a1}T{\\2a0}f{\\2a1}I{\\2a0}g{\\2a1}F{\\2a0}B{\\2a1}?{\\2a0}k{\\2a1}u{\\2a0}C{\\2a1}m',
|
||||
].join('\n');
|
||||
|
||||
assert.deepEqual(
|
||||
parseSubtitleCues(content, 'test.ass').map((cue) => cue.text),
|
||||
[
|
||||
"So Doloris was actually Uika-chan from sumimi! That's amazing!",
|
||||
"Hanasakigawa Girl's School",
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues drops transparent texture payloads across an animated sign', () => {
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
"Dialogue: 90,0:00:01.00,0:00:04.00,Alt,,0,0,0,,Even if you want to see her, she doesn't want to see you!",
|
||||
'Dialogue: 0,0:00:01.00,0:00:01.08,FrogSigns,,0,0,0,,{\\pos(699,803)\\fnSerangkaian Pattern Regular\\clip(300,380,1130,1050)}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a0}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\\\\\\\\\\\\\\\\\\\\\',
|
||||
'Dialogue: 3,0:00:01.00,0:00:01.08,FrogSigns,Street,0,0,0,,{\\pos(285,653)\\fnGrain\\alpha&HE0&}Street performance by Mortis from\\NMujica - Acting prodigy in action!',
|
||||
'Dialogue: 5,0:00:01.00,0:00:01.08,FrogSigns,Street,0,0,0,,{\\pos(285,653)\\fnRoboto Medium\\alpha&H00&}Street performance by Mortis from\\NMujica - Acting prodigy in action!',
|
||||
'Dialogue: 6,0:00:01.00,0:00:01.08,FrogSigns,Street,0,0,0,,{\\pos(285,653)\\fnGrain\\alpha&HE0&}H1.4igcAhGYHVWD"kHcVlG2W9eKEWj"!X\\N\'uNVaEVpTXMd9rk7dnRX\'P!RhsS"Wn90k6',
|
||||
'Dialogue: 6,0:00:01.00,0:00:01.08,FrogSigns,18K,0,0,0,,{\\pos(284,821)\\fnGrain\\alpha&HE0&}ou:QepiiPqQ.4n.IYbFaGHtPzWyKI9CUSq:',
|
||||
'Dialogue: 1,0:00:01.08,0:00:04.00,FrogSigns,,0,0,0,,{\\pos(581,921)\\fnSerangkaian Pattern Regular\\clip(195,495,986,1120)}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{',
|
||||
'Dialogue: 3,0:00:01.08,0:00:04.00,FrogSigns,,0,0,0,,{\\pos(151,769)\\fnGrain\\alpha&HE0&}Street performance by Mortis from\\NMujica - Acting prodigy in action!',
|
||||
'Dialogue: 5,0:00:01.08,0:00:04.00,FrogSigns,,0,0,0,,{\\pos(151,769)\\fnRoboto Medium\\alpha&H00&}Street performance by Mortis from\\NMujica - Acting prodigy in action!',
|
||||
'Dialogue: 3,0:00:01.08,0:00:04.00,FrogSigns,,0,0,0,,{\\pos(151,769)\\fnGrain\\alpha&HF0&}9LF\'GpPCTlOkLxBLV:QN,8R8NUVM"ha.s\\NNUUPNTBdJih4jUthK34i,yYe;9EBgLXbET',
|
||||
"Dialogue: 6,0:00:01.08,0:00:04.00,FrogSigns,,0,0,0,,{\\pos(150,936)\\fnGrain\\alpha&HE0&}JS7vl:lD;'PzkCb!bGT;.7TbA.KCkEH0LOk",
|
||||
].join('\n');
|
||||
|
||||
assert.deepEqual(
|
||||
parseSubtitleCues(content, 'test.ass').map((cue) => cue.text),
|
||||
[
|
||||
'Street performance by Mortis from\nMujica - Acting prodigy in action!',
|
||||
"Even if you want to see her, she doesn't want to see you!",
|
||||
'Street performance by Mortis from\nMujica - Acting prodigy in action!',
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues does not reconstruct short texture pieces under another actor', () => {
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
'Dialogue: 4,0:00:01.00,0:00:04.00,FrogSigns,bubble,0,0,0,,{\\pos(245,-102)\\fnSerangkaian Pattern Regular\\clip(224,-1,831,106)}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L{\\2a0}L{\\2a1}L',
|
||||
'Dialogue: 4,0:00:01.00,0:00:04.00,FrogSigns,read,0,0,0,,{\\pos(917,293)\\alpha&H20&\\fnSerangkaian Pattern Regular\\clip(904,289,1010,336)}L{\\2a0}L{\\2a1}L{\\2a0}L',
|
||||
'Dialogue: 4,0:00:01.00,0:00:04.00,FrogSigns,read,0,0,0,,{\\pos(911,293)\\alpha&H58&\\fnSerangkaian Pattern Regular\\clip(904,289,1010,336)}L{\\2a0}L{\\2a1}L{\\2a0}L',
|
||||
'Dialogue: 4,0:00:01.00,0:00:04.00,FrogSigns,read,0,0,0,,{\\pos(845,300)\\alpha&H00&\\fnSerangkaian Pattern Regular\\clip(904,289,1010,336)}L{\\2a0}L{\\2a1}L{\\2a0}L',
|
||||
'Dialogue: 7,0:00:01.00,0:00:04.00,FrogSigns,read,0,0,0,,{\\pos(907,293)\\alpha&HD0&\\fnSerangkaian Pattern Regular\\clip(891,289,1010,338)}L{\\2a0}L{\\2a1}L{\\2a0}L',
|
||||
'Dialogue: 7,0:00:01.00,0:00:04.00,FrogSigns,read,0,0,0,,{\\pos(911,293)\\alpha&HD0&\\fnSerangkaian Pattern Regular\\clip(891,289,1010,338)}L{\\2a0}L{\\2a1}L{\\2a0}L',
|
||||
'Dialogue: 7,0:00:01.00,0:00:04.00,FrogSigns,read,0,0,0,,{\\pos(922,130)\\alpha&HD0&\\fnSerangkaian Pattern Regular\\clip(891,120,1010,173)}L{\\2a0}L{\\2a1}L{\\2a0}L',
|
||||
'Dialogue: 5,0:00:01.00,0:00:04.00,FrogSigns,,0,0,0,,{\\pos(893,311)\\fnSFProDisplay-Regular-STR}Read 3',
|
||||
].join('\n');
|
||||
|
||||
assert.deepEqual(
|
||||
parseSubtitleCues(content, 'test.ass').map((cue) => cue.text),
|
||||
['Read 3'],
|
||||
);
|
||||
});
|
||||
|
||||
test('parseSubtitleCues separates overlapping positioned English lyric sequences', () => {
|
||||
const fragments = [
|
||||
['my', 642, '0:00:01.00', '0:00:04.05'],
|
||||
['song!', 713, '0:00:01.00', '0:00:04.05'],
|
||||
['I', 533, '0:00:01.67', '0:00:04.09'],
|
||||
['h', 557, '0:00:01.67', '0:00:04.09'],
|
||||
['u', 575, '0:00:01.67', '0:00:04.09'],
|
||||
['m', 597, '0:00:01.67', '0:00:04.09'],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([text, x, start, end], index) =>
|
||||
`Dialogue: ${layer},${start},${end},OP English,,0,0,0,,{\\pos(${x},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${text}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'my song! I hum');
|
||||
});
|
||||
|
||||
const eventsHeader = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
];
|
||||
|
||||
test('parseSubtitleCues keeps a tall CC-style dialogue block publishable, not a fragment grid', () => {
|
||||
const content = [
|
||||
...eventsHeader,
|
||||
'Dialogue: 0,0:00:06.11,0:00:10.11,Default,,0,0,0,,{\\pos(212,383)\\fscx50\\fscy50}たき',
|
||||
'Dialogue: 0,0:00:06.11,0:00:10.11,Default,,0,0,0,,{\\pos(172,437)\\fscx50}({\\fscx100}立希{\\fscx50})',
|
||||
'Dialogue: 0,0:00:06.11,0:00:10.11,Default,,0,0,0,,{\\pos(332,443)\\fscx50\\fscy50}ともり',
|
||||
'Dialogue: 0,0:00:06.11,0:00:10.11,Default,,0,0,0,,{\\pos(192,497)}お前…{\\fscx50} {\\fscx100}燈をバンドに誘ったの?',
|
||||
].join('\n');
|
||||
|
||||
const cue = parseSubtitleCues(content, 'test.ass')[0];
|
||||
assert.equal(cue?.text, 'たき(立希)ともりお前… 燈をバンドに誘ったの?');
|
||||
assert.equal(cue?.assLayout?.kind, 'positioned');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues marks re-shown countdown frames as a fragment grid', () => {
|
||||
const rows = [
|
||||
['juu', '10'],
|
||||
['juu', '10'],
|
||||
['kyuu', '9'],
|
||||
['kyuu', '9'],
|
||||
['hachi', '8'],
|
||||
['hachi', '8'],
|
||||
] as const;
|
||||
const content = [
|
||||
...eventsHeader,
|
||||
...rows.flatMap(([word, num], index) => {
|
||||
const timestamp = (seconds: number) => `0:00:${seconds.toFixed(2).padStart(5, '0')}`;
|
||||
const start = timestamp(6 + index * 0.4);
|
||||
const end = timestamp(6 + index * 0.4 + 0.4);
|
||||
return [0, 1].flatMap((layer) => [
|
||||
`Dialogue: ${layer},${start},${end},ED Romaji,,0,0,0,,{\\pos(${300 + index * 8},40)\\t(0,100,\\fscx120)}${word}`,
|
||||
`Dialogue: ${layer},${start},${end},ED Romaji,,0,0,0,,{\\pos(${300 + index * 8},93)\\t(0,100,\\fscx120)}${num}`,
|
||||
]);
|
||||
}),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.assLayout?.kind, 'fragment-grid');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues marks scattered single-glyph typesetting as a fragment grid', () => {
|
||||
const glyphs = ['の', 'こ', '部', 'そ', '屋'];
|
||||
const content = [
|
||||
...eventsHeader,
|
||||
...[0, 1].flatMap((layer) =>
|
||||
glyphs.map(
|
||||
(glyph, index) =>
|
||||
`Dialogue: ${layer},0:00:06.00,0:00:09.00,OP-JP,,0,0,0,,{\\pos(${500 + index * 30},${-30 + index * 35})\\t(0,100,\\fscx120)}${glyph}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.assLayout?.kind, 'fragment-grid');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues marks a repeated-token sign wall as a fragment grid', () => {
|
||||
const content = [
|
||||
...eventsHeader,
|
||||
...[0, 1].flatMap((layer) =>
|
||||
Array.from(
|
||||
{ length: 6 },
|
||||
(_, index) =>
|
||||
`Dialogue: ${layer},0:00:06.00,0:00:09.00,Sign,,0,0,0,,{\\pos(${200 + index * 60},${100 + index * 30})\\t(0,100,\\fscx120)}${index % 2 === 0 ? 'Maid' : 'Cafe'}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.assLayout?.kind, 'fragment-grid');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues keeps a wrapped lyric with a staggered repeated token publishable', () => {
|
||||
const fragments = [
|
||||
['dreams', 300, 115, '0:00:01.00'],
|
||||
['ju', 250, 39, '0:00:01.00'],
|
||||
['n', 280, 39, '0:00:01.00'],
|
||||
['jo', 300, 39, '0:00:01.00'],
|
||||
['u', 330, 39, '0:00:01.00'],
|
||||
['to', 360, 39, '0:00:01.00'],
|
||||
['jo', 395, 39, '0:00:01.02'],
|
||||
['u', 425, 39, '0:00:01.00'],
|
||||
['ne', 455, 39, '0:00:01.00'],
|
||||
['tsu!', 485, 39, '0:00:01.00'],
|
||||
] as const;
|
||||
const content = [
|
||||
...eventsHeader,
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([text, x, y, start], index) =>
|
||||
`Dialogue: ${layer},${start},0:00:04.00,ED Romaji,,0,0,0,,{\\pos(${x},${y})\\t(${index * 2},${index * 2 + 100},\\fscx120)}${text}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
const cue = parseSubtitleCues(content, 'test.ass')[0];
|
||||
assert.notEqual(cue?.assLayout?.kind, 'fragment-grid');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues adds a missing word space after positioned punctuation', () => {
|
||||
const fragments = [
|
||||
['H', 100],
|
||||
['i,', 119],
|
||||
['t', 153],
|
||||
['h', 168],
|
||||
['e', 186],
|
||||
['r', 202],
|
||||
['e', 216],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP English,,0,0,0,,{\\pos(${x},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'Hi, there');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues does not split a positioned thousands separator', () => {
|
||||
const fragments = [
|
||||
['1,', 100],
|
||||
['000', 145],
|
||||
['0', 185],
|
||||
['0', 205],
|
||||
['0', 225],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP English,,0,0,0,,{\\pos(${x},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, '1,000000');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues does not split a wide glyph from its punctuated suffix', () => {
|
||||
const fragments = [
|
||||
['v', 904],
|
||||
['o', 924],
|
||||
['i', 939],
|
||||
['c', 955],
|
||||
['e', 976],
|
||||
['r', 1004],
|
||||
['e', 1021],
|
||||
['a', 1042],
|
||||
['c', 1063],
|
||||
['h', 1083],
|
||||
['e', 1104],
|
||||
['d', 1125],
|
||||
['m', 1161],
|
||||
['e,', 1193],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP English,,0,0,0,,{\\pos(${x},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'voice reached me,');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues spaces positioned lyric fragments across authored rows', () => {
|
||||
const fragments = [
|
||||
['My', 472, 39],
|
||||
['song!', 543, 39],
|
||||
['My', 507, 78],
|
||||
['song!', 578, 78],
|
||||
['ku', 643, 39],
|
||||
['chi', 683, 39],
|
||||
['zu', 722, 39],
|
||||
['sa', 757, 39],
|
||||
['n', 783, 39],
|
||||
['de', 811, 39],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x, y], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP Romaji,,0,0,0,,{\\pos(${x},${y})\\t(${index * 2},${index * 2 + 100},\\fscx120)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'My song! My song! kuchizusande');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues recovers positioned word gaps between romaji fragments', () => {
|
||||
const fragments = [
|
||||
['sa', 380],
|
||||
['ga', 421],
|
||||
['shi', 467],
|
||||
['te', 510],
|
||||
['ta', 545],
|
||||
['ha', 593],
|
||||
['ji', 624],
|
||||
['ke', 655],
|
||||
['ta', 693],
|
||||
['i', 726],
|
||||
['ro', 749],
|
||||
['no', 798],
|
||||
['yu', 849],
|
||||
['me', 895],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP Romaji,,0,0,0,,{\\pos(${x},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'sagashiteta hajiketa iro no yume');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues recovers clear word gaps in a short romaji line', () => {
|
||||
const fragments = [
|
||||
['bo', 542],
|
||||
['ku', 584],
|
||||
['wo', 640],
|
||||
['yo', 697],
|
||||
['bu', 738],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,OP Romaji,,0,0,0,,{\\pos(${x},110)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'boku wo yobu');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues suppresses a karaoke highlight sweep without publishing it', () => {
|
||||
// Main lyric: per-glyph fragments alive together for the whole line.
|
||||
const lineFragments = [
|
||||
['to', 972],
|
||||
['so', 1051],
|
||||
['u', 1113],
|
||||
['o', 1166],
|
||||
['mo', 1204],
|
||||
] as const;
|
||||
// Highlight sweep: one syllable at a time over the same lyric, each event ending
|
||||
// exactly as the next begins, so no two syllables are ever on screen together.
|
||||
const sweepFragments = [
|
||||
['to', 972, '0:00:01.00', '0:00:01.40'],
|
||||
['so', 1051, '0:00:01.40', '0:00:01.80'],
|
||||
['u', 1113, '0:00:01.80', '0:00:02.20'],
|
||||
['o', 1166, '0:00:02.20', '0:00:02.60'],
|
||||
['mo', 1204, '0:00:02.60', '0:00:03.00'],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...[0, 1].flatMap((layer) =>
|
||||
lineFragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:00:01.00,0:00:04.00,ED Romaji,,0,0,0,fx,{\\pos(${x},60)\\t(${index * 2},${index * 2 + 100},\\fscx120)}${fragment}`,
|
||||
),
|
||||
),
|
||||
...sweepFragments.flatMap(([fragment, x, start, end]) =>
|
||||
[
|
||||
[40, x, 60],
|
||||
[41, x + 4, 64],
|
||||
].map(
|
||||
([layer, copyX, copyY]) =>
|
||||
`Dialogue: ${layer},${start},${end},ED Romaji2,,0,0,0,fx,{\\an5\\pos(${copyX},${copyY})\\t(150,290,\\1a&HFF&)}${fragment}`,
|
||||
),
|
||||
),
|
||||
'Dialogue: 42,0:00:01.20,0:00:01.30,ED Romaji2,,0,0,0,fx,{\\fnWebdings\\pos(900,50)\\t(0,100,\\fscx120)}a',
|
||||
'Dialogue: 42,0:00:04.00,0:00:04.20,ED Romaji2,,0,0,0,fx,{\\fnWebdings\\pos(900,50)\\t(0,100,\\fscx120)}z',
|
||||
].join('\n');
|
||||
|
||||
const cues = parseSubtitleCues(content, 'test.ass');
|
||||
assert.equal(cues.length, 2);
|
||||
assert.equal(cues[0]?.text.replace(/\s+/gu, ''), 'tosouomo');
|
||||
assert.equal(cues[1]?.text, 'z');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues collapses drop-shadow layer copies offset by a few pixels', () => {
|
||||
const fragments = [
|
||||
['me', 580],
|
||||
['no', 668],
|
||||
['mae', 770],
|
||||
['ni', 864],
|
||||
['no', 939],
|
||||
['bi', 996],
|
||||
['ru', 1049],
|
||||
] as const;
|
||||
const content = [
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
...fragments.flatMap(([fragment, x], index) => [
|
||||
`Dialogue: 30,0:01:42.00,0:01:46.92,OP Romaji,,0,0,0,fx,{\\pos(${x},25)\\bord0\\t(${index * 2},${index * 2 + 120},\\blur0.5)}${fragment}`,
|
||||
// Shadow copy sits 4px off the base glyph and must not read as a second syllable.
|
||||
`Dialogue: 29,0:01:42.00,0:01:46.92,OP Romaji,,0,0,0,fx,{\\pos(${x + 4},29)\\c&HFFFFFF&\\t(${index * 2},${index * 2 + 120},\\blur9)}${fragment}`,
|
||||
`Dialogue: 28,0:01:42.00,0:01:46.92,OP Romaji,,0,0,0,fx,{\\pos(${x},25)\\c&HFFFFFF&\\t(${index * 2},${index * 2 + 120},\\blur9)}${fragment}`,
|
||||
]),
|
||||
].join('\n');
|
||||
|
||||
const cues = parseSubtitleCues(content, 'test.ass');
|
||||
assert.equal(cues.length, 1);
|
||||
assert.equal(cues[0]?.text.replace(/\s+/gu, ''), 'menomaeninobiru');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues recovers positional word gaps beside an authored space', () => {
|
||||
// Real ED line: every glyph is placed by `\move`, but the `star` fragment alone carries
|
||||
// a literal leading space. The authored space must not disable positional recovery for
|
||||
// the rest of the line.
|
||||
const fragments = [
|
||||
['s', 633],
|
||||
['e', 665],
|
||||
['a', 697],
|
||||
['r', 723],
|
||||
['c', 747],
|
||||
['h', 774],
|
||||
['i', 793],
|
||||
['n', 813],
|
||||
['g', 838],
|
||||
['f', 884],
|
||||
['o', 911],
|
||||
['r', 937],
|
||||
['a', 986],
|
||||
['s', 1041],
|
||||
['h', 1070],
|
||||
['o', 1098],
|
||||
['o', 1128],
|
||||
['t', 1153],
|
||||
['i', 1169],
|
||||
['n', 1188],
|
||||
['g', 1214],
|
||||
[' s', 1264],
|
||||
['t', 1290],
|
||||
['a', 1316],
|
||||
['r', 1342],
|
||||
] as const;
|
||||
const content = [
|
||||
...eventsHeader,
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:22:44.83,0:22:47.70,ED English,,0,0,0,fx,{\\move(${x},1020,${x},1020,0,300)\\t(${index * 2},${index * 2 + 300},\\fs90)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(parseSubtitleCues(content, 'test.ass')[0]?.text, 'searching for a shooting star');
|
||||
});
|
||||
|
||||
test('parseSubtitleCues splits chunked words whose gap only the excess rule catches', () => {
|
||||
// `Choices|presumably` normalizes to just under the ratio threshold because both
|
||||
// neighbors are wide three-letter chunks; its constant word-space excess still shows.
|
||||
const fragments = [
|
||||
['Ch', 526],
|
||||
['oi', 584],
|
||||
['ces', 648],
|
||||
['pre', 747],
|
||||
['su', 819],
|
||||
['mab', 904],
|
||||
['ly', 981],
|
||||
['ma', 1059],
|
||||
['de', 1128],
|
||||
['by', 1203],
|
||||
['cha', 1295],
|
||||
['nce', 1385],
|
||||
] as const;
|
||||
const content = [
|
||||
...eventsHeader,
|
||||
...[0, 1].flatMap((layer) =>
|
||||
fragments.map(
|
||||
([fragment, x], index) =>
|
||||
`Dialogue: ${layer},0:01:53.01,0:01:55.52,OP English,,0,0,0,fx,{\\pos(${x},1055)\\t(${index * 2},${index * 2 + 120},\\blur0.5)}${fragment}`,
|
||||
),
|
||||
),
|
||||
].join('\n');
|
||||
|
||||
assert.equal(
|
||||
parseSubtitleCues(content, 'test.ass')[0]?.text,
|
||||
'Choices presumably made by chance',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -2,7 +2,9 @@ import {
|
||||
assOverrideSignature,
|
||||
assToPlainText,
|
||||
collectAssOverrideCommands,
|
||||
hasAssTemporalOverride,
|
||||
parseAssEffectField,
|
||||
removeAssControlDebrisLines,
|
||||
type AssEffectKind,
|
||||
type AssOverrideCommand,
|
||||
} from './ass-text';
|
||||
@@ -91,6 +93,10 @@ function sanitizeSubtitleCueText(text: string): string {
|
||||
return decodeSubtitleCueText(text).trim();
|
||||
}
|
||||
|
||||
function sanitizeAssCueText(text: string): string {
|
||||
return removeAssControlDebrisLines(decodeSubtitleCueText(text)).trim();
|
||||
}
|
||||
|
||||
function attachAssLayout<T extends SubtitleCue>(cue: T, assLayout: AssCueLayout | undefined): T {
|
||||
if (assLayout) {
|
||||
Object.defineProperty(cue, 'assLayout', { value: assLayout, enumerable: false });
|
||||
@@ -334,6 +340,27 @@ function fragmentPlacementAnchors(event: AnnotatedSubtitleCue): Set<string> {
|
||||
return anchors;
|
||||
}
|
||||
|
||||
// Drop-shadow layer copies sit a few pixels off their base glyph, while even tightly
|
||||
// kerned repeated glyphs in one line ("ii") measure 10px apart or more.
|
||||
const LAYER_COPY_OFFSET_TOLERANCE_PX = 6;
|
||||
|
||||
// One representative point per placement command: the `\pos` point or the `\move`
|
||||
// midpoint. Comparing raw `\move` endpoints cross-wise misreads a travel distance that
|
||||
// matches the glyph advance as a layer copy of a neighboring same-letter glyph.
|
||||
function fragmentAnchorPoints(event: AnnotatedSubtitleCue): AssFragmentPosition[] {
|
||||
const points: AssFragmentPosition[] = [];
|
||||
for (const command of event.overrides) {
|
||||
const name = command.name.toLowerCase();
|
||||
const args = command.args.split(',').map((value) => Number(value.trim()));
|
||||
if (name === 'pos' && args.length >= 2 && args.slice(0, 2).every(Number.isFinite)) {
|
||||
points.push({ x: args[0]!, y: args[1]! });
|
||||
} else if (name === 'move' && args.length >= 4 && args.slice(0, 4).every(Number.isFinite)) {
|
||||
points.push({ x: (args[0]! + args[2]!) / 2, y: (args[1]! + args[3]!) / 2 });
|
||||
}
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
function isRepeatedFragmentCopy(
|
||||
previous: AnnotatedSubtitleCue,
|
||||
current: AnnotatedSubtitleCue,
|
||||
@@ -342,6 +369,17 @@ function isRepeatedFragmentCopy(
|
||||
if ([...fragmentPlacementAnchors(current)].some((anchor) => previousAnchors.has(anchor))) {
|
||||
return true;
|
||||
}
|
||||
const previousPoints = fragmentAnchorPoints(previous);
|
||||
const nearbyAnchor = fragmentAnchorPoints(current).some((point) =>
|
||||
previousPoints.some(
|
||||
(previousPoint) =>
|
||||
Math.abs(point.x - previousPoint.x) <= LAYER_COPY_OFFSET_TOLERANCE_PX &&
|
||||
Math.abs(point.y - previousPoint.y) <= LAYER_COPY_OFFSET_TOLERANCE_PX,
|
||||
),
|
||||
);
|
||||
if (nearbyAnchor) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
previous.startTime === current.startTime &&
|
||||
previous.endTime === current.endTime &&
|
||||
@@ -387,6 +425,292 @@ interface AssFragmentPart {
|
||||
text: string;
|
||||
}
|
||||
|
||||
interface AssFragmentPosition {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
const MIN_LATIN_POSITION_GAP_SAMPLES = 4;
|
||||
const LATIN_FRAGMENT_WORD_GAP_RATIO = 1.16;
|
||||
const LATIN_GLYPH_WORD_GAP_RATIO = 1.4;
|
||||
// Word-space advance beyond the width-predicted glyph advance, as a fraction of the
|
||||
// line's common unit. Measured corpus extremes: widest within-word excess 0.32 (`pp`
|
||||
// with tracking), narrowest word gap 0.40 (`s w` across a wide glyph). That margin only
|
||||
// holds when the common unit is estimated from enough glyph pairs; a short single-word
|
||||
// line (`Swelling`) skews the unit low and its ordinary advances read as word gaps.
|
||||
const LATIN_GLYPH_WORD_EXCESS_RATIO = 0.36;
|
||||
const MIN_LATIN_GLYPH_EXCESS_GAP_SAMPLES = 10;
|
||||
// Multi-character syllable chunks average out proportional-font variation, so their
|
||||
// advances track the width model far more closely than single glyphs do. Measured on a
|
||||
// chunked lyric line, within-word excess stayed under 0.07 of the common unit while every
|
||||
// word gap cleared 0.31, so a tighter margin separates them without splitting words.
|
||||
const LATIN_CHUNK_WORD_EXCESS_RATIO = 0.2;
|
||||
const MIN_LATIN_CHUNK_EXCESS_GAP_SAMPLES = 6;
|
||||
const LATIN_TWO_GLYPH_WORD_NEXT_GAP_RATIO = 1.2;
|
||||
|
||||
function fragmentPosition(cue: AnnotatedSubtitleCue): AssFragmentPosition | null {
|
||||
for (const command of cue.overrides) {
|
||||
if (command.animated) continue;
|
||||
const name = command.name.toLowerCase();
|
||||
const args = command.args.split(',').map((value) => Number(value.trim()));
|
||||
if (
|
||||
name === 'pos' &&
|
||||
args.length >= 2 &&
|
||||
Number.isFinite(args[0]) &&
|
||||
Number.isFinite(args[1])
|
||||
) {
|
||||
return { x: args[0]!, y: args[1]! };
|
||||
}
|
||||
if (
|
||||
name === 'move' &&
|
||||
args.length >= 4 &&
|
||||
args.slice(0, 4).every((value) => Number.isFinite(value))
|
||||
) {
|
||||
return { x: (args[0]! + args[2]!) / 2, y: (args[1]! + args[3]!) / 2 };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function latinGlyphWidthWeight(glyph: string): number {
|
||||
if (/[ilIj]/u.test(glyph)) return 0.6;
|
||||
if (/[tfr]/u.test(glyph)) return 0.8;
|
||||
if (/[mwMW]/u.test(glyph)) return 1.4;
|
||||
if (/[A-Z]/u.test(glyph)) return 1.1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
function latinFragmentWidthWeight(text: string): number | null {
|
||||
if (!/^[A-Za-z0-9'’.,!?;:-]+$/u.test(text)) return null;
|
||||
const punctuationWeight = /^[A-Za-z0-9]['’.,!?;:-]$/u.test(text) ? 0.5 : 0.25;
|
||||
return [...text].reduce(
|
||||
(width, glyph) =>
|
||||
width + (/['’.,!?;:-]/u.test(glyph) ? punctuationWeight : latinGlyphWidthWeight(glyph)),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
function isSingleLatinGlyphFragment(text: string): boolean {
|
||||
return [...text].filter((glyph) => /[A-Za-z0-9]/u.test(glyph)).length <= 1;
|
||||
}
|
||||
|
||||
interface LatinFragmentGapMeasure {
|
||||
distance: number;
|
||||
meanWeight: number;
|
||||
}
|
||||
|
||||
function latinFragmentGapMeasure(
|
||||
previous: AssFragmentPart,
|
||||
current: AssFragmentPart,
|
||||
): LatinFragmentGapMeasure | null {
|
||||
const previousWeight = latinFragmentWidthWeight(previous.text);
|
||||
const currentWeight = latinFragmentWidthWeight(current.text);
|
||||
const previousPosition = fragmentPosition(previous.cue);
|
||||
const currentPosition = fragmentPosition(current.cue);
|
||||
if (previousWeight === null || currentWeight === null || !previousPosition || !currentPosition) {
|
||||
return null;
|
||||
}
|
||||
const xDistance = currentPosition.x - previousPosition.x;
|
||||
const yDistance = Math.abs(currentPosition.y - previousPosition.y);
|
||||
if (yDistance <= 2 && xDistance <= 0) return null;
|
||||
|
||||
// A wrapped authored line can return to the left on its next visual row. Preserve
|
||||
// that measured row transition as a separator without treating backwards movement
|
||||
// on the same row as a word gap.
|
||||
const distance = yDistance <= 2 ? xDistance : Math.abs(xDistance) + yDistance;
|
||||
return { distance, meanWeight: (previousWeight + currentWeight) / 2 };
|
||||
}
|
||||
|
||||
function normalizedLatinFragmentGap(
|
||||
previous: AssFragmentPart,
|
||||
current: AssFragmentPart,
|
||||
): number | null {
|
||||
const measure = latinFragmentGapMeasure(previous, current);
|
||||
return measure === null ? null : measure.distance / measure.meanWeight;
|
||||
}
|
||||
|
||||
function startsNewPositionedFragmentSequence(
|
||||
previous: AssFragmentPart,
|
||||
current: AssFragmentPart,
|
||||
): boolean {
|
||||
const previousPosition = fragmentPosition(previous.cue);
|
||||
const currentPosition = fragmentPosition(current.cue);
|
||||
return Boolean(
|
||||
previousPosition &&
|
||||
currentPosition &&
|
||||
Math.abs(currentPosition.y - previousPosition.y) <= 2 &&
|
||||
currentPosition.x <= previousPosition.x &&
|
||||
current.cue.startTime > previous.cue.startTime,
|
||||
);
|
||||
}
|
||||
|
||||
function commonLatinFragmentGap(values: readonly number[]): number {
|
||||
const sorted = [...values].sort((left, right) => left - right);
|
||||
// Romaji lines contain many short particles, so real word gaps can outnumber
|
||||
// within-word transitions. A lower quantile still represents ordinary glyph advance
|
||||
// while ignoring the narrowest character pair as an outlier.
|
||||
return sorted[Math.floor((sorted.length - 1) * 0.35)]!;
|
||||
}
|
||||
|
||||
function isLikelyTwoGlyphCapitalizedWord(options: {
|
||||
parts: readonly AssFragmentPart[];
|
||||
index: number;
|
||||
gap: number;
|
||||
wordGapThreshold: number;
|
||||
}): boolean {
|
||||
const first = options.parts[options.index - 1]!;
|
||||
const second = options.parts[options.index]!;
|
||||
if (!/^[A-Z]$/u.test(first.text) || !/^[a-z]$/u.test(second.text)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const precedingGap =
|
||||
options.index > 1 ? normalizedLatinFragmentGap(options.parts[options.index - 2]!, first) : null;
|
||||
const following = options.parts[options.index + 1];
|
||||
const followingGap = following ? normalizedLatinFragmentGap(second, following) : null;
|
||||
const startsAtWordBoundary =
|
||||
options.index === 1 || (precedingGap !== null && precedingGap > options.wordGapThreshold);
|
||||
|
||||
return (
|
||||
startsAtWordBoundary &&
|
||||
followingGap !== null &&
|
||||
followingGap > options.wordGapThreshold &&
|
||||
followingGap > options.gap * LATIN_TWO_GLYPH_WORD_NEXT_GAP_RATIO
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Character-by-character typesetting often omits literal spaces because the authored
|
||||
* word gap exists only in each glyph's `\pos`. Estimate the normal adjacent-glyph
|
||||
* advance within that one line, then preserve only materially larger horizontal gaps.
|
||||
* Normalizing each gap by the neighboring fragment widths supports both single glyphs
|
||||
* and multi-character karaoke syllables without guessing from the text itself. Per-glyph
|
||||
* runs use a wider safety margin because proportional fonts vary more than syllable chunks.
|
||||
*
|
||||
* The ratio test alone under-detects a word gap next to a wide fragment (`waves within`
|
||||
* measured across `s`/`w`, or `Choices presumably` across two three-letter chunks, both
|
||||
* normalize to nearly a common advance), so a gap also counts as a word boundary when its
|
||||
* advance exceeds the width-predicted advance by a material fraction of the line's common
|
||||
* unit -- a word space adds a roughly constant extra distance no matter how wide its
|
||||
* neighbors are. Chunk runs use a tighter margin than per-glyph runs because their
|
||||
* advances deviate less from the width model.
|
||||
*
|
||||
* A line may mix both conventions: one fragment carrying a literal space while its
|
||||
* neighbors rely on position alone. Whitespace-bearing fragments have no width weight, so
|
||||
* they drop out of the estimate and their own boundary comes from the authored space,
|
||||
* leaving the surrounding positional gaps to be recovered normally.
|
||||
*/
|
||||
function joinAssFragmentParts(parts: readonly AssFragmentPart[]): string {
|
||||
const normalizedGaps: number[] = [];
|
||||
for (let index = 1; index < parts.length; index += 1) {
|
||||
const gap = normalizedLatinFragmentGap(parts[index - 1]!, parts[index]!);
|
||||
if (gap !== null) normalizedGaps.push(gap);
|
||||
}
|
||||
const isGlyphRun = parts.every((part) => isSingleLatinGlyphFragment(part.text));
|
||||
const commonGap =
|
||||
normalizedGaps.length >= MIN_LATIN_POSITION_GAP_SAMPLES
|
||||
? commonLatinFragmentGap(normalizedGaps)
|
||||
: null;
|
||||
const wordGapThreshold =
|
||||
commonGap === null
|
||||
? Infinity
|
||||
: commonGap * (isGlyphRun ? LATIN_GLYPH_WORD_GAP_RATIO : LATIN_FRAGMENT_WORD_GAP_RATIO);
|
||||
|
||||
let text = parts[0]?.text ?? '';
|
||||
for (let index = 1; index < parts.length; index += 1) {
|
||||
const previous = parts[index - 1]!;
|
||||
const current = parts[index]!;
|
||||
const hasAuthoredSpace = /\s$/u.test(previous.text) || /^\s/u.test(current.text);
|
||||
const measure = latinFragmentGapMeasure(previous, current);
|
||||
const normalizedGap = measure === null ? null : measure.distance / measure.meanWeight;
|
||||
// A capital into lowercase is almost always a capitalized word's own first letters
|
||||
// (`S|miles`), and capitals overrun the width table too easily, so the excess rule
|
||||
// never fires there. A lone capital word like `I` is narrow enough for the ratio
|
||||
// test to catch its word gap on its own.
|
||||
const excessRatio = isGlyphRun ? LATIN_GLYPH_WORD_EXCESS_RATIO : LATIN_CHUNK_WORD_EXCESS_RATIO;
|
||||
const minimumExcessSamples = isGlyphRun
|
||||
? MIN_LATIN_GLYPH_EXCESS_GAP_SAMPLES
|
||||
: MIN_LATIN_CHUNK_EXCESS_GAP_SAMPLES;
|
||||
const hasAdvanceExcess =
|
||||
commonGap !== null &&
|
||||
normalizedGaps.length >= minimumExcessSamples &&
|
||||
measure !== null &&
|
||||
!(/^[A-Z]$/u.test(previous.text) && /^[a-z]$/u.test(current.text)) &&
|
||||
measure.distance - measure.meanWeight * commonGap > excessRatio * commonGap;
|
||||
const hasPositionedWordGap =
|
||||
startsNewPositionedFragmentSequence(previous, current) ||
|
||||
(normalizedGap !== null &&
|
||||
(normalizedGap > wordGapThreshold || hasAdvanceExcess) &&
|
||||
!isLikelyTwoGlyphCapitalizedWord({
|
||||
parts,
|
||||
index,
|
||||
gap: normalizedGap,
|
||||
wordGapThreshold,
|
||||
}));
|
||||
if (!hasAuthoredSpace && hasPositionedWordGap) {
|
||||
text += ' ';
|
||||
}
|
||||
text += current.text;
|
||||
}
|
||||
return text.trim();
|
||||
}
|
||||
|
||||
// A tall multi-part layout is only a visual grid when its parts read like tiling
|
||||
// rather than prose: a couple of texts repeated across many fragments (sign walls),
|
||||
// the same text re-shown at the same spot over time (countdown/animation frames),
|
||||
// nothing but scattered single glyphs, or cells aligned into table columns. Wrapped
|
||||
// lyric rows with repeated karaoke syllables and CC-style dialogue blocks (speaker
|
||||
// labels plus a sentence) share the same tall geometry but stay publishable.
|
||||
function looksLikeFragmentGridParts(parts: readonly AssFragmentPart[]): boolean {
|
||||
const positioned = parts
|
||||
.map((part) => ({
|
||||
text: part.text.trim(),
|
||||
layout: part.cue.assLayout,
|
||||
position: fragmentPosition(part.cue),
|
||||
startTime: part.cue.startTime,
|
||||
}))
|
||||
.filter((part) => part.text && part.layout?.kind === 'positioned');
|
||||
if (positioned.length === 0) return true;
|
||||
|
||||
const uniqueTexts = new Set(positioned.map((part) => part.text));
|
||||
if (uniqueTexts.size * 3 <= positioned.length) return true;
|
||||
|
||||
if (positioned.every((part) => [...part.text].length <= 1)) return true;
|
||||
|
||||
const seenPlacements = new Map<string, number>();
|
||||
for (const part of positioned) {
|
||||
if (part.layout?.kind !== 'positioned' || !part.position) continue;
|
||||
const placement = `${part.text}@${Math.round(part.position.x)},${Math.round(part.position.y)}`;
|
||||
const earlierStart = seenPlacements.get(placement);
|
||||
if (earlierStart !== undefined && Math.abs(part.startTime - earlierStart) > 0.01) {
|
||||
return true;
|
||||
}
|
||||
seenPlacements.set(placement, part.startTime);
|
||||
}
|
||||
|
||||
// Table cells align into columns: several x values each reused on multiple rows.
|
||||
// Requiring two such columns holding at least half the parts keeps a wrapped lyric
|
||||
// whose rows accidentally share one x coordinate out of the grid bucket.
|
||||
const columnRows = new Map<number, Set<number>>();
|
||||
for (const part of positioned) {
|
||||
if (!part.position) continue;
|
||||
const x = Math.round(part.position.x);
|
||||
const rows = columnRows.get(x) ?? new Set<number>();
|
||||
rows.add(Math.round(part.position.y));
|
||||
columnRows.set(x, rows);
|
||||
}
|
||||
let alignedColumns = 0;
|
||||
let alignedParts = 0;
|
||||
for (const part of positioned) {
|
||||
if (!part.position) continue;
|
||||
if ((columnRows.get(Math.round(part.position.x))?.size ?? 0) >= 2) alignedParts += 1;
|
||||
}
|
||||
for (const rows of columnRows.values()) {
|
||||
if (rows.size >= 2) alignedColumns += 1;
|
||||
}
|
||||
return alignedColumns >= 2 && alignedParts * 2 >= positioned.length;
|
||||
}
|
||||
|
||||
function reconstructedAssFragmentLayout(
|
||||
parts: readonly AssFragmentPart[],
|
||||
owner: AnnotatedSubtitleCue,
|
||||
@@ -404,7 +728,8 @@ function reconstructedAssFragmentLayout(
|
||||
|
||||
if (
|
||||
positionedPartCount >= MIN_FRAGMENT_LINE_PARTS &&
|
||||
maximumY - minimumY > MAX_FRAGMENT_LINE_VERTICAL_SPAN
|
||||
maximumY - minimumY > MAX_FRAGMENT_LINE_VERTICAL_SPAN &&
|
||||
looksLikeFragmentGridParts(parts)
|
||||
) {
|
||||
return { kind: 'fragment-grid', sourceOrder: owner.order };
|
||||
}
|
||||
@@ -479,6 +804,90 @@ function clusterAssFragmentEvents(
|
||||
return clusters;
|
||||
}
|
||||
|
||||
interface FragmentInterval {
|
||||
startTime: number;
|
||||
endTime: number;
|
||||
}
|
||||
|
||||
/** Event time ranges with repeated same-text, same-time layer copies collapsed. */
|
||||
function distinctFragmentIntervals(events: readonly AnnotatedSubtitleCue[]): FragmentInterval[] {
|
||||
const intervals: FragmentInterval[] = [];
|
||||
const previousEvents: AnnotatedSubtitleCue[] = [];
|
||||
for (const event of events) {
|
||||
const compactText = compactCueMatchText(event);
|
||||
const isLayerCopy = previousEvents.some(
|
||||
(previous) =>
|
||||
compactCueMatchText(previous) === compactText &&
|
||||
previous.startTime === event.startTime &&
|
||||
previous.endTime === event.endTime &&
|
||||
isRepeatedFragmentCopy(previous, event),
|
||||
);
|
||||
previousEvents.push(event);
|
||||
if (isLayerCopy) continue;
|
||||
intervals.push({ startTime: event.startTime, endTime: event.endTime });
|
||||
}
|
||||
return intervals.sort((a, b) => a.startTime - b.startTime || a.endTime - b.endTime);
|
||||
}
|
||||
|
||||
function intervalsNeverCoexist(intervals: readonly FragmentInterval[]): boolean {
|
||||
let latestEnd = -Infinity;
|
||||
for (const interval of intervals) {
|
||||
if (interval.startTime < latestEnd - 0.001) {
|
||||
return false;
|
||||
}
|
||||
latestEnd = Math.max(latestEnd, interval.endTime);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* A karaoke highlight sweep repaints one syllable at a time over an already-visible
|
||||
* lyric line: each event ends as the next begins, so the cluster's concatenated text is
|
||||
* never on screen as a whole. Publishing it would emit rolling partial copies of the
|
||||
* lyric ("to sou omo" beside "akenakute ii to sou omotteta"). Layer copies share one
|
||||
* placement and timing, so the test is whether any two distinct placements coexist.
|
||||
*/
|
||||
function isProgressiveHighlightSweep(events: readonly AnnotatedSubtitleCue[]): boolean {
|
||||
const intervals = distinctFragmentIntervals(events);
|
||||
return intervals.length >= 2 && intervalsNeverCoexist(intervals);
|
||||
}
|
||||
|
||||
/**
|
||||
* Timing clusters split a long sweep unevenly, leaving stragglers the per-cluster check
|
||||
* cannot judge: a two-event tail reconstructs on relaxed evidence, and a lone held
|
||||
* syllable stays raw and publishes as its own flickering cue. When an entire style group
|
||||
* reads as one chained repaint -- many short positioned animated fragments, no two ever
|
||||
* on screen together, transitions mostly back-to-back -- the whole group is highlight
|
||||
* decoration and none of it is publishable text. Independent one-off signs sharing a
|
||||
* style stay published: they are few, longer, or separated by real gaps.
|
||||
*/
|
||||
function isProgressiveHighlightSweepGroup(events: readonly AnnotatedSubtitleCue[]): boolean {
|
||||
if (
|
||||
events.length < MIN_FRAGMENT_LINE_EVENTS ||
|
||||
!events.every((event) => fragmentPlacementAnchors(event).size > 0) ||
|
||||
!hasAssAnimationEvidence(events)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const lengths = events
|
||||
.map((event) => compactCueMatchText(event).length)
|
||||
.sort((left, right) => left - right);
|
||||
if ((lengths[Math.floor(lengths.length / 2)] ?? Infinity) > MAX_FRAGMENT_MEDIAN_LENGTH) {
|
||||
return false;
|
||||
}
|
||||
const intervals = distinctFragmentIntervals(events);
|
||||
if (intervals.length < 2 || !intervalsNeverCoexist(intervals)) {
|
||||
return false;
|
||||
}
|
||||
let abutting = 0;
|
||||
for (let index = 1; index < intervals.length; index += 1) {
|
||||
if (Math.abs(intervals[index]!.startTime - intervals[index - 1]!.endTime) <= 0.1) {
|
||||
abutting += 1;
|
||||
}
|
||||
}
|
||||
return abutting * 2 >= intervals.length - 1;
|
||||
}
|
||||
|
||||
function decodeSingleAssFragment(cue: AnnotatedSubtitleCue): string | null {
|
||||
const visibleLines = decodeSubtitleCueText(cue.rawText)
|
||||
.split('\n')
|
||||
@@ -522,10 +931,7 @@ function reconstructAssFragmentLine(
|
||||
return null;
|
||||
}
|
||||
|
||||
const text = parts
|
||||
.map((part) => part.text)
|
||||
.join('')
|
||||
.trim();
|
||||
const text = joinAssFragmentParts(parts);
|
||||
if (!text) {
|
||||
return null;
|
||||
}
|
||||
@@ -547,6 +953,197 @@ function reconstructAssFragmentLine(
|
||||
};
|
||||
}
|
||||
|
||||
// `\fnSplit splat splodge` tokenizes as name `fnSplit` + args `splat splodge`, while
|
||||
// `\fnArial` is all name and `\fn04b` is all args, so the font is both pieces rejoined.
|
||||
function staticFontOverride(cue: AnnotatedSubtitleCue): string | null {
|
||||
let font: string | null = null;
|
||||
for (const command of cue.overrides) {
|
||||
if (command.animated || !command.name.toLowerCase().startsWith('fn')) continue;
|
||||
font = [command.name.slice(2), command.args].filter(Boolean).join(' ').trim().toLowerCase();
|
||||
}
|
||||
return font;
|
||||
}
|
||||
|
||||
const MIN_TEXTURE_GLYPH_RUN = 8;
|
||||
const MIN_TEXTURE_ALPHA_OVERRIDES = 6;
|
||||
const MIN_TEXTURE_LAYER_ALPHA = 0xe0;
|
||||
const ASS_ALPHA_VALUE_PATTERN = /^&?H([0-9a-f]{1,2})&?$/iu;
|
||||
|
||||
function hasStaticOverride(cue: AnnotatedSubtitleCue, expectedName: string): boolean {
|
||||
return cue.overrides.some(
|
||||
(command) => !command.animated && command.name.toLowerCase() === expectedName,
|
||||
);
|
||||
}
|
||||
|
||||
function isClippedRepeatedGlyphFragment(cue: AnnotatedSubtitleCue): boolean {
|
||||
const glyphs = [...compactCueMatchText(cue)];
|
||||
return (
|
||||
glyphs.length > 0 &&
|
||||
glyphs.every((glyph) => glyph === glyphs[0]) &&
|
||||
(hasStaticOverride(cue, 'clip') || hasStaticOverride(cue, 'iclip'))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Some ASS signs build image textures from clipped placeholder glyphs, optionally through
|
||||
* a texture font. A long clipped single-glyph run or frequent changing secondary alpha
|
||||
* tags identifies the effect without guessing from its visible text or font name.
|
||||
*/
|
||||
function isAssTextureSeed(cue: AnnotatedSubtitleCue): boolean {
|
||||
if (fragmentPosition(cue) === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const glyphs = [...compactCueMatchText(cue)];
|
||||
const isClippedRepeatedGlyphRun =
|
||||
glyphs.length >= MIN_TEXTURE_GLYPH_RUN && isClippedRepeatedGlyphFragment(cue);
|
||||
if (isClippedRepeatedGlyphRun) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (staticFontOverride(cue) === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const secondaryAlpha = cue.overrides.filter(
|
||||
(command) => !command.animated && command.name.toLowerCase() === '2a',
|
||||
);
|
||||
if (secondaryAlpha.length < MIN_TEXTURE_ALPHA_OVERRIDES) {
|
||||
return false;
|
||||
}
|
||||
const alphaValues = secondaryAlpha.map((command) => command.args.toLowerCase());
|
||||
return new Set(alphaValues).size >= 2;
|
||||
}
|
||||
|
||||
function staticGlobalAlpha(cue: AnnotatedSubtitleCue): number | null {
|
||||
let alpha: number | null = null;
|
||||
for (const command of cue.overrides) {
|
||||
if (command.animated || command.name.toLowerCase() !== 'alpha') continue;
|
||||
const match = ASS_ALPHA_VALUE_PATTERN.exec(command.args.trim());
|
||||
const alphaValue = match?.[1];
|
||||
if (alphaValue !== undefined) {
|
||||
alpha = Number.parseInt(alphaValue, 16);
|
||||
}
|
||||
}
|
||||
return alpha;
|
||||
}
|
||||
|
||||
function isNearlyTransparentPositionedText(cue: AnnotatedSubtitleCue): boolean {
|
||||
const alpha = staticGlobalAlpha(cue);
|
||||
return (
|
||||
alpha !== null &&
|
||||
alpha >= MIN_TEXTURE_LAYER_ALPHA &&
|
||||
staticFontOverride(cue) !== null &&
|
||||
fragmentPosition(cue) !== null
|
||||
);
|
||||
}
|
||||
|
||||
function assFontTextureGroupKey(cue: AnnotatedSubtitleCue): string | null {
|
||||
const font = staticFontOverride(cue);
|
||||
return font === null ? null : `${cue.style}\0${cue.startTime}\0${cue.endTime}\0${font}`;
|
||||
}
|
||||
|
||||
function assTextureTimingGroupKey(cue: AnnotatedSubtitleCue): string {
|
||||
return `${cue.style}\0${cue.startTime}\0${cue.endTime}`;
|
||||
}
|
||||
|
||||
function removeAssFontTextureEvents(events: ParsedAssEvents): ParsedAssEvents {
|
||||
const seeds = events.dialogue.filter(isAssTextureSeed);
|
||||
const seedSet = new Set(seeds);
|
||||
// Short pieces can share the seeded font effect under another actor without carrying
|
||||
// enough tags to identify themselves. The exact style, time, and font group catches
|
||||
// those pieces without inspecting their content.
|
||||
const textureGroups = new Set(
|
||||
seeds.map(assFontTextureGroupKey).filter((key): key is string => key !== null),
|
||||
);
|
||||
const noFontTextureTimings = new Set(
|
||||
seeds
|
||||
.filter((seed) => staticFontOverride(seed) === null)
|
||||
.map((seed) => assTextureTimingGroupKey(seed)),
|
||||
);
|
||||
// Some signs switch actor and font between the texture mask and its payload. A nearly
|
||||
// transparent text event that overlaps a proven seed in the same style is another input
|
||||
// to that visual effect. Opaque authored text in the same sign remains publishable.
|
||||
const seedsByStyle = new Map<string, AnnotatedSubtitleCue[]>();
|
||||
for (const seed of seeds) {
|
||||
const styleSeeds = seedsByStyle.get(seed.style);
|
||||
if (styleSeeds) {
|
||||
styleSeeds.push(seed);
|
||||
} else {
|
||||
seedsByStyle.set(seed.style, [seed]);
|
||||
}
|
||||
}
|
||||
const seedIndexesByStyle = new Map(
|
||||
[...seedsByStyle].map(([style, styleSeeds]) => [style, buildAssEventGroupIndex(styleSeeds)]),
|
||||
);
|
||||
|
||||
return {
|
||||
dialogue: events.dialogue.filter((cue) => {
|
||||
if (seedSet.has(cue)) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
staticFontOverride(cue) === null &&
|
||||
noFontTextureTimings.has(assTextureTimingGroupKey(cue)) &&
|
||||
isClippedRepeatedGlyphFragment(cue)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const key = assFontTextureGroupKey(cue);
|
||||
if (key !== null && textureGroups.has(key)) {
|
||||
return false;
|
||||
}
|
||||
if (!isNearlyTransparentPositionedText(cue)) {
|
||||
return true;
|
||||
}
|
||||
const styleSeedIndex = seedIndexesByStyle.get(cue.style);
|
||||
return (
|
||||
styleSeedIndex === undefined ||
|
||||
eventsOverlappingWindow(styleSeedIndex, cue.startTime, cue.endTime).length === 0
|
||||
);
|
||||
}),
|
||||
comments: events.comments,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated lyric effects often layer decoration over the real syllables: single letters
|
||||
* positioned above each glyph, animated in, and rendered through a `\fn` override to a
|
||||
* symbol font where `a` draws as a sparkle rather than a letter. Reading them as text
|
||||
* corrupts the reconstructed line (`sotto mimi ni ateru to` gains a trailing `a z x`).
|
||||
* Within one style/name group, a font used only for scattered animated single glyphs --
|
||||
* while the group's actual text renders in another font -- marks those events as
|
||||
* decoration rather than dialogue.
|
||||
*/
|
||||
function decorativeGlyphEvents(events: readonly AnnotatedSubtitleCue[]): Set<AnnotatedSubtitleCue> {
|
||||
const byFont = new Map<string, AnnotatedSubtitleCue[]>();
|
||||
for (const cue of events) {
|
||||
const font = staticFontOverride(cue);
|
||||
if (font === null) continue;
|
||||
const group = byFont.get(font);
|
||||
if (group) {
|
||||
group.push(cue);
|
||||
} else {
|
||||
byFont.set(font, [cue]);
|
||||
}
|
||||
}
|
||||
|
||||
const decorative = new Set<AnnotatedSubtitleCue>();
|
||||
for (const fontEvents of byFont.values()) {
|
||||
if (fontEvents.length * 2 >= events.length) continue;
|
||||
const allScatteredGlyphs = fontEvents.every(
|
||||
(cue) =>
|
||||
[...compactCueMatchText(cue)].length === 1 &&
|
||||
fragmentPosition(cue) !== null &&
|
||||
hasAssTemporalOverride(cue.overrides),
|
||||
);
|
||||
if (allScatteredGlyphs) {
|
||||
fontEvents.forEach((cue) => decorative.add(cue));
|
||||
}
|
||||
}
|
||||
return decorative;
|
||||
}
|
||||
|
||||
function recoverFragmentOnlyAssLines(dialogue: AnnotatedSubtitleCue[]): AnnotatedSubtitleCue[] {
|
||||
const groups = new Map<string, AnnotatedSubtitleCue[]>();
|
||||
for (const cue of dialogue) {
|
||||
@@ -565,16 +1162,44 @@ function recoverFragmentOnlyAssLines(dialogue: AnnotatedSubtitleCue[]): Annotate
|
||||
const recovered: AnnotatedSubtitleCue[] = [];
|
||||
const suppressed = new Set<AnnotatedSubtitleCue>();
|
||||
for (const events of groups.values()) {
|
||||
for (const cluster of clusterAssFragmentEvents(events)) {
|
||||
const decorative = decorativeGlyphEvents(events);
|
||||
const lineEvents = decorative.size ? events.filter((event) => !decorative.has(event)) : events;
|
||||
if (isProgressiveHighlightSweepGroup(lineEvents)) {
|
||||
lineEvents.forEach((event) => suppressed.add(event));
|
||||
const spanStart = Math.min(...lineEvents.map((event) => event.startTime));
|
||||
const spanEnd = Math.max(...lineEvents.map((event) => event.endTime));
|
||||
for (const overlay of decorative) {
|
||||
if (overlay.startTime < spanEnd && overlay.endTime > spanStart) {
|
||||
suppressed.add(overlay);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (const cluster of clusterAssFragmentEvents(lineEvents)) {
|
||||
const line = reconstructAssFragmentLine(cluster.events);
|
||||
if (!line) {
|
||||
continue;
|
||||
}
|
||||
// A sweep only re-highlights the lyric it decorates: hide its events without
|
||||
// publishing the reconstruction.
|
||||
if (isProgressiveHighlightSweep(cluster.events)) {
|
||||
cluster.events.forEach((event) => suppressed.add(event));
|
||||
continue;
|
||||
}
|
||||
recovered.push(line);
|
||||
cluster.events.forEach((event) => suppressed.add(event));
|
||||
// Decoration is timed to the line it overlays, so it disappears with the line's
|
||||
// full animation span. Decoration outside any recovered span stays published.
|
||||
const spanStart = line.animationStartTime ?? line.startTime;
|
||||
const spanEnd = line.animationEndTime ?? line.endTime;
|
||||
for (const overlay of decorative) {
|
||||
if (overlay.startTime < spanEnd && overlay.endTime > spanStart) {
|
||||
suppressed.add(overlay);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (recovered.length === 0) {
|
||||
if (recovered.length === 0 && suppressed.size === 0) {
|
||||
return dialogue;
|
||||
}
|
||||
return [...dialogue.filter((cue) => !suppressed.has(cue)), ...recovered].sort(
|
||||
@@ -911,12 +1536,12 @@ function parseAnnotatedAssEvents(content: string): ParsedAssEvents {
|
||||
|
||||
const startTime = parseAssTimestamp(fields[fieldIndex.start]!);
|
||||
const endTime = parseAssTimestamp(fields[fieldIndex.end]!);
|
||||
if (startTime === null || endTime === null) {
|
||||
if (startTime === null || endTime === null || endTime <= startTime) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const rawText = fields.slice(fieldIndex.text).join(',');
|
||||
const text = sanitizeSubtitleCueText(rawText);
|
||||
const text = sanitizeAssCueText(rawText);
|
||||
if (!text) {
|
||||
continue;
|
||||
}
|
||||
@@ -951,7 +1576,8 @@ function parseAnnotatedAssEvents(content: string): ParsedAssEvents {
|
||||
}
|
||||
|
||||
function parseAnnotatedAssCues(content: string): AnnotatedSubtitleCue[] {
|
||||
return recoverFragmentOnlyAssLines(recoverCanonicalAssEvents(parseAnnotatedAssEvents(content)));
|
||||
const events = removeAssFontTextureEvents(parseAnnotatedAssEvents(content));
|
||||
return recoverFragmentOnlyAssLines(recoverCanonicalAssEvents(events));
|
||||
}
|
||||
|
||||
export function parseAssCues(content: string): SubtitleCue[] {
|
||||
|
||||
@@ -2059,7 +2059,6 @@ const cachedInternalSubtitleTrackExtractor = createCachedInternalSubtitleTrackEx
|
||||
const detectRemoteMediaPath = createRemoteMediaPathDetector();
|
||||
const resolveActiveSubtitleSidebarSourceHandler = createResolveActiveSubtitleSidebarSourceHandler({
|
||||
getFfmpegPath: () => configService.getConfig().subsync.ffmpeg_path.trim() || 'ffmpeg',
|
||||
isRemoteMediaPath: detectRemoteMediaPath,
|
||||
extractInternalSubtitleTrack: (ffmpegPath, videoPath, track) =>
|
||||
cachedInternalSubtitleTrackExtractor.extract(ffmpegPath, videoPath, track),
|
||||
logDebug: (message) => logger.debug(message),
|
||||
|
||||
@@ -864,15 +864,16 @@ test('subtitle sidebar snapshot prefers cached YouTube parsed cues before active
|
||||
);
|
||||
});
|
||||
|
||||
test('main process guards internal subtitle extraction with the remote media detector', () => {
|
||||
test('main process extracts internal subtitle tracks without a network-mount guard', () => {
|
||||
const source = readMainSource();
|
||||
const resolverWiring = source.match(
|
||||
/const resolveActiveSubtitleSidebarSourceHandler = createResolveActiveSubtitleSidebarSourceHandler\(\{(?<body>[\s\S]*?)\n\}\);/,
|
||||
)?.groups?.body;
|
||||
|
||||
assert.ok(resolverWiring);
|
||||
assert.match(source, /const detectRemoteMediaPath = createRemoteMediaPathDetector\(\);/);
|
||||
assert.match(resolverWiring, /isRemoteMediaPath:\s*detectRemoteMediaPath/);
|
||||
// Network-mounted files are extracted like local ones; only remote URLs skip
|
||||
// extraction, handled inside the resolver itself.
|
||||
assert.doesNotMatch(resolverWiring, /isRemoteMediaPath/);
|
||||
assert.match(
|
||||
resolverWiring,
|
||||
/extractInternalSubtitleTrack:[\s\S]*cachedInternalSubtitleTrackExtractor\.extract/,
|
||||
|
||||
@@ -46,7 +46,10 @@ export type InternalSubtitleTrackExtractor = (
|
||||
track: MpvSubtitleTrackLike,
|
||||
) => Promise<ExtractedInternalSubtitleTrack | null>;
|
||||
|
||||
const DEFAULT_EXTRACTION_TIMEOUT_MS = 30_000;
|
||||
// Subtitle packets are interleaved through the container, so extraction reads the
|
||||
// entire file. Network mounts move ~100 MB/s on gigabit, so large Bluray remuxes
|
||||
// need well over 30 seconds.
|
||||
const DEFAULT_EXTRACTION_TIMEOUT_MS = 120_000;
|
||||
|
||||
export function parseTrackId(value: unknown): number | null {
|
||||
if (typeof value === 'number' && Number.isInteger(value) && value >= 0) {
|
||||
|
||||
@@ -426,6 +426,13 @@ test('canonical ASS cues replace live glyph spam for display, history, and immer
|
||||
text: '飛び越えてみたくて',
|
||||
source: 'canonical-ass',
|
||||
},
|
||||
{
|
||||
startTime: 10,
|
||||
endTime: 12,
|
||||
text: 'MaidCafeMaidCafe',
|
||||
source: 'reconstructed-ass',
|
||||
assLayout: { kind: 'fragment-grid', sourceOrder: 2 },
|
||||
},
|
||||
],
|
||||
currentMediaPath: '/video.mkv',
|
||||
currentSubText: '',
|
||||
@@ -503,6 +510,11 @@ test('canonical ASS cues replace live glyph spam for display, history, and immer
|
||||
handlers.recordSubtitleTiming('今', 0.8, 1.5);
|
||||
|
||||
assert.deepEqual(timing.slice(4), [{ text: '今 手にある物差しでは', start: 1.2, end: 3.8 }]);
|
||||
|
||||
handlers.recordImmersionSubtitleLine('Maid\nCafe', 10, 12);
|
||||
handlers.recordSubtitleTiming('Maid\nCafe', 10, 12);
|
||||
assert.equal(immersion.length, 3);
|
||||
assert.equal(timing.length, 5);
|
||||
});
|
||||
|
||||
test('subtitle-track changes stop stale canonical cues from substituting immediately', () => {
|
||||
|
||||
@@ -218,6 +218,9 @@ export function createBuildBindMpvMainEventHandlersMainDepsHandler(deps: {
|
||||
return;
|
||||
}
|
||||
text = stripFragmentsForRecording(text, start);
|
||||
if (!text.trim()) {
|
||||
return;
|
||||
}
|
||||
if (!immersionLineDedupGate.shouldRecord({ text, startSec: start, endSec: end })) {
|
||||
return;
|
||||
}
|
||||
@@ -228,8 +231,12 @@ export function createBuildBindMpvMainEventHandlersMainDepsHandler(deps: {
|
||||
const secondaryText = deps.appState.mpvClient?.currentSecondarySubText || undefined;
|
||||
const canonical = resolveCanonicalSample(text, start);
|
||||
if (!canonical) {
|
||||
const recordableText = stripFragmentsForRecording(text, start);
|
||||
if (!recordableText.trim()) {
|
||||
return;
|
||||
}
|
||||
deps.appState.subtitleTimingTracker?.recordSubtitle?.(
|
||||
stripFragmentsForRecording(text, start),
|
||||
recordableText,
|
||||
start,
|
||||
end,
|
||||
secondaryText,
|
||||
|
||||
@@ -64,6 +64,34 @@ test('resolvePrimarySubtitleText combines unique simultaneous parsed cues', () =
|
||||
);
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText removes duplicate lines across multiline parsed cues', () => {
|
||||
assert.equal(
|
||||
resolvePrimarySubtitleText({
|
||||
liveText: 'First line\nSecond line\nFirst line',
|
||||
currentTimeSec: 2,
|
||||
cues: [
|
||||
{ startTime: 1, endTime: 3, text: 'First line\nSecond line' },
|
||||
{ startTime: 1, endTime: 3, text: 'First line' },
|
||||
],
|
||||
}),
|
||||
'First line\nSecond line',
|
||||
);
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText removes equivalent full-width duplicate lines', () => {
|
||||
assert.equal(
|
||||
resolvePrimarySubtitleText({
|
||||
liveText: '20分53秒\n20分53秒',
|
||||
currentTimeSec: 2,
|
||||
cues: [
|
||||
{ startTime: 1, endTime: 3, text: '20分53秒' },
|
||||
{ startTime: 1, endTime: 3, text: '20分53秒' },
|
||||
],
|
||||
}),
|
||||
'20分53秒',
|
||||
);
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText collapses whitespace variants of one ASS lyric', () => {
|
||||
const ass = [
|
||||
'[Events]',
|
||||
@@ -152,6 +180,73 @@ test('resolvePrimarySubtitleText keeps concurrent dialogue that is not part of t
|
||||
assert.equal(text, '普通のセリフ\n今\n手にある');
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText combines parsed dialogue with a reconstructed lyric', () => {
|
||||
const text = resolvePrimarySubtitleText({
|
||||
liveText: '普通のセリフ\n今\n今\n手\n手\nにある\nにある',
|
||||
currentTimeSec: 2,
|
||||
cues: [
|
||||
{ startTime: 1, endTime: 3, text: '普通のセリフ' },
|
||||
{
|
||||
startTime: 1.2,
|
||||
endTime: 3.8,
|
||||
text: '今 手にある',
|
||||
source: 'reconstructed-ass',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
assert.equal(text, '普通のセリフ\n今 手にある');
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText uses fragment grids only to account for live sign pieces', () => {
|
||||
const text = resolvePrimarySubtitleText({
|
||||
liveText: 'Ordinary dialogue\nMaid\nCafe',
|
||||
currentTimeSec: 2,
|
||||
cues: [
|
||||
{ startTime: 1, endTime: 3, text: 'Ordinary dialogue' },
|
||||
{
|
||||
startTime: 1,
|
||||
endTime: 3,
|
||||
text: 'MaidCafeMaidCafe',
|
||||
source: 'reconstructed-ass',
|
||||
assLayout: { kind: 'fragment-grid', sourceOrder: 2 },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
assert.equal(text, 'Ordinary dialogue');
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText drops malformed ASS control debris from live text', () => {
|
||||
const cues = parseSubtitleCues(
|
||||
[
|
||||
'[Events]',
|
||||
'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text',
|
||||
'Dialogue: 0,0:00:01.00,0:00:03.00,Default,,0,0,0,,Visible line',
|
||||
].join('\n'),
|
||||
'test.ass',
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
resolvePrimarySubtitleText({
|
||||
liveText: 'Visible line\n\\\n{\\fr0',
|
||||
currentTimeSec: 2,
|
||||
cues,
|
||||
}),
|
||||
'Visible line',
|
||||
);
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText preserves SRT text that resembles ASS control debris', () => {
|
||||
const liveText = 'Visible line\n\\\n{\\fr0';
|
||||
const cues = parseSubtitleCues(
|
||||
['1', '00:00:01,000 --> 00:00:03,000', liveText].join('\n'),
|
||||
'test.srt',
|
||||
);
|
||||
|
||||
assert.equal(resolvePrimarySubtitleText({ liveText, currentTimeSec: 2, cues }), liveText);
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText keeps a fresh line starting just after the animation ended', () => {
|
||||
const text = resolvePrimarySubtitleText({
|
||||
liveText: '次のセリフ',
|
||||
@@ -382,3 +477,23 @@ test('resolveCanonicalPrimarySubtitle picks the cue its fragments spell, not the
|
||||
'今 手にある',
|
||||
);
|
||||
});
|
||||
|
||||
test('resolvePrimarySubtitleText suppresses a live glyph wall when no cues are available', () => {
|
||||
const wall = [...'wansdumretoikhI'].join('\n');
|
||||
assert.equal(
|
||||
resolvePrimarySubtitleText({ liveText: `${wall}\ntai`, currentTimeSec: 1355, cues: null }),
|
||||
'',
|
||||
);
|
||||
});
|
||||
|
||||
test('stripCanonicalFragmentLines drops a live glyph wall with no nearby canonical cues', () => {
|
||||
const wall = [...'wansdumretoikhI'].join('\n');
|
||||
assert.equal(
|
||||
stripCanonicalFragmentLines({
|
||||
liveText: `${wall}\nそれよりも ノート…`,
|
||||
currentTimeSec: 1355,
|
||||
cues: [],
|
||||
}),
|
||||
'それよりも ノート…',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import type { SubtitleCue } from '../../types';
|
||||
import {
|
||||
removeAssControlDebrisLines,
|
||||
removeLiveGlyphFragmentLines,
|
||||
} from '../../core/services/ass-text';
|
||||
|
||||
// Slack on top of each cue's recorded animation envelope, for time-pos observation
|
||||
// staleness and small user sub-delay offsets. The envelope itself covers how far
|
||||
@@ -13,6 +17,15 @@ export interface ResolvedPrimarySubtitle {
|
||||
cues: SubtitleCue[];
|
||||
}
|
||||
|
||||
function cuesUseAssSyntax(cues: readonly SubtitleCue[] | null | undefined): boolean {
|
||||
return (cues ?? []).some(
|
||||
(cue) =>
|
||||
cue.source === 'canonical-ass' ||
|
||||
cue.source === 'reconstructed-ass' ||
|
||||
cue.assLayout !== undefined,
|
||||
);
|
||||
}
|
||||
|
||||
function animationSpan(cue: SubtitleCue): { start: number; end: number } {
|
||||
return {
|
||||
start: cue.animationStartTime ?? cue.startTime,
|
||||
@@ -23,9 +36,13 @@ function animationSpan(cue: SubtitleCue): { start: number; end: number } {
|
||||
function nearbyCanonicalCues(
|
||||
cues: readonly SubtitleCue[] | null | undefined,
|
||||
currentTimeSec: number,
|
||||
includeFragmentGrids = false,
|
||||
): SubtitleCue[] {
|
||||
return (cues ?? []).filter((cue) => {
|
||||
if (cue.source !== 'canonical-ass' && cue.source !== 'reconstructed-ass') {
|
||||
if (
|
||||
(cue.source !== 'canonical-ass' && cue.source !== 'reconstructed-ass') ||
|
||||
(!includeFragmentGrids && cue.assLayout?.kind === 'fragment-grid')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
const span = animationSpan(cue);
|
||||
@@ -37,7 +54,7 @@ function nearbyCanonicalCues(
|
||||
}
|
||||
|
||||
function compactWhitespace(text: string): string {
|
||||
return text.replace(/\s+/gu, '');
|
||||
return text.normalize('NFKC').replace(/\s+/gu, '');
|
||||
}
|
||||
|
||||
// ASS layers can encode the same visible spacing with ordinary, hard, or
|
||||
@@ -47,10 +64,12 @@ function uniqueCueTexts(cues: readonly SubtitleCue[]): string[] {
|
||||
const texts: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
for (const cue of cues) {
|
||||
const compactText = compactWhitespace(cue.text);
|
||||
if (seen.has(compactText)) continue;
|
||||
seen.add(compactText);
|
||||
texts.push(cue.text);
|
||||
for (const line of cue.text.split('\n')) {
|
||||
const compactText = compactWhitespace(line);
|
||||
if (!compactText || seen.has(compactText)) continue;
|
||||
seen.add(compactText);
|
||||
texts.push(line);
|
||||
}
|
||||
}
|
||||
return texts;
|
||||
}
|
||||
@@ -87,18 +106,37 @@ function resolveActiveParsedPrimarySubtitle(options: {
|
||||
return false;
|
||||
}
|
||||
const cueSegments = compactLineSegments(cue.text);
|
||||
return cueSegments.length > 0 && cueSegments.every((segment) => liveSegmentSet.has(segment));
|
||||
if (cueSegments.length === 0) return false;
|
||||
if (cue.source === 'canonical-ass' || cue.source === 'reconstructed-ass') {
|
||||
return liveSegments.some((segment) =>
|
||||
cueSegments.some((cueSegment) => cueSegment.includes(segment)),
|
||||
);
|
||||
}
|
||||
return cueSegments.every((segment) => liveSegmentSet.has(segment));
|
||||
});
|
||||
if (selected.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsedSegmentSet = new Set(selected.flatMap((cue) => compactLineSegments(cue.text)));
|
||||
if (!liveSegments.every((segment) => parsedSegmentSet.has(segment))) {
|
||||
const parsedSegments = selected.flatMap((cue) =>
|
||||
compactLineSegments(cue.text).map((segment) => ({
|
||||
segment,
|
||||
recovered: cue.source === 'canonical-ass' || cue.source === 'reconstructed-ass',
|
||||
})),
|
||||
);
|
||||
if (
|
||||
!liveSegments.every((liveSegment) =>
|
||||
parsedSegments.some(({ segment, recovered }) =>
|
||||
recovered ? segment.includes(liveSegment) : segment === liveSegment,
|
||||
),
|
||||
)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const texts = uniqueCueTexts(selected);
|
||||
// Dense sign grids still explain their raw mpv fragments, but are visual
|
||||
// typesetting rather than a publishable subtitle line.
|
||||
const texts = uniqueCueTexts(selected.filter((cue) => cue.assLayout?.kind !== 'fragment-grid'));
|
||||
return {
|
||||
text: texts.join('\n'),
|
||||
startTime: Math.min(...selected.map((cue) => cue.startTime)),
|
||||
@@ -179,8 +217,9 @@ export function resolveCanonicalPrimarySubtitle(options: {
|
||||
/**
|
||||
* Live text with generated-animation fragment lines removed. Recording paths use this
|
||||
* when full canonical substitution declined -- concurrent dialogue during an insert
|
||||
* song: the dialogue is worth recording, the glyph fragments beside it are not. Returns
|
||||
* the input unchanged when no canonical cue is near or nothing non-fragment remains.
|
||||
* song: the dialogue is worth recording, the glyph fragments beside it are not. An
|
||||
* all-fragment visual grid becomes empty; other all-matched input remains unchanged as a
|
||||
* defensive fallback.
|
||||
*/
|
||||
export function stripCanonicalFragmentLines(options: {
|
||||
liveText: string;
|
||||
@@ -188,18 +227,20 @@ export function stripCanonicalFragmentLines(options: {
|
||||
cues: readonly SubtitleCue[] | null | undefined;
|
||||
}): string {
|
||||
if (!Number.isFinite(options.currentTimeSec)) {
|
||||
return options.liveText;
|
||||
return removeLiveGlyphFragmentLines(options.liveText);
|
||||
}
|
||||
const nearby = nearbyCanonicalCues(options.cues, options.currentTimeSec);
|
||||
const nearby = nearbyCanonicalCues(options.cues, options.currentTimeSec, true);
|
||||
if (nearby.length === 0) {
|
||||
return options.liveText;
|
||||
return removeLiveGlyphFragmentLines(options.liveText);
|
||||
}
|
||||
const compactCues = nearby.map((cue) => compactWhitespace(cue.text));
|
||||
const kept = options.liveText.split('\n').filter((line) => {
|
||||
const compact = compactWhitespace(line);
|
||||
return compact && !compactCues.some((cueText) => cueText.includes(compact));
|
||||
});
|
||||
return kept.length > 0 ? kept.join('\n') : options.liveText;
|
||||
if (kept.length > 0) return removeLiveGlyphFragmentLines(kept.join('\n'));
|
||||
if (nearby.some((cue) => cue.assLayout?.kind === 'fragment-grid')) return '';
|
||||
return removeLiveGlyphFragmentLines(options.liveText);
|
||||
}
|
||||
|
||||
export function resolvePrimarySubtitleText(options: {
|
||||
@@ -207,16 +248,19 @@ export function resolvePrimarySubtitleText(options: {
|
||||
currentTimeSec: number;
|
||||
cues: readonly SubtitleCue[] | null | undefined;
|
||||
}): string {
|
||||
if (!options.liveText.trim()) {
|
||||
return options.liveText;
|
||||
const liveText = cuesUseAssSyntax(options.cues)
|
||||
? removeAssControlDebrisLines(options.liveText)
|
||||
: options.liveText;
|
||||
if (!liveText.trim()) {
|
||||
return liveText;
|
||||
}
|
||||
return (
|
||||
resolveCanonicalPrimarySubtitle({
|
||||
liveText: options.liveText,
|
||||
liveText,
|
||||
currentTimeSec: options.currentTimeSec,
|
||||
cues: options.cues,
|
||||
})?.text ??
|
||||
resolveActiveParsedPrimarySubtitle(options)?.text ??
|
||||
options.liveText
|
||||
resolveActiveParsedPrimarySubtitle({ ...options, liveText })?.text ??
|
||||
removeLiveGlyphFragmentLines(liveText)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,32 @@ test('findActiveSubtitleText combines unique simultaneous parsed cues', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('findActiveSubtitleText removes duplicate lines across multiline cues', () => {
|
||||
assert.equal(
|
||||
findActiveSubtitleText(
|
||||
[
|
||||
{ startTime: 1, endTime: 3, text: 'First line\nSecond line' },
|
||||
{ startTime: 1, endTime: 3, text: 'First line' },
|
||||
],
|
||||
2,
|
||||
),
|
||||
'First line\nSecond line',
|
||||
);
|
||||
});
|
||||
|
||||
test('findActiveSubtitleText removes equivalent full-width duplicate lines', () => {
|
||||
assert.equal(
|
||||
findActiveSubtitleText(
|
||||
[
|
||||
{ startTime: 1, endTime: 3, text: '真白~' },
|
||||
{ startTime: 1, endTime: 3, text: '真白~' },
|
||||
],
|
||||
2,
|
||||
),
|
||||
'真白~',
|
||||
);
|
||||
});
|
||||
|
||||
test('findActiveSubtitleText collapses whitespace variants of one ASS lyric', () => {
|
||||
assert.equal(
|
||||
findActiveSubtitleText(
|
||||
@@ -144,6 +170,30 @@ test('findActiveSubtitleText keeps a canonical ASS cue for its generated animati
|
||||
assert.equal(findActiveSubtitleText([poof], 1111.59), '');
|
||||
});
|
||||
|
||||
test('findActiveSubtitleText advances when the next canonical lyric animation starts', () => {
|
||||
const cues = [
|
||||
{
|
||||
startTime: 121.73,
|
||||
endTime: 124.1,
|
||||
text: 'Torn at the seams, a sound pours out',
|
||||
source: 'canonical-ass' as const,
|
||||
animationStartTime: 121.4,
|
||||
animationEndTime: 124.1,
|
||||
},
|
||||
{
|
||||
startTime: 124.13,
|
||||
endTime: 126.38,
|
||||
text: 'It’s silent, yet spreads all around',
|
||||
source: 'canonical-ass' as const,
|
||||
animationStartTime: 123.8,
|
||||
animationEndTime: 126.38,
|
||||
},
|
||||
];
|
||||
|
||||
assert.equal(findActiveSubtitleText(cues, 123.79), cues[0]!.text);
|
||||
assert.equal(findActiveSubtitleText(cues, 123.8), cues[1]!.text);
|
||||
});
|
||||
|
||||
test('ASS fragment karaoke stays separated by style with authored word spacing', () => {
|
||||
const lineEvents = (
|
||||
style: string,
|
||||
@@ -380,6 +430,125 @@ test('secondary track controller falls back to live mpv text without a readable
|
||||
assert.deepEqual(broadcasts, ['live fallback']);
|
||||
});
|
||||
|
||||
test('secondary ASS live fallback drops malformed control debris', async () => {
|
||||
const broadcasts: string[] = [];
|
||||
const controller = createSecondarySubtitleTrackController({
|
||||
getMpvClient: () => ({
|
||||
connected: true,
|
||||
requestProperty: async (name) => {
|
||||
if (name === 'secondary-sid') return 2;
|
||||
if (name === 'track-list') return [{ type: 'sub', id: 2 }];
|
||||
if (name === 'path') return '/media/video.mkv';
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
getCurrentTimePos: () => 2,
|
||||
resolveSubtitleSource: async () => ({ path: '/subs/english.ass', sourceKey: 'english' }),
|
||||
loadSubtitleSourceText: async () => '',
|
||||
parseSubtitleCues: () => [],
|
||||
setCurrentSecondaryText: () => {},
|
||||
broadcastSecondaryText: (text) => broadcasts.push(text),
|
||||
});
|
||||
|
||||
await controller.refresh();
|
||||
broadcasts.length = 0;
|
||||
controller.handleLiveText('Visible line\n\\\n{\\fr0');
|
||||
|
||||
assert.deepEqual(broadcasts, ['Visible line']);
|
||||
});
|
||||
|
||||
test('secondary SRT live fallback preserves text that resembles ASS control debris', async () => {
|
||||
const broadcasts: string[] = [];
|
||||
const controller = createSecondarySubtitleTrackController({
|
||||
getMpvClient: () => ({
|
||||
connected: true,
|
||||
requestProperty: async (name) => {
|
||||
if (name === 'secondary-sid') return 2;
|
||||
if (name === 'track-list') return [{ type: 'sub', id: 2 }];
|
||||
if (name === 'path') return '/media/video.mkv';
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
getCurrentTimePos: () => 2,
|
||||
resolveSubtitleSource: async () => ({ path: '/subs/english.srt', sourceKey: 'english' }),
|
||||
loadSubtitleSourceText: async () => '',
|
||||
parseSubtitleCues: () => [],
|
||||
setCurrentSecondaryText: () => {},
|
||||
broadcastSecondaryText: (text) => broadcasts.push(text),
|
||||
});
|
||||
|
||||
await controller.refresh();
|
||||
broadcasts.length = 0;
|
||||
controller.handleLiveText('Visible line\n\\\n{\\fr0');
|
||||
|
||||
assert.deepEqual(broadcasts, ['Visible line\n\\\n{\\fr0']);
|
||||
});
|
||||
|
||||
test('secondary disconnect clears stale ASS fallback sanitization state', async () => {
|
||||
let connected = true;
|
||||
const broadcasts: string[] = [];
|
||||
const controller = createSecondarySubtitleTrackController({
|
||||
getMpvClient: () => ({
|
||||
connected,
|
||||
requestProperty: async (name) => {
|
||||
if (name === 'secondary-sid') return 2;
|
||||
if (name === 'track-list') return [{ type: 'sub', id: 2 }];
|
||||
if (name === 'path') return '/media/video.mkv';
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
getCurrentTimePos: () => 2,
|
||||
resolveSubtitleSource: async () => ({ path: '/subs/english.ass', sourceKey: 'english' }),
|
||||
loadSubtitleSourceText: async () => '',
|
||||
parseSubtitleCues: () => [],
|
||||
setCurrentSecondaryText: () => {},
|
||||
broadcastSecondaryText: (text) => broadcasts.push(text),
|
||||
});
|
||||
|
||||
await controller.refresh();
|
||||
connected = false;
|
||||
await controller.refresh();
|
||||
broadcasts.length = 0;
|
||||
controller.handleLiveText('Visible line\n\\\n{\\fr0');
|
||||
|
||||
assert.deepEqual(broadcasts, ['Visible line\n\\\n{\\fr0']);
|
||||
});
|
||||
|
||||
test('secondary source refresh failure clears stale ASS fallback sanitization state', async () => {
|
||||
let resolveCalls = 0;
|
||||
const broadcasts: string[] = [];
|
||||
const controller = createSecondarySubtitleTrackController({
|
||||
getMpvClient: () => ({
|
||||
connected: true,
|
||||
requestProperty: async (name) => {
|
||||
if (name === 'secondary-sid') return 2;
|
||||
if (name === 'track-list') return [{ type: 'sub', id: 2 }];
|
||||
if (name === 'path') return '/media/video.mkv';
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
getCurrentTimePos: () => 2,
|
||||
resolveSubtitleSource: async () => {
|
||||
resolveCalls += 1;
|
||||
if (resolveCalls === 1) {
|
||||
return { path: '/subs/english.ass', sourceKey: 'english' };
|
||||
}
|
||||
throw new Error('source refresh failed');
|
||||
},
|
||||
loadSubtitleSourceText: async () => '',
|
||||
parseSubtitleCues: () => [],
|
||||
setCurrentSecondaryText: () => {},
|
||||
broadcastSecondaryText: (text) => broadcasts.push(text),
|
||||
});
|
||||
|
||||
await controller.refresh();
|
||||
await controller.refresh();
|
||||
broadcasts.length = 0;
|
||||
controller.handleLiveText('Visible line\n\\\n{\\fr0');
|
||||
|
||||
assert.deepEqual(broadcasts, ['Visible line\n\\\n{\\fr0']);
|
||||
});
|
||||
|
||||
test('secondary track controller reuses parsed cues for an unchanged embedded track', async () => {
|
||||
let resolveCalls = 0;
|
||||
let parseCalls = 0;
|
||||
@@ -471,3 +640,36 @@ test('secondary track controller ignores and cleans up a refresh invalidated by
|
||||
assert.equal(parseCalls, 0);
|
||||
assert.equal(cleanupCalls, 1);
|
||||
});
|
||||
|
||||
test('secondary live fallback suppresses a per-glyph typesetting wall', async () => {
|
||||
let currentText = '';
|
||||
const controller = createSecondarySubtitleTrackController({
|
||||
getMpvClient: () => ({
|
||||
connected: true,
|
||||
requestProperty: async (name) => {
|
||||
if (name === 'secondary-sid') return 2;
|
||||
if (name === 'track-list') return [{ type: 'sub', id: 2 }];
|
||||
if (name === 'path') return '/mnt/nas/video.mkv';
|
||||
if (name === 'secondary-sub-delay') return 0;
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
getCurrentTimePos: () => 1355,
|
||||
// Network-mounted media: embedded extraction is skipped, so no parsed cues exist.
|
||||
resolveSubtitleSource: async () => null,
|
||||
loadSubtitleSourceText: async () => '',
|
||||
parseSubtitleCues,
|
||||
setCurrentSecondaryText: (text) => {
|
||||
currentText = text;
|
||||
},
|
||||
broadcastSecondaryText: () => {},
|
||||
});
|
||||
|
||||
await controller.refresh();
|
||||
const wall = [...'wansdumretoikhI'].join('\n');
|
||||
controller.handleLiveText(`${wall}\ntai`);
|
||||
assert.equal(currentText, '');
|
||||
|
||||
controller.handleLiveText(`${wall}\nそれよりも ノート…`);
|
||||
assert.equal(currentText, 'それよりも ノート…');
|
||||
});
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import type { SubtitleCue } from '../../types/subtitle';
|
||||
import { flattenedSecondarySubtitleLineIdentity } from '../../core/services/secondary-subtitle-line-identity';
|
||||
import {
|
||||
removeAssControlDebrisLines,
|
||||
removeLiveGlyphFragmentLines,
|
||||
} from '../../core/services/ass-text';
|
||||
|
||||
type SecondarySubtitleMpvClient = {
|
||||
connected?: boolean;
|
||||
@@ -23,6 +27,11 @@ type SecondarySubtitleSourceInput = {
|
||||
|
||||
const DEFAULT_REFRESH_DELAY_MS = 500;
|
||||
|
||||
function sourceUsesAssSyntax(source: string): boolean {
|
||||
const sourceWithoutQuery = source.split(/[?#]/u, 1)[0] ?? '';
|
||||
return /\.(?:ass|ssa)$/iu.test(sourceWithoutQuery);
|
||||
}
|
||||
|
||||
function finiteNumber(value: unknown, fallback = 0): number {
|
||||
const number = typeof value === 'number' ? value : Number(value);
|
||||
return Number.isFinite(number) ? number : fallback;
|
||||
@@ -82,7 +91,26 @@ export function findActiveSubtitleText(cues: readonly SubtitleCue[], timeSeconds
|
||||
(cue) =>
|
||||
cue.source === 'canonical-ass' && cue.startTime <= timeSeconds && cue.endTime > timeSeconds,
|
||||
);
|
||||
const selectedCanonical = new Set<SubtitleCue>(authoredCanonical);
|
||||
const enteringCanonical = cues.filter(
|
||||
(cue) =>
|
||||
cue.source === 'canonical-ass' &&
|
||||
(cue.animationStartTime ?? cue.startTime) <= timeSeconds &&
|
||||
cue.startTime > timeSeconds &&
|
||||
(cue.animationEndTime ?? cue.endTime) > timeSeconds,
|
||||
);
|
||||
const nextAuthoredStart = enteringCanonical.reduce(
|
||||
(earliest, cue) => Math.min(earliest, cue.startTime),
|
||||
Infinity,
|
||||
);
|
||||
// Generated lyrics can begin drawing before their canonical Comment timing. Once that
|
||||
// entrance starts, replace a preceding lyric that ends before the new authored span;
|
||||
// genuinely concurrent subtitles that continue through the new span stay selected.
|
||||
const selectedCanonical = new Set<SubtitleCue>([
|
||||
...authoredCanonical.filter(
|
||||
(cue) => enteringCanonical.length === 0 || cue.endTime > nextAuthoredStart,
|
||||
),
|
||||
...enteringCanonical,
|
||||
]);
|
||||
if (selectedCanonical.size === 0) {
|
||||
const animatedCanonical = cues.filter(
|
||||
(cue) =>
|
||||
@@ -153,15 +181,17 @@ export function findActiveSubtitleText(cues: readonly SubtitleCue[], timeSeconds
|
||||
activeCues.sort(compareAuthoredSubtitleOrder);
|
||||
|
||||
for (const { cue } of activeCues) {
|
||||
const text = cue.text.trim();
|
||||
const compactText = text.replace(/\s+/gu, '');
|
||||
if (!compactText || seenExact.has(compactText)) continue;
|
||||
seenExact.add(compactText);
|
||||
for (const line of cue.text.split('\n')) {
|
||||
const text = line.trim();
|
||||
const compactText = text.normalize('NFKC').replace(/\s+/gu, '');
|
||||
if (!compactText || seenExact.has(compactText)) continue;
|
||||
seenExact.add(compactText);
|
||||
|
||||
const flattenedIdentity = flattenedSecondarySubtitleLineIdentity(text);
|
||||
if (flattenedIdentity && seenFlattened.has(flattenedIdentity)) continue;
|
||||
if (flattenedIdentity) seenFlattened.add(flattenedIdentity);
|
||||
activeText.push(text);
|
||||
const flattenedIdentity = flattenedSecondarySubtitleLineIdentity(text);
|
||||
if (flattenedIdentity && seenFlattened.has(flattenedIdentity)) continue;
|
||||
if (flattenedIdentity) seenFlattened.add(flattenedIdentity);
|
||||
activeText.push(text);
|
||||
}
|
||||
}
|
||||
return activeText.join('\n');
|
||||
}
|
||||
@@ -182,6 +212,7 @@ export function createSecondarySubtitleTrackController(deps: {
|
||||
let parsedCues: SubtitleCue[] | null = null;
|
||||
let parsedSourceKey: string | null = null;
|
||||
let parsedTrackIdentity: string | null = null;
|
||||
let activeSourceUsesAssSyntax = false;
|
||||
let secondaryDelaySeconds = 0;
|
||||
let lastLiveText = '';
|
||||
let lastBroadcastText: string | null = null;
|
||||
@@ -211,6 +242,7 @@ export function createSecondarySubtitleTrackController(deps: {
|
||||
const generation = ++refreshGeneration;
|
||||
const client = deps.getMpvClient();
|
||||
if (!client?.connected) {
|
||||
activeSourceUsesAssSyntax = false;
|
||||
useLiveFallback();
|
||||
return;
|
||||
}
|
||||
@@ -227,6 +259,7 @@ export function createSecondarySubtitleTrackController(deps: {
|
||||
|
||||
const videoPath = typeof videoPathRaw === 'string' ? videoPathRaw.trim() : '';
|
||||
if (!videoPath || secondarySid === null || secondarySid === 'no') {
|
||||
activeSourceUsesAssSyntax = false;
|
||||
useLiveFallback();
|
||||
return;
|
||||
}
|
||||
@@ -248,11 +281,14 @@ export function createSecondarySubtitleTrackController(deps: {
|
||||
});
|
||||
if (generation !== refreshGeneration) return;
|
||||
if (!resolvedSource) {
|
||||
activeSourceUsesAssSyntax = false;
|
||||
deps.logDebug?.('[secondary-subtitle-track] selected source is not readable');
|
||||
useLiveFallback();
|
||||
return;
|
||||
}
|
||||
|
||||
activeSourceUsesAssSyntax = sourceUsesAssSyntax(resolvedSource.path);
|
||||
|
||||
if (resolvedSource.sourceKey === parsedSourceKey && parsedCues) {
|
||||
parsedTrackIdentity = selectedTrackIdentity;
|
||||
publish(resolveAtTime(deps.getCurrentTimePos()));
|
||||
@@ -274,6 +310,7 @@ export function createSecondarySubtitleTrackController(deps: {
|
||||
publish(resolveAtTime(deps.getCurrentTimePos()));
|
||||
} catch (error) {
|
||||
if (generation !== refreshGeneration) return;
|
||||
activeSourceUsesAssSyntax = false;
|
||||
deps.logWarn?.('[secondary-subtitle-track] failed to parse selected source', error);
|
||||
useLiveFallback();
|
||||
} finally {
|
||||
@@ -296,6 +333,7 @@ export function createSecondarySubtitleTrackController(deps: {
|
||||
parsedCues = null;
|
||||
parsedSourceKey = null;
|
||||
parsedTrackIdentity = null;
|
||||
activeSourceUsesAssSyntax = false;
|
||||
secondaryDelaySeconds = 0;
|
||||
lastLiveText = '';
|
||||
publish('');
|
||||
@@ -305,7 +343,9 @@ export function createSecondarySubtitleTrackController(deps: {
|
||||
refresh,
|
||||
scheduleRefresh,
|
||||
handleLiveText(text: string): void {
|
||||
lastLiveText = text;
|
||||
lastLiveText = removeLiveGlyphFragmentLines(
|
||||
activeSourceUsesAssSyntax ? removeAssControlDebrisLines(text) : text,
|
||||
);
|
||||
publish(resolveAtTime(deps.getCurrentTimePos()));
|
||||
},
|
||||
handleTimePos(timeSeconds: number): void {
|
||||
|
||||
@@ -157,14 +157,16 @@ test('subtitle prefetch runtime does not extract internal subtitle tracks from r
|
||||
assert.equal(extracted, false);
|
||||
});
|
||||
|
||||
test('subtitle prefetch runtime does not extract internal subtitle tracks from network mounts', async () => {
|
||||
test('subtitle prefetch runtime extracts internal subtitle tracks from network-mounted media', async () => {
|
||||
let extracted = false;
|
||||
const resolveSource = createResolveActiveSubtitleSidebarSourceHandler({
|
||||
getFfmpegPath: () => 'ffmpeg-custom',
|
||||
isRemoteMediaPath: async (videoPath) => videoPath.startsWith('/Volumes/jellyfin/'),
|
||||
extractInternalSubtitleTrack: async () => {
|
||||
extracted = true;
|
||||
return null;
|
||||
return {
|
||||
path: '/tmp/subminer-sidebar-123/track_7.ass',
|
||||
cleanup: async () => {},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -181,8 +183,8 @@ test('subtitle prefetch runtime does not extract internal subtitle tracks from n
|
||||
videoPath: '/Volumes/jellyfin/movie.mkv',
|
||||
});
|
||||
|
||||
assert.equal(resolved, null);
|
||||
assert.equal(extracted, false);
|
||||
assert.equal(resolved?.path, '/tmp/subminer-sidebar-123/track_7.ass');
|
||||
assert.equal(extracted, true);
|
||||
});
|
||||
|
||||
test('subtitle prefetch refresh logs a warning when source resolution throws', async () => {
|
||||
|
||||
@@ -17,8 +17,6 @@ type ActiveSubtitleSidebarSource = {
|
||||
cleanup?: () => Promise<void>;
|
||||
};
|
||||
|
||||
type RemoteMediaPathDetector = (mediaPath: string) => boolean | Promise<boolean>;
|
||||
|
||||
function parseTrackId(value: unknown): number | null {
|
||||
if (typeof value === 'number' && Number.isInteger(value)) {
|
||||
return value;
|
||||
@@ -88,7 +86,6 @@ function getActiveSubtitleTrack(
|
||||
|
||||
export function createResolveActiveSubtitleSidebarSourceHandler(deps: {
|
||||
getFfmpegPath: () => string;
|
||||
isRemoteMediaPath?: RemoteMediaPathDetector;
|
||||
extractInternalSubtitleTrack: (
|
||||
ffmpegPath: string,
|
||||
videoPath: string,
|
||||
@@ -129,8 +126,10 @@ export function createResolveActiveSubtitleSidebarSourceHandler(deps: {
|
||||
return { path: externalFilename, sourceKey: externalFilename };
|
||||
}
|
||||
|
||||
const isRemoteMediaPath = deps.isRemoteMediaPath ?? isRemoteMediaUrl;
|
||||
if (await isRemoteMediaPath(input.videoPath)) {
|
||||
// Network-mounted files extract like local ones: demuxing reads the whole
|
||||
// container (~10s/GB on gigabit), which a LAN handles alongside playback.
|
||||
// Only true remote URLs have no on-disk container to demux.
|
||||
if (isRemoteMediaUrl(input.videoPath)) {
|
||||
deps.logDebug?.('[subtitle-prefetch] skipping internal subtitle extraction for remote media');
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user