import { z } from 'zod' export const transcriptionProviderLanguageCodeSchema = z.string().regex(/^[a-z]{2,3}$/) export const chineseTranscriptLanguageSchema = z.enum(['zh-Hans', 'zh-Hant']) export const transcriptLanguageCodeSchema = transcriptionProviderLanguageCodeSchema.or( chineseTranscriptLanguageSchema, ) export type SavedTranscriptLanguages = { languages: string[] } export type TranscriptSegment = { id: string startMs: number endMs: number text: string } export type SavedTranscript = { id: string language: string segments: TranscriptSegment[] } export const generateTranscriptRequestSchema = z.object({ language: transcriptLanguageCodeSchema.optional(), }) export const updateTranscriptSegmentRequestSchema = z.strictObject({ text: z.string().refine((text) => !text.includes('\0')), }) interface TranscriptVideoProxy { key?: string resolution?: string width: number height: number } // Archive and restore move the original while the full video proxy stays available. export const transcriptVideoStatuses = ['processed', 'archiving', 'archived', 'restoring'] as const export function isTranscriptVideoReady(status: string) { return transcriptVideoStatuses.some((readyStatus) => readyStatus === status) } export function selectTranscriptVideoProxyKey( media?: { videoTranscodes?: TranscriptVideoProxy[]; videoPreview?: { key?: string } } | null, ): string | undefined { let selectedKey: string | undefined let smallestArea = Infinity for (const proxy of media?.videoTranscodes ?? []) { if (!proxy.key || proxy.key === media?.videoPreview?.key || proxy.resolution === '180p') { continue } const area = proxy.width > 0 && proxy.height > 0 ? proxy.width * proxy.height : Infinity if (area < smallestArea) { selectedKey = proxy.key smallestArea = area } } return selectedKey } export const transcriptionErrors = { noAudio: 'The video does not contain an audio track', noProxy: 'A completed full video proxy is required to generate transcripts', tooLarge: 'Audio exceeds the embedding server upload limit', } as const export type GenerateTranscriptResult = | { kind: 'queued' | 'busy'; taskId: string } | { kind: 'existing'; transcriptId: string; language: string } | { kind: 'no_speech' } export type TranscriptJobStatus = | { status: 'pending' | 'processing' | 'completed' | 'no_speech'; taskId: string } | { status: 'failed'; taskId: string; reason: keyof typeof transcriptionErrors | 'unknown' }