- 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>
153 lines
7.3 KiB
JavaScript
153 lines
7.3 KiB
JavaScript
// The signage editor's pure helpers (plan step 5.6a): unit-tested in tests/signage.test.mjs.
|
||
|
||
export const KINDS = ['image', 'video', 'text', 'menu']
|
||
export const TRANSITIONS = ['fade', 'slide', 'zoom', 'none']
|
||
export const FONT_KEYS = ['sans', 'serif', 'rounded', 'condensed', 'display']
|
||
|
||
/** Ready-made slides to start from: { key, kind, slide fields, content }. '@x' texts are filled from
|
||
* `signage.templates.<key>.<x>` in the manager's language when the slide is made. */
|
||
export const TEMPLATES = [
|
||
{ key: 'promo', kind: 'image', duration_s: 8, content: { fit: 'cover' } },
|
||
{ key: 'announcement', kind: 'text', duration_s: 10, content: {
|
||
title: '', body: '', align: 'center', vertical: 'middle', font: 'sans', text_colour: '#ffffff', text_size: 100,
|
||
background: { kind: 'colour', colour: '#1e293b', media_id: null, dim: 0, fit: 'cover' },
|
||
widgets: [{ kind: 'name', position: 'top-left' }, { kind: 'clock', position: 'top-right' }],
|
||
} },
|
||
{ key: 'happyHour', kind: 'text', duration_s: 10, time_from: '17:00', time_to: '19:00', content: {
|
||
title: '@title', body: '@body', align: 'center', vertical: 'middle', font: 'display', text_colour: '#fde68a', text_size: 120,
|
||
background: { kind: 'colour', colour: '#7c2d12', media_id: null, dim: 0, fit: 'cover' },
|
||
} },
|
||
{ key: 'welcome', kind: 'text', duration_s: 10, content: {
|
||
title: '@title', body: '', align: 'center', vertical: 'middle', font: 'serif', text_colour: '#ffffff', text_size: 110,
|
||
background: { kind: 'colour', colour: '#14532d', media_id: null, dim: 0, fit: 'cover' },
|
||
widgets: [{ kind: 'logo', position: 'top-center', size: 'large' }, { kind: 'message', position: 'bottom-bar' }, { kind: 'date', position: 'bottom-bar' }],
|
||
} },
|
||
{ key: 'menuBoard', kind: 'menu', duration_s: 20, content: {
|
||
title: '@title', select: 'all', group_by_category: true, columns: 2, sales_point: 'in_store', sold_out: 'strike',
|
||
background: { kind: 'colour', colour: '#111827', media_id: null, dim: 0, fit: 'cover' }, accent_colour: '#f59e0b',
|
||
widgets: [{ kind: 'clock', position: 'top-right' }, { kind: 'message', position: 'bottom-bar' }],
|
||
} },
|
||
{ key: 'video', kind: 'video', video_to_end: true, content: { fit: 'cover', muted: true } },
|
||
]
|
||
|
||
/** The body to create a slide from a template; `t` fills its '@' texts. */
|
||
export function slideFromTemplate(tpl, t = k => k) {
|
||
const { key, kind, content, ...fields } = tpl
|
||
const filled = JSON.parse(JSON.stringify(content || {}), (_, v) =>
|
||
(typeof v === 'string' && v.startsWith('@') ? t(`signage.templates.${key}.${v.slice(1)}`) : v))
|
||
return { kind, ...fields, content: filled }
|
||
}
|
||
|
||
/** Does a picture / video fit a playlist's orientation? null when its size isn't known. */
|
||
export function fitsOrientation(media, orientation) {
|
||
if (!media?.width || !media?.height) return null
|
||
const wide = media.width >= media.height
|
||
return orientation === 'portrait' ? !wide || media.width === media.height : wide
|
||
}
|
||
|
||
/** The media ids a slide shows big (its picture / video and its background): what must fit the shape. */
|
||
export function bigMedia(slide) {
|
||
const c = slide?.content || {}
|
||
return [c.media_id, c.background?.kind === 'media' ? c.background.media_id : null].filter(Boolean)
|
||
}
|
||
|
||
/** The slides whose big media doesn't fit the playlist's orientation: { slideId: [mediaId] }. */
|
||
export function misfits(slides, media, orientation) {
|
||
const out = {}
|
||
for (const s of slides || []) {
|
||
const bad = bigMedia(s).filter(id => fitsOrientation(media?.[String(id)], orientation) === false)
|
||
if (bad.length) out[s.id] = bad
|
||
}
|
||
return out
|
||
}
|
||
|
||
/** A short line about when a slide shows: [{ key, params }] for the timeline card ([] = always). */
|
||
export function windowParts(slide) {
|
||
const parts = []
|
||
if (slide.days?.length && slide.days.length < 7) parts.push({ key: 'days', params: { days: slide.days } })
|
||
if (slide.time_from || slide.time_to) parts.push({ key: 'hours', params: { from: slide.time_from || '00:00', to: slide.time_to || '24:00' } })
|
||
if (slide.date_from || slide.date_to) parts.push({ key: 'dates', params: { from: slide.date_from || '…', to: slide.date_to || '…' } })
|
||
return parts
|
||
}
|
||
|
||
/** The playlist's total time for one round (videos to their end count by their length if known). */
|
||
export function totalSeconds(slides, media) {
|
||
let total = 0
|
||
for (const s of slides || []) {
|
||
if (!s.enabled) continue
|
||
if (s.kind === 'video' && s.video_to_end) {
|
||
const ms = media?.[String(s.content?.media_id)]?.duration_ms
|
||
total += ms ? Math.round(ms / 1000) : s.duration_s || 10
|
||
} else total += s.duration_s || 10
|
||
}
|
||
return total
|
||
}
|
||
|
||
/** Move the item at `from` to `to` in a copy of `list`. */
|
||
export function moved(list, from, to) {
|
||
const out = [...list]
|
||
if (from === to || from < 0 || to < 0 || from >= out.length || to >= out.length) return out
|
||
const [x] = out.splice(from, 1)
|
||
out.splice(to, 0, x)
|
||
return out
|
||
}
|
||
|
||
/** The fields of a slide that the editor sends back (PUT /api/signage/slides/{id}). */
|
||
export function slideBody(s) {
|
||
return {
|
||
name: s.name || null, duration_s: s.duration_s, transition: s.transition || null, video_to_end: !!s.video_to_end, enabled: !!s.enabled,
|
||
days: s.days || [], time_from: s.time_from || null, time_to: s.time_to || null, date_from: s.date_from || null, date_to: s.date_to || null,
|
||
content: s.content || {},
|
||
}
|
||
}
|
||
|
||
/** A library item as the slide views need it (the same shape as the playlist's media map). */
|
||
export function mediaEntry(item) {
|
||
const u = item.urls || {}
|
||
return { id: item.id, kind: item.kind, name: item.name, width: item.width, height: item.height, duration_ms: item.duration_ms,
|
||
url: u.full || u.original, thumb: u.thumb, builtin: !!item.builtin }
|
||
}
|
||
|
||
/** media(id) for the slide views: the playlist's media map (+ what the editor just picked). */
|
||
export function mediaLookup(map) {
|
||
return id => {
|
||
const m = map?.[String(id)]
|
||
return m ? { ...m, src: m.url, poster: m.poster || undefined } : null
|
||
}
|
||
}
|
||
|
||
// ── menu boards and widgets (5.6b) ───────────────────────────────────────────
|
||
|
||
export const WIDGET_KINDS = ['logo', 'name', 'date', 'clock', 'message', 'qr', 'weather']
|
||
export const WIDGET_POSITIONS = ['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right', 'top-bar', 'bottom-bar']
|
||
export const BADGES = ['new', 'chef', 'offer', 'popular', 'spicy', 'vegetarian', 'vegan']
|
||
|
||
/**
|
||
* The board's own order from the groups as they are shown: categories in that order, every product
|
||
* in that order (the server puts listed ids first, so this pins the whole board).
|
||
*/
|
||
export function boardOrder(groups) {
|
||
return {
|
||
category_order: groups.filter(g => g.category).map(g => g.category.id),
|
||
product_order: groups.flatMap(g => g.items.map(i => i.id)),
|
||
}
|
||
}
|
||
|
||
/** Groups with group `i` moved by `d` (−1 up, +1 down). */
|
||
export function moveGroup(groups, i, d) {
|
||
return moved(groups, i, i + d)
|
||
}
|
||
|
||
/** Groups with item `ii` of group `gi` moved by `d` inside its group. */
|
||
export function moveItem(groups, gi, ii, d) {
|
||
return groups.map((g, j) => (j === gi ? { ...g, items: moved(g.items, ii, ii + d) } : g))
|
||
}
|
||
|
||
/** Toggle an id in a list (kept sorted for stable saves). */
|
||
export function toggled(list, id) {
|
||
const set = new Set(list || [])
|
||
if (set.has(id)) set.delete(id)
|
||
else set.add(id)
|
||
return [...set].sort((a, b) => a - b)
|
||
}
|