From ec14e12b3f3bbc06e4a63672c1e32165b44cc1b5 Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 30 May 2026 12:09:27 +0800 Subject: [PATCH] feat(transcode): implement new image and video transcode strategies with UI detail enhancements --- src/dtos/team.ts | 13 +-- src/prisma-json-types.ts | 5 +- src/services/collection/collection.test.ts | 2 +- src/services/team/team.ts | 2 +- src/services/upload/upload.test.ts | 3 +- src/services/upload/upload.ts | 4 +- src/transcode/transcode.test.ts | 4 +- src/transcode/transcoder.ts | 5 - src/transcode/workflows/transcode.test.ts | 3 +- src/transcode/workflows/transcode.ts | 110 +++++++-------------- src/ui/components/file-viewer.tsx | 77 ++++++++++++++- src/ui/components/viewers/video-player.tsx | 25 +++-- src/ui/routes/teams/$teamId/settings.tsx | 90 +++-------------- 13 files changed, 149 insertions(+), 194 deletions(-) diff --git a/src/dtos/team.ts b/src/dtos/team.ts index 4cc0b2cb..0f8b0ccb 100644 --- a/src/dtos/team.ts +++ b/src/dtos/team.ts @@ -62,20 +62,13 @@ export const updateTeamSettingsRequestSchema = z.object({ export type UpdateTeamSettingsRequest = z.infer export const VideoTranscodeStrategy = { - disable: 'disable', - single: 'single', - full: 'full', + // eslint-disable-next-line @typescript-eslint/naming-convention + best_match: 'best_match', + all: 'all', } as const export type VideoTranscodeStrategy = (typeof VideoTranscodeStrategy)[keyof typeof VideoTranscodeStrategy] -export const ImageTranscodeStrategy = { - disable: 'disable', - single: 'single', -} as const -export type ImageTranscodeStrategy = - (typeof ImageTranscodeStrategy)[keyof typeof ImageTranscodeStrategy] - export const sandboxSettingsSchema = z.object({ allowedDomains: z.array(z.string()), }) diff --git a/src/prisma-json-types.ts b/src/prisma-json-types.ts index 3bc6756b..957d4b9b 100644 --- a/src/prisma-json-types.ts +++ b/src/prisma-json-types.ts @@ -46,17 +46,15 @@ declare global { // ---------------------------------------------------------------------- // Team Settings // ---------------------------------------------------------------------- - export type VideoTranscodeStrategy = 'disable' | 'single' | 'full' + export type VideoTranscodeStrategy = 'best_match' | 'all' // ---------------------------------------------------------------------- // Collection Filter // ---------------------------------------------------------------------- export type CollectionFilter = import('./dtos/collection').CollectionFilter - export type ImageTranscodeStrategy = 'disable' | 'single' export interface TranscodeSettings { videoStrategy: VideoTranscodeStrategy - imageStrategy: ImageTranscodeStrategy } export interface Settings { @@ -254,7 +252,6 @@ declare global { // ---------------------------------------------------------------------- export interface TaskSpec { videoStrategy?: VideoTranscodeStrategy - imageStrategy?: ImageTranscodeStrategy thumbnail?: boolean sprite?: boolean poster?: boolean diff --git a/src/services/collection/collection.test.ts b/src/services/collection/collection.test.ts index 9bf07722..9fb470e8 100644 --- a/src/services/collection/collection.test.ts +++ b/src/services/collection/collection.test.ts @@ -16,7 +16,7 @@ describe('CollectionService', () => { name: 'Test Team', settings: { enablePublicSignup: true, - transcode: { videoStrategy: 'disable', imageStrategy: 'disable' }, + transcode: { videoStrategy: 'best_match' }, }, sandbox: { create: {} }, }, diff --git a/src/services/team/team.ts b/src/services/team/team.ts index 9d481ccc..5666ccd3 100644 --- a/src/services/team/team.ts +++ b/src/services/team/team.ts @@ -25,7 +25,7 @@ export class TeamService { name: 'Default Team', settings: { enablePublicSignup: true, - transcode: { videoStrategy: 'disable', imageStrategy: 'disable' }, + transcode: { videoStrategy: 'best_match' }, }, sandbox: { create: {} }, }, diff --git a/src/services/upload/upload.test.ts b/src/services/upload/upload.test.ts index 44bb9684..aebf2cff 100644 --- a/src/services/upload/upload.test.ts +++ b/src/services/upload/upload.test.ts @@ -127,7 +127,7 @@ describe('UploadService', () => { expect(workflowTask?.payload).toEqual({ projectId: projectId, transcode: { - videoStrategy: 'single', + videoStrategy: 'best_match', sprite: true, poster: true, }, @@ -164,7 +164,6 @@ describe('UploadService', () => { expect(workflowTask?.payload).toEqual({ projectId: projectId, transcode: { - imageStrategy: 'single', thumbnail: true, }, }) diff --git a/src/services/upload/upload.ts b/src/services/upload/upload.ts index 4bcaecba..21d72400 100644 --- a/src/services/upload/upload.ts +++ b/src/services/upload/upload.ts @@ -299,7 +299,7 @@ export class UploadService { } else { const settings = team.settings as PrismaJson.Settings | null if (isVideo) { - const strategy = settings?.transcode?.videoStrategy || 'single' + const strategy = settings?.transcode?.videoStrategy || 'best_match' // eslint-disable-next-line @typescript-eslint/no-explicit-any await new VideoTranscoder(tx as any, asset.id, team.id, asset.projectId) .setStrategy(strategy) @@ -307,10 +307,8 @@ export class UploadService { .withPoster() .submit() } else if (isImage) { - const strategy = settings?.transcode?.imageStrategy || 'single' // eslint-disable-next-line @typescript-eslint/no-explicit-any await new ImageTranscoder(tx as any, asset.id, team.id, asset.projectId) - .setStrategy(strategy) .withThumbnail() .submit() } diff --git a/src/transcode/transcode.test.ts b/src/transcode/transcode.test.ts index 685123dc..819f5cce 100644 --- a/src/transcode/transcode.test.ts +++ b/src/transcode/transcode.test.ts @@ -217,7 +217,7 @@ describe('TranscodeService', () => { it('should create transcode tasks correctly', async () => { const task = (await transcodeService.createVideoTranscodeTask('asset-123', 'proj-123', { - videoStrategy: 'single', + videoStrategy: 'best_match', thumbnail: true, })) as WorkflowTask @@ -225,6 +225,6 @@ describe('TranscodeService', () => { expect(task.type).toBe('transcode') expect(task.status).toBe('pending') expect(task.payload?.projectId).toBe('proj-123') - expect(task.payload?.transcode?.videoStrategy).toBe('single') + expect(task.payload?.transcode?.videoStrategy).toBe('best_match') }) }) diff --git a/src/transcode/transcoder.ts b/src/transcode/transcoder.ts index 907b3ec8..799287eb 100644 --- a/src/transcode/transcoder.ts +++ b/src/transcode/transcoder.ts @@ -60,11 +60,6 @@ export class ImageTranscoder { private readonly projectId: string, ) {} - setStrategy(strategy: PrismaJson.ImageTranscodeStrategy): this { - this.spec.imageStrategy = strategy - return this - } - withThumbnail(): this { this.spec.thumbnail = true return this diff --git a/src/transcode/workflows/transcode.test.ts b/src/transcode/workflows/transcode.test.ts index 826d1721..7b4efeac 100644 --- a/src/transcode/workflows/transcode.test.ts +++ b/src/transcode/workflows/transcode.test.ts @@ -76,7 +76,7 @@ describe('Transcode Workflow', () => { payload: { projectId: 'proj-1', transcode: { - videoStrategy: 'single', + videoStrategy: 'best_match', thumbnail: true, }, }, @@ -151,7 +151,6 @@ describe('Transcode Workflow', () => { payload: { projectId: 'proj-1', transcode: { - imageStrategy: 'single', thumbnail: true, }, }, diff --git a/src/transcode/workflows/transcode.ts b/src/transcode/workflows/transcode.ts index d571ec76..669526e5 100644 --- a/src/transcode/workflows/transcode.ts +++ b/src/transcode/workflows/transcode.ts @@ -78,7 +78,7 @@ export async function transcodeMedia(task: WorkflowTask): Promise { // 6. Video Transcoding if (mimeType.startsWith('video/') && metadata) { const videoResolutions = getTargetVideoResolutions( - spec.videoStrategy || 'disable', + spec.videoStrategy || 'best_match', metadata.originalHeight, ) for (const res of videoResolutions) { @@ -87,7 +87,6 @@ export async function transcodeMedia(task: WorkflowTask): Promise { metadata.originalWidth, metadata.originalHeight, ) - if (width > metadata.originalWidth) continue const videoSpec: PrismaJson.VideoTranscode = { resolution: res, @@ -122,27 +121,7 @@ export async function transcodeMedia(task: WorkflowTask): Promise { // 7. Image Transcoding const isImage = mimeType.startsWith('image/') const isPsd = isMimePsd(mimeType) - if (isImage || isPsd) { - if (metadata) { - const imageSpecs = getTargetImageResolutions( - spec.imageStrategy || 'disable', - metadata.originalWidth, - ) - for (const imageSpec of imageSpecs) { - if (imageSpec.width > metadata.originalWidth) continue - - const imageTranscode = await executeActivity(workerQueue, transcodeImageActivity, { - assetKey: key, - filePath, - imageSpec, - }) - mediaInfo.imageTranscodes.push(imageTranscode) - } - } - } - - // 8. PSD / Raw fallback - if (mediaInfo.imageTranscodes.length === 0 && isPsd && metadata) { + if ((isImage || isPsd) && metadata) { const imageSpec: PrismaJson.ImageTranscode = { width: metadata.originalWidth, height: metadata.originalHeight, @@ -154,18 +133,7 @@ export async function transcodeMedia(task: WorkflowTask): Promise { filePath, imageSpec, }) - imageTranscode.isRaw = true mediaInfo.imageTranscodes.push(imageTranscode) - } else if (isImage && metadata) { - // Always keep raw for image - mediaInfo.imageTranscodes.push({ - key: mediaInfo.original?.key, - width: metadata.originalWidth, - height: metadata.originalHeight, - format: mimeType, - isRaw: true, - quality: 100, - }) } // 9. System: Thumbnail @@ -271,6 +239,9 @@ function resolutionToDimensions( case '720p': height = 720 break + case '540p': + height = 540 + break case '480p': height = 480 break @@ -292,57 +263,52 @@ function resolutionToDimensions( return [width, height] } +const TARGET_RESOLUTIONS = [ + { name: '2160p', height: 2160 }, + { name: '1080p', height: 1080 }, + { name: '720p', height: 720 }, + { name: '540p', height: 540 }, + { name: '360p', height: 360 }, +] + +function getBestMatchResolution(originalHeight: number): { name: string; height: number } { + const lower = TARGET_RESOLUTIONS.filter((r) => r.height < originalHeight) + if (lower.length > 0) { + lower.sort((a, b) => b.height - a.height) + return lower[0] + } + return TARGET_RESOLUTIONS[TARGET_RESOLUTIONS.length - 1] // 360p +} + function getTargetVideoResolutions( strategy: PrismaJson.VideoTranscodeStrategy, originalHeight: number, ): string[] { const resolutions = ['180p'] - if (strategy === 'disable') return resolutions - const targets = [2160, 1440, 1080, 720, 480] - const targetMap: Record = { - '2160': '2160p', - '1440': '1440p', - '1080': '1080p', - '720': '720p', - '480': '480p', + // Normalize legacy strategies for backward compatibility + let normalizedStrategy: string = strategy + const stratStr = strategy as string + if (stratStr === 'single' || stratStr === 'disable') { + normalizedStrategy = 'best_match' + } else if (stratStr === 'full') { + normalizedStrategy = 'all' } - const validTargets = targets.filter((t) => t <= originalHeight) + const bestMatch = getBestMatchResolution(originalHeight) - if (strategy === 'single') { - if (validTargets.length > 0) { - resolutions.push(targetMap[validTargets[0]]) - } - } else if (strategy === 'full') { - for (const t of validTargets) { - resolutions.push(targetMap[t]) + 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) + } } } return resolutions } -function getTargetImageResolutions( - strategy: PrismaJson.ImageTranscodeStrategy, - originalWidth: number, -): PrismaJson.ImageTranscode[] { - if (strategy === 'disable') return [] - - const targets = [ - { width: 1920, quality: 80, format: 'webp' }, - { width: 1280, quality: 80, format: 'webp' }, - { width: 854, quality: 80, format: 'webp' }, - ] - - if (strategy === 'single') { - const valid = targets.filter((t) => t.width <= originalWidth) - if (valid.length > 0) { - return [{ ...valid[0], height: 0 }] - } - } - - return [] -} - export const transcodeWorkflow = transcodeMedia diff --git a/src/ui/components/file-viewer.tsx b/src/ui/components/file-viewer.tsx index 9d70bbc0..a254e8d4 100644 --- a/src/ui/components/file-viewer.tsx +++ b/src/ui/components/file-viewer.tsx @@ -1,7 +1,7 @@ import type { AssetInfo } from '@/dtos/asset' import { useScreenSize } from '@/ui/hooks/useScreenSize' import { getBestTranscode } from '@/ui/lib/media' -import { Minus, Plus } from 'lucide-react' +import { Minus, Plus, Copy, Download, Check } from 'lucide-react' import type { RefObject } from 'react' import { useEffect, useRef, useState } from 'react' import type Player from 'video.js/dist/types/player' @@ -52,6 +52,7 @@ export function FileViewer({ const containerRef = useRef(null) const [zoom, setZoom] = useState(1) const [pan, setPan] = useState({ x: 0, y: 0 }) + const [copied, setCopied] = useState(false) const imgW = file.media?.metadata?.originalWidth ?? 1920 const imgH = file.media?.metadata?.originalHeight ?? 1080 @@ -122,6 +123,58 @@ export function FileViewer({ } } + const handleDownload = () => { + const url = file.media?.original?.downloadUrl + if (!url) return + const link = document.createElement('a') + link.href = url + link.download = file.name || 'image' + document.body.appendChild(link) + link.click() + document.body.removeChild(link) + } + + const handleCopy = async () => { + const webpUrl = bestUrl + if (!webpUrl) return + try { + const response = await fetch(webpUrl) + const blob = await response.blob() + + const img = new Image() + img.crossOrigin = 'anonymous' + img.src = URL.createObjectURL(blob) + await new Promise((resolve, reject) => { + img.onload = resolve + img.onerror = reject + }) + + const canvas = document.createElement('canvas') + canvas.width = img.width + canvas.height = img.height + const ctx = canvas.getContext('2d') + if (!ctx) throw new Error('Could not get canvas context') + ctx.drawImage(img, 0, 0) + + canvas.toBlob(async (pngBlob) => { + if (!pngBlob) return + try { + await navigator.clipboard.write([ + new ClipboardItem({ + 'image/png': pngBlob, + }), + ]) + setCopied(true) + setTimeout(() => setCopied(false), 2000) + } catch (err) { + console.error('Failed to copy image to clipboard:', err) + } + }, 'image/png') + } catch (err) { + console.error('Failed to copy image:', err) + } + } + if (!bestUrl) { return (
@@ -160,7 +213,7 @@ export function FileViewer({
{/* Zoom Toolbar */}
-
+
+ +
)} diff --git a/src/ui/components/viewers/video-player.tsx b/src/ui/components/viewers/video-player.tsx index 9e8c4aa7..3a92c0a9 100644 --- a/src/ui/components/viewers/video-player.tsx +++ b/src/ui/components/viewers/video-player.tsx @@ -99,6 +99,9 @@ const ControlBar: React.FC = ({ if (!data.media?.metadata) { return null } + + const previewResolutions = resolutions.filter((r) => !r.isRaw) + return (
= ({ Quality - {resolutions.map((res) => ( + {previewResolutions.map((res) => ( changeResolution(res)} @@ -378,33 +381,29 @@ const VideoPlayer: React.FC = ({ resolution: t.isRaw ? 'Original' : `${t.height}p`, })) + const previewResolutions = resolutions.filter((r) => !r.isRaw) + // Logic to select best resolution based on screen size const getInitialResolution = (): DisplayTranscode => { - if (typeof window === 'undefined') return originalRes + if (previewResolutions.length === 0) return originalRes + + if (typeof window === 'undefined') return previewResolutions[0] // Use device pixel ratio for high DPI screens const screenWidth = window.innerWidth * (window.devicePixelRatio || 1) - // Strategy: Find smallest resolution that is >= screenWidth. - // Filter out raw versions unless they are the only option. - - const nonRaw = resolutions.filter((r) => !r.isRaw) - const candidates = nonRaw.length > 0 ? nonRaw : resolutions - // 1. Sort by width ascending - const sortedResolutions = [...candidates].sort((a, b) => { + const sortedResolutions = [...previewResolutions].sort((a, b) => { const wA = a.width ?? 0 const wB = b.width ?? 0 - if (wA !== wB) return wA - wB - // If equal width, prefer non-raw (optimized) - return (a.isRaw ? 1 : 0) - (b.isRaw ? 1 : 0) + return wA - wB }) // 2. Find first one >= screenWidth const bestFit = sortedResolutions.find((r) => (r.width ?? 0) >= screenWidth) // 3. If found, return it. If not (all are smaller), return the last one (largest). - return bestFit || sortedResolutions[sortedResolutions.length - 1] || originalRes + return bestFit || sortedResolutions[sortedResolutions.length - 1] } const initialRes = getInitialResolution() diff --git a/src/ui/routes/teams/$teamId/settings.tsx b/src/ui/routes/teams/$teamId/settings.tsx index 7609c707..2e337895 100644 --- a/src/ui/routes/teams/$teamId/settings.tsx +++ b/src/ui/routes/teams/$teamId/settings.tsx @@ -1,4 +1,4 @@ -import { ImageTranscodeStrategy, VideoTranscodeStrategy } from '@/dtos/team' +import { VideoTranscodeStrategy } from '@/dtos/team' import { client } from '@/ui/api/client' import { AgentsSettings } from '@/ui/components/settings/AgentsSettings' import { ProvidersSettings } from '@/ui/components/settings/ProvidersSettings' @@ -81,16 +81,6 @@ function TeamSettingsPage() { }) } - const handleImageStrategyChange = (value: ImageTranscodeStrategy) => { - updateSettings({ - teamId, - data: { - key: 'transcode.imageStrategy', - value: value, - }, - }) - } - if (isSettingsLoading || isMeLoading) { return (
@@ -103,13 +93,14 @@ function TeamSettingsPage() { return
Failed to load settings.
} - const currentVideoStrategy = + let currentVideoStrategy = // eslint-disable-next-line @typescript-eslint/no-explicit-any - (settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.single - - const currentImageStrategy = - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (settings as any)?.transcode?.imageStrategy || ImageTranscodeStrategy.single + (settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.best_match + if (currentVideoStrategy === 'single' || currentVideoStrategy === 'disable') { + currentVideoStrategy = VideoTranscodeStrategy.best_match + } else if (currentVideoStrategy === 'full') { + currentVideoStrategy = VideoTranscodeStrategy.all + } return (
@@ -342,11 +333,13 @@ function TeamSettingsPage() {
handleVideoStrategyChange(VideoTranscodeStrategy.single)} + onClick={() => + handleVideoStrategyChange(VideoTranscodeStrategy.best_match) + } >
Best match
@@ -357,72 +350,17 @@ function TeamSettingsPage() {
handleVideoStrategyChange(VideoTranscodeStrategy.full)} + onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.all)} >
All resolutions
Generates all supported resolutions up to the source quality.
- -
- handleVideoStrategyChange(VideoTranscodeStrategy.disable) - } - > -
Disable
-
- Transcoding is disabled. Only system artifacts are generated. -
-
-
-
- - {/* Image Strategy */} -
-

Image Strategy

-
-
handleImageStrategyChange(ImageTranscodeStrategy.single)} - > -
Best match
-
- Generates a single optimal resolution matching the source quality. -
-
- -
- handleImageStrategyChange(ImageTranscodeStrategy.disable) - } - > -
Disable
-
- Transcoding is disabled. Only system thumbnails are generated. -
-