mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
feat(transcode): add multi-resolution strategy with configurable ladders and fallback (#438)
* feat(transcode): add multi-resolution strategy with configurable ladders and fallback * feat(settings): show toast on settings update and add manual verification plan to PR template * fix(webui): preserve video resolution and fix 480p label in download dropdowns * fix(webui): display standard short-side resolution for vertical videos in seekbar * fix(webui): fix portrait transcode resolution labeling and serialize settings toggles
This commit is contained in:
@@ -214,4 +214,95 @@ describe('CompareVideoPane', () => {
|
||||
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<ComparePaneHandle>()
|
||||
const reportedStates: PaneReportedState[] = []
|
||||
|
||||
render(
|
||||
<CompareVideoPane
|
||||
ref={ref}
|
||||
file={hlsVideo}
|
||||
isActive={true}
|
||||
annotations={[]}
|
||||
onActivate={vi.fn()}
|
||||
onStateChange={(s) => 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(
|
||||
<CompareVideoPane
|
||||
file={legacyPortraitVideo}
|
||||
isActive={true}
|
||||
annotations={[]}
|
||||
onActivate={vi.fn()}
|
||||
onStateChange={(s) => reportedStates.push(s)}
|
||||
/>,
|
||||
)
|
||||
|
||||
const lastState = reportedStates[reportedStates.length - 1]
|
||||
expect(lastState?.video?.resolutions?.[0]?.resolution).toBe('1080p')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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<ComparePaneHandle, CompareVideoPanePr
|
||||
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
|
||||
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<ComparePaneHandle, CompareVideoPanePr
|
||||
],
|
||||
)
|
||||
|
||||
const displayAnnotations = [...annotations, ...(isActive ? draftAnnotations : [])]
|
||||
const displayAnnotations = [...(annotations ?? []), ...(isActive ? draftAnnotations : [])]
|
||||
|
||||
const scale = zoom
|
||||
const defaultPanX = (containerSize.width - vidW * scale) / 2
|
||||
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
import { Slider } from '../../ui/slider'
|
||||
import ProgressBar from './progress-bar'
|
||||
import { formatTime, formatTimecode } from './utils'
|
||||
import { getVideoResolutionLabel } from '@/ui/lib/media'
|
||||
|
||||
export interface PlayerState {
|
||||
isPlaying: boolean
|
||||
@@ -431,8 +432,7 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
||||
? 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 (
|
||||
<DropdownMenuItem
|
||||
|
||||
@@ -495,4 +495,75 @@ describe('VideoViewer', () => {
|
||||
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(<VideoViewer file={hlsVideo} />)
|
||||
|
||||
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(<VideoViewer file={legacyPortraitVideo} />)
|
||||
const settingsButton = container.querySelector('button:has(.lucide-settings)')
|
||||
expect(settingsButton?.textContent).toContain('1080p')
|
||||
expect(settingsButton?.textContent).not.toContain('1920p')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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<MediaController, FileViewerProps>(
|
||||
? (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<MediaController, FileViewerProps>(
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user