diff --git a/AGENTS.md b/AGENTS.md index 033ff051..5e5bbc13 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -92,6 +92,13 @@ Briefly describe what this PR changes and why. - Describe the checks or tests you ran. - Include relevant outputs if applicable. +## Manual Verification Plan + +- Provide concise instructions for manually verifying this change. +- Assume familiarity with the project and existing development workflow. +- Skip boilerplate steps (e.g. starting the app, logging in, opening common pages) unless they are part of the change. +- Focus on what needs to be verified, including scenarios, edge cases, and expected results. + ## Notes Add any additional context, caveats, or follow-up work. diff --git a/CHANGELOG.md b/CHANGELOG.md index e52cb36c..fb79fece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Added +- **transcode**: Add configurable target resolution ladders (480p, 720p, 1080p, 1440p, 2160p) for MP4 Multi Resolutions transcoding in team settings, with unified resolution matching and lowest-resolution fallback for both MP4 and HLS - **transcode**: Add HLS live streaming support with fMP4/CMAF adaptive bitrate ladders (480p, 720p, 1080p, 1440p, 2160p), configurable in Transcode Settings, with seamless adaptive streaming, auto quality switching, manual resolution selection in the video viewer, and full watermarking support for shared links - **transcode**: Add basic camera RAW image support for popular camera formats (including Sony ARW, Canon CR2/CR3, Nikon NEF, Adobe DNG, Fujifilm RAF, and Panasonic RW2), extracting high-quality embedded JPEG previews and automatically applying camera orientation for fast thumbnail and proxy generation ### Fixed +- **webui**: Fix an issue where legacy portrait video transcodes were mislabeled in download menus and the video player (e.g. "1920p" instead of "1080p") by correctly deriving resolution labels from the long side +- **settings**: Fix an issue where rapid consecutive toggling of transcode resolution checkboxes could submit outdated selections or drop previous toggles +- **webui**: Fix an issue where the resolution selector in the video seekbar displayed vertical video resolutions by long side (e.g. "Auto (3840p)") instead of standard short side (e.g. "Auto (2160p)") +- **webui**: Fix an issue where 480p videos were mislabeled as 360p in the navigation bar and mobile file header download menus - **share**: Fix an issue where public HLS playback and watermarked streams failed to load for shared version stacks and symlinks - **trash**: Fix an issue where the "Empty Trash" confirmation dialog would close immediately instead of showing a loading state while files were being permanently deleted diff --git a/packages/api/src/team.test.ts b/packages/api/src/team.test.ts index 71d7d452..503a1960 100644 --- a/packages/api/src/team.test.ts +++ b/packages/api/src/team.test.ts @@ -245,17 +245,17 @@ describe('team api', () => { }) it('PATCH /teams/:teamId/settings updates settings', async () => { - mockUpdateSettings.mockResolvedValue({ transcode: { videoStrategy: 'all' } }) + mockUpdateSettings.mockResolvedValue({ transcode: { videoStrategy: 'multi' } }) const res = await app.request('/teams/t1/settings', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ key: 'transcode.videoStrategy', value: 'all' }), + body: JSON.stringify({ key: 'transcode.videoStrategy', value: 'multi' }), }) expect(res.status).toBe(200) const data = await res.json() - expect(data.transcode.videoStrategy).toBe('all') + expect(data.transcode.videoStrategy).toBe('multi') expect(authzService.hasPermission).toHaveBeenCalledWith( expect.objectContaining({ type: ResourceType.Team, @@ -263,7 +263,30 @@ describe('team api', () => { permission: Permission.Admin, }), ) - expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'transcode.videoStrategy', 'all') + expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'transcode.videoStrategy', 'multi') + }) + + it('PATCH /teams/:teamId/settings updates transcode.videoResolutions', async () => { + mockUpdateSettings.mockResolvedValue({ + transcode: { videoResolutions: ['720p', '1080p'] }, + }) + + const res = await app.request('/teams/t1/settings', { + method: 'PATCH', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + key: 'transcode.videoResolutions', + value: ['720p', '1080p'], + }), + }) + + expect(res.status).toBe(200) + const data = await res.json() + expect(data.transcode.videoResolutions).toEqual(['720p', '1080p']) + expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'transcode.videoResolutions', [ + '720p', + '1080p', + ]) }) it('PATCH /teams/:teamId/settings updates transcode.hardwareAcceleration', async () => { diff --git a/packages/core/src/team/team.test.ts b/packages/core/src/team/team.test.ts index bae693d6..c9feaabb 100644 --- a/packages/core/src/team/team.test.ts +++ b/packages/core/src/team/team.test.ts @@ -369,6 +369,7 @@ describe('TeamService', () => { semanticSearchEnabled: false, transcode: { videoStrategy: 'best_match', + videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'], hardwareAcceleration: 'off', threads: 0, hlsEnabled: false, @@ -385,6 +386,7 @@ describe('TeamService', () => { semanticSearchEnabled: false, transcode: { videoStrategy: 'best_match', + videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'], hardwareAcceleration: 'off', threads: 0, hlsEnabled: false, @@ -393,7 +395,8 @@ describe('TeamService', () => { appearance: { hideAgent: false }, }) - await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'all') + await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'multi') + await teamService.updateSettings(team.id, 'transcode.videoResolutions', ['720p', '1080p']) await teamService.updateSettings(team.id, 'transcode.hardwareAcceleration', 'auto') await teamService.updateSettings(team.id, 'transcode.threads', 4) await teamService.updateSettings(team.id, 'transcode.hlsEnabled', true) @@ -408,7 +411,8 @@ describe('TeamService', () => { expect(finalSettings).toEqual({ theme: 'dark', transcode: { - videoStrategy: 'all', + videoStrategy: 'multi', + videoResolutions: ['720p', '1080p'], hardwareAcceleration: 'auto', threads: 4, hlsEnabled: true, diff --git a/packages/core/src/team/team.ts b/packages/core/src/team/team.ts index efcd20dc..58546680 100644 --- a/packages/core/src/team/team.ts +++ b/packages/core/src/team/team.ts @@ -35,6 +35,7 @@ export class TeamService { settings: { transcode: { videoStrategy: 'best_match', + videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'], hardwareAcceleration: 'off', threads: 0, }, @@ -312,12 +313,22 @@ export class TeamService { if (!settings.transcode) { settings.transcode = { videoStrategy: 'best_match', + videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'], hardwareAcceleration: 'off', threads: 0, hlsEnabled: false, hlsResolutions: ['480p', '720p', '1080p'], } } else { + const strategyStr = settings.transcode.videoStrategy as string | undefined + if (strategyStr === 'single' || strategyStr === 'disable') { + settings.transcode.videoStrategy = 'best_match' + } else if (strategyStr === 'full' || strategyStr === 'all') { + settings.transcode.videoStrategy = 'multi' + } + if (!settings.transcode.videoResolutions) { + settings.transcode.videoResolutions = ['480p', '720p', '1080p', '1440p', '2160p'] + } if (settings.transcode.hlsEnabled === undefined) { settings.transcode.hlsEnabled = false } @@ -345,10 +356,25 @@ export class TeamService { if (!settings.transcode) { settings.transcode = {} } - settings.transcode.videoStrategy = value as NonNullable< + const rawStrategy = value as string + let strategy = rawStrategy + if (rawStrategy === 'single' || rawStrategy === 'disable') { + strategy = 'best_match' + } else if (rawStrategy === 'full' || rawStrategy === 'all') { + strategy = 'multi' + } + settings.transcode.videoStrategy = strategy as NonNullable< TeamSettingsResponse['transcode'] >['videoStrategy'] delete settings['transcode.videoStrategy'] + } else if (key === 'transcode.videoResolutions') { + if (!settings.transcode) { + settings.transcode = {} + } + settings.transcode.videoResolutions = value as NonNullable< + TeamSettingsResponse['transcode'] + >['videoResolutions'] + delete settings['transcode.videoResolutions'] } else if (key === 'transcode.hardwareAcceleration') { if (!settings.transcode) { settings.transcode = {} diff --git a/packages/core/src/upload/upload.test.ts b/packages/core/src/upload/upload.test.ts index d6acab57..23561fab 100644 --- a/packages/core/src/upload/upload.test.ts +++ b/packages/core/src/upload/upload.test.ts @@ -203,13 +203,14 @@ describe('UploadService', () => { }) }) - it('should pass custom transcode.threads from team settings to transcode task', async () => { + it('should pass custom transcode.threads and videoResolutions from team settings to transcode task', async () => { await prisma.team.update({ where: { id: teamId }, data: { settings: { transcode: { - videoStrategy: 'all', + videoStrategy: 'multi', + videoResolutions: ['720p', '1080p'], hardwareAcceleration: 'auto', threads: 12, }, @@ -245,7 +246,8 @@ describe('UploadService', () => { expect(workflowTask?.payload).toEqual({ projectId: projectId, transcode: { - videoStrategy: 'all', + videoStrategy: 'multi', + videoResolutions: ['720p', '1080p'], hardwareAcceleration: 'auto', threads: 12, sprite: true, diff --git a/packages/core/src/upload/upload.ts b/packages/core/src/upload/upload.ts index 5a28e74a..212c6941 100644 --- a/packages/core/src/upload/upload.ts +++ b/packages/core/src/upload/upload.ts @@ -334,12 +334,14 @@ export class UploadService { const threads = settings?.transcode?.threads ?? 0 if (isVideo) { const strategy = settings?.transcode?.videoStrategy || 'best_match' + const videoResolutions = settings?.transcode?.videoResolutions const hardwareAcceleration = settings?.transcode?.hardwareAcceleration || 'off' const hlsEnabled = settings?.transcode?.hlsEnabled ?? false const hlsResolutions = settings?.transcode?.hlsResolutions // eslint-disable-next-line @typescript-eslint/no-explicit-any await new VideoTranscoder(tx as any, asset.id, team.id, projectId) .setStrategy(strategy) + .setVideoResolutions(videoResolutions) .setHardwareAcceleration(hardwareAcceleration) .setThreads(threads) .setHls(hlsEnabled, hlsResolutions) diff --git a/packages/db/prisma/migrations/20261001015042_migrate_video_strategy_multi_and_resolutions/migration.sql b/packages/db/prisma/migrations/20261001015042_migrate_video_strategy_multi_and_resolutions/migration.sql new file mode 100644 index 00000000..93344606 --- /dev/null +++ b/packages/db/prisma/migrations/20261001015042_migrate_video_strategy_multi_and_resolutions/migration.sql @@ -0,0 +1,16 @@ +-- Migrate videoStrategy 'all' / 'full' to 'multi' +UPDATE "teams" +SET "settings" = jsonb_set("settings", '{transcode,videoStrategy}', '"multi"') +WHERE "settings"->'transcode'->>'videoStrategy' IN ('all', 'full'); + +-- Normalize legacy videoStrategy values 'single' / 'disable' to 'best_match' +UPDATE "teams" +SET "settings" = jsonb_set("settings", '{transcode,videoStrategy}', '"best_match"') +WHERE "settings"->'transcode'->>'videoStrategy' IN ('single', 'disable'); + +-- Backfill default videoResolutions where transcode settings exist without videoResolutions +UPDATE "teams" +SET "settings" = jsonb_set("settings", '{transcode,videoResolutions}', '["480p", "720p", "1080p", "1440p", "2160p"]'::jsonb) +WHERE "settings" IS NOT NULL + AND jsonb_typeof("settings"->'transcode') = 'object' + AND NOT ("settings"->'transcode' ? 'videoResolutions'); \ No newline at end of file diff --git a/packages/db/src/prisma-json-types.ts b/packages/db/src/prisma-json-types.ts index 03d2a081..d667ccc6 100644 --- a/packages/db/src/prisma-json-types.ts +++ b/packages/db/src/prisma-json-types.ts @@ -48,7 +48,8 @@ declare global { // ---------------------------------------------------------------------- // Team Settings // ---------------------------------------------------------------------- - export type VideoTranscodeStrategy = 'best_match' | 'all' + export type VideoTranscodeStrategy = 'best_match' | 'multi' | 'all' + export type VideoResolutionLadder = '480p' | '720p' | '1080p' | '1440p' | '2160p' export type HardwareAcceleration = 'off' | 'auto' export type HdrType = 'pq' | 'hlg' | 'dovi_p5' | 'dovi_p8' | 'sdr' @@ -61,6 +62,7 @@ declare global { export interface TranscodeSettings { videoStrategy: VideoTranscodeStrategy + videoResolutions?: VideoResolutionLadder[] hardwareAcceleration?: HardwareAcceleration threads?: number hlsEnabled?: boolean @@ -348,6 +350,7 @@ declare global { // ---------------------------------------------------------------------- export interface TaskSpec { videoStrategy?: VideoTranscodeStrategy + videoResolutions?: VideoResolutionLadder[] hardwareAcceleration?: HardwareAcceleration threads?: number thumbnail?: boolean diff --git a/packages/dtos/src/asset.ts b/packages/dtos/src/asset.ts index fdd2c8ed..99ec59d3 100644 --- a/packages/dtos/src/asset.ts +++ b/packages/dtos/src/asset.ts @@ -105,6 +105,7 @@ export const assetInfoSchema = z.object({ width: z.number(), height: z.number(), size: z.number(), + resolution: z.string().optional(), hdr: z.boolean().optional(), }), ) @@ -357,6 +358,7 @@ export interface VideoTranscode { width: number height: number size: number + resolution?: string hdr?: boolean } diff --git a/packages/dtos/src/team.ts b/packages/dtos/src/team.ts index d75cc0f4..2923196a 100644 --- a/packages/dtos/src/team.ts +++ b/packages/dtos/src/team.ts @@ -69,11 +69,30 @@ export const listMembersQuerySchema = z.object({ export const VideoTranscodeStrategy = { // eslint-disable-next-line @typescript-eslint/naming-convention best_match: 'best_match', + multi: 'multi', all: 'all', } as const export type VideoTranscodeStrategy = (typeof VideoTranscodeStrategy)[keyof typeof VideoTranscodeStrategy] +export const VideoResolutionLadder = { + r480p: '480p', + r720p: '720p', + r1080p: '1080p', + r1440p: '1440p', + r2160p: '2160p', +} as const +export type VideoResolutionLadder = + (typeof VideoResolutionLadder)[keyof typeof VideoResolutionLadder] + +export const DEFAULT_VIDEO_RESOLUTIONS: VideoResolutionLadder[] = [ + '480p', + '720p', + '1080p', + '1440p', + '2160p', +] + export const HardwareAcceleration = { off: 'off', auto: 'auto', @@ -96,6 +115,10 @@ export const updateTeamSettingsRequestSchema = z.union([ key: z.literal('transcode.videoStrategy'), value: z.nativeEnum(VideoTranscodeStrategy), }), + z.object({ + key: z.literal('transcode.videoResolutions'), + value: z.array(z.nativeEnum(VideoResolutionLadder)), + }), z.object({ key: z.literal('transcode.hardwareAcceleration'), value: z.nativeEnum(HardwareAcceleration), @@ -126,6 +149,7 @@ export interface TeamAppearanceSettings { export interface TeamSettingsResponse { transcode?: { videoStrategy?: VideoTranscodeStrategy + videoResolutions?: VideoResolutionLadder[] hardwareAcceleration?: HardwareAcceleration threads?: number hlsEnabled?: boolean diff --git a/packages/transcode/src/transcoder.ts b/packages/transcode/src/transcoder.ts index 0702819c..34986f49 100644 --- a/packages/transcode/src/transcoder.ts +++ b/packages/transcode/src/transcoder.ts @@ -22,6 +22,13 @@ export class VideoTranscoder { return this } + setVideoResolutions(resolutions?: PrismaJson.VideoResolutionLadder[]): this { + if (resolutions) { + this.spec.videoResolutions = resolutions + } + return this + } + setHardwareAcceleration(hardwareAcceleration?: PrismaJson.HardwareAcceleration): this { if (hardwareAcceleration) { this.spec.hardwareAcceleration = hardwareAcceleration diff --git a/packages/transcode/src/workflows/transcode-utils.test.ts b/packages/transcode/src/workflows/transcode-utils.test.ts index cd1034e0..f4530cc3 100644 --- a/packages/transcode/src/workflows/transcode-utils.test.ts +++ b/packages/transcode/src/workflows/transcode-utils.test.ts @@ -43,10 +43,14 @@ describe('transcode-utils', () => { expect(res).toEqual(['1080p', '720p', '480p']) }) - it('returns empty array when source is smaller than smallest ladder', () => { - // 640x360 source is smaller than 480p (854 long side) + it('falls back to lowest selected ladder when source is smaller than smallest ladder', () => { + // 640x360 source is smaller than 480p (854 long side) -> falls back to lowest selected (480p) const res = getTargetHlsResolutions(undefined, 640, 360) - expect(res).toEqual([]) + expect(res).toEqual(['480p']) + + // When user selected 720p and 1440p, and source is 480p (854 long side) -> falls back to lowest selected (720p) + const resCustom = getTargetHlsResolutions(['720p', '1440p'], 854, 480) + expect(resCustom).toEqual(['720p']) }) it('respects selective subset of configured ladders', () => { @@ -79,9 +83,82 @@ describe('transcode-utils', () => { }) describe('getTargetVideoResolutions', () => { - it('returns best match resolution for standard video', () => { - const resolutions = getTargetVideoResolutions('best_match', 1920, 1080) - expect(resolutions).toEqual(['180p', '1080p']) + describe('best_match strategy', () => { + it('returns best match resolution for 4K video', () => { + const resolutions = getTargetVideoResolutions('best_match', 3840, 2160) + expect(resolutions).toEqual(['180p', '2160p']) + }) + + it('returns best match resolution for 1080p video', () => { + const resolutions = getTargetVideoResolutions('best_match', 1920, 1080) + expect(resolutions).toEqual(['180p', '1080p']) + }) + + it('returns best match resolution for 720p video', () => { + const resolutions = getTargetVideoResolutions('best_match', 1280, 720) + expect(resolutions).toEqual(['180p', '720p']) + }) + + it('falls back to 480p for smaller videos (460p and 230p)', () => { + const res460 = getTargetVideoResolutions('best_match', 818, 460) + expect(res460).toEqual(['180p', '480p']) + + const res230 = getTargetVideoResolutions('best_match', 408, 230) + expect(res230).toEqual(['180p', '480p']) + }) + }) + + describe('multi strategy', () => { + it('returns all supported resolutions up to source for 4K video', () => { + const resolutions = getTargetVideoResolutions('multi', 3840, 2160) + expect(resolutions).toEqual(['180p', '2160p', '1440p', '1080p', '720p', '480p']) + }) + + it('returns resolutions up to 720p for 1280x720 video', () => { + const resolutions = getTargetVideoResolutions('multi', 1280, 720) + expect(resolutions).toEqual(['180p', '720p', '480p']) + }) + + it('returns resolutions up to 1080p for 2276x1280 video', () => { + const resolutions = getTargetVideoResolutions('multi', 2276, 1280) + expect(resolutions).toEqual(['180p', '1080p', '720p', '480p']) + }) + + it('falls back to lowest selected resolution (480p) for 460p and 230p videos', () => { + const res460 = getTargetVideoResolutions('multi', 818, 460) + expect(res460).toEqual(['180p', '480p']) + + const res230 = getTargetVideoResolutions('multi', 408, 230) + expect(res230).toEqual(['180p', '480p']) + }) + + it('respects user-selected ladders and falls back to lowest selected ladder', () => { + const customLadders = ['720p', '1440p', '2160p'] as PrismaJson.VideoResolutionLadder[] + + // 4K video: matches 2160p, 1440p, 720p (1080p was not selected) + const res4k = getTargetVideoResolutions('multi', 3840, 2160, customLadders) + expect(res4k).toEqual(['180p', '2160p', '1440p', '720p']) + + // 230p video: nothing matches <= 408 -> falls back to lowest selected (720p) + const res230 = getTargetVideoResolutions('multi', 408, 230, customLadders) + expect(res230).toEqual(['180p', '720p']) + }) + + it('normalizes legacy "all" and "full" strategy values to multi', () => { + const resAll = getTargetVideoResolutions( + 'all' as unknown as PrismaJson.VideoTranscodeStrategy, + 1280, + 720, + ) + expect(resAll).toEqual(['180p', '720p', '480p']) + + const resFull = getTargetVideoResolutions( + 'full' as unknown as PrismaJson.VideoTranscodeStrategy, + 1280, + 720, + ) + expect(resFull).toEqual(['180p', '720p', '480p']) + }) }) }) diff --git a/packages/transcode/src/workflows/transcode-utils.ts b/packages/transcode/src/workflows/transcode-utils.ts index 956b9a85..36c9b5a9 100644 --- a/packages/transcode/src/workflows/transcode-utils.ts +++ b/packages/transcode/src/workflows/transcode-utils.ts @@ -30,6 +30,35 @@ export const HLS_LADDER_ORDER: PrismaJson.HlsResolutionLadder[] = [ '480p', ] +export const VIDEO_LADDER_ORDER: PrismaJson.VideoResolutionLadder[] = [ + '2160p', + '1440p', + '1080p', + '720p', + '480p', +] + +export function matchResolutionsByLongSide( + selectedLadders: T[], + ladderOrder: T[], + rawLongSide: number, +): T[] { + const orderedSelected = ladderOrder.filter((ladder) => selectedLadders.includes(ladder)) + if (orderedSelected.length === 0) return [] + + const matched = orderedSelected.filter((ladder) => { + const targetLongSide = RESOLUTION_LONG_SIDES[ladder] + return targetLongSide !== undefined && targetLongSide <= rawLongSide + }) + + if (matched.length === 0) { + // Falls back to the lowest resolution selected if nothing matched + return [orderedSelected[orderedSelected.length - 1]] + } + + return matched +} + export function getTargetHlsResolutions( configuredLadders: PrismaJson.HlsResolutionLadder[] | undefined, originalWidth: number, @@ -42,11 +71,7 @@ export function getTargetHlsResolutions( const rawLongSide = Math.max(originalWidth, originalHeight) - return HLS_LADDER_ORDER.filter((ladder) => { - if (!selectedLadders.includes(ladder)) return false - const targetLongSide = RESOLUTION_LONG_SIDES[ladder] - return targetLongSide <= rawLongSide - }) + return matchResolutionsByLongSide(selectedLadders, HLS_LADDER_ORDER, rawLongSide) } export function resolutionToDimensions( @@ -74,31 +99,11 @@ export function resolutionToDimensions( return [width, height] } -const TARGET_RESOLUTIONS = [ - { name: '2160p', longSide: 3840 }, - { name: '1080p', longSide: 1920 }, - { name: '720p', longSide: 1280 }, - { name: '540p', longSide: 960 }, - { name: '360p', longSide: 640 }, -] - -function getBestMatchResolution( - originalWidth: number, - originalHeight: number, -): { name: string; longSide: number } { - const rawLongSide = Math.max(originalWidth, originalHeight) - const lower = TARGET_RESOLUTIONS.filter((r) => r.longSide <= rawLongSide) - if (lower.length > 0) { - lower.sort((a, b) => b.longSide - a.longSide) - return lower[0] - } - return TARGET_RESOLUTIONS[TARGET_RESOLUTIONS.length - 1] -} - export function getTargetVideoResolutions( strategy: PrismaJson.VideoTranscodeStrategy, originalWidth: number, originalHeight: number, + configuredResolutions?: PrismaJson.VideoResolutionLadder[], ): string[] { const resolutions = ['180p'] @@ -106,21 +111,27 @@ export function getTargetVideoResolutions( const stratStr = strategy as string if (stratStr === 'single' || stratStr === 'disable') { normalizedStrategy = 'best_match' - } else if (stratStr === 'full') { - normalizedStrategy = 'all' + } else if (stratStr === 'full' || stratStr === 'all' || stratStr === 'multi') { + normalizedStrategy = 'multi' } - const bestMatch = getBestMatchResolution(originalWidth, originalHeight) + const selectedLadders = + configuredResolutions && configuredResolutions.length > 0 + ? configuredResolutions + : (['480p', '720p', '1080p', '1440p', '2160p'] as PrismaJson.VideoResolutionLadder[]) + + const rawLongSide = Math.max(originalWidth, originalHeight) if (normalizedStrategy === 'best_match') { - resolutions.push(bestMatch.name) - } else if (normalizedStrategy === 'all') { - const bestMatchIndex = TARGET_RESOLUTIONS.findIndex((r) => r.name === bestMatch.name) - if (bestMatchIndex !== -1) { - for (let i = bestMatchIndex; i < TARGET_RESOLUTIONS.length; i++) { - resolutions.push(TARGET_RESOLUTIONS[i].name) - } - } + const matched = matchResolutionsByLongSide( + ['480p', '720p', '1080p', '1440p', '2160p'] as PrismaJson.VideoResolutionLadder[], + VIDEO_LADDER_ORDER, + rawLongSide, + ) + resolutions.push(matched[0]) + } else if (normalizedStrategy === 'multi') { + const matched = matchResolutionsByLongSide(selectedLadders, VIDEO_LADDER_ORDER, rawLongSide) + resolutions.push(...matched) } return resolutions diff --git a/packages/transcode/src/workflows/transcode-video.test.ts b/packages/transcode/src/workflows/transcode-video.test.ts index 0dbc4d31..35b80b80 100644 --- a/packages/transcode/src/workflows/transcode-video.test.ts +++ b/packages/transcode/src/workflows/transcode-video.test.ts @@ -796,4 +796,82 @@ describe('transcodeVideoWorkflow', () => { status: WorkflowTaskStatus.completed, }) }) + + it('should transcode selected multi resolutions when videoStrategy is multi', () => { + const task: WorkflowTask = { + id: 'task-multi', + assetId: 'asset-multi', + type: WorkflowTaskType.transcode_video, + status: WorkflowTaskStatus.pending, + sessionId: null, + output: null, + payload: { + projectId: 'proj-1', + transcode: { + videoStrategy: 'multi', + videoResolutions: ['720p', '1080p', '2160p'], + }, + }, + createdAt: new Date(), + updatedAt: new Date(), + heartbeat: null, + teamId: 'team-1', + projectId: 'proj-1', + uid: 'task-uid', + model: null, + inputTokens: 0, + outputTokens: 0, + } + + mockActivities.getAssetActivity.mockResolvedValue({ + id: 'asset-multi', + storageKey: { key: 'files/asset-multi/video.mp4' }, + mediaType: 'video/mp4', + }) + + mockActivities.getMediaInfoActivity.mockResolvedValue({ + proxyType: 'video', + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + startTimecode: '00:00:00:00', + bitRate: 1000, + hasAudio: false, + format: {}, + }, + videoTranscodes: [], + imageTranscodes: [], + }) + + mockActivities.transcodeVideoActivity.mockImplementation(async (params) => ({ + key: `files/asset-multi/${params.videoSpec.resolution}.mp4`, + width: params.videoSpec.width, + height: params.videoSpec.height, + resolution: params.videoSpec.resolution, + })) + + return transcodeVideoWorkflow(task).then(() => { + // 180p preview + 1080p + 720p (2160p is skipped since > 1920) + expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledTimes(3) + + expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith( + expect.objectContaining({ + videoSpec: expect.objectContaining({ resolution: '180p' }), + }), + ) + expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith( + expect.objectContaining({ + videoSpec: expect.objectContaining({ resolution: '1080p' }), + }), + ) + expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith( + expect.objectContaining({ + videoSpec: expect.objectContaining({ resolution: '720p' }), + }), + ) + }) + }) }) diff --git a/packages/transcode/src/workflows/transcode-video.ts b/packages/transcode/src/workflows/transcode-video.ts index 4596e7e4..e61f017b 100644 --- a/packages/transcode/src/workflows/transcode-video.ts +++ b/packages/transcode/src/workflows/transcode-video.ts @@ -129,6 +129,7 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise spec.videoStrategy || 'best_match', metadata.originalWidth, metadata.originalHeight, + spec.videoResolutions, ) for (const res of videoResolutions) { const [width, height] = resolutionToDimensions( diff --git a/packages/webui/components/breadcrumb-nav.test.tsx b/packages/webui/components/breadcrumb-nav.test.tsx index 2b602305..743159e8 100644 --- a/packages/webui/components/breadcrumb-nav.test.tsx +++ b/packages/webui/components/breadcrumb-nav.test.tsx @@ -1,6 +1,6 @@ // @vitest-environment happy-dom import React from 'react' -import { cleanup, render, screen } from '@testing-library/react' +import { cleanup, fireEvent, render, screen } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { BreadcrumbNav } from './breadcrumb-nav' @@ -160,4 +160,107 @@ describe('BreadcrumbNav component', () => { const triggerButton = screen.getByRole('button', { name: 'sample.mov' }) expect(triggerButton).toBeDefined() }) + + it('displays 480p instead of 360p for 480p video transcode in download menu', () => { + render( + + + , + ) + + const triggerButton = screen.getByRole('button', { name: 'sample.mov' }) + fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false }) + + const downloadSubTrigger = screen.getByText('Download') + fireEvent.click(downloadSubTrigger) + + const items = screen.getAllByRole('menuitem') + expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true) + expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false) + }) + + it('displays explicit resolution when provided in download menu', () => { + render( + + + , + ) + + const triggerButton = screen.getByRole('button', { name: 'sample.mov' }) + fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false }) + + const downloadSubTrigger = screen.getByText('Download') + fireEvent.click(downloadSubTrigger) + + const items = screen.getAllByRole('menuitem') + expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true) + expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false) + }) + + it('correctly labels legacy portrait video transcode in download menu using long side fallback', () => { + render( + + + , + ) + + const triggerButton = screen.getByRole('button', { name: 'portrait.mp4' }) + fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false }) + + const downloadSubTrigger = screen.getByText('Download') + fireEvent.click(downloadSubTrigger) + + const items = screen.getAllByRole('menuitem') + expect(items.some((i) => i.textContent?.includes('1080p'))).toBe(true) + expect(items.some((i) => i.textContent?.includes('1920p'))).toBe(false) + }) }) diff --git a/packages/webui/components/breadcrumb-nav.tsx b/packages/webui/components/breadcrumb-nav.tsx index 211ead4b..dca3fa60 100644 --- a/packages/webui/components/breadcrumb-nav.tsx +++ b/packages/webui/components/breadcrumb-nav.tsx @@ -48,6 +48,7 @@ import { useState } from 'react' import { useDualSidebarStore } from '@/ui/stores/dual-sidebar' import { ManageVersionsDialog } from './manage-versions-dialog' import { AssetLinkedTasksDialog } from './kanban/asset-linked-tasks-dialog' +import { getVideoResolutionLabel } from '@/ui/lib/media' interface BreadcrumbNavProps { teamId: string @@ -84,6 +85,7 @@ interface BreadcrumbNavProps { key: string width: number height: number + resolution?: string hdr?: boolean }> } @@ -287,14 +289,7 @@ export function BreadcrumbNav({ {m.download()} {downloadInfo?.videoTranscodes?.map((t) => { - const longSide = Math.max(t.width, t.height) - let resolution = `${t.height}p` - if (longSide >= 3840) resolution = '2160p' - else if (longSide >= 1920) resolution = '1080p' - else if (longSide >= 1280) resolution = '720p' - else if (longSide >= 960) resolution = '540p' - else if (longSide >= 640) resolution = '360p' - else if (longSide >= 320) resolution = '180p' + const resolution = getVideoResolutionLabel(t) const label = t.hdr ? `${resolution} (HDR)` : resolution return ( { expect(menuBtn).toBeDefined() fireEvent.click(menuBtn) }) + + it('displays 480p instead of 360p for 480p video transcode in download menu', () => { + render( + , + ) + + const menuBtn = screen.getByLabelText(/More options/i) + fireEvent.pointerDown(menuBtn, { button: 0, ctrlKey: false }) + + const downloadSubTrigger = screen.getByText('Download') + fireEvent.click(downloadSubTrigger) + + const items = screen.getAllByRole('menuitem') + expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true) + expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false) + }) + + it('correctly labels legacy portrait video transcode in download menu using long side fallback', () => { + render( + , + ) + + const menuBtn = screen.getByLabelText(/More options/i) + fireEvent.pointerDown(menuBtn, { button: 0, ctrlKey: false }) + + const downloadSubTrigger = screen.getByText('Download') + fireEvent.click(downloadSubTrigger) + + const items = screen.getAllByRole('menuitem') + expect(items.some((i) => i.textContent?.includes('1080p'))).toBe(true) + expect(items.some((i) => i.textContent?.includes('1920p'))).toBe(false) + }) }) diff --git a/packages/webui/components/file-viewer/mobile-file-header.tsx b/packages/webui/components/file-viewer/mobile-file-header.tsx index 3f6a7a30..458852c8 100644 --- a/packages/webui/components/file-viewer/mobile-file-header.tsx +++ b/packages/webui/components/file-viewer/mobile-file-header.tsx @@ -26,6 +26,7 @@ import { Trash2, } from 'lucide-react' import React from 'react' +import { getVideoResolutionLabel } from '@/ui/lib/media' export interface MobileFileHeaderProps { fileName: string @@ -46,6 +47,7 @@ export interface MobileFileHeaderProps { key: string width: number height: number + resolution?: string hdr?: boolean }> } @@ -238,14 +240,7 @@ export function MobileFileHeader({ {m.download()} {downloadInfo?.videoTranscodes?.map((t) => { - const longSide = Math.max(t.width, t.height) - let resolution = `${t.height}p` - if (longSide >= 3840) resolution = '2160p' - else if (longSide >= 1920) resolution = '1080p' - else if (longSide >= 1280) resolution = '720p' - else if (longSide >= 960) resolution = '540p' - else if (longSide >= 640) resolution = '360p' - else if (longSide >= 320) resolution = '180p' + const resolution = getVideoResolutionLabel(t) const label = t.hdr ? `${resolution} (HDR)` : resolution return ( { vi.useRealTimers() } }) + + it('reports 2160p instead of 3840p for 4K vertical video when HLS switches level on Auto', () => { + mockHlsInstance.levels = [{ height: 3840, width: 2160, bitrate: 10000000 }] + + const hlsVideo: AssetInfo = { + id: 'video-hls-compare-vertical-4k', + name: 'vertical-4k.m3u8', + proxyType: 'video', + media: { + isHls: true, + hls: { + key: 'hls-key', + url: 'https://cdn.example.com/master.m3u8', + resolutions: [{ width: 2160, height: 3840, resolution: '2160p' }], + }, + metadata: { + originalWidth: 2160, + originalHeight: 3840, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + }, + } as unknown as AssetInfo + + const ref = createRef() + const reportedStates: PaneReportedState[] = [] + + render( + reportedStates.push(s)} + />, + ) + + const levelSwitchedHandler = mockHlsInstance.on.mock.calls.find( + ([event]) => event === 'hlsLevelSwitched', + )?.[1] + expect(levelSwitchedHandler).toBeDefined() + + act(() => { + levelSwitchedHandler('hlsLevelSwitched', { level: 0 }) + }) + + const lastState = reportedStates[reportedStates.length - 1] + expect(lastState?.video?.activeAutoResolution).toBe('2160p') + }) + + it('derives resolution from long side for legacy portrait MP4 transcode without explicit resolution', () => { + const legacyPortraitVideo: AssetInfo = { + id: 'legacy-portrait-compare-mp4', + name: 'portrait.mp4', + proxyType: 'video', + media: { + videoTranscodes: [ + { + key: 'portrait-1080p.mp4', + url: 'https://cdn.example.com/portrait-1080p.mp4', + width: 1080, + height: 1920, + size: 5000000, + }, + ], + metadata: { + originalWidth: 1080, + originalHeight: 1920, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + }, + } as unknown as AssetInfo + + const reportedStates: PaneReportedState[] = [] + render( + reportedStates.push(s)} + />, + ) + + const lastState = reportedStates[reportedStates.length - 1] + expect(lastState?.video?.resolutions?.[0]?.resolution).toBe('1080p') + }) }) diff --git a/packages/webui/components/viewers/video/compare-video-pane.tsx b/packages/webui/components/viewers/video/compare-video-pane.tsx index ad902a10..1049a9b5 100644 --- a/packages/webui/components/viewers/video/compare-video-pane.tsx +++ b/packages/webui/components/viewers/video/compare-video-pane.tsx @@ -11,6 +11,7 @@ import { useFramePlayer } from './use-frame-player' import { calculateFrameCenterTime, resolveTotalFrames } from './utils' import { clampFrame as clampFrameUtil } from '../../compare/compare-utils' import type { ComparePaneHandle, DisplayTranscode, PaneReportedState } from '../../compare/types' +import { getVideoResolutionLabel } from '@/ui/lib/media' interface CompareVideoPaneProps { file: AssetInfo @@ -36,20 +37,10 @@ function computeResolutions(file: AssetInfo): DisplayTranscode[] { ? (file.media?.hls?.resolutions ?? []) : (file.media?.videoTranscodes ?? []) - return baseTranscodes.map((t) => { - const longSide = Math.max(t.width, t.height) - const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined - let resolution = resCandidate || `${t.height}p` - if (!resCandidate) { - if (longSide >= 3840) resolution = '2160p' - else if (longSide >= 1920) resolution = '1080p' - else if (longSide >= 1280) resolution = '720p' - else if (longSide >= 960) resolution = '540p' - else if (longSide >= 640) resolution = '360p' - else if (longSide >= 320) resolution = '180p' - } - return { ...t, resolution } - }) + return baseTranscodes.map((t) => ({ + ...t, + resolution: getVideoResolutionLabel(t), + })) } function getInitialResolution(resolutions: DisplayTranscode[]): DisplayTranscode | null { @@ -216,7 +207,18 @@ export const CompareVideoPane = forwardRef { const level = hls?.levels[eventData.level] if (level) { - setActiveAutoResolution(`${level.height}p`) + const matched = resolutions.find( + (r) => r.width === level.width && r.height === level.height, + ) + if (matched?.resolution) { + setActiveAutoResolution(matched.resolution) + } else { + const shortSide = + level.width && level.height + ? Math.min(level.width, level.height) + : level.height || level.width + setActiveAutoResolution(shortSide ? `${shortSide}p` : undefined) + } } }) @@ -617,7 +619,7 @@ export const CompareVideoPane = forwardRef = ({ ? data.media.videoTranscodes : resolutions ).map((res) => { - const resLabel = - 'resolution' in res && res.resolution ? res.resolution : `${res.height}p` + const resLabel = getVideoResolutionLabel(res) const itemKey = res.key || `${resLabel}-${res.hdr ? 'hdr' : 'sdr'}` return ( { vi.useRealTimers() } }) + + it('displays 2160p instead of 3840p for 4K vertical video when HLS switches level on Auto', () => { + mockHlsInstance.levels = [{ height: 3840, width: 2160, bitrate: 10000000 }] + + const hlsVideo: AssetInfo = { + id: 'video-hls-vertical-4k', + name: 'vertical-4k.m3u8', + proxyType: 'video', + media: { + isHls: true, + hls: { + key: 'hls-key', + url: 'https://cdn.example.com/master.m3u8', + resolutions: [{ width: 2160, height: 3840, resolution: '2160p' }], + }, + metadata: { + originalWidth: 2160, + originalHeight: 3840, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + }, + } as unknown as AssetInfo + + const { container } = render() + + const levelSwitchedHandler = mockHlsInstance.on.mock.calls.find( + ([event]) => event === 'hlsLevelSwitched', + )?.[1] + expect(levelSwitchedHandler).toBeDefined() + + act(() => { + levelSwitchedHandler('hlsLevelSwitched', { level: 0 }) + }) + + const settingsButton = container.querySelector('button:has(.lucide-settings)') + expect(settingsButton?.textContent).toContain('2160p') + expect(settingsButton?.textContent).not.toContain('3840p') + }) + + it('derives resolution from long side for legacy portrait MP4 transcode without explicit resolution', () => { + const legacyPortraitVideo: AssetInfo = { + id: 'legacy-portrait-mp4', + name: 'portrait.mp4', + proxyType: 'video', + media: { + videoTranscodes: [ + { + key: 'portrait-1080p.mp4', + url: 'https://cdn.example.com/portrait-1080p.mp4', + width: 1080, + height: 1920, + size: 5000000, + }, + ], + metadata: { + originalWidth: 1080, + originalHeight: 1920, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + }, + } as unknown as AssetInfo + + const { container } = render() + const settingsButton = container.querySelector('button:has(.lucide-settings)') + expect(settingsButton?.textContent).toContain('1080p') + expect(settingsButton?.textContent).not.toContain('1920p') + }) }) diff --git a/packages/webui/components/viewers/video/video-viewer.tsx b/packages/webui/components/viewers/video/video-viewer.tsx index 86472281..cb62faaf 100644 --- a/packages/webui/components/viewers/video/video-viewer.tsx +++ b/packages/webui/components/viewers/video/video-viewer.tsx @@ -10,6 +10,7 @@ import { MobileVideoControlBar } from './mobile-video-control-bar' import { useIsMobile } from '@/ui/hooks/use-mobile' import DrawingCanvas from '@/ui/components/drawing-canvas' import { useAnnotationStore } from '@/ui/stores/annotation-store' +import { getVideoResolutionLabel } from '@/ui/lib/media' import { FileViewerProps, MediaController } from '../types' import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom' import { usePanZoomGestures } from '../use-pan-zoom' @@ -39,24 +40,10 @@ const VideoViewer = React.forwardRef( ? (data.media?.hls?.resolutions ?? []) : (data.media?.videoTranscodes ?? []) - const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => { - const longSide = Math.max(t.width, t.height) - const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined - let resolution = resCandidate || `${t.height}p` - if (!resCandidate) { - if (longSide >= 3840) resolution = '2160p' - else if (longSide >= 1920) resolution = '1080p' - else if (longSide >= 1280) resolution = '720p' - else if (longSide >= 960) resolution = '540p' - else if (longSide >= 640) resolution = '360p' - else if (longSide >= 320) resolution = '180p' - } - - return { - ...t, - resolution, - } - }) + const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => ({ + ...t, + resolution: getVideoResolutionLabel(t), + })) // Only transcoded proxy versions are ever displayed; the raw original file // is never used as a playback source. const hasMedia = (isHls || resolutions.length > 0) && !!data.media?.metadata @@ -365,8 +352,18 @@ const VideoViewer = React.forwardRef( hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => { const level = hls?.levels[eventData.level] if (level) { - const h = level.height || Math.min(level.width, level.height) - setActiveAutoResolution(h ? `${h}p` : undefined) + const matched = resolutions.find( + (r) => r.width === level.width && r.height === level.height, + ) + if (matched?.resolution) { + setActiveAutoResolution(matched.resolution) + } else { + const shortSide = + level.width && level.height + ? Math.min(level.width, level.height) + : level.height || level.width + setActiveAutoResolution(shortSide ? `${shortSide}p` : undefined) + } } }) diff --git a/packages/webui/lib/media.test.ts b/packages/webui/lib/media.test.ts index 7a938e58..c9c9ad5e 100644 --- a/packages/webui/lib/media.test.ts +++ b/packages/webui/lib/media.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { isImageFileName, getBestTranscode } from './media' +import { isImageFileName, getBestTranscode, getVideoResolutionLabel } from './media' describe('isImageFileName', () => { it('identifies web-supported image formats as images', () => { @@ -49,3 +49,48 @@ describe('getBestTranscode', () => { expect(getBestTranscode(transcodes, 1500)?.width).toBe(1200) }) }) + +describe('getVideoResolutionLabel', () => { + it('preserves explicit resolution when present', () => { + expect(getVideoResolutionLabel({ width: 854, height: 480, resolution: '480p' })).toBe('480p') + expect(getVideoResolutionLabel({ width: 480, height: 854, resolution: '480p' })).toBe('480p') + expect(getVideoResolutionLabel({ width: 1920, height: 1080, resolution: '1080p' })).toBe( + '1080p', + ) + }) + + it('derives resolution from long side for portrait legacy transcodes without explicit resolution', () => { + // 4K portrait + expect(getVideoResolutionLabel({ width: 2160, height: 3840 })).toBe('2160p') + // 2K / 1440p portrait + expect(getVideoResolutionLabel({ width: 1440, height: 2560 })).toBe('1440p') + // 1080p portrait + expect(getVideoResolutionLabel({ width: 1080, height: 1920 })).toBe('1080p') + // 720p portrait + expect(getVideoResolutionLabel({ width: 720, height: 1280 })).toBe('720p') + // 480p portrait + expect(getVideoResolutionLabel({ width: 480, height: 854 })).toBe('480p') + // 360p portrait + expect(getVideoResolutionLabel({ width: 360, height: 640 })).toBe('360p') + // 180p portrait + expect(getVideoResolutionLabel({ width: 180, height: 320 })).toBe('180p') + }) + + it('derives resolution from long side for landscape legacy transcodes without explicit resolution', () => { + expect(getVideoResolutionLabel({ width: 3840, height: 2160 })).toBe('2160p') + expect(getVideoResolutionLabel({ width: 2560, height: 1440 })).toBe('1440p') + expect(getVideoResolutionLabel({ width: 1920, height: 1080 })).toBe('1080p') + expect(getVideoResolutionLabel({ width: 1280, height: 720 })).toBe('720p') + expect(getVideoResolutionLabel({ width: 854, height: 480 })).toBe('480p') + expect(getVideoResolutionLabel({ width: 640, height: 360 })).toBe('360p') + expect(getVideoResolutionLabel({ width: 320, height: 180 })).toBe('180p') + }) + + it('falls back to short side or single dimension for non-standard sizes', () => { + expect(getVideoResolutionLabel({ width: 200, height: 100 })).toBe('100p') + expect(getVideoResolutionLabel({ height: 720 })).toBe('720p') + expect(getVideoResolutionLabel({ width: 500 })).toBe('500p') + expect(getVideoResolutionLabel(null)).toBe('') + expect(getVideoResolutionLabel(undefined)).toBe('') + }) +}) diff --git a/packages/webui/lib/media.ts b/packages/webui/lib/media.ts index 59b3c3da..5554ad35 100644 --- a/packages/webui/lib/media.ts +++ b/packages/webui/lib/media.ts @@ -40,3 +40,43 @@ export function getBestTranscode( // If no candidate >= screenWidth, simply return the largest available (first in original sorted) return sorted[0] } + +export function getVideoResolutionLabel( + transcode?: { + width?: number + height?: number + resolution?: string + } | null, +): string { + if (!transcode) return '' + + if (transcode.resolution) { + return transcode.resolution + } + + const width = transcode.width ?? 0 + const height = transcode.height ?? 0 + + if (width > 0 && height > 0) { + const longSide = Math.max(width, height) + if (longSide >= 3840) return '2160p' + if (longSide >= 2560) return '1440p' + if (longSide >= 1920) return '1080p' + if (longSide >= 1280) return '720p' + if (longSide >= 854) return '480p' + if (longSide >= 640) return '360p' + if (longSide >= 320) return '180p' + + const shortSide = Math.min(width, height) + return `${shortSide}p` + } + + if (height > 0) { + return `${height}p` + } + if (width > 0) { + return `${width}p` + } + + return '' +} diff --git a/packages/webui/messages/en.json b/packages/webui/messages/en.json index 068f83e5..d41896af 100644 --- a/packages/webui/messages/en.json +++ b/packages/webui/messages/en.json @@ -254,6 +254,10 @@ "video_strategy": "Video Strategy", "best_match": "Best match", "best_match_description": "Generates a single optimal resolution matching the source quality.", + "multi_resolutions": "Multi resolutions", + "multi_resolutions_description": "Generates selected resolutions up to the source quality.", + "video_resolution_ladders": "Resolution Ladders", + "video_resolution_ladders_description": "Select the target resolutions to generate for MP4 transcoding. Resolutions are only generated up to the source video resolution.", "all_resolutions": "All resolutions", "all_resolutions_description": "Generates all supported resolutions up to the source quality.", "hardware_acceleration": "Hardware Acceleration", diff --git a/packages/webui/messages/zh.json b/packages/webui/messages/zh.json index e32339d2..1bb72232 100644 --- a/packages/webui/messages/zh.json +++ b/packages/webui/messages/zh.json @@ -254,6 +254,10 @@ "video_strategy": "视频策略", "best_match": "最佳匹配", "best_match_description": "生成与源质量匹配的单一最优分辨率。", + "multi_resolutions": "多分辨率", + "multi_resolutions_description": "生成选中的不超过源质量的分辨率。", + "video_resolution_ladders": "分辨率阶梯", + "video_resolution_ladders_description": "选择 MP4 转码的目标分辨率。仅生成不超过原始视频分辨率的分辨率阶梯。", "all_resolutions": "所有分辨率", "all_resolutions_description": "生成不超过源质量的所有支持分辨率。", "hardware_acceleration": "硬件加速", diff --git a/packages/webui/routes/projects/$projectId/files/$fileId.lazy.tsx b/packages/webui/routes/projects/$projectId/files/$fileId.lazy.tsx index 95cfeca3..2b42bea0 100644 --- a/packages/webui/routes/projects/$projectId/files/$fileId.lazy.tsx +++ b/packages/webui/routes/projects/$projectId/files/$fileId.lazy.tsx @@ -300,6 +300,7 @@ function FileViewPage() { key: t.key, width: t.width, height: t.height, + resolution: t.resolution, hdr: t.hdr, })), }, diff --git a/packages/webui/routes/teams/$teamId/settings.lazy.tsx b/packages/webui/routes/teams/$teamId/settings.lazy.tsx index db3cf8c4..996a3d80 100644 --- a/packages/webui/routes/teams/$teamId/settings.lazy.tsx +++ b/packages/webui/routes/teams/$teamId/settings.lazy.tsx @@ -1,6 +1,8 @@ import { createLazyFileRoute } from '@tanstack/react-router' import { VideoTranscodeStrategy, + VideoResolutionLadder, + DEFAULT_VIDEO_RESOLUTIONS, HardwareAcceleration, UpdateTeamSettingsRequest, TeamSettingsResponse, @@ -97,7 +99,7 @@ function getTabFromHash(): SettingsTab { : 'general' } -function TeamSettingsPage() { +export function TeamSettingsPage() { const { teamId } = Route.useParams() const queryClient = useQueryClient() const [activeTab, setActiveTabState] = useState(getTabFromHash) @@ -297,6 +299,10 @@ function TeamSettingsPage() { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'settings'], }) + toast.success(m.settings_updated()) + }, + onError: () => { + toast.error(m.failed_update_settings()) }, }) @@ -310,6 +316,102 @@ function TeamSettingsPage() { }) } + const pendingVideoResolutionsRef = useRef(null) + const [optimisticVideoResolutions, setOptimisticVideoResolutions] = useState< + VideoResolutionLadder[] | null + >(null) + const videoResolutionsQueueRef = useRef>(Promise.resolve()) + const isVideoResolutionsUpdatingRef = useRef(false) + + const pendingHlsResolutionsRef = useRef(null) + const [optimisticHlsResolutions, setOptimisticHlsResolutions] = useState< + HlsResolutionLadder[] | null + >(null) + const hlsResolutionsQueueRef = useRef>(Promise.resolve()) + const isHlsResolutionsUpdatingRef = useRef(false) + + useEffect(() => { + if (!isVideoResolutionsUpdatingRef.current) { + pendingVideoResolutionsRef.current = null + setOptimisticVideoResolutions(null) + } + }, [settings]) + + useEffect(() => { + if (!isHlsResolutionsUpdatingRef.current) { + pendingHlsResolutionsRef.current = null + setOptimisticHlsResolutions(null) + } + }, [settings]) + + const handleVideoLadderToggle = (ladder: VideoResolutionLadder) => { + const current = + pendingVideoResolutionsRef.current ?? + optimisticVideoResolutions ?? + (settings as TeamSettingsResponse | undefined)?.transcode?.videoResolutions ?? + DEFAULT_VIDEO_RESOLUTIONS + const updated = current.includes(ladder) + ? current.filter((l) => l !== ladder) + : [...current, ladder] + + if (updated.length === 0) return + + pendingVideoResolutionsRef.current = updated + setOptimisticVideoResolutions(updated) + + queryClient.setQueryData( + ['teams', teamId, 'settings'], + (old: TeamSettingsResponse | undefined) => { + if (!old) return old + return { + ...old, + transcode: { + ...old.transcode, + videoResolutions: updated, + }, + } + }, + ) + + isVideoResolutionsUpdatingRef.current = true + + videoResolutionsQueueRef.current = videoResolutionsQueueRef.current + .then(async () => { + const target = pendingVideoResolutionsRef.current + if (!target) return + + const res = await client.api.teams[':teamId'].settings.$patch({ + param: { teamId }, + json: { + key: 'transcode.videoResolutions', + value: target, + }, + }) + if (!res.ok) throw new Error('Failed to update settings') + await res.json() + + if (pendingVideoResolutionsRef.current === target) { + pendingVideoResolutionsRef.current = null + isVideoResolutionsUpdatingRef.current = false + setOptimisticVideoResolutions(null) + await queryClient.invalidateQueries({ + queryKey: ['teams', teamId, 'settings'], + }) + toast.success(m.settings_updated()) + } + }) + .catch((err) => { + console.error(err) + pendingVideoResolutionsRef.current = null + isVideoResolutionsUpdatingRef.current = false + setOptimisticVideoResolutions(null) + queryClient.invalidateQueries({ + queryKey: ['teams', teamId, 'settings'], + }) + toast.error(m.failed_update_settings()) + }) + } + const handleHardwareAccelerationChange = (value: HardwareAcceleration) => { updateSettings({ teamId, @@ -349,6 +451,8 @@ function TeamSettingsPage() { const handleHlsLadderToggle = (ladder: HlsResolutionLadder) => { const current = + pendingHlsResolutionsRef.current ?? + optimisticHlsResolutions ?? (settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ?? DEFAULT_HLS_RESOLUTIONS const updated = current.includes(ladder) @@ -357,13 +461,60 @@ function TeamSettingsPage() { if (updated.length === 0) return - updateSettings({ - teamId, - data: { - key: 'transcode.hlsResolutions', - value: updated, + pendingHlsResolutionsRef.current = updated + setOptimisticHlsResolutions(updated) + + queryClient.setQueryData( + ['teams', teamId, 'settings'], + (old: TeamSettingsResponse | undefined) => { + if (!old) return old + return { + ...old, + transcode: { + ...old.transcode, + hlsResolutions: updated, + }, + } }, - }) + ) + + isHlsResolutionsUpdatingRef.current = true + + hlsResolutionsQueueRef.current = hlsResolutionsQueueRef.current + .then(async () => { + const target = pendingHlsResolutionsRef.current + if (!target) return + + const res = await client.api.teams[':teamId'].settings.$patch({ + param: { teamId }, + json: { + key: 'transcode.hlsResolutions', + value: target, + }, + }) + if (!res.ok) throw new Error('Failed to update settings') + await res.json() + + if (pendingHlsResolutionsRef.current === target) { + pendingHlsResolutionsRef.current = null + isHlsResolutionsUpdatingRef.current = false + setOptimisticHlsResolutions(null) + await queryClient.invalidateQueries({ + queryKey: ['teams', teamId, 'settings'], + }) + toast.success(m.settings_updated()) + } + }) + .catch((err) => { + console.error(err) + pendingHlsResolutionsRef.current = null + isHlsResolutionsUpdatingRef.current = false + setOptimisticHlsResolutions(null) + queryClient.invalidateQueries({ + queryKey: ['teams', teamId, 'settings'], + }) + toast.error(m.failed_update_settings()) + }) } if (isSettingsLoading || isMeLoading) { @@ -383,10 +534,20 @@ function TeamSettingsPage() { (settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.best_match if (currentVideoStrategy === 'single' || currentVideoStrategy === 'disable') { currentVideoStrategy = VideoTranscodeStrategy.best_match - } else if (currentVideoStrategy === 'full') { - currentVideoStrategy = VideoTranscodeStrategy.all + } else if ( + currentVideoStrategy === 'full' || + currentVideoStrategy === 'all' || + currentVideoStrategy === 'multi' + ) { + currentVideoStrategy = VideoTranscodeStrategy.multi } + const currentVideoResolutions: VideoResolutionLadder[] = + optimisticVideoResolutions ?? + (settings as TeamSettingsResponse | undefined)?.transcode?.videoResolutions ?? + DEFAULT_VIDEO_RESOLUTIONS + const ALL_VIDEO_LADDERS: VideoResolutionLadder[] = ['480p', '720p', '1080p', '1440p', '2160p'] + const currentHardwareAcceleration = // eslint-disable-next-line @typescript-eslint/no-explicit-any (settings as any)?.transcode?.hardwareAcceleration || HardwareAcceleration.off @@ -395,6 +556,7 @@ function TeamSettingsPage() { const currentHlsEnabled = (settings as TeamSettingsResponse | undefined)?.transcode?.hlsEnabled ?? false const currentHlsResolutions: HlsResolutionLadder[] = + optimisticHlsResolutions ?? (settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ?? DEFAULT_HLS_RESOLUTIONS const ALL_HLS_LADDERS: HlsResolutionLadder[] = ['480p', '720p', '1080p', '1440p', '2160p'] @@ -842,17 +1004,47 @@ function TeamSettingsPage() {
handleVideoStrategyChange(VideoTranscodeStrategy.all)} + onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.multi)} > -
{m.all_resolutions()}
+
{m.multi_resolutions()}
- {m.all_resolutions_description()} + {m.multi_resolutions_description()}
+ + {currentVideoStrategy === VideoTranscodeStrategy.multi && ( +
+
+

+ {m.video_resolution_ladders()} +

+

+ {m.video_resolution_ladders_description()} +

+
+
+ {ALL_VIDEO_LADDERS.map((ladder) => { + const checked = currentVideoResolutions.includes(ladder) + return ( + + ) + })} +
+
+ )} diff --git a/packages/webui/routes/teams/$teamId/settings.test.tsx b/packages/webui/routes/teams/$teamId/settings.test.tsx new file mode 100644 index 00000000..29d5658d --- /dev/null +++ b/packages/webui/routes/teams/$teamId/settings.test.tsx @@ -0,0 +1,241 @@ +// @vitest-environment happy-dom +import { cleanup, render, screen, fireEvent, waitFor, act } from '@testing-library/react' +import React from 'react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { TeamSettingsPage } from './settings.lazy' +import { toast } from 'sonner' +import { VideoTranscodeStrategy } from '@shumai/dtos' + +interface PatchSettingsArgs { + param: { teamId: string } + json: { key: string; value: string[] } +} + +const mockGetSettings = vi.fn() +const mockPatchSettings = vi.fn() +const mockGetMe = vi.fn() + +vi.mock('@tanstack/react-router', () => ({ + createLazyFileRoute: () => (opts: Record) => ({ + ...opts, + useParams: () => ({ teamId: 'team-1' }), + }), + useNavigate: () => vi.fn(), + Link: ({ children, ...props }: React.AnchorHTMLAttributes) => ( + {children} + ), +})) + +vi.mock('@/ui/api/client', () => ({ + client: { + api: { + teams: { + ':teamId': { + settings: { + $get: () => mockGetSettings(), + $patch: (args: unknown) => mockPatchSettings(args), + }, + me: { + $get: () => mockGetMe(), + $patch: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }), + }, + }, + }, + }, + }, +})) + +vi.mock('sonner', () => ({ + toast: { + success: vi.fn(), + error: vi.fn(), + }, +})) + +describe('TeamSettingsPage Transcode Settings', () => { + let queryClient: QueryClient + + beforeEach(() => { + vi.clearAllMocks() + window.location.hash = '#transcode' + + queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + }, + }) + + mockGetMe.mockResolvedValue({ + ok: true, + json: async () => ({ + id: 'user-1', + name: 'Owner', + role: 'owner', + }), + }) + + mockGetSettings.mockResolvedValue({ + ok: true, + json: async () => ({ + transcode: { + videoStrategy: VideoTranscodeStrategy.multi, + videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'], + hardwareAcceleration: 'off', + threads: 0, + hlsEnabled: true, + hlsResolutions: ['480p', '720p', '1080p', '1440p', '2160p'], + }, + }), + }) + }) + + afterEach(() => { + cleanup() + }) + + it('keeps pending selections optimistically and serializes consecutive toggles without submitting stale arrays', async () => { + let resolveFirstPatch!: (value: unknown) => void + const firstPatchPromise = new Promise((resolve) => { + resolveFirstPatch = resolve + }) + + const patchCalls: PatchSettingsArgs[] = [] + + mockPatchSettings.mockImplementation(async (args: PatchSettingsArgs) => { + patchCalls.push(args) + if (patchCalls.length === 1) { + await firstPatchPromise + } + return { + ok: true, + json: async () => ({ + transcode: { + videoResolutions: args.json.value, + }, + }), + } + }) + + render( + + + , + ) + + // Wait for settings to load + await waitFor(() => { + expect(screen.getAllByText('480p').length).toBeGreaterThan(0) + expect(screen.getAllByText('720p').length).toBeGreaterThan(0) + }) + + const label480p = screen.getAllByText('480p')[0].closest('label') + const checkbox480p = label480p?.querySelector('button[role="checkbox"]') as HTMLElement + expect(checkbox480p).toBeDefined() + expect(checkbox480p.getAttribute('aria-checked')).toBe('true') + + const label720p = screen.getAllByText('720p')[0].closest('label') + const checkbox720p = label720p?.querySelector('button[role="checkbox"]') as HTMLElement + expect(checkbox720p).toBeDefined() + expect(checkbox720p.getAttribute('aria-checked')).toBe('true') + + // Click 480p to uncheck it + act(() => { + fireEvent.click(checkbox480p) + }) + + // Optimistically 480p should be unchecked immediately + expect(checkbox480p.getAttribute('aria-checked')).toBe('false') + + // Wait for the first PATCH request to be in-flight + await waitFor(() => { + expect(patchCalls.length).toBe(1) + }) + expect(patchCalls[0].json.value).toEqual(['720p', '1080p', '1440p', '2160p']) + + // Rapid consecutive click: Click 720p to uncheck it WHILE first patch is in-flight + act(() => { + fireEvent.click(checkbox720p) + }) + + // Optimistically 720p should also be unchecked immediately + expect(checkbox720p.getAttribute('aria-checked')).toBe('false') + + // Resolve first patch so the queue advances to the second queued update + await act(async () => { + resolveFirstPatch({ + ok: true, + json: async () => ({ + transcode: { + videoResolutions: ['720p', '1080p', '1440p', '2160p'], + }, + }), + }) + }) + + // Wait for the second queued patch to complete + await waitFor(() => { + expect(patchCalls.length).toBe(2) + }) + + // The second patch MUST NOT submit stale array with 480p present! + // It must contain neither 480p nor 720p + expect(patchCalls[1].json.value).toEqual(['1080p', '1440p', '2160p']) + expect(patchCalls[1].json.value.includes('480p')).toBe(false) + expect(patchCalls[1].json.value.includes('720p')).toBe(false) + + // Verify toast notification on success + expect(toast.success).toHaveBeenCalled() + }) + + it('coalesces rapid consecutive toggles before network dispatch into a single PATCH', async () => { + const patchCalls: PatchSettingsArgs[] = [] + + mockPatchSettings.mockImplementation(async (args: PatchSettingsArgs) => { + patchCalls.push(args) + return { + ok: true, + json: async () => ({ + transcode: { + videoResolutions: args.json.value, + }, + }), + } + }) + + render( + + + , + ) + + await waitFor(() => { + expect(screen.getAllByText('480p').length).toBeGreaterThan(0) + }) + + const label480p = screen.getAllByText('480p')[0].closest('label') + const checkbox480p = label480p?.querySelector('button[role="checkbox"]') as HTMLElement + + const label720p = screen.getAllByText('720p')[0].closest('label') + const checkbox720p = label720p?.querySelector('button[role="checkbox"]') as HTMLElement + + // Rapid synchronous clicks in the same event tick + act(() => { + fireEvent.click(checkbox480p) + fireEvent.click(checkbox720p) + }) + + // Both reflect optimistically + expect(checkbox480p.getAttribute('aria-checked')).toBe('false') + expect(checkbox720p.getAttribute('aria-checked')).toBe('false') + + // Wait for the coalesced PATCH to complete + await waitFor(() => { + expect(patchCalls.length).toBeGreaterThanOrEqual(1) + }) + + // The submitted payload must reflect both toggles + const lastPatch = patchCalls[patchCalls.length - 1] + expect(lastPatch.json.value).toEqual(['1080p', '1440p', '2160p']) + }) +}) diff --git a/packages/webui/stores/top-nav.ts b/packages/webui/stores/top-nav.ts index e1538a2d..a21a9858 100644 --- a/packages/webui/stores/top-nav.ts +++ b/packages/webui/stores/top-nav.ts @@ -23,6 +23,7 @@ export interface TopNavProjectState { key: string width: number height: number + resolution?: string hdr?: boolean }> }