Embedding video¶
This page covers video files that ship inside your template ZIP: a looping background clip, an intro animation, a sponsor reel. For a live HLS stream, see Embedding an HLS livestream.
Everything here applies to both Physical Room and Event Scoped templates. Where the two differ, the difference is called out.
The minimum that plays¶
<div id="your-content" class="your-content">
<video class="background-video" src="assets/video/background-loop.mp4"
autoplay muted loop playsinline preload="auto" aria-hidden="true"></video>
<!-- Everything after the video paints on top of it. -->
</div>
Attribute by attribute:
| Attribute | Why it is there |
|---|---|
autoplay |
Starts playback without a script. |
muted |
Required for autoplay to work. See the next section. |
loop |
Restarts the clip at the end, but not reliably forever; see the watchdog below. |
playsinline |
Keeps the video in the page instead of handing it to a full-screen player on devices that would otherwise take over. |
preload="auto" |
Lets the browser fetch the whole clip up front. On a sign the file is local and precached anyway. |
object-fit: cover |
Fills the area and crops. Use contain when the whole frame must stay visible. |
Put the video first inside your content wrapper: later siblings paint on top of it, so no
z-index is needed.
autoplay requires muted¶
Chrome refuses to start an unmuted video without a user gesture. When it refuses, play() rejects
with NotAllowedError, and if nothing handles that rejection the failure is completely silent: a
black rectangle and an empty console.
A background clip has no reason to carry sound, so muted is the answer. If a video genuinely must be
audible, the sign's Chrome has to be launched with --autoplay-policy=no-user-gesture-required (see
Chrome kiosk setup), and the template should still attempt
sound first and fall back to muted if refused. That pattern is written up in
Embedding an HLS livestream.
Where the file lives decides whether it works offline¶
Files inside the template ZIP are listed as template assets and precached by the sign's service
worker as soon as the template is assigned, so they keep playing when the venue network drops. A
video fetched from anywhere else, another server, a CDN, a share link, is not precached: the
worker fetches it the first time the sign asks for it and, if that succeeds, keeps a copy that it
serves from then on, refreshing it in the background when the sign next asks for it after six hours.
That copy exists only if the first load happened while the network was up, and the browser may evict
it under storage pressure, so only a bundled file is guaranteed to play offline. Stream playlists and
segments from other hosts (.m3u8, .ts, .m4s and the like) are never cached.
Keep the video under assets/ (for example assets/video/). The ZIP root may only hold index.html,
styles.css and script.js; see Getting started with templates.
Encode for the sign, not for the web: H.264 in an MP4 container plays everywhere the sign player runs, and a clip rendered at the sign's own resolution avoids scaling work on every frame.
Clean up in ctx.onDispose¶
Physical Room templates are torn down and rebuilt on every gap-to-content switch, many times a day on a busy room. A video element that is merely removed from the page keeps its decoder and its buffered data until the browser gets around to releasing them. Release them deliberately:
window.preseriaTemplateInit = (ctx) => {
const video = document.querySelector(".background-video");
if (!video) return;
ctx.onDispose(() => {
video.pause();
video.removeAttribute("src");
video.load(); // releases the decoder and the buffered data
});
};
If you start playback from a script rather than with autoplay, do it inside preseriaTemplateInit
as well, not at the top level of the script: in the template preview the inline script runs twice, and
a player started at top level is started twice.
Browsers do stop looping¶
A customer reported it and it reproduces: after hours of playback the browser stops a looping video at the end of a cycle and it never starts again. The fix is a small watchdog that samples progress and restarts the clip when it stalls:
window.preseriaTemplateInit = (ctx) => {
const video = document.querySelector(".background-video");
if (!video) return;
// Deliberately NOT a multiple of the clip length. See the trap below.
const SAMPLE_MS = 7000;
ctx.state.lastTime = -1;
ctx.setInterval(() => {
if (video.paused || video.ended) {
video.play().catch(() => {});
} else if (video.currentTime === ctx.state.lastTime) {
// Playing according to the element, but the picture has not moved: wedged.
video.currentTime = 0;
video.play().catch(() => {});
}
ctx.state.lastTime = video.currentTime;
}, SAMPLE_MS);
ctx.onDispose(() => {
video.pause();
video.removeAttribute("src");
video.load();
});
};
The sampling-interval trap¶
The watchdog decides "stalled" by asking whether currentTime moved since the last sample. That signal
collapses when the interval sits at or near a multiple of the clip length: a 10.03 s clip sampled every
10 s wraps past the loop point between samples, and the position read at each sample differs from the
last by only about 30 milliseconds, nearly indistinguishable from not moving at all and easily lost
to timer jitter. Pick an
interval unrelated to the clip length (7 s against a 10 s clip, for instance) and the difference
between playing and stalled is seconds, not milliseconds.
Length versus bitrate¶
Bitrate drives decode work per second. Clip length drives the number of loop transitions per day, and a loop transition is exactly where the "stopped looping" fault occurs. A shorter, lighter clip therefore loops more often and is not automatically the safer choice; a longer clip at a moderate bitrate can be the more robust one. Test the real clip for a full day on the real device.
Physical Room versus Event Scoped¶
- Physical Room templates are rebuilt on every gap-to-content switch, so cleanup is what
matters most: without the
ctx.onDisposerelease above, every switch leaks a decoder. - Event Scoped templates are not switched by any schedule; the one on a sign is replaced only when an admin assigns a different template, so the video normally lives for days and the watchdog is what matters most.
Both patterns are cheap. Use both everywhere.
Checklist¶
Before using the template on a sign, verify each item:
autoplay muted loop playsinlineon a background video.- The file lives under
assets/inside the ZIP, not on another host. ctx.onDisposepauses, clearssrcand callsload().- A progress watchdog restarts a paused or wedged loop.
- Its sampling interval is not a multiple of the clip length.
- Tested for a full day on the sign hardware.
Related¶
- Embedding an HLS livestream
- Template JavaScript best practices —
ctx.onDispose,ctx.setInterval - Livestream and web page templates — a full-screen video or stream with no code
- How digital signs work — what the sign caches