MCPcopy Create free account
hub / github.com/heygen-com/hyperframes / parseCaptionComposition

Function parseCaptionComposition

packages/studio/src/captions/parser.ts:137–263  ·  view source on GitHub ↗
(
  iframeDoc: Document,
  iframeWin: Window,
  source: string,
  compositionWidth: number,
  compositionHeight: number,
  compositionDuration: number,
)

Source from the content-addressed store, hash-verified

135 * Returns null if no transcript is found in the source.
136 */
137export function parseCaptionComposition(
138 iframeDoc: Document,
139 iframeWin: Window,
140 source: string,
141 compositionWidth: number,
142 compositionHeight: number,
143 compositionDuration: number,
144): CaptionModel | null {
145 // Step 1: Extract transcript words from source
146 const transcript = extractTranscript(source);
147 if (transcript.length === 0) {
148 return null;
149 }
150
151 // Step 2: Look for grouping and word elements in the iframe DOM
152 const groupEls = iframeDoc.querySelectorAll(".caption-group, .caption-line, .caption-block");
153 const wordEls = iframeDoc.querySelectorAll(".word, .caption-word");
154
155 // Step 3: Infer wordsPerGroup from element counts
156 let wordsPerGroup = 5; // default
157 if (groupEls.length > 0 && wordEls.length > 0) {
158 wordsPerGroup = Math.round(wordEls.length / groupEls.length);
159 if (wordsPerGroup < 1) {
160 wordsPerGroup = 1;
161 }
162 }
163
164 // Step 4: Build the caption model with inferred grouping
165 const model = buildCaptionModel(transcript, {
166 width: compositionWidth,
167 height: compositionHeight,
168 duration: compositionDuration,
169 wordsPerGroup,
170 });
171
172 // Step 5: Read computed styles from the first word or group element
173 const firstWordEl = wordEls.item(0) as Element | null;
174 const firstGroupEl = groupEls.item(0) as Element | null;
175 const styleSourceEl = firstWordEl ?? firstGroupEl;
176
177 if (styleSourceEl) {
178 const computed = iframeWin.getComputedStyle(styleSourceEl);
179
180 // Build partial style overrides from computed values
181 const styleOverrides: Partial<CaptionStyle> = {};
182
183 const fontSize = parseFloat(computed.fontSize);
184 if (!isNaN(fontSize) && fontSize > 0) {
185 styleOverrides.fontSize = fontSize;
186 }
187
188 const fontWeight = computed.fontWeight;
189 if (fontWeight) {
190 const numericWeight = parseInt(fontWeight, 10);
191 styleOverrides.fontWeight = isNaN(numericWeight) ? fontWeight : numericWeight;
192 }
193
194 const fontFamily = computed.fontFamily;

Callers 1

tryActivateCaptionsFunction · 0.90

Calls 3

extractTranscriptFunction · 0.85
buildCaptionModelFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected