feat(video): add SMPTE timecode support and format selection dropdown

This commit is contained in:
yiling
2026-06-28 21:04:40 +08:00
parent 837fe5bd7a
commit eee7b5a677
11 changed files with 246 additions and 27 deletions
@@ -0,0 +1,58 @@
import { describe, it, expect } from 'vitest'
import { formatTimecode } from './utils'
describe('formatTimecode tests', () => {
describe('non-drop-frame calculations', () => {
it('formats non-drop-frame correctly at 25 fps', () => {
// 2999 frames at 25 fps => 00:01:59:24
expect(formatTimecode(2999, 25, 'timecode')).toBe('00:01:59:24')
// 3000 frames at 25 fps => 00:02:00:00
expect(formatTimecode(3000, 25, 'timecode')).toBe('00:02:00:00')
// 15000 frames at 25 fps => 00:10:00:00
expect(formatTimecode(15000, 25, 'timecode')).toBe('00:10:00:00')
// 900000 frames at 25 fps => 10:00:00:00
expect(formatTimecode(900000, 25, 'timecode')).toBe('10:00:00:00')
})
})
describe('drop-frame calculations', () => {
it('formats drop-frame correctly at 29.97 fps', () => {
// 3597 frames at 29.97 fps => 00:01:59;29
expect(formatTimecode(3597, 29.97, 'timecode')).toBe('00:01:59;29')
// 17982 frames at 29.97 fps => 00:10:00;00
expect(formatTimecode(17982, 29.97, 'timecode')).toBe('00:10:00;00')
// 1078920 frames at 29.97 fps => 10:00:00;00
expect(formatTimecode(1078920, 29.97, 'timecode')).toBe('10:00:00;00')
})
it('formats drop-frame correctly at 59.94 fps', () => {
// 3597 * 2 + 1 = 7195 frames at 59.94 fps => 00:01:59;59
expect(formatTimecode(7195, 59.94, 'timecode')).toBe('00:01:59;59')
// 17982 * 2 = 35964 frames at 59.94 fps => 00:10:00;00
expect(formatTimecode(35964, 59.94, 'timecode')).toBe('00:10:00;00')
// 1078920 * 2 = 2157840 frames at 59.94 fps => 10:00:00;00
expect(formatTimecode(2157840, 59.94, 'timecode')).toBe('10:00:00;00')
})
})
describe('starting timecode offsets', () => {
it('offsets timecode correctly based on startTimecode string', () => {
// 0 frames at 29.97 fps with start '01:00:00:00' => 01:00:00:00
expect(formatTimecode(0, 29.97, 'timecode', '01:00:00:00')).toBe('01:00:00:00')
// 0 frames at 29.97 fps with start '01:00:00;00' => 01:00:00;00
expect(formatTimecode(0, 29.97, 'timecode', '01:00:00;00')).toBe('01:00:00;00')
// 30 frames at 30 fps with start '01:00:00:00' => 01:00:01:00
expect(formatTimecode(30, 30, 'timecode', '01:00:00:00')).toBe('01:00:01:00')
})
})
describe('other display modes', () => {
it('formats as frames index', () => {
expect(formatTimecode(150, 30, 'frames')).toBe('150 fr')
})
it('formats as standard MM:SS time', () => {
expect(formatTimecode(150, 30, 'standard')).toBe('00:05')
})
})
})
+72 -8
View File
@@ -2,22 +2,86 @@ export const formatTime = (seconds: number): string => {
if (isNaN(seconds)) return '00:00'
const date = new Date(seconds * 1000)
const hh = date.getUTCHours()
const mm = date.getUTCMinutes()
const mm = date.getUTCMinutes().toString().padStart(2, '0')
const ss = date.getUTCSeconds().toString().padStart(2, '0')
if (hh) {
return `${hh}:${mm.toString().padStart(2, '0')}:${ss}`
return `${hh}:${mm}:${ss}`
}
return `${mm}:${ss}`
}
export const formatTimecode = (
frameIndex: number,
frameRate: number,
mode: 'standard' | 'frames' | 'timecode',
startTimecode?: string | null,
): string => {
if (mode === 'frames') {
return `${frameIndex} fr`
}
if (mode === 'standard') {
return formatTime(frameIndex / frameRate)
}
// Determine if drop-frame should be used (standard for NTSC 29.97 and 59.94)
const frameRateRound = Math.round(frameRate)
const isFractional = frameRate !== frameRateRound
let dropFrame = false
if (isFractional && (frameRateRound === 30 || frameRateRound === 60)) {
dropFrame = true
}
// Parse start timecode if provided
let startFrameCount = 0
if (startTimecode) {
const parts = startTimecode.match(/^([012]\d):(\d\d):(\d\d)(:|;|\.)(\d+)$/)
if (parts) {
const hours = parseInt(parts[1], 10)
const minutes = parseInt(parts[2], 10)
const seconds = parseInt(parts[3], 10)
const frames = parseInt(parts[5], 10)
// Timecode string format overrides the default dropFrame setting
dropFrame = parts[4] !== ':'
// Calculate frame count from timecode
startFrameCount = (hours * 3600 + minutes * 60 + seconds) * frameRateRound + frames
if (dropFrame) {
const totalMinutes = hours * 60 + minutes
const df = frameRate < 30 ? 2 : 4
startFrameCount -= df * (totalMinutes - Math.floor(totalMinutes / 10))
}
}
}
// Calculate absolute frame count
let fc = frameIndex + startFrameCount
// Convert frame count to timecode using the exact smpte-timecode formula
if (dropFrame) {
const df = frameRate < 30 ? 2 : 4
const d = Math.floor(fc / ((17982 * df) / 2))
let m = fc % ((17982 * df) / 2)
if (m < df) {
m = m + df
}
fc += 9 * df * d + df * Math.floor((m - df) / ((1798 * df) / 2))
}
const frames = fc % frameRateRound
const seconds = Math.floor(fc / frameRateRound) % 60
const minutes = Math.floor(fc / (frameRateRound * 60)) % 60
const hours = Math.floor(fc / (frameRateRound * 3600)) % 24
const separator = dropFrame ? ';' : ':'
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}${separator}${frames.toString().padStart(2, '0')}`
}
export const formatTimestamp = (seconds: number, fps: number): string => {
if (isNaN(seconds)) return '00:00:00:00'
const hh = Math.floor(seconds / 3600)
const mm = Math.floor((seconds % 3600) / 60)
const ss = Math.floor(seconds % 60)
const ff = Math.floor((seconds % 1) * fps)
return `${hh.toString().padStart(2, '0')}:${mm.toString().padStart(2, '0')}:${ss.toString().padStart(2, '0')}:${ff.toString().padStart(2, '0')}`
const frameIndex = Math.round(seconds * fps)
return formatTimecode(frameIndex, fps, 'timecode')
}
export const formatFrame = (seconds: number, fps: number): number => {
@@ -1,6 +1,8 @@
import type { AssetInfo, VideoTranscode } from '@shumai/dtos'
import { cn } from '@/ui/lib/utils'
import {
Check,
ChevronDown,
Download,
Maximize,
Minimize,
@@ -25,8 +27,9 @@ import {
} from '../ui/dropdown-menu'
import { Slider } from '../ui/slider'
import ProgressBar from './progress-bar'
import { formatTime } from './utils'
import { formatTimecode } from './utils'
import { useFramePlayer } from './use-frame-player'
import { useUiStore } from '@/ui/stores/ui'
import type { Annotation } from '@/ui/types'
import DrawingCanvas from '../drawing-canvas'
@@ -71,7 +74,6 @@ interface ControlBarProps {
toggleLoop: () => void
toggleMute: () => void
handleVolumeChange: (newVolume: number) => void
setState: React.Dispatch<React.SetStateAction<PlayerState>>
changePlaybackRate: (rate: number) => void
changeResolution: (res: DisplayTranscode) => void
handleDownload: (url: string, resolution: string) => void
@@ -95,7 +97,6 @@ const ControlBar: React.FC<ControlBarProps> = ({
toggleLoop,
toggleMute,
handleVolumeChange,
setState,
changePlaybackRate,
changeResolution,
handleDownload,
@@ -112,6 +113,14 @@ const ControlBar: React.FC<ControlBarProps> = ({
const previewResolutions = resolutions.filter((r) => !r.isRaw)
const { videoTimeDisplayMode, setVideoTimeDisplayMode } = useUiStore()
const startTimecode = data.media?.metadata?.startTimecode
const displayString =
videoTimeDisplayMode === 'frames'
? `${currentFrame} / ${totalFrames} fr`
: `${formatTimecode(currentFrame, frameRate, videoTimeDisplayMode, startTimecode)} / ${formatTimecode(totalFrames, frameRate, videoTimeDisplayMode, startTimecode)}`
return (
<div
className={cn(
@@ -189,17 +198,47 @@ const ControlBar: React.FC<ControlBarProps> = ({
</div>
</div>
<div
className="min-w-[100px] cursor-pointer text-sm font-medium tabular-nums hover:text-primary"
onClick={() => setState((p) => ({ ...p, showFrames: !p.showFrames }))}
title="Click to toggle Time/Frames"
>
{state.showFrames
? `${currentFrame} / ${totalFrames}`
: `${formatTime(currentFrame / frameRate)} / ${formatTime(totalFrames / frameRate)}`}
<span className="ml-1 text-xs text-muted-foreground">
{state.showFrames ? 'fr' : ''}
</span>
<div className="flex items-center gap-1 text-sm font-medium tabular-nums select-none min-w-[100px]">
<span className="text-muted-foreground font-mono">{displayString}</span>
<DropdownMenu modal={false}>
<DropdownMenuTrigger asChild>
<button className="h-6 w-6 inline-flex items-center justify-center rounded-md hover:bg-muted text-muted-foreground hover:text-foreground transition-colors cursor-pointer focus:outline-none">
<ChevronDown className="h-3.5 w-3.5" />
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-40 z-30">
<DropdownMenuLabel className="text-[10px] font-bold text-muted-foreground uppercase tracking-wider">
Format
</DropdownMenuLabel>
<DropdownMenuItem
onClick={() => setVideoTimeDisplayMode('standard')}
className="flex items-center justify-between cursor-pointer text-xs"
>
<span>Standard Time</span>
{videoTimeDisplayMode === 'standard' && (
<Check className="h-3.5 w-3.5 text-primary" />
)}
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => setVideoTimeDisplayMode('frames')}
className="flex items-center justify-between cursor-pointer text-xs"
>
<span>Frames</span>
{videoTimeDisplayMode === 'frames' && (
<Check className="h-3.5 w-3.5 text-primary" />
)}
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => setVideoTimeDisplayMode('timecode')}
className="flex items-center justify-between cursor-pointer text-xs"
>
<span>Timecode</span>
{videoTimeDisplayMode === 'timecode' && (
<Check className="h-3.5 w-3.5 text-primary" />
)}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>