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)
}
}
+11 -10
View File
@@ -51,14 +51,13 @@ $$\text{Time}_{\text{boundary}} = \frac{N}{\text{frameRate}}$$
---
### B. Time-to-Frame Conversion (Floor with Epsilon)
When converting a raw player timestamp ($t$) back to an integer frame index, we use a **Floor with Epsilon** formula to absorb sub-millisecond clock drift and presentation lag without triggering premature frame jumps:
### B. Time-to-Frame Conversion (Floor with 0.45 Offset)
When converting a raw player timestamp ($t$) back to an integer frame index, we use a **Floor with 0.45 Offset** formula to absorb sub-millisecond clock drift, compositor presentation offsets, and V-Sync boundaries:
$$\text{Frame} = \lfloor(t \cdot \text{frameRate}) + \epsilon\rfloor$$
$$\text{Frame} = \lfloor(t \cdot \text{frameRate}) + 0.45\rfloor$$
*Where $\epsilon = 0.001$ frames.*
* **Start Boundary Guard:** If the browser reports time slightly early due to float precision (e.g. $119.9999$ frames instead of $120.0$), the $+ 0.001$ bumps it to $120.0009$, flooring correctly to Frame `120`.
* **Late Pause Guard:** If the playhead stops very late in the frame (e.g. $120.96$ frames), adding `0.001` yields $120.961$, which still floors correctly to **Frame `120`**, matching what is actually displayed on the screen.
* **Compositor Lag Guard:** Since the browser compositor typically presents a frame slightly before the playhead crosses the exact mathematical start time (due to V-Sync ticks and decoding latency), the browser transitions visually around the half-frame mark. An offset of `0.45` shifts the transition boundary to `0.55` frames, matching user perception.
* **Seek Buffer Guard:** When we seek to the safe center of a frame ($N + 0.5$ frames), using a `0.45` offset yields $N + 0.95$, which floors back to the correct frame index $N$ even if the browser has minor floating-point seek target deviations.
---
@@ -121,8 +120,10 @@ When playing, the playhead loop coordinates two mechanisms in parallel to preven
2. The `pause` event listener triggers `handlePause()`:
* Cancels active VFC (`cancelVideoFrameCallback`) and RAF (`cancelAnimationFrame`) loops.
* Performs a final playhead synchronization:
$$\text{clampedFrame} = \max\left(0, \min\left(\lfloor(video.currentTime \cdot frameRate) + 0.001\rfloor, \text{totalFrames} - 1\right)\right)$$
$$\text{clampedFrame} = \max\left(0, \min\left(\lfloor(video.currentTime \cdot frameRate) + 0.45\rfloor, \text{totalFrames} - 1\right)\right)$$
* Calls `setCurrentFrame(clampedFrame)` to lock the UI to the correct frame.
* **Snaps the browser playhead** to the calculated frame's center time to force the browser compositor to align:
$$\text{currentTime} = \text{calculateFrameCenterTime}(\text{clampedFrame}, \text{frameRate})$$
---
@@ -164,9 +165,9 @@ When playing, the playhead loop coordinates two mechanisms in parallel to preven
```
3. The browser seeks the media element. Once finished, it fires the native `seeked` event.
4. The `handleExternalSeeked` listener in `useFramePlayer` triggers because `isSeekingRef.current` is `false`:
* Maps `video.currentTime` back to the frame index:
$$\text{finalFrame} = \lfloor(video.currentTime \cdot frameRate) + 0.001\rfloor = \lfloor(5.005005 \cdot 23.976) + 0.001\rfloor = 120$$
* Computes the safe center time: `safeCenterTime = calculateFrameCenterTime(120, 23.976) = 5.025859s`.
* Maps `video.currentTime` back to the frame index:
$$\text{finalFrame} = \lfloor(video.currentTime \cdot frameRate) + 0.45\rfloor = \lfloor(5.005005 \cdot 23.976) + 0.45\rfloor = 120$$
* Computes the safe center time: `safeCenterTime = calculateFrameCenterTime(120, 23.976) = 5.025859s`.
* Detects if the current time is offset from the center by more than a quarter-frame:
$$\text{Math.abs}(5.005005 - 5.025859) = 0.020854\text{s} > \frac{0.041708}{4}\text{s}$$
* Nudges the browser's playhead: `video.currentTime = safeCenterTime` (`5.025859`s). This forces the browser to render Frame 120 cleanly.