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,9 +6,13 @@ whatever copies the database must copy this folder too):
|
||||
<sha[:2]>/<sha>/{thumb,card,full}.webp images only, made on upload (and again on
|
||||
request if missing, e.g. after a partial restore)
|
||||
|
||||
<sha[:2]>/<sha>/play.mp4 videos: the version every screen plays, when one is needed
|
||||
(step 5.4e; videos get thumb/card/full from a still frame)
|
||||
|
||||
Images: JPEG, PNG, WebP, AVIF or GIF (first frame), turned upright from their EXIF, never made
|
||||
bigger than they are. Videos: MP4 or WebM, kept as uploaded (no transcoding on the venue's
|
||||
box); an MP4's length and size are read from its header when it has them.
|
||||
bigger than they are. Videos: MP4, MOV or WebM, kept as uploaded; an MP4's length and size are
|
||||
read from its header when it has them. Their still frame and playable version are made in the
|
||||
background afterwards (services/video.py).
|
||||
|
||||
Usage: what points at a media item registers a check with `register_usage(kind, fn)`,
|
||||
fn(db, media_id) -> [{"kind", "id", "name"}]. Delete is refused while any check finds a use.
|
||||
@@ -36,8 +40,8 @@ SIZES = {"thumb": 200, "card": 600, "full": 1920}
|
||||
WEBP_QUALITY = 80
|
||||
IMAGE_TYPES = {"JPEG": ("image/jpeg", "jpg"), "PNG": ("image/png", "png"),
|
||||
"WEBP": ("image/webp", "webp"), "GIF": ("image/gif", "gif"), "AVIF": ("image/avif", "avif")}
|
||||
VIDEO_TYPES = {"mp4": "video/mp4", "webm": "video/webm"}
|
||||
VARIANTS = (*SIZES, "original")
|
||||
VIDEO_TYPES = {"mp4": "video/mp4", "mov": "video/quicktime", "webm": "video/webm"}
|
||||
VARIANTS = (*SIZES, "original", "play")
|
||||
|
||||
_lock = threading.Lock() # one writer per file set at a time
|
||||
|
||||
@@ -63,6 +67,10 @@ def variant_path(sha: str, variant: str) -> str:
|
||||
return os.path.join(folder(sha), f"{variant}.webp")
|
||||
|
||||
|
||||
def play_path(sha: str) -> str:
|
||||
return os.path.join(folder(sha), "play.mp4")
|
||||
|
||||
|
||||
def image_of(item) -> dict | None:
|
||||
"""What an app needs to show a picture from the library: {"id", "name", "thumb", "card", "full"}."""
|
||||
if item is None or item.kind != "image":
|
||||
@@ -75,9 +83,20 @@ def urls(item) -> dict:
|
||||
out = {"original": f"{base}/original"}
|
||||
if item.kind == "image":
|
||||
out.update({v: f"{base}/{v}" for v in SIZES})
|
||||
else: # a video's still frame and version, once made (5.4e)
|
||||
if os.path.exists(variant_path(item.sha256, "thumb")):
|
||||
out.update({v: f"{base}/{v}" for v in SIZES})
|
||||
if os.path.exists(play_path(item.sha256)):
|
||||
out["play"] = f"{base}/play"
|
||||
return out
|
||||
|
||||
|
||||
def video_src(item) -> str:
|
||||
"""The file a screen plays: the version made for screens when there is one, else the original."""
|
||||
u = urls(item)
|
||||
return u.get("play") or u["original"]
|
||||
|
||||
|
||||
# ── receiving an upload ─────────────────────────────────────────────────────
|
||||
|
||||
def receive(src: IO[bytes], max_bytes: int) -> tuple[str, str, int]:
|
||||
@@ -109,11 +128,13 @@ def receive(src: IO[bytes], max_bytes: int) -> tuple[str, str, int]:
|
||||
|
||||
|
||||
def sniff(path: str) -> str | None:
|
||||
"""'image' | 'mp4' | 'webm' | None, from the first bytes (never from the name)."""
|
||||
"""'image' | 'mp4' | 'mov' | 'webm' | None, from the first bytes (never from the name)."""
|
||||
with open(path, "rb") as f:
|
||||
head = f.read(16)
|
||||
if head[4:8] == b"ftyp":
|
||||
return "image" if head[8:12] in (b"avif", b"avis") else "mp4"
|
||||
if head[8:12] in (b"avif", b"avis"):
|
||||
return "image"
|
||||
return "mov" if head[8:12] == b"qt " else "mp4"
|
||||
if head[:4] == b"\x1a\x45\xdf\xa3":
|
||||
return "webm"
|
||||
if head[:3] == b"\xff\xd8\xff" or head[:8] == b"\x89PNG\r\n\x1a\n" or head[:6] in (b"GIF87a", b"GIF89a") \
|
||||
@@ -161,8 +182,14 @@ def ensure_variant(item, variant: str) -> str | None:
|
||||
"""The file to serve for a variant, re-making image sizes that are missing."""
|
||||
if variant == "original":
|
||||
return original_path(item.sha256)
|
||||
if item.kind != "image" or variant not in SIZES:
|
||||
if variant == "play":
|
||||
p = play_path(item.sha256)
|
||||
return p if item.kind == "video" and os.path.exists(p) else None
|
||||
if variant not in SIZES:
|
||||
return None
|
||||
if item.kind != "image": # a video's still frame: made by services/video.py
|
||||
p = variant_path(item.sha256, variant)
|
||||
return p if os.path.exists(p) else None
|
||||
p = variant_path(item.sha256, variant)
|
||||
if os.path.exists(p):
|
||||
return p
|
||||
@@ -192,7 +219,7 @@ def ingest(tmp: str, sha: str, size: int) -> dict:
|
||||
make_variants(img, sha)
|
||||
elif found in VIDEO_TYPES:
|
||||
ext = found
|
||||
info = {"kind": "video", "content_type": VIDEO_TYPES[found], **(mp4_info(tmp) if found == "mp4" else {})}
|
||||
info = {"kind": "video", "content_type": VIDEO_TYPES[found], **(mp4_info(tmp) if found in ("mp4", "mov") else {})}
|
||||
else:
|
||||
raise XeniaError("media.type")
|
||||
store(tmp, sha, ext)
|
||||
@@ -220,12 +247,14 @@ def files_info(item) -> dict:
|
||||
from PIL import Image
|
||||
out = {}
|
||||
for v in VARIANTS:
|
||||
p = original_path(item.sha256) if v == "original" else variant_path(item.sha256, v)
|
||||
p = original_path(item.sha256) if v == "original" else play_path(item.sha256) if v == "play" else variant_path(item.sha256, v)
|
||||
if not p or not os.path.exists(p):
|
||||
continue
|
||||
info = {"bytes": os.path.getsize(p), "width": None, "height": None}
|
||||
if v == "original":
|
||||
info.update(width=item.width, height=item.height)
|
||||
elif v == "play":
|
||||
info.update(zip(("width", "height"), fitted(item.width, item.height)))
|
||||
else:
|
||||
try:
|
||||
with Image.open(p) as im:
|
||||
@@ -236,6 +265,14 @@ def files_info(item) -> dict:
|
||||
return out
|
||||
|
||||
|
||||
def fitted(w, h, side: int = 1920) -> tuple:
|
||||
"""A video's size in its playable version (at most `side` on the long side, even numbers)."""
|
||||
if not w or not h:
|
||||
return None, None
|
||||
k = min(1.0, side / max(w, h))
|
||||
return int(w * k) // 2 * 2, int(h * k) // 2 * 2
|
||||
|
||||
|
||||
def store(tmp: str, sha: str, ext: str) -> None:
|
||||
"""Move the received temp file into place as the original."""
|
||||
d = folder(sha)
|
||||
|
||||
Reference in New Issue
Block a user