Skip to content

Commit 224a104

Browse files
authored
Merge pull request #675 from dreamgene/feature/609-speech-to-text-subtitling
[Mobile] Speech-to-Text Native Subtitling #609
2 parents ae03214 + 482356f commit 224a104

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Device-dictation speech-to-text with media-overlay timestamp alignment and WebVTT export
2+
export interface VTTCue {
3+
startTime: number;
4+
endTime: number;
5+
text: string;
6+
}
7+
8+
export interface SubtitleSession {
9+
cues: VTTCue[];
10+
isActive: boolean;
11+
}
12+
13+
function toVTTTime(seconds: number): string {
14+
const h = Math.floor(seconds / 3600);
15+
const m = Math.floor((seconds % 3600) / 60);
16+
const s = seconds % 60;
17+
return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${s.toFixed(3).padStart(6, '0')}`;
18+
}
19+
20+
export function exportToWebVTT(cues: VTTCue[]): string {
21+
const body = cues
22+
.map((cue, i) => `${i + 1}\n${toVTTTime(cue.startTime)} --> ${toVTTTime(cue.endTime)}\n${cue.text}`)
23+
.join('\n\n');
24+
return `WEBVTT\n\n${body}`;
25+
}
26+
27+
export function createSubtitleSession(): SubtitleSession {
28+
return { cues: [], isActive: false };
29+
}
30+
31+
export function appendCue(
32+
session: SubtitleSession,
33+
text: string,
34+
startTime: number,
35+
endTime: number,
36+
): SubtitleSession {
37+
return { ...session, cues: [...session.cues, { text, startTime, endTime }] };
38+
}
39+
40+
export function startSession(session: SubtitleSession): SubtitleSession {
41+
return { ...session, isActive: true };
42+
}
43+
44+
export function stopSession(session: SubtitleSession): SubtitleSession {
45+
return { ...session, isActive: false };
46+
}

0 commit comments

Comments
 (0)