diff --git a/packages/core/src/transcode/transcode.ts b/packages/core/src/transcode/transcode.ts index 640cd624..6e50e818 100644 --- a/packages/core/src/transcode/transcode.ts +++ b/packages/core/src/transcode/transcode.ts @@ -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 || '', } diff --git a/packages/db/src/prisma-json-types.ts b/packages/db/src/prisma-json-types.ts index a26e9b72..a3614c45 100644 --- a/packages/db/src/prisma-json-types.ts +++ b/packages/db/src/prisma-json-types.ts @@ -107,6 +107,7 @@ declare global { bitRate: number frameRate: number totalFrames?: number + startTimecode?: string videoCodec?: string audioCodec?: string audioChannels?: number diff --git a/packages/dtos/src/asset.ts b/packages/dtos/src/asset.ts index eb2b3e3c..57503246 100644 --- a/packages/dtos/src/asset.ts +++ b/packages/dtos/src/asset.ts @@ -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 diff --git a/packages/transcode/src/activities/transcode.ts b/packages/transcode/src/activities/transcode.ts index 79b9c70d..7d21a604 100644 --- a/packages/transcode/src/activities/transcode.ts +++ b/packages/transcode/src/activities/transcode.ts @@ -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, diff --git a/packages/webui/components/chat/message-card.tsx b/packages/webui/components/chat/message-card.tsx index 0a7d70b1..0cfea015 100644 --- a/packages/webui/components/chat/message-card.tsx +++ b/packages/webui/components/chat/message-card.tsx @@ -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 = ({ isSelected, onSelect, frameRate, + startTimecode, }) => { const message = initialMessage const hasDrawInfo = @@ -66,6 +69,13 @@ export const MessageCard: React.FC = ({ 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 = ({
{message.second !== null && message.second !== undefined && frameRate && (
- {formatTimestamp(message.second, frameRate)} + {displayTime}
)} diff --git a/packages/webui/components/chat/message-input.tsx b/packages/webui/components/chat/message-input.tsx index 4b82209b..4fb853fb 100644 --- a/packages/webui/components/chat/message-input.tsx +++ b/packages/webui/components/chat/message-input.tsx @@ -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( disableMentions = false, currentTime, frameRate, + startTimecode, onTyping, }, ref, @@ -92,6 +95,13 @@ export const ChatInput = React.forwardRef( const [viewingFile, setViewingFile] = useState(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( 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(null) const fileInputRef = useRef(null) const mentionRangeRef = useRef(null) @@ -645,7 +665,7 @@ export const ChatInput = React.forwardRef( }} 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}
)}
( 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}`} />
diff --git a/packages/webui/components/file-viewer-right-sidebar.tsx b/packages/webui/components/file-viewer-right-sidebar.tsx index b77a31db..2c982264 100644 --- a/packages/webui/components/file-viewer-right-sidebar.tsx +++ b/packages/webui/components/file-viewer-right-sidebar.tsx @@ -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} /> diff --git a/packages/webui/components/viewers/utils.test.ts b/packages/webui/components/viewers/utils.test.ts new file mode 100644 index 00000000..b25125f1 --- /dev/null +++ b/packages/webui/components/viewers/utils.test.ts @@ -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') + }) + }) +}) diff --git a/packages/webui/components/viewers/utils.ts b/packages/webui/components/viewers/utils.ts index b20f9dd4..e44c73ee 100644 --- a/packages/webui/components/viewers/utils.ts +++ b/packages/webui/components/viewers/utils.ts @@ -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 => { diff --git a/packages/webui/components/viewers/video-player.tsx b/packages/webui/components/viewers/video-player.tsx index 1303cae9..c89b5d76 100644 --- a/packages/webui/components/viewers/video-player.tsx +++ b/packages/webui/components/viewers/video-player.tsx @@ -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> changePlaybackRate: (rate: number) => void changeResolution: (res: DisplayTranscode) => void handleDownload: (url: string, resolution: string) => void @@ -95,7 +97,6 @@ const ControlBar: React.FC = ({ toggleLoop, toggleMute, handleVolumeChange, - setState, changePlaybackRate, changeResolution, handleDownload, @@ -112,6 +113,14 @@ const ControlBar: React.FC = ({ 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 (
= ({
-
setState((p) => ({ ...p, showFrames: !p.showFrames }))} - title="Click to toggle Time/Frames" - > - {state.showFrames - ? `${currentFrame} / ${totalFrames}` - : `${formatTime(currentFrame / frameRate)} / ${formatTime(totalFrames / frameRate)}`} - - {state.showFrames ? 'fr' : ''} - +
+ {displayString} + + + + + + + Format + + setVideoTimeDisplayMode('standard')} + className="flex items-center justify-between cursor-pointer text-xs" + > + Standard Time + {videoTimeDisplayMode === 'standard' && ( + + )} + + setVideoTimeDisplayMode('frames')} + className="flex items-center justify-between cursor-pointer text-xs" + > + Frames + {videoTimeDisplayMode === 'frames' && ( + + )} + + setVideoTimeDisplayMode('timecode')} + className="flex items-center justify-between cursor-pointer text-xs" + > + Timecode + {videoTimeDisplayMode === 'timecode' && ( + + )} + + +
diff --git a/packages/webui/stores/ui.ts b/packages/webui/stores/ui.ts index 300450e0..4669a787 100644 --- a/packages/webui/stores/ui.ts +++ b/packages/webui/stores/ui.ts @@ -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()( @@ -56,6 +59,9 @@ export const useUiStore = create()( set((state) => ({ viewModes: { ...state.viewModes, [projectId]: mode }, })), + + videoTimeDisplayMode: 'standard', + setVideoTimeDisplayMode: (mode) => set({ videoTimeDisplayMode: mode }), }), { name: 'ui-storage',