mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 23:54:25 +00:00
fix(video): preserve transcript seek timestamps
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|||||||
- **sessions**: See total tokens and estimated cost in the session logs footer, with a breakdown of input, output, and cached tokens.
|
- **sessions**: See total tokens and estimated cost in the session logs footer, with a breakdown of input, output, and cached tokens.
|
||||||
- **projects**: Middle-click or Command/Ctrl-click a project to open it in a new tab. Use **Open in new tab** in a file or folder's context menu to keep your current view open.
|
- **projects**: Middle-click or Command/Ctrl-click a project to open it in a new tab. Use **Open in new tab** in a file or folder's context menu to keep your current view open.
|
||||||
- **video**: Use **Generate Transcripts** in a video's context menu to save a transcript with the local embedding server. Choose a language or let the server detect it automatically.
|
- **video**: Use **Generate Transcripts** in a video's context menu to save a transcript with the local embedding server. Choose a language or let the server detect it automatically.
|
||||||
- **video**: Open saved transcripts from the video player, switch languages, search the text, and jump to a timestamp by selecting a segment. Follow along as the video plays.
|
- **video**: Open saved transcripts from the video player, switch languages, search the text, and jump to the exact start of a segment by selecting it. Follow along as the video plays.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ test('reads, searches and follows a saved transcript in the video player', async
|
|||||||
text: `Intervening segment ${index + 1}`,
|
text: `Intervening segment ${index + 1}`,
|
||||||
})),
|
})),
|
||||||
{ position: 29, startMs: 720, endMs: 980, text: 'Final signal.' },
|
{ position: 29, startMs: 720, endMs: 980, text: 'Final signal.' },
|
||||||
|
{ position: 30, startMs: 995, endMs: 1000, text: 'Closing word.' },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -122,6 +123,19 @@ test('reads, searches and follows a saved transcript in the video player', async
|
|||||||
await expect(last).toHaveAttribute('aria-current', 'true')
|
await expect(last).toHaveAttribute('aria-current', 'true')
|
||||||
await expect(video).toHaveJSProperty('paused', true)
|
await expect(video).toHaveJSProperty('paused', true)
|
||||||
|
|
||||||
|
const closingWord = panel.getByRole('button', { name: /Closing word\./ })
|
||||||
|
await closingWord.click()
|
||||||
|
await expect
|
||||||
|
.poll(() =>
|
||||||
|
video.evaluate((element) => {
|
||||||
|
const media = element as HTMLVideoElement
|
||||||
|
return { seeking: media.seeking, timeMs: Math.round(media.currentTime * 1000) }
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.toEqual({ seeking: false, timeMs: 995 })
|
||||||
|
await expect(closingWord).toHaveAttribute('aria-current', 'true')
|
||||||
|
await expect(video).toHaveJSProperty('paused', true)
|
||||||
|
|
||||||
await panel.getByRole('combobox', { name: 'Language' }).selectOption('zh')
|
await panel.getByRole('combobox', { name: 'Language' }).selectOption('zh')
|
||||||
await expect(panel.getByText('你好。')).toBeVisible()
|
await expect(panel.getByText('你好。')).toBeVisible()
|
||||||
await expect(last).toHaveCount(0)
|
await expect(last).toHaveCount(0)
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ const SegmentRow = memo(function SegmentRow({
|
|||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mediaControllerRef.current?.seekTo(segment.startMs / 1000)
|
mediaControllerRef.current?.seekTo(segment.startMs / 1000, { exact: true })
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span className="mb-1 block font-mono text-xs tabular-nums text-muted-foreground" dir="ltr">
|
<span className="mb-1 block font-mono text-xs tabular-nums text-muted-foreground" dir="ltr">
|
||||||
|
|||||||
@@ -29,4 +29,14 @@ describe('createTranscriptTimeline', () => {
|
|||||||
expect(at(4000)).toBeUndefined()
|
expect(at(4000)).toBeUndefined()
|
||||||
expect(createTranscriptTimeline([])(0)).toBeUndefined()
|
expect(createTranscriptTimeline([])(0)).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('uses the new segment at an exact millisecond after converting the seek through seconds', () => {
|
||||||
|
const at = createTranscriptTimeline([
|
||||||
|
{ id: 'before', startMs: 1800, endMs: 2010, text: '' },
|
||||||
|
{ id: 'after', startMs: 2010, endMs: 2200, text: '' },
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(at(2009.999)).toBe('before')
|
||||||
|
expect(at((2010 / 1000) * 1000)).toBe('after')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ export function createTranscriptTimeline(segments: TranscriptSegment[]) {
|
|||||||
return { segment, latestEndMs }
|
return { segment, latestEndMs }
|
||||||
})
|
})
|
||||||
|
|
||||||
return (timeMs: number): string | undefined => {
|
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 first = 0
|
||||||
let last = timeline.length
|
let last = timeline.length
|
||||||
while (first < last) {
|
while (first < last) {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import type React from 'react'
|
|||||||
export interface MediaController {
|
export interface MediaController {
|
||||||
play: () => void
|
play: () => void
|
||||||
pause: () => void
|
pause: () => void
|
||||||
seekTo: (second: number) => void
|
seekTo: (second: number, options?: { exact?: boolean }) => void
|
||||||
getCurrentTime?: () => number
|
getCurrentTime?: () => number
|
||||||
getDuration?: () => number
|
getDuration?: () => number
|
||||||
subscribeToPlaybackTime?: (listener: (second: number) => void) => (() => void) | undefined
|
subscribeToPlaybackTime?: (listener: (second: number) => void) => (() => void) | undefined
|
||||||
|
|||||||
@@ -109,6 +109,136 @@ afterEach(() => {
|
|||||||
|
|
||||||
// --- Tests --------------------------------------------------------------------
|
// --- Tests --------------------------------------------------------------------
|
||||||
|
|
||||||
|
describe('useFramePlayer exact timestamp seeking', () => {
|
||||||
|
it.each([true, false])('keeps the settled timestamp with paused=%s', async (paused) => {
|
||||||
|
const video = new FakeVideo()
|
||||||
|
video.paused = paused
|
||||||
|
const ref = makeRef(video)
|
||||||
|
const { result } = renderHook(() => useFramePlayer(ref, 30, 30))
|
||||||
|
|
||||||
|
let exactSeek: Promise<void> | undefined
|
||||||
|
act(() => {
|
||||||
|
exactSeek = result.current.seekToTime(0.995, true)
|
||||||
|
})
|
||||||
|
await act(async () => {
|
||||||
|
video.dispatchEvent(new Event('seeked'))
|
||||||
|
await exactSeek
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(video.currentTime).toBe(0.995)
|
||||||
|
expect(video.paused).toBe(paused)
|
||||||
|
expect(result.current.currentFrame).toBe(29)
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
video.currentTime = 0.996
|
||||||
|
video.dispatchEvent(new Event('seeked'))
|
||||||
|
})
|
||||||
|
expect(video.currentTime).toBeCloseTo(29.5 / 30)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('keeps the latest transcript seek queued behind a frame seek when playback is paused mid-seek', async () => {
|
||||||
|
const video = new FakeVideo()
|
||||||
|
video.paused = false
|
||||||
|
const ref = makeRef(video)
|
||||||
|
const { result } = renderHook(() => useFramePlayer(ref, 30, 30))
|
||||||
|
|
||||||
|
const seeks: Promise<void>[] = []
|
||||||
|
act(() => {
|
||||||
|
seeks.push(result.current.seekToFrame(18))
|
||||||
|
seeks.push(result.current.seekToTime(0.005, true))
|
||||||
|
seeks.push(result.current.seekToTime(0.995, true))
|
||||||
|
})
|
||||||
|
expect(video.currentTime).toBeCloseTo(18.5 / 30)
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
video.dispatchEvent(new Event('seeked'))
|
||||||
|
})
|
||||||
|
expect(video.currentTime).toBe(0.995)
|
||||||
|
|
||||||
|
act(() => video.pause())
|
||||||
|
await act(async () => {
|
||||||
|
video.dispatchEvent(new Event('seeked'))
|
||||||
|
await Promise.all(seeks)
|
||||||
|
})
|
||||||
|
expect(video.currentTime).toBe(0.995)
|
||||||
|
expect(video.paused).toBe(true)
|
||||||
|
expect(result.current.currentFrame).toBe(29)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('keeps the exact timestamp when the native seek completes after the seek queue times out', async () => {
|
||||||
|
vi.useFakeTimers()
|
||||||
|
try {
|
||||||
|
const video = new FakeVideo()
|
||||||
|
const ref = makeRef(video)
|
||||||
|
const { result } = renderHook(() => useFramePlayer(ref, 30, 30))
|
||||||
|
|
||||||
|
let exactSeek: Promise<void> | undefined
|
||||||
|
act(() => {
|
||||||
|
exactSeek = result.current.seekToTime(0.995, true)
|
||||||
|
})
|
||||||
|
await act(async () => {
|
||||||
|
await vi.advanceTimersByTimeAsync(501)
|
||||||
|
await exactSeek
|
||||||
|
})
|
||||||
|
act(() => video.dispatchEvent(new Event('seeked')))
|
||||||
|
|
||||||
|
expect(video.currentTime).toBe(0.995)
|
||||||
|
} finally {
|
||||||
|
vi.useRealTimers()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it.each(['controller', 'native'] as const)(
|
||||||
|
'lets a newer ordinary %s seek supersede a timed-out exact seek',
|
||||||
|
async (source) => {
|
||||||
|
vi.useFakeTimers()
|
||||||
|
try {
|
||||||
|
const video = new FakeVideo()
|
||||||
|
const ref = makeRef(video)
|
||||||
|
const { result } = renderHook(() => useFramePlayer(ref, 30, 30))
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
const exactSeek = result.current.seekToTime(0.995, true)
|
||||||
|
await vi.advanceTimersByTimeAsync(501)
|
||||||
|
await exactSeek
|
||||||
|
})
|
||||||
|
act(() => {
|
||||||
|
if (source === 'controller') {
|
||||||
|
void result.current.seekToTime(0.995)
|
||||||
|
} else {
|
||||||
|
video.currentTime = 0.996
|
||||||
|
}
|
||||||
|
video.dispatchEvent(new Event('seeking'))
|
||||||
|
video.dispatchEvent(new Event('seeked'))
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(video.currentTime).toBeCloseTo(29.5 / 30)
|
||||||
|
} finally {
|
||||||
|
vi.useRealTimers()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
it('lets a comment seek supersede both the active and queued transcript seeks', async () => {
|
||||||
|
const video = new FakeVideo()
|
||||||
|
const ref = makeRef(video)
|
||||||
|
const { result } = renderHook(() => useFramePlayer(ref, 30, 30))
|
||||||
|
|
||||||
|
const transcriptSeeks: Promise<void>[] = []
|
||||||
|
act(() => {
|
||||||
|
transcriptSeeks.push(result.current.seekToTime(0.5, true))
|
||||||
|
transcriptSeeks.push(result.current.seekToTime(0.7, true))
|
||||||
|
void result.current.seekToTime(0.996)
|
||||||
|
})
|
||||||
|
expect(video.currentTime).toBe(0.996)
|
||||||
|
|
||||||
|
await act(async () => video.dispatchEvent(new Event('seeked')))
|
||||||
|
expect(video.currentTime).toBeCloseTo(29.5 / 30)
|
||||||
|
await Promise.all(transcriptSeeks)
|
||||||
|
expect(result.current.currentFrame).toBe(29)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('useFramePlayer Safari playhead synchronization', () => {
|
describe('useFramePlayer Safari playhead synchronization', () => {
|
||||||
it('does not rewind the playhead on play when currentTime leads the presented frame', () => {
|
it('does not rewind the playhead on play when currentTime leads the presented frame', () => {
|
||||||
const video = new FakeVideo()
|
const video = new FakeVideo()
|
||||||
|
|||||||
@@ -5,10 +5,22 @@ export interface UseFramePlayerResult {
|
|||||||
currentFrame: number
|
currentFrame: number
|
||||||
setCurrentFrame: React.Dispatch<React.SetStateAction<number>>
|
setCurrentFrame: React.Dispatch<React.SetStateAction<number>>
|
||||||
seekToFrame: (targetFrame: number) => Promise<void>
|
seekToFrame: (targetFrame: number) => Promise<void>
|
||||||
|
seekToTime: (second: number, exact?: boolean) => Promise<void>
|
||||||
pauseForScrub: () => void
|
pauseForScrub: () => void
|
||||||
isSeeking: React.RefObject<boolean>
|
isSeeking: React.RefObject<boolean>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface SeekTarget {
|
||||||
|
frame: number
|
||||||
|
exactSecond?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SeekSession {
|
||||||
|
completion: Promise<void> | null
|
||||||
|
pending: SeekTarget | null
|
||||||
|
finishWait: (() => void) | null
|
||||||
|
}
|
||||||
|
|
||||||
interface HtmlVideoElementWithCallback {
|
interface HtmlVideoElementWithCallback {
|
||||||
requestVideoFrameCallback?: (
|
requestVideoFrameCallback?: (
|
||||||
callback: (now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => void,
|
callback: (now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => void,
|
||||||
@@ -29,9 +41,9 @@ export function useFramePlayer(
|
|||||||
): UseFramePlayerResult {
|
): UseFramePlayerResult {
|
||||||
const [currentFrame, setCurrentFrame] = useState<number>(0)
|
const [currentFrame, setCurrentFrame] = useState<number>(0)
|
||||||
const isSeekingRef = useRef<boolean>(false)
|
const isSeekingRef = useRef<boolean>(false)
|
||||||
const seekCompletionRef = useRef<Promise<void> | null>(null)
|
const exactSeekRef = useRef<{ video: HTMLVideoElement; second: number } | null>(null)
|
||||||
|
const seekSessionRef = useRef<SeekSession | null>(null)
|
||||||
const isPlayingRef = useRef<boolean>(false)
|
const isPlayingRef = useRef<boolean>(false)
|
||||||
const pendingSeekFrameRef = useRef<number | null>(null)
|
|
||||||
const scrubPauseRef = useRef(false)
|
const scrubPauseRef = useRef(false)
|
||||||
const currentFrameRef = useRef<number>(currentFrame)
|
const currentFrameRef = useRef<number>(currentFrame)
|
||||||
currentFrameRef.current = currentFrame
|
currentFrameRef.current = currentFrame
|
||||||
@@ -186,7 +198,9 @@ export function useFramePlayer(
|
|||||||
|
|
||||||
// Snap the playhead to the center of the paused frame to align browser compositor
|
// Snap the playhead to the center of the paused frame to align browser compositor
|
||||||
// Skip snapping if the video has ended to preserve the native ended state and allow native replay
|
// Skip snapping if the video has ended to preserve the native ended state and allow native replay
|
||||||
if (!video.ended && !scrubPause) {
|
const exactSeekPending =
|
||||||
|
exactSeekRef.current?.video === video && (isSeekingRef.current || video.seeking)
|
||||||
|
if (!video.ended && !scrubPause && !exactSeekPending) {
|
||||||
const safeCenterTime = calculateFrameCenterTime(clamped, frameRate)
|
const safeCenterTime = calculateFrameCenterTime(clamped, frameRate)
|
||||||
video.currentTime = safeCenterTime
|
video.currentTime = safeCenterTime
|
||||||
}
|
}
|
||||||
@@ -237,47 +251,50 @@ export function useFramePlayer(
|
|||||||
}
|
}
|
||||||
}, [videoRef])
|
}, [videoRef])
|
||||||
|
|
||||||
const seekToFrame = useCallback(
|
const seek = useCallback(
|
||||||
async (targetFrame: number) => {
|
async (target: SeekTarget) => {
|
||||||
const video = videoRef.current
|
const video = videoRef.current
|
||||||
if (!video) return
|
if (!video) return
|
||||||
|
|
||||||
const roundedFrame = Math.round(targetFrame)
|
|
||||||
const clampedFrame = Math.max(0, Math.min(roundedFrame, totalFrames - 1))
|
|
||||||
|
|
||||||
// If a seek is already active, queue this target and update UI state optimistically
|
// If a seek is already active, queue this target and update UI state optimistically
|
||||||
if (seekCompletionRef.current) {
|
const activeSession = seekSessionRef.current
|
||||||
pendingSeekFrameRef.current = clampedFrame
|
if (activeSession?.completion) {
|
||||||
setCurrentFrame(clampedFrame)
|
activeSession.pending = target
|
||||||
return seekCompletionRef.current
|
setCurrentFrame(target.frame)
|
||||||
|
return activeSession.completion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const session: SeekSession = { completion: null, pending: null, finishWait: null }
|
||||||
|
seekSessionRef.current = session
|
||||||
isSeekingRef.current = true
|
isSeekingRef.current = true
|
||||||
setCurrentFrame(clampedFrame) // Optimistic update for active seek
|
setCurrentFrame(target.frame) // Optimistic update for active seek
|
||||||
|
|
||||||
const performSeek = async (frameToSeek: number) => {
|
const performSeek = async ({ frame, exactSecond }: SeekTarget) => {
|
||||||
const safeTargetTime = calculateFrameCenterTime(frameToSeek, frameRate)
|
exactSeekRef.current = exactSecond === undefined ? null : { video, second: exactSecond }
|
||||||
const wasPlaying = !video.paused
|
const wasPlaying = !video.paused
|
||||||
|
|
||||||
video.currentTime = safeTargetTime
|
video.currentTime = exactSecond ?? calculateFrameCenterTime(frame, frameRate)
|
||||||
|
|
||||||
// Wait for native seek to complete (with a 500ms fallback timeout to prevent hanging)
|
// Wait for native seek to complete (with a 500ms fallback timeout to prevent hanging)
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
let timeoutId: ReturnType<typeof setTimeout> | null = null
|
const finishWait = () => {
|
||||||
const onSeeked = () => {
|
clearTimeout(timeoutId)
|
||||||
if (timeoutId) clearTimeout(timeoutId)
|
|
||||||
video.removeEventListener('seeked', onSeeked)
|
video.removeEventListener('seeked', onSeeked)
|
||||||
|
session.finishWait = null
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
const onSeeked = () => {
|
||||||
|
if (!video.seeking) finishWait()
|
||||||
|
}
|
||||||
|
const timeoutId = setTimeout(finishWait, 500)
|
||||||
|
session.finishWait = finishWait
|
||||||
video.addEventListener('seeked', onSeeked)
|
video.addEventListener('seeked', onSeeked)
|
||||||
timeoutId = setTimeout(() => {
|
|
||||||
video.removeEventListener('seeked', onSeeked)
|
|
||||||
resolve()
|
|
||||||
}, 500)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (seekSessionRef.current !== session) return
|
||||||
|
|
||||||
// Pause only if we started paused and the user hasn't pressed play in the meantime
|
// Pause only if we started paused and the user hasn't pressed play in the meantime
|
||||||
if (!wasPlaying && !isPlayingRef.current) {
|
if (exactSecond === undefined && !wasPlaying && !isPlayingRef.current) {
|
||||||
video.pause()
|
video.pause()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,25 +317,55 @@ export function useFramePlayer(
|
|||||||
const runSeeks = async () => {
|
const runSeeks = async () => {
|
||||||
try {
|
try {
|
||||||
// Process the latest queued seek before signaling completion to any caller.
|
// Process the latest queued seek before signaling completion to any caller.
|
||||||
let nextFrame: number | null = clampedFrame
|
let next: SeekTarget | null = target
|
||||||
while (nextFrame !== null) {
|
while (next !== null && seekSessionRef.current === session) {
|
||||||
await performSeek(nextFrame)
|
await performSeek(next)
|
||||||
nextFrame = pendingSeekFrameRef.current
|
next = session.pending
|
||||||
pendingSeekFrameRef.current = null
|
session.pending = null
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
seekCompletionRef.current = null
|
if (seekSessionRef.current === session) {
|
||||||
pendingSeekFrameRef.current = null
|
seekSessionRef.current = null
|
||||||
isSeekingRef.current = false
|
isSeekingRef.current = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
seekCompletionRef.current = runSeeks()
|
session.completion = runSeeks()
|
||||||
await seekCompletionRef.current
|
return session.completion
|
||||||
},
|
},
|
||||||
[videoRef, frameRate, totalFrames],
|
[videoRef, frameRate, totalFrames],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const seekToFrame = useCallback(
|
||||||
|
(targetFrame: number) =>
|
||||||
|
seek({ frame: Math.max(0, Math.min(Math.round(targetFrame), totalFrames - 1)) }),
|
||||||
|
[seek, totalFrames],
|
||||||
|
)
|
||||||
|
|
||||||
|
const seekToTime = useCallback(
|
||||||
|
(second: number, exact = false) => {
|
||||||
|
if (!exact) {
|
||||||
|
// Ordinary timestamp seeks take effect immediately, superseding queued frame and transcript seeks.
|
||||||
|
const previousSession = seekSessionRef.current
|
||||||
|
seekSessionRef.current = null
|
||||||
|
isSeekingRef.current = false
|
||||||
|
previousSession?.finishWait?.()
|
||||||
|
exactSeekRef.current = null
|
||||||
|
const video = videoRef.current
|
||||||
|
if (video) video.currentTime = second
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
const frame = Math.floor(second * frameRate + 0.45)
|
||||||
|
return seek({
|
||||||
|
frame: Math.max(0, Math.min(frame, totalFrames - 1)),
|
||||||
|
exactSecond: second,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
[seek, videoRef, frameRate, totalFrames],
|
||||||
|
)
|
||||||
|
|
||||||
// Keyboard Shortcuts Keydown Listener
|
// Keyboard Shortcuts Keydown Listener
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
@@ -353,12 +400,26 @@ export function useFramePlayer(
|
|||||||
const video = videoRef.current
|
const video = videoRef.current
|
||||||
if (!video) return
|
if (!video) return
|
||||||
|
|
||||||
|
const handleSeeking = () => {
|
||||||
|
const exactSeek = exactSeekRef.current
|
||||||
|
// A newer native seek can replace the pending target.
|
||||||
|
if (exactSeek?.video === video && Math.abs(exactSeek.second - video.currentTime) > 0.000001) {
|
||||||
|
exactSeekRef.current = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const handleExternalSeeked = () => {
|
const handleExternalSeeked = () => {
|
||||||
// Only sync if this seek was not triggered internally by seekToFrame
|
if (video.seeking) return
|
||||||
|
// The queue may have timed out while the browser was still seeking.
|
||||||
|
const preserveTime = exactSeekRef.current?.video === video
|
||||||
|
if (preserveTime) exactSeekRef.current = null
|
||||||
|
|
||||||
|
// The queue updates its own seeks when they settle.
|
||||||
if (!isSeekingRef.current) {
|
if (!isSeekingRef.current) {
|
||||||
const actualFrame = Math.floor(video.currentTime * frameRate + 0.45)
|
const actualFrame = Math.floor(video.currentTime * frameRate + 0.45)
|
||||||
const finalFrame = Math.max(0, Math.min(actualFrame, totalFrames - 1))
|
const finalFrame = Math.max(0, Math.min(actualFrame, totalFrames - 1))
|
||||||
setCurrentFrame(finalFrame)
|
setCurrentFrame(finalFrame)
|
||||||
|
if (preserveTime) return
|
||||||
|
|
||||||
// Nudge to safe frame center to guarantee correct browser decoding
|
// Nudge to safe frame center to guarantee correct browser decoding
|
||||||
const safeCenterTime = calculateFrameCenterTime(finalFrame, frameRate)
|
const safeCenterTime = calculateFrameCenterTime(finalFrame, frameRate)
|
||||||
@@ -369,8 +430,10 @@ export function useFramePlayer(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
video.addEventListener('seeking', handleSeeking)
|
||||||
video.addEventListener('seeked', handleExternalSeeked)
|
video.addEventListener('seeked', handleExternalSeeked)
|
||||||
return () => {
|
return () => {
|
||||||
|
video.removeEventListener('seeking', handleSeeking)
|
||||||
video.removeEventListener('seeked', handleExternalSeeked)
|
video.removeEventListener('seeked', handleExternalSeeked)
|
||||||
}
|
}
|
||||||
}, [videoRef.current, frameRate, totalFrames])
|
}, [videoRef.current, frameRate, totalFrames])
|
||||||
@@ -379,6 +442,7 @@ export function useFramePlayer(
|
|||||||
currentFrame,
|
currentFrame,
|
||||||
setCurrentFrame,
|
setCurrentFrame,
|
||||||
seekToFrame,
|
seekToFrame,
|
||||||
|
seekToTime,
|
||||||
pauseForScrub,
|
pauseForScrub,
|
||||||
isSeeking: isSeekingRef,
|
isSeeking: isSeekingRef,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,15 +126,13 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
|||||||
pause: () => {
|
pause: () => {
|
||||||
videoRef.current?.pause()
|
videoRef.current?.pause()
|
||||||
},
|
},
|
||||||
seekTo: (second: number) => {
|
seekTo: (second, options) => {
|
||||||
if (pendingResolutionCorrectionRef.current) {
|
if (pendingResolutionCorrectionRef.current) {
|
||||||
pendingResolutionCorrectionRef.current()
|
pendingResolutionCorrectionRef.current()
|
||||||
pendingResolutionCorrectionRef.current = null
|
pendingResolutionCorrectionRef.current = null
|
||||||
}
|
}
|
||||||
setHasStartedPlaying(true)
|
setHasStartedPlaying(true)
|
||||||
if (videoRef.current) {
|
void seekToTime(second, options?.exact)
|
||||||
videoRef.current.currentTime = second
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
getCurrentTime: () => {
|
getCurrentTime: () => {
|
||||||
return (videoRef.current ? videoRef.current.currentTime : 0) ?? 0
|
return (videoRef.current ? videoRef.current.currentTime : 0) ?? 0
|
||||||
@@ -288,7 +286,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
|||||||
|
|
||||||
const isAudio = data.proxyType === 'audio'
|
const isAudio = data.proxyType === 'audio'
|
||||||
const totalFrames = resolveTotalFrames({ dbTotalFrames, containerDuration, frameRate })
|
const totalFrames = resolveTotalFrames({ dbTotalFrames, containerDuration, frameRate })
|
||||||
const { currentFrame, seekToFrame, pauseForScrub } = useFramePlayer(
|
const { currentFrame, seekToFrame, seekToTime, pauseForScrub } = useFramePlayer(
|
||||||
videoRef,
|
videoRef,
|
||||||
frameRate,
|
frameRate,
|
||||||
totalFrames,
|
totalFrames,
|
||||||
|
|||||||
Reference in New Issue
Block a user