Files
shumai/packages/webui/components/file-viewer/transcript-timeline.ts
T
Yiling-J d3899d1257 feat(video): add video transcripts (#559)
* feat(video): generate transcripts with the local embedding server

* feat(video): show saved transcripts in the player

* fix(video): preserve transcript seek timestamps

* fix(video): refine transcript viewing and generation

* feat(video): resize transcripts and follow playback earlier

* fix(video): animate transcript playback scrolling

* fix(video): transcribe from the smallest full proxy

* fix(video): refine the sync to playback button

* feat(video): edit transcript segment text inline

* fix(video): keep transcript segment highlighted over edit button

* docs(video): note embedding server version for transcripts
2026-12-04 13:49:25 +08:00

31 lines
1.2 KiB
TypeScript

import type { TranscriptSegment } from '@shumai/dtos'
export function createTranscriptTimeline(segments: TranscriptSegment[]) {
const ordered = [...segments].sort((a, b) => a.startMs - b.startMs)
let latestEndMs = 0
const timeline = ordered.map((segment) => {
latestEndMs = Math.max(latestEndMs, segment.endMs)
return { segment, latestEndMs }
})
return (playbackTimeMs: number): string | undefined => {
// Remove floating-point noise around millisecond boundaries while keeping microsecond precision.
const timeMs = Math.round(playbackTimeMs * 1000) / 1000
let first = 0
let last = timeline.length
while (first < last) {
const middle = Math.floor((first + last) / 2)
if (timeline[middle].segment.startMs <= timeMs) first = middle + 1
else last = middle
}
// Prefer the most recent start when segments overlap. The running maximum bounds
// the backward scan even in long gaps or after the last segment.
for (let index = first - 1; index >= 0; index--) {
const { segment, latestEndMs } = timeline[index]
if (latestEndMs <= timeMs) break
if (segment.endMs > timeMs) return segment.id
}
}
}