fix(webui): resolve 1-frame comment mismatch via 0.45 offset and pause snapping

This commit is contained in:
yiling
2026-06-30 11:10:53 +08:00
parent 954d8ee497
commit 899b600417
4 changed files with 26 additions and 21 deletions
@@ -75,7 +75,7 @@ export function useFramePlayer(
const isVfcStalled = Date.now() - lastVfcTime > stallThreshold
// Print debug log roughly once per second during playback
if (Math.floor(video.currentTime * frameRate + 0.001) % 30 === 0) {
if (Math.floor(video.currentTime * frameRate + 0.45) % 30 === 0) {
console.log(
`[useFramePlayer] currentTime: ${video.currentTime}, isVfcStalled: ${isVfcStalled}, stallThreshold: ${stallThreshold}, totalFrames: ${totalFrames}`,
)
@@ -83,7 +83,7 @@ export function useFramePlayer(
// Only update playhead via currentTime (RAF) if rVFC has stalled or isn't supported
if (!videoWithCallback.requestVideoFrameCallback || isVfcStalled) {
const frame = Math.floor(video.currentTime * frameRate + 0.001)
const frame = Math.floor(video.currentTime * frameRate + 0.45)
const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
setCurrentFrame(clamped)
}
@@ -98,7 +98,7 @@ export function useFramePlayer(
rVfcId = null
lastVfcTime = Date.now() // Reset stall timer
const compositorFrame = Math.floor(metadata.mediaTime * frameRate + 0.001)
const compositorFrame = Math.floor(metadata.mediaTime * frameRate + 0.45)
const compositorClamped = Math.max(0, Math.min(compositorFrame, totalFrames - 1))
setCurrentFrame(compositorClamped)
},
@@ -128,9 +128,13 @@ export function useFramePlayer(
}
// Synchronize frame on pause
const frame = Math.floor(video.currentTime * frameRate + 0.001)
const frame = Math.floor(video.currentTime * frameRate + 0.45)
const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
setCurrentFrame(clamped)
// Snap the playhead to the center of the paused frame to align browser compositor
const safeCenterTime = calculateFrameCenterTime(clamped, frameRate)
video.currentTime = safeCenterTime
}
video.addEventListener('play', handlePlay)
@@ -205,8 +209,8 @@ export function useFramePlayer(
video.pause()
}
// Seek complete: update frame immediately using Math.floor (accounts for half-frame offset)
const actualFrame = Math.floor(video.currentTime * frameRate)
// Seek complete: update frame immediately using Math.floor
const actualFrame = Math.floor(video.currentTime * frameRate + 0.45)
const finalFrame = Math.max(0, Math.min(actualFrame, totalFrames - 1))
setCurrentFrame(finalFrame)
@@ -214,7 +218,7 @@ export function useFramePlayer(
const videoWithCallback = video as unknown as HtmlVideoElementWithCallback
if (videoWithCallback.requestVideoFrameCallback) {
videoWithCallback.requestVideoFrameCallback((_now, metadata) => {
const compositorFrame = Math.floor(metadata.mediaTime * frameRate + 0.001)
const compositorFrame = Math.floor(metadata.mediaTime * frameRate + 0.45)
const verifiedFrame = Math.max(0, Math.min(compositorFrame, totalFrames - 1))
setCurrentFrame(verifiedFrame)
})
@@ -272,7 +276,7 @@ export function useFramePlayer(
const handleExternalSeeked = () => {
// Only sync if this seek was not triggered internally by seekToFrame
if (!isSeekingRef.current) {
const actualFrame = Math.floor(video.currentTime * frameRate + 0.001)
const actualFrame = Math.floor(video.currentTime * frameRate + 0.45)
const finalFrame = Math.max(0, Math.min(actualFrame, totalFrames - 1))
setCurrentFrame(finalFrame)
+2 -2
View File
@@ -80,13 +80,13 @@ export const formatTimecode = (
export const formatTimestamp = (seconds: number, fps: number): string => {
if (isNaN(seconds)) return '00:00:00:00'
const frameIndex = Math.floor(seconds * fps + 0.001)
const frameIndex = Math.floor(seconds * fps + 0.45)
return formatTimecode(frameIndex, fps, 'timecode')
}
export const formatFrame = (seconds: number, fps: number): number => {
if (isNaN(seconds) || !fps) return 0
return Math.floor(seconds * fps + 0.001)
return Math.floor(seconds * fps + 0.45)
}
export const calculateFrameCenterTime = (frameIndex: number, frameRate: number): number => {
@@ -672,7 +672,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
if (startTime !== undefined && startTime !== null && startTime > 0) {
if (startTime !== lastProcessedStartTimeRef.current) {
lastProcessedStartTimeRef.current = startTime
const targetFrame = Math.floor(startTime * frameRate + 0.001)
const targetFrame = Math.floor(startTime * frameRate + 0.45)
seekToFrame(targetFrame)
}
}