| 35 | * DEFAULT_STYLE, DEFAULT_ANIMATION_SET, and DEFAULT_CONTAINER. |
| 36 | */ |
| 37 | export function buildCaptionModel( |
| 38 | transcript: TranscriptWord[], |
| 39 | options: BuildOptions, |
| 40 | ): CaptionModel { |
| 41 | const { width, height, duration, wordsPerGroup = 5 } = options; |
| 42 | |
| 43 | const segments = new Map<string, CaptionSegment>(); |
| 44 | const groups = new Map<string, CaptionGroup>(); |
| 45 | const groupOrder: string[] = []; |
| 46 | |
| 47 | // Chunk the transcript into groups of wordsPerGroup |
| 48 | for (let groupIdx = 0; groupIdx < transcript.length; groupIdx += wordsPerGroup) { |
| 49 | const chunk = transcript.slice(groupIdx, groupIdx + wordsPerGroup); |
| 50 | const groupId = `group-${groupIdx / wordsPerGroup}`; |
| 51 | const segmentIds: string[] = []; |
| 52 | |
| 53 | chunk.forEach((word, wordIdx) => { |
| 54 | const segmentId = `segment-${groupIdx + wordIdx}`; |
| 55 | const segment: CaptionSegment = { |
| 56 | id: segmentId, |
| 57 | wordId: word.id ?? `w${groupIdx + wordIdx}`, |
| 58 | text: word.text, |
| 59 | start: word.start, |
| 60 | end: word.end, |
| 61 | groupIndex: wordIdx, |
| 62 | style: {}, |
| 63 | animation: {}, |
| 64 | }; |
| 65 | segments.set(segmentId, segment); |
| 66 | segmentIds.push(segmentId); |
| 67 | }); |
| 68 | |
| 69 | const group: CaptionGroup = { |
| 70 | id: groupId, |
| 71 | segmentIds, |
| 72 | style: { ...DEFAULT_STYLE }, |
| 73 | animation: { |
| 74 | entrance: { ...DEFAULT_ANIMATION_SET.entrance }, |
| 75 | highlight: DEFAULT_ANIMATION_SET.highlight, |
| 76 | exit: { ...DEFAULT_ANIMATION_SET.exit }, |
| 77 | }, |
| 78 | containerStyle: { ...DEFAULT_CONTAINER }, |
| 79 | }; |
| 80 | groups.set(groupId, group); |
| 81 | groupOrder.push(groupId); |
| 82 | } |
| 83 | |
| 84 | return { |
| 85 | width, |
| 86 | height, |
| 87 | duration, |
| 88 | segments, |
| 89 | groups, |
| 90 | groupOrder, |
| 91 | defaultAnimation: { |
| 92 | entrance: { ...DEFAULT_ANIMATION_SET.entrance }, |
| 93 | highlight: DEFAULT_ANIMATION_SET.highlight, |
| 94 | exit: { ...DEFAULT_ANIMATION_SET.exit }, |