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
+4
View File
@@ -19,6 +19,7 @@ export interface MediaMetadata {
bitRate: number
frameRate: number
totalFrames: number
startTimecode?: string
hasAudio: boolean
videoCodec?: string
audioCodec?: string
@@ -123,6 +124,7 @@ export class TranscodeService {
if (!totalFrames && fps > 0 && !isNaN(duration)) {
totalFrames = Math.round(duration * fps)
}
const startTimecode = videoStream.tags?.timecode || info.format?.tags?.timecode
return {
originalWidth: videoStream.width,
@@ -131,6 +133,7 @@ export class TranscodeService {
bitRate: parseFloat(info.format.bit_rate),
frameRate: fps || 30,
totalFrames: totalFrames || 0,
startTimecode,
hasAudio: !!audioStream,
videoCodec: this.resolveCodecName(videoStream),
audioCodec: audioStream ? this.resolveCodecName(audioStream) : undefined,
@@ -161,6 +164,7 @@ export class TranscodeService {
bitRate: 0,
frameRate: 0,
totalFrames: 0,
startTimecode: undefined,
hasAudio: false,
mimeType: metadata.format || '',
}
+1
View File
@@ -107,6 +107,7 @@ declare global {
bitRate: number
frameRate: number
totalFrames?: number
startTimecode?: string
videoCodec?: string
audioCodec?: string
audioChannels?: number
+1
View File
@@ -48,6 +48,7 @@ export const mediaMetadataSchema = z.object({
originalHeight: z.number().optional(),
frameRate: z.number().optional(),
totalFrames: z.number().optional(),
startTimecode: z.string().optional(),
})
export type MediaMetadata = z.infer<typeof mediaMetadataSchema>
@@ -91,6 +91,7 @@ export async function getMediaInfoActivity(params: {
bitRate: info.bitRate,
frameRate: info.frameRate,
totalFrames: info.totalFrames,
startTimecode: info.startTimecode,
hasAudio: info.hasAudio,
videoCodec: info.videoCodec,
audioCodec: info.audioCodec,
@@ -17,8 +17,9 @@ import Markdown from 'react-markdown'
import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar'
import { DrawAnnotation } from '../ui/icons'
import { Skeleton } from '../ui/skeleton'
import { formatTimestamp } from '../viewers/utils'
import { formatTimecode } from '../viewers/utils'
import { m } from '@/ui/paraglide/messages.js'
import { useUiStore } from '@/ui/stores/ui'
interface MessageCardProps {
teamId?: string
@@ -32,6 +33,7 @@ interface MessageCardProps {
isSelected?: boolean
onSelect?: () => void
frameRate?: number
startTimecode?: string
rootParentId?: string
}
@@ -55,6 +57,7 @@ export const MessageCard: React.FC<MessageCardProps> = ({
isSelected,
onSelect,
frameRate,
startTimecode,
}) => {
const message = initialMessage
const hasDrawInfo =
@@ -66,6 +69,13 @@ export const MessageCard: React.FC<MessageCardProps> = ({
const [isLogsOpen, setIsLogsOpen] = React.useState(false)
const { videoTimeDisplayMode } = useUiStore()
const displayTime = React.useMemo(() => {
if (message.second === null || message.second === undefined || !frameRate) return ''
const frameIndex = Math.round(message.second * frameRate)
return formatTimecode(frameIndex, frameRate, videoTimeDisplayMode, startTimecode)
}, [message.second, frameRate, videoTimeDisplayMode, startTimecode])
const { data: me } = useQuery({
queryKey: ['teams', teamId, 'me'],
queryFn: async () => {
@@ -171,7 +181,7 @@ export const MessageCard: React.FC<MessageCardProps> = ({
<div className="flow-root mt-1">
{message.second !== null && message.second !== undefined && frameRate && (
<div className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2.5 py-1 mt-[2px] mr-2 rounded-sm text-xs leading-none font-mono font-bold flex items-center gap-1">
{formatTimestamp(message.second, frameRate)}
{displayTime}
</div>
)}
@@ -27,7 +27,8 @@ import { Button } from '../ui/button'
import { DrawAnnotation } from '../ui/icons'
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
import { ProgressCircle } from '../ui/progress-circle'
import { formatTimestamp } from '../viewers/utils'
import { formatTimecode } from '../viewers/utils'
import { useUiStore } from '@/ui/stores/ui'
type UploadingFile = {
id: string // A unique ID for the file, e.g., timestamp + name
@@ -54,6 +55,7 @@ interface ChatInputProps {
disableMentions?: boolean
currentTime?: number
frameRate?: number
startTimecode?: string
onTyping?: () => void
}
@@ -84,6 +86,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
disableMentions = false,
currentTime,
frameRate,
startTimecode,
onTyping,
},
ref,
@@ -92,6 +95,13 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
const [viewingFile, setViewingFile] = useState<File | null>(null)
const [isTimestampEnabled, setIsTimestampEnabled] = useState(true)
const { videoTimeDisplayMode } = useUiStore()
const displayTime = React.useMemo(() => {
if (currentTime === undefined || !frameRate) return ''
const frameIndex = Math.round(currentTime * frameRate)
return formatTimecode(frameIndex, frameRate, videoTimeDisplayMode, startTimecode)
}, [currentTime, frameRate, videoTimeDisplayMode, startTimecode])
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
const { members: storeMembers, loading: membersLoading, fetchMembers } = useMemberStore()
@@ -123,6 +133,16 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
const [collapsedSections, setCollapsedSections] = useState({ bots: false, users: false })
const [hasContent, setHasContent] = useState(false)
const paddingLeftClass = React.useMemo(() => {
if (!isTimestampEnabled || currentTime === undefined || !frameRate || hasContent) {
return ''
}
if (videoTimeDisplayMode === 'timecode') {
return 'pl-[120px]'
}
return 'pl-[85px]'
}, [isTimestampEnabled, currentTime, frameRate, hasContent, videoTimeDisplayMode])
const editorRef = useRef<HTMLDivElement>(null)
const fileInputRef = useRef<HTMLInputElement>(null)
const mentionRangeRef = useRef<Range | null>(null)
@@ -645,7 +665,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
}}
className="float-left select-none bg-blue-100 dark:bg-blue-900/40 text-blue-600 dark:text-blue-300 px-2.5 py-0.5 mt-3 mr-2 rounded-sm text-xs font-mono font-bold flex items-center gap-1 cursor-text"
>
{formatTimestamp(currentTime, frameRate)}
{displayTime}
</div>
)}
<div
@@ -655,7 +675,7 @@ export const ChatInput = React.forwardRef<HTMLDivElement, ChatInputProps>(
onInput={handleInput}
onKeyDown={handleKeyDown}
data-placeholder={replyingTo ? 'Reply...' : 'Message...'}
className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block ${isTimestampEnabled && currentTime !== undefined && frameRate !== undefined && !hasContent ? 'pl-[120px]' : ''}`}
className={`bg-transparent border-none focus:ring-0 resize-none min-h-[40px] leading-relaxed py-2 focus:outline-none block ${paddingLeftClass}`}
/>
</div>
@@ -359,6 +359,11 @@ export function FileViewerRightSidebar({
? file?.media?.metadata?.frameRate || 30
: undefined
}
startTimecode={
file?.mediaType?.startsWith('video/')
? file?.media?.metadata?.startTimecode
: undefined
}
isSelected={selectedCommentId === comment.id}
onSelect={() => {
onCommentSelect?.(comment)
@@ -380,6 +385,11 @@ export function FileViewerRightSidebar({
? file?.media?.metadata?.frameRate || 30
: undefined
}
startTimecode={
file?.mediaType?.startsWith('video/')
? file?.media?.metadata?.startTimecode
: undefined
}
isSelected={selectedCommentId === reply.id}
onSelect={() => {
onCommentSelect?.(reply)
@@ -413,6 +423,11 @@ export function FileViewerRightSidebar({
? file?.media?.metadata?.frameRate || 30
: undefined
}
startTimecode={
file?.mediaType?.startsWith('video/')
? file?.media?.metadata?.startTimecode
: undefined
}
onTyping={onTyping}
/>
</GuestIdentityPopup>
@@ -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>
+6
View File
@@ -22,6 +22,9 @@ interface UiState {
viewModes: { [projectId: string]: 'card' | 'list' }
setViewMode: (projectId: string, mode: 'card' | 'list') => void
videoTimeDisplayMode: 'standard' | 'frames' | 'timecode'
setVideoTimeDisplayMode: (mode: 'standard' | 'frames' | 'timecode') => void
}
export const useUiStore = create<UiState>()(
@@ -56,6 +59,9 @@ export const useUiStore = create<UiState>()(
set((state) => ({
viewModes: { ...state.viewModes, [projectId]: mode },
})),
videoTimeDisplayMode: 'standard',
setVideoTimeDisplayMode: (mode) => set({ videoTimeDisplayMode: mode }),
}),
{
name: 'ui-storage',