feat(media): video versions: still frame + a version every screen plays (platform-v2 5.4e)

- services/video.py: background worker (one video at a time, low priority) makes a still
  frame (thumb/card/full WebP) and, when needed, play.mp4 (H.264+AAC, <=1920 px, HDR
  tone-mapped, faststart); the original is kept. Media.processing/progress (migration 0031,
  existing videos queued). ffmpeg from the pinned imageio-ffmpeg wheel; none = as uploaded.
- MOV (video/quicktime) accepted; /api/media/file/<sha>/play served with ranges.
- Signage plays the version (poster = still frame) and tells the screens playing it when ready.
- Manager library: still frames in the grid, preparing progress, status in the details.
- Tests: test_video.py (8), backend 541 passed; e2e media/signage/stack new checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 02:10:22 +02:00
co-authored by Claude Opus 5.5
parent f8d0d29d8f
commit b5361e10ca
28 changed files with 756 additions and 39 deletions
@@ -6,9 +6,9 @@ import toast from 'react-hot-toast'
import { Trash2 } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dialog, Field, Input, Spinner } from '@xenia/ui'
import { Button, Dialog, Field, Input, Spinner, cx } from '@xenia/ui'
import { deleteMedia, getMedia, renameMedia } from './mediaApi'
import { duration, fileSize, resolution, VARIANT_ORDER } from './mediaLogic'
import { duration, fileSize, preparing, resolution, VARIANT_ORDER, videoState } from './mediaLogic'
export default function MediaDetails({ id, onClose, onChanged, onDeleted }) {
const { t } = useTranslation('manager')
@@ -26,6 +26,16 @@ export default function MediaDetails({ id, onClose, onChanged, onDeleted }) {
.catch(err => { toast.error(errorMessage(err, t)); close.current() })
return () => { live = false }
}, [id, t])
// A video being prepared for screens (5.4e): follow it until it is done.
const changed = useRef(onChanged)
useEffect(() => { changed.current = onChanged })
const working = preparing([item])
useEffect(() => {
if (!working) return
const h = setInterval(() => { getMedia(id).then(m => { setItem(it => ({ ...it, ...m })); changed.current?.(m) }).catch(() => {}) }, 2000)
return () => clearInterval(h)
}, [working, id])
const state = videoState(item)
const save = async () => {
setBusy(true)
@@ -68,7 +78,7 @@ export default function MediaDetails({ id, onClose, onChanged, onDeleted }) {
<div className="grid gap-4" data-testid="media-details" data-id={item.id}>
<div className="rounded-card bg-surface-3 overflow-hidden flex items-center justify-center" style={{ maxHeight: '45vh' }}>
{item.kind === 'video'
? <video src={item.urls.original} controls preload="metadata" className="max-h-[45vh] max-w-full" />
? <video src={item.urls.play || item.urls.original} poster={item.urls.full} controls preload="metadata" className="max-h-[45vh] max-w-full" />
: <img src={item.urls.full} alt={item.name} className="max-h-[45vh] max-w-full object-contain" />}
</div>
@@ -90,6 +100,8 @@ export default function MediaDetails({ id, onClose, onChanged, onDeleted }) {
<section className="grid gap-1">
<h3 className="text-label font-semibold text-ink">{t('media.sizes')}</h3>
<p className="text-caption text-ink-3">{t(item.kind === 'video' ? 'media.sizesVideo' : 'media.sizesHint')}</p>
{state && <p className={cx('text-label font-medium', state === 'failed' ? 'text-danger-ink' : state === 'ready' || state === 'asIs' ? 'text-success-ink' : 'text-accent-ink')}
data-testid="media-video-status" data-state={state}>{t(`media.processing.${state}`, { progress: item.progress ?? 0 })}</p>}
<ul className="grid gap-1" data-testid="media-sizes">
{VARIANT_ORDER.filter(v => item.files?.[v]).map(v => (
<li key={v} className="flex gap-3 text-label">
@@ -9,7 +9,7 @@ import { errorMessage } from '@xenia/api'
import { useTranslation } from '@xenia/i18n'
import { Button, EmptyState, Input, Select, Spinner, cx } from '@xenia/ui'
import { listMedia } from './mediaApi'
import { ACCEPT } from './mediaLogic'
import { ACCEPT, preparing } from './mediaLogic'
import MediaDetails from './MediaDetails'
import { MediaGrid, UploadQueue } from './MediaParts'
import { useUploadQueue } from './mediaHooks'
@@ -32,6 +32,13 @@ export default function MediaPage() {
try { setPage(await listMedia({ kind, q: search, collection })); setError(null) } catch (err) { setError(errorMessage(err, t)) }
}, [kind, search, collection, t])
useEffect(() => { load() }, [load])
// Videos are prepared for screens in the background (5.4e): read the list again while any is.
const busy = preparing(page?.items)
useEffect(() => {
if (!busy) return
const h = setInterval(load, 3000)
return () => clearInterval(h)
}, [busy, load])
// Each finished upload shows at once; the list is read again when the batch is done.
const { queue, add, clear, summary } = useUploadQueue(useCallback(item => {
@@ -4,14 +4,22 @@ import { CheckCircle2, Copy, Film, ImageOff, Loader2, XCircle } from 'lucide-rea
import { useTranslation } from '@xenia/i18n'
import { cx } from '@xenia/ui'
import { useMediaMeta } from './mediaHooks'
import { videoState } from './mediaLogic'
/** The small picture of a library item (a film mark for videos, which have no still frame yet: 5.4e). */
/** The small picture of a library item; a video shows its still frame (5.4e) with a film mark, or just the mark. */
export function MediaThumb({ item, className }) {
const [broken, setBroken] = useState(false)
if (item?.kind === 'video') {
return <div className={cx('flex items-center justify-center bg-surface-3 text-ink-2', className)}><Film size={28} aria-hidden="true" /></div>
}
const src = item?.urls?.thumb || item?.thumb
if (item?.kind === 'video') {
return (
<div className={cx('relative flex items-center justify-center bg-surface-3 text-ink-2 overflow-hidden', className)}>
{src && !broken && <img src={src} alt="" loading="lazy" onError={() => setBroken(true)} className="absolute inset-0 w-full h-full object-cover" />}
<span className={cx('relative flex items-center justify-center', src && !broken && 'absolute bottom-1.5 left-1.5 w-7 h-7 rounded-full bg-surface/85 text-ink')}>
<Film size={src && !broken ? 16 : 28} aria-hidden="true" />
</span>
</div>
)
}
if (!src || broken) {
return <div className={cx('flex items-center justify-center bg-surface-3 text-ink-3', className)}><ImageOff size={24} aria-hidden="true" /></div>
}
@@ -34,6 +42,7 @@ export function MediaGrid({ items, onOpen, selectedId, selectedIds, testid = 'me
<span className="block px-2 pt-1.5 text-label font-medium text-ink truncate" title={item.name}>{item.name}</span>
{item.builtin && <span className="block px-2 text-caption text-info-ink" data-testid="media-builtin">{t('media.builtin')}</span>}
<span className="block px-2 pb-2 text-caption text-ink-3 truncate">{meta(item)}</span>
<VideoBadge item={item} />
</button>
</li>
))}
@@ -41,6 +50,20 @@ export function MediaGrid({ items, onOpen, selectedId, selectedIds, testid = 'me
)
}
/** Under a video in the grid while it is being prepared for screens, or when that failed (5.4e). */
function VideoBadge({ item }) {
const { t } = useTranslation('manager')
const state = videoState(item)
if (state !== 'waiting' && state !== 'working' && state !== 'failed') return null
return (
<span className={cx('flex items-center gap-1 px-2 pb-2 -mt-1 text-caption truncate', state === 'failed' ? 'text-danger-ink' : 'text-accent-ink')}
data-testid="media-video-state" data-state={state}>
{state !== 'failed' && <Loader2 size={12} className="animate-spin shrink-0" aria-hidden="true" />}
{t(`media.processingShort.${state}`, { progress: item.progress ?? 0 })}
</span>
)
}
/** The list of files being uploaded, with how each one went. */
export function UploadQueue({ queue, summary, onClear }) {
const { t } = useTranslation('manager')
@@ -1,7 +1,7 @@
// The media library's pure helpers (plan step 5.2): no React, unit-tested in tests/media.test.mjs.
/** What file inputs accept: everything the backend takes (services/media.py). */
export const ACCEPT = 'image/jpeg,image/png,image/webp,image/avif,image/gif,video/mp4,video/webm'
export const ACCEPT = 'image/jpeg,image/png,image/webp,image/avif,image/gif,video/mp4,video/quicktime,.mov,video/webm'
/** What an app needs to show a library picture (the same shape as the API's `image`). */
export function imageOf(item) {
@@ -34,7 +34,23 @@ export function duration(ms) {
}
/** Sizes in the order people read them: small → large → the original. */
export const VARIANT_ORDER = ['thumb', 'card', 'full', 'original']
export const VARIANT_ORDER = ['thumb', 'card', 'full', 'play', 'original']
/**
* How far a video is in being prepared for screens (plan step 5.4e; services/video.py):
* 'waiting' | 'working' | 'ready' (a version for screens was made) | 'asIs' (it plays everywhere as
* uploaded) | 'failed' | null (a picture, or no video tool on the server).
*/
export function videoState(item) {
if (item?.kind !== 'video' || !item.processing) return null
if (item.processing === 'done') return item.urls?.play ? 'ready' : 'asIs'
return item.processing
}
/** Is any of these videos still being prepared? (the page then reads them again every few seconds) */
export function preparing(items) {
return (items || []).some(i => i?.kind === 'video' && (i.processing === 'waiting' || i.processing === 'working'))
}
/**
* A batch upload's queue: one entry per file, uploaded one after another.
@@ -112,7 +112,7 @@ export function mediaEntry(item) {
export function mediaLookup(map) {
return id => {
const m = map?.[String(id)]
return m ? { ...m, src: m.url } : null
return m ? { ...m, src: m.url, poster: m.poster || undefined } : null
}
}