mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 00:29:50 +00:00
Merge pull request #42 from shumaiOne/feature/transcode-updates
feat(transcode): implement new image and video transcode strategies with UI detail enhancements
This commit is contained in:
+3
-10
@@ -62,20 +62,13 @@ export const updateTeamSettingsRequestSchema = z.object({
|
||||
export type UpdateTeamSettingsRequest = z.infer<typeof updateTeamSettingsRequestSchema>
|
||||
|
||||
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()),
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -16,7 +16,7 @@ describe('CollectionService', () => {
|
||||
name: 'Test Team',
|
||||
settings: {
|
||||
enablePublicSignup: true,
|
||||
transcode: { videoStrategy: 'disable', imageStrategy: 'disable' },
|
||||
transcode: { videoStrategy: 'best_match' },
|
||||
},
|
||||
sandbox: { create: {} },
|
||||
},
|
||||
|
||||
@@ -25,7 +25,7 @@ export class TeamService {
|
||||
name: 'Default Team',
|
||||
settings: {
|
||||
enablePublicSignup: true,
|
||||
transcode: { videoStrategy: 'disable', imageStrategy: 'disable' },
|
||||
transcode: { videoStrategy: 'best_match' },
|
||||
},
|
||||
sandbox: { create: {} },
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -78,7 +78,8 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
|
||||
// 6. Video Transcoding
|
||||
if (mimeType.startsWith('video/') && metadata) {
|
||||
const videoResolutions = getTargetVideoResolutions(
|
||||
spec.videoStrategy || 'disable',
|
||||
spec.videoStrategy || 'best_match',
|
||||
metadata.originalWidth,
|
||||
metadata.originalHeight,
|
||||
)
|
||||
for (const res of videoResolutions) {
|
||||
@@ -87,7 +88,6 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
|
||||
metadata.originalWidth,
|
||||
metadata.originalHeight,
|
||||
)
|
||||
if (width > metadata.originalWidth) continue
|
||||
|
||||
const videoSpec: PrismaJson.VideoTranscode = {
|
||||
resolution: res,
|
||||
@@ -122,27 +122,7 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
|
||||
// 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 +134,7 @@ export async function transcodeMedia(task: WorkflowTask): Promise<void> {
|
||||
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
|
||||
@@ -251,98 +220,94 @@ function isMimePsd(mimeType: string): boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
const RESOLUTION_LONG_SIDES: Record<string, number> = {
|
||||
'2160p': 3840,
|
||||
'1080p': 1920,
|
||||
'720p': 1280,
|
||||
'540p': 960,
|
||||
'360p': 640,
|
||||
'180p': 320,
|
||||
}
|
||||
|
||||
function resolutionToDimensions(
|
||||
resolution: string,
|
||||
originalWidth: number,
|
||||
originalHeight: number,
|
||||
): [number, number] {
|
||||
const aspectRatio = originalWidth / originalHeight
|
||||
let height = 0
|
||||
switch (resolution) {
|
||||
case '2160p':
|
||||
height = 2160
|
||||
break
|
||||
case '1440p':
|
||||
height = 1440
|
||||
break
|
||||
case '1080p':
|
||||
height = 1080
|
||||
break
|
||||
case '720p':
|
||||
height = 720
|
||||
break
|
||||
case '480p':
|
||||
height = 480
|
||||
break
|
||||
case '360p':
|
||||
height = 360
|
||||
break
|
||||
case '180p':
|
||||
height = 180
|
||||
break
|
||||
default:
|
||||
return [0, 0]
|
||||
}
|
||||
let width = Math.round(height * aspectRatio)
|
||||
const targetLongSide = RESOLUTION_LONG_SIDES[resolution]
|
||||
if (!targetLongSide) return [0, 0]
|
||||
|
||||
// Ensure even dimensions
|
||||
let width = 0
|
||||
let height = 0
|
||||
|
||||
if (originalWidth >= originalHeight) {
|
||||
// Landscape or square: width is the long side
|
||||
width = targetLongSide
|
||||
height = Math.round(width * (originalHeight / originalWidth))
|
||||
} else {
|
||||
// Portrait: height is the long side
|
||||
height = targetLongSide
|
||||
width = Math.round(height * (originalWidth / originalHeight))
|
||||
}
|
||||
|
||||
// Ensure even dimensions for H.264
|
||||
if (width % 2 !== 0) width++
|
||||
if (height % 2 !== 0) height++
|
||||
|
||||
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] // 360p (longSide 640)
|
||||
}
|
||||
|
||||
function getTargetVideoResolutions(
|
||||
strategy: PrismaJson.VideoTranscodeStrategy,
|
||||
originalWidth: number,
|
||||
originalHeight: number,
|
||||
): string[] {
|
||||
const resolutions = ['180p']
|
||||
if (strategy === 'disable') return resolutions
|
||||
|
||||
const targets = [2160, 1440, 1080, 720, 480]
|
||||
const targetMap: Record<number, string> = {
|
||||
'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(originalWidth, 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
|
||||
|
||||
@@ -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<HTMLDivElement>(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,69 @@ 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 {
|
||||
// To support Safari and iOS, we must invoke navigator.clipboard.write
|
||||
// synchronously inside the click handler. We can pass a promise to ClipboardItem
|
||||
// which resolves to the Blob asynchronously.
|
||||
const copyPromise = (async () => {
|
||||
const response = await fetch(webpUrl)
|
||||
const blob = await response.blob()
|
||||
|
||||
const img = new Image()
|
||||
img.crossOrigin = 'anonymous'
|
||||
const objectUrl = URL.createObjectURL(blob)
|
||||
img.src = objectUrl
|
||||
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
img.onload = resolve
|
||||
img.onerror = (e) => reject(new Error('Failed to load image: ' + String(e)))
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
const pngBlob = await new Promise<Blob | null>((resolve) => {
|
||||
canvas.toBlob(resolve, 'image/png')
|
||||
})
|
||||
|
||||
if (!pngBlob) throw new Error('Failed to convert image to PNG')
|
||||
return pngBlob
|
||||
} finally {
|
||||
URL.revokeObjectURL(objectUrl)
|
||||
}
|
||||
})()
|
||||
|
||||
await navigator.clipboard.write([
|
||||
new ClipboardItem({
|
||||
'image/png': copyPromise,
|
||||
}),
|
||||
])
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 2000)
|
||||
} catch (err) {
|
||||
console.error('Failed to copy image to clipboard:', err)
|
||||
}
|
||||
}
|
||||
|
||||
if (!bestUrl) {
|
||||
return (
|
||||
<div className="flex flex-1 items-center justify-center">
|
||||
@@ -160,7 +224,7 @@ export function FileViewer({
|
||||
</div>
|
||||
{/* Zoom Toolbar */}
|
||||
<div className="relative px-4 py-3 bg-gray-50 dark:bg-gray-800 border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
|
||||
<div className="flex items-center gap-1 bg-gray-200/50 dark:bg-white/10 rounded-md p-0.5">
|
||||
<div className="flex items-center gap-1 bg-gray-200/50 dark:bg-white/10 rounded-md p-0.5 mr-auto">
|
||||
<button
|
||||
onClick={() => handleZoom(0.8)}
|
||||
className="p-1.5 rounded hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors"
|
||||
@@ -181,10 +245,28 @@ export function FileViewer({
|
||||
</div>
|
||||
<button
|
||||
onClick={handleFit}
|
||||
className="text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent"
|
||||
className="text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent animate-in fade-in zoom-in-95 duration-200"
|
||||
>
|
||||
Fit
|
||||
</button>
|
||||
<button
|
||||
onClick={handleCopy}
|
||||
disabled={!bestUrl}
|
||||
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
|
||||
title="Copy optimized image to clipboard"
|
||||
>
|
||||
{copied ? <Check size={14} className="text-green-500" /> : <Copy size={14} />}
|
||||
{copied ? 'Copied' : 'Copy'}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleDownload}
|
||||
disabled={!file.media?.original?.downloadUrl}
|
||||
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
|
||||
title="Download original image"
|
||||
>
|
||||
<Download size={14} />
|
||||
Download
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -99,6 +99,9 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
if (!data.media?.metadata) {
|
||||
return null
|
||||
}
|
||||
|
||||
const previewResolutions = resolutions.filter((r) => !r.isRaw)
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
@@ -239,7 +242,7 @@ const ControlBar: React.FC<ControlBarProps> = ({
|
||||
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuLabel>Quality</DropdownMenuLabel>
|
||||
{resolutions.map((res) => (
|
||||
{previewResolutions.map((res) => (
|
||||
<DropdownMenuItem
|
||||
key={res.resolution}
|
||||
onClick={() => changeResolution(res)}
|
||||
@@ -373,38 +376,45 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
||||
resolution: 'Original',
|
||||
} as DisplayTranscode
|
||||
|
||||
const resolutions: DisplayTranscode[] = (data.media.videoTranscodes ?? []).map((t) => ({
|
||||
...t,
|
||||
resolution: t.isRaw ? 'Original' : `${t.height}p`,
|
||||
}))
|
||||
const resolutions: DisplayTranscode[] = (data.media.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'
|
||||
|
||||
return {
|
||||
...t,
|
||||
resolution: t.isRaw ? 'Original' : resolution,
|
||||
}
|
||||
})
|
||||
|
||||
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()
|
||||
|
||||
@@ -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 (
|
||||
<div className="flex h-screen items-center justify-center">
|
||||
@@ -103,13 +93,14 @@ function TeamSettingsPage() {
|
||||
return <div className="p-8 text-center text-red-500">Failed to load settings.</div>
|
||||
}
|
||||
|
||||
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 (
|
||||
<div className="h-full bg-slate-50 dark:bg-slate-950 font-sans selection:bg-blue-100 dark:selection:bg-blue-900 transition-colors duration-300">
|
||||
@@ -342,11 +333,13 @@ function TeamSettingsPage() {
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentVideoStrategy === VideoTranscodeStrategy.single
|
||||
currentVideoStrategy === VideoTranscodeStrategy.best_match
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.single)}
|
||||
onClick={() =>
|
||||
handleVideoStrategyChange(VideoTranscodeStrategy.best_match)
|
||||
}
|
||||
>
|
||||
<div className="font-semibold">Best match</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
@@ -357,72 +350,17 @@ function TeamSettingsPage() {
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentVideoStrategy === VideoTranscodeStrategy.full
|
||||
currentVideoStrategy === VideoTranscodeStrategy.all
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.full)}
|
||||
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.all)}
|
||||
>
|
||||
<div className="font-semibold">All resolutions</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Generates all supported resolutions up to the source quality.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentVideoStrategy === VideoTranscodeStrategy.disable
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() =>
|
||||
handleVideoStrategyChange(VideoTranscodeStrategy.disable)
|
||||
}
|
||||
>
|
||||
<div className="font-semibold">Disable</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Transcoding is disabled. Only system artifacts are generated.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Image Strategy */}
|
||||
<div className="space-y-3">
|
||||
<h3 className="text-lg font-medium">Image Strategy</h3>
|
||||
<div className="space-y-3">
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentImageStrategy === ImageTranscodeStrategy.single
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() => handleImageStrategyChange(ImageTranscodeStrategy.single)}
|
||||
>
|
||||
<div className="font-semibold">Best match</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Generates a single optimal resolution matching the source quality.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
className={cn(
|
||||
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
|
||||
currentImageStrategy === ImageTranscodeStrategy.disable
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border',
|
||||
)}
|
||||
onClick={() =>
|
||||
handleImageStrategyChange(ImageTranscodeStrategy.disable)
|
||||
}
|
||||
>
|
||||
<div className="font-semibold">Disable</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
Transcoding is disabled. Only system thumbnails are generated.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
Reference in New Issue
Block a user