refactor(webui): use Floor with Epsilon (0.001) for frame conversion calculations

This commit is contained in:
yiling
2026-06-30 10:22:32 +08:00
parent 07d82377bd
commit 1704fddc67
3 changed files with 9 additions and 9 deletions
@@ -75,7 +75,7 @@ export function useFramePlayer(
const isVfcStalled = Date.now() - lastVfcTime > stallThreshold const isVfcStalled = Date.now() - lastVfcTime > stallThreshold
// Print debug log roughly once per second during playback // Print debug log roughly once per second during playback
if (Math.round(video.currentTime * frameRate) % 30 === 0) { if (Math.floor(video.currentTime * frameRate + 0.001) % 30 === 0) {
console.log( console.log(
`[useFramePlayer] currentTime: ${video.currentTime}, isVfcStalled: ${isVfcStalled}, stallThreshold: ${stallThreshold}, totalFrames: ${totalFrames}`, `[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 // Only update playhead via currentTime (RAF) if rVFC has stalled or isn't supported
if (!videoWithCallback.requestVideoFrameCallback || isVfcStalled) { if (!videoWithCallback.requestVideoFrameCallback || isVfcStalled) {
const frame = Math.round(video.currentTime * frameRate) const frame = Math.floor(video.currentTime * frameRate + 0.001)
const clamped = Math.max(0, Math.min(frame, totalFrames - 1)) const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
setCurrentFrame(clamped) setCurrentFrame(clamped)
} }
@@ -98,7 +98,7 @@ export function useFramePlayer(
rVfcId = null rVfcId = null
lastVfcTime = Date.now() // Reset stall timer lastVfcTime = Date.now() // Reset stall timer
const compositorFrame = Math.round(metadata.mediaTime * frameRate) const compositorFrame = Math.floor(metadata.mediaTime * frameRate + 0.001)
const compositorClamped = Math.max(0, Math.min(compositorFrame, totalFrames - 1)) const compositorClamped = Math.max(0, Math.min(compositorFrame, totalFrames - 1))
setCurrentFrame(compositorClamped) setCurrentFrame(compositorClamped)
}, },
@@ -128,7 +128,7 @@ export function useFramePlayer(
} }
// Synchronize frame on pause // Synchronize frame on pause
const frame = Math.round(video.currentTime * frameRate) const frame = Math.floor(video.currentTime * frameRate + 0.001)
const clamped = Math.max(0, Math.min(frame, totalFrames - 1)) const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
setCurrentFrame(clamped) setCurrentFrame(clamped)
} }
@@ -214,7 +214,7 @@ export function useFramePlayer(
const videoWithCallback = video as unknown as HtmlVideoElementWithCallback const videoWithCallback = video as unknown as HtmlVideoElementWithCallback
if (videoWithCallback.requestVideoFrameCallback) { if (videoWithCallback.requestVideoFrameCallback) {
videoWithCallback.requestVideoFrameCallback((_now, metadata) => { videoWithCallback.requestVideoFrameCallback((_now, metadata) => {
const compositorFrame = Math.round(metadata.mediaTime * frameRate) const compositorFrame = Math.floor(metadata.mediaTime * frameRate + 0.001)
const verifiedFrame = Math.max(0, Math.min(compositorFrame, totalFrames - 1)) const verifiedFrame = Math.max(0, Math.min(compositorFrame, totalFrames - 1))
setCurrentFrame(verifiedFrame) setCurrentFrame(verifiedFrame)
}) })
@@ -272,7 +272,7 @@ export function useFramePlayer(
const handleExternalSeeked = () => { const handleExternalSeeked = () => {
// Only sync if this seek was not triggered internally by seekToFrame // Only sync if this seek was not triggered internally by seekToFrame
if (!isSeekingRef.current) { if (!isSeekingRef.current) {
const actualFrame = Math.round(video.currentTime * frameRate) const actualFrame = Math.floor(video.currentTime * frameRate + 0.001)
const finalFrame = Math.max(0, Math.min(actualFrame, totalFrames - 1)) const finalFrame = Math.max(0, Math.min(actualFrame, totalFrames - 1))
setCurrentFrame(finalFrame) setCurrentFrame(finalFrame)
+2 -2
View File
@@ -80,13 +80,13 @@ export const formatTimecode = (
export const formatTimestamp = (seconds: number, fps: number): string => { export const formatTimestamp = (seconds: number, fps: number): string => {
if (isNaN(seconds)) return '00:00:00:00' if (isNaN(seconds)) return '00:00:00:00'
const frameIndex = Math.round(seconds * fps) const frameIndex = Math.floor(seconds * fps + 0.001)
return formatTimecode(frameIndex, fps, 'timecode') return formatTimecode(frameIndex, fps, 'timecode')
} }
export const formatFrame = (seconds: number, fps: number): number => { export const formatFrame = (seconds: number, fps: number): number => {
if (isNaN(seconds) || !fps) return 0 if (isNaN(seconds) || !fps) return 0
return Math.floor(seconds * fps) return Math.floor(seconds * fps + 0.001)
} }
export const calculateFrameCenterTime = (frameIndex: number, frameRate: number): number => { export const calculateFrameCenterTime = (frameIndex: number, frameRate: number): number => {
@@ -653,7 +653,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
if (startTime !== undefined && startTime !== null && startTime > 0) { if (startTime !== undefined && startTime !== null && startTime > 0) {
if (startTime !== lastProcessedStartTimeRef.current) { if (startTime !== lastProcessedStartTimeRef.current) {
lastProcessedStartTimeRef.current = startTime lastProcessedStartTimeRef.current = startTime
const targetFrame = Math.round(startTime * frameRate) const targetFrame = Math.floor(startTime * frameRate + 0.001)
seekToFrame(targetFrame) seekToFrame(targetFrame)
} }
} }