mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 14:28:18 +00:00
test(video): extract calculateFrameCenterTime helper and add unit tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import React, { useState, useEffect, useRef, useCallback } from 'react'
|
||||
import { calculateFrameCenterTime } from './utils'
|
||||
|
||||
export interface UseFramePlayerResult {
|
||||
currentFrame: number
|
||||
@@ -158,8 +159,7 @@ export function useFramePlayer(
|
||||
setCurrentFrame(clampedFrame) // Optimistic update for active seek
|
||||
|
||||
const performSeek = async (frameToSeek: number) => {
|
||||
const frameDuration = 1 / frameRate
|
||||
const safeTargetTime = frameToSeek * frameDuration + frameDuration / 2
|
||||
const safeTargetTime = calculateFrameCenterTime(frameToSeek, frameRate)
|
||||
const wasPlaying = !video.paused
|
||||
|
||||
video.currentTime = safeTargetTime
|
||||
@@ -250,8 +250,8 @@ export function useFramePlayer(
|
||||
setCurrentFrame(finalFrame)
|
||||
|
||||
// Nudge to safe frame center to guarantee correct browser decoding
|
||||
const safeCenterTime = calculateFrameCenterTime(finalFrame, frameRate)
|
||||
const frameDuration = 1 / frameRate
|
||||
const safeCenterTime = finalFrame * frameDuration + frameDuration / 2
|
||||
if (Math.abs(video.currentTime - safeCenterTime) > frameDuration / 4) {
|
||||
video.currentTime = safeCenterTime
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { formatTimecode } from './utils'
|
||||
import { formatTimecode, calculateFrameCenterTime } from './utils'
|
||||
|
||||
describe('formatTimecode tests', () => {
|
||||
describe('non-drop-frame calculations', () => {
|
||||
@@ -55,4 +55,15 @@ describe('formatTimecode tests', () => {
|
||||
expect(formatTimecode(150, 30, 'standard')).toBe('00:05')
|
||||
})
|
||||
})
|
||||
|
||||
describe('calculateFrameCenterTime tests', () => {
|
||||
it('calculates frame center time correctly', () => {
|
||||
// frame 0 at 30 fps => 0 * 1/30 + 1/60 = 1/60 = 0.0166666...
|
||||
expect(calculateFrameCenterTime(0, 30)).toBeCloseTo(0.0166666, 5)
|
||||
// frame 251 at 30 fps => 251 * 1/30 + 1/60 = 251.5 / 30 = 8.3833333...
|
||||
expect(calculateFrameCenterTime(251, 30)).toBeCloseTo(8.3833333, 5)
|
||||
// frame rate 0 returns 0
|
||||
expect(calculateFrameCenterTime(10, 0)).toBe(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -88,3 +88,9 @@ export const formatFrame = (seconds: number, fps: number): number => {
|
||||
if (isNaN(seconds) || !fps) return 0
|
||||
return Math.floor(seconds * fps)
|
||||
}
|
||||
|
||||
export const calculateFrameCenterTime = (frameIndex: number, frameRate: number): number => {
|
||||
if (!frameRate) return 0
|
||||
const frameDuration = 1 / frameRate
|
||||
return frameIndex * frameDuration + frameDuration / 2
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user