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:
@@ -6,8 +6,9 @@
|
||||
GET /api/media/{id} one item, with what uses it
|
||||
PATCH /api/media/{id} rename
|
||||
DELETE /api/media/{id} refused with `media.in_use` while something uses it
|
||||
GET /api/media/file/{sha}/{variant} the bytes: thumb | card | full (images, WebP)
|
||||
| original. Public (LAN, like the customer display's
|
||||
GET /api/media/file/{sha}/{variant} the bytes: thumb | card | full (WebP: a picture,
|
||||
or a video's still frame) | original | play (a video's
|
||||
version every screen plays, step 5.4e). Public (LAN, like the customer display's
|
||||
images), cached for a year: a URL never changes meaning.
|
||||
|
||||
Whoever manages the menu, the customer display, signage or the menu tablets manages the
|
||||
@@ -30,6 +31,7 @@ from database import get_db
|
||||
from datetime import datetime
|
||||
from models.media import Media
|
||||
from services import media as store
|
||||
from services import video
|
||||
|
||||
router = APIRouter()
|
||||
MANAGE = ("catalog.menu.manage", "customer_display.manage", "signage.manage", "menu_tablet.manage")
|
||||
@@ -53,6 +55,8 @@ class MediaOut(BaseModel):
|
||||
uploaded_by_name: Optional[str] = None
|
||||
created_at: datetime
|
||||
builtin: bool = False
|
||||
processing: Optional[str] = None # videos: waiting | working | done | failed (5.4e)
|
||||
progress: Optional[int] = None
|
||||
urls: dict = Field(default_factory=dict)
|
||||
|
||||
|
||||
@@ -131,6 +135,8 @@ def upload_media(response: Response, file: UploadFile = File(...), name: Optiona
|
||||
info = store.ingest(tmp, sha, size)
|
||||
item = Media(name=(name or "").strip()[:120] or _name_from(file.filename),
|
||||
original_name=(file.filename or None), sha256=sha, bytes=size, uploaded_by=user.id, **info)
|
||||
if item.kind == "video":
|
||||
video.queued(item) # its still frame and playable version, in the background
|
||||
try:
|
||||
db.add(item)
|
||||
db.commit()
|
||||
@@ -138,6 +144,8 @@ def upload_media(response: Response, file: UploadFile = File(...), name: Optiona
|
||||
store.delete_files(sha)
|
||||
raise
|
||||
db.refresh(item)
|
||||
if item.processing:
|
||||
video.wake()
|
||||
return _out(item, UploadOut)
|
||||
|
||||
|
||||
@@ -149,10 +157,10 @@ def media_file(sha: str, variant: str, request: Request, db: Session = Depends(g
|
||||
path = store.ensure_variant(item, variant) if item is not None else None
|
||||
if not path or not os.path.exists(path):
|
||||
raise XeniaError("media.not_found", media_id=sha)
|
||||
ctype = item.content_type if variant == "original" else "image/webp"
|
||||
ctype = item.content_type if variant == "original" else "video/mp4" if variant == "play" else "image/webp"
|
||||
headers = {"Cache-Control": CACHE, "Accept-Ranges": "bytes"}
|
||||
rng = request.headers.get("range")
|
||||
if rng and variant == "original":
|
||||
if rng and variant in ("original", "play"):
|
||||
return _ranged(path, ctype, rng, headers)
|
||||
return FileResponse(path, media_type=ctype, headers=headers)
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ from core.errors import XeniaError
|
||||
from database import get_db
|
||||
from models.signage import SignagePlaylist, SignageSchedule, SignageSlide
|
||||
from services import media as media_store
|
||||
from services import video as video_store
|
||||
from services.ws_bus import broadcast_sync
|
||||
|
||||
router = APIRouter()
|
||||
@@ -406,8 +407,12 @@ def _media_map(db, ids) -> dict:
|
||||
out = {}
|
||||
for m in (db.query(Media).filter(Media.id.in_(ids)).all() if ids else []):
|
||||
urls = media_store.urls(m)
|
||||
video = m.kind == "video"
|
||||
out[str(m.id)] = {"id": m.id, "kind": m.kind, "name": m.name, "width": m.width, "height": m.height,
|
||||
"duration_ms": m.duration_ms, "url": urls.get("full") or urls["original"],
|
||||
"duration_ms": m.duration_ms,
|
||||
# a video plays its version made for screens once there is one (5.4e)
|
||||
"url": media_store.video_src(m) if video else urls.get("full") or urls["original"],
|
||||
"poster": urls.get("full") if video else None,
|
||||
"thumb": urls.get("thumb"), "builtin": bool(m.builtin)}
|
||||
return out
|
||||
|
||||
@@ -428,6 +433,17 @@ def _uses(db, media_id: int) -> list:
|
||||
media_store.register_usage("signage_slide", _uses)
|
||||
|
||||
|
||||
def _video_done(db, media_id: int) -> None:
|
||||
"""A video's playable version is ready (services/video.py): the screens playing it switch to it."""
|
||||
playlists = {s.playlist_id for s in db.query(SignageSlide).filter(SignageSlide.kind == "video")
|
||||
if media_id in media_ids(s.content or {})}
|
||||
if playlists:
|
||||
_tell([d for pid in playlists for d in _screens_playing(db, pid)])
|
||||
|
||||
|
||||
video_store.on_done(_video_done)
|
||||
|
||||
|
||||
def tell_all_screens(db) -> None:
|
||||
"""Something every screen may show changed (the message of the day, the weather)."""
|
||||
_tell([d.id for d in _screens(db)])
|
||||
|
||||
Reference in New Issue
Block a user