Skill Tracer
Skills › HyperFrames › media-use › parakeet-words.mjs

parakeet-words.mjs

27 lines · skills/media-use/scripts/lib/parakeet-words.mjs at a595755

1// Merge Parakeet-MLX token timestamps into word timestamps.
2//
3// parakeet-mlx JSON emits SUB-WORD tokens (" H", "ello", ...) with per-token
4// start/end. Captions + transcript-cut need WORD timestamps, so join tokens
5// into words on the space boundary: a token whose text starts with a space
6// (or the very first token) begins a new word; the rest append. Output matches
7// the { words: [{ text, start, end }] } shape the rest of media-use consumes
8// (see words.mjs / cutlist.mjs).
9
10export function mergeTokensToWords(parakeet) {
11 const sentences = Array.isArray(parakeet?.sentences) ? parakeet.sentences : [];
12 const words = [];
13 for (const s of sentences) {
14 for (const t of s.tokens ?? []) {
15 const raw = typeof t.text === "string" ? t.text : "";
16 const startsWord = raw.startsWith(" ") || words.length === 0;
17 if (startsWord) {
18 words.push({ text: raw.trim(), start: t.start, end: t.end });
19 } else {
20 const w = words[words.length - 1];
21 w.text += raw;
22 w.end = t.end;
23 }
24 }
25 }
26 return { text: (parakeet?.text ?? "").trim(), words: words.filter((w) => w.text.length > 0) };