The ecosystem · packages/video
Videos made of your components
A composition is a component that reads the frame it is at and renders that moment. The same composition plays live in a page and renders, a frame at a time, to an MP4 or a WebM. It is made of your real components, so when one of them changes, its videos change with it.
This is Remotion's idea, for Roc Solid. Use it for a landing page's animation that shows the product's real screens, for the same thing as a file to post, and for a short video of a release. A frame is worked out from its number, so it looks the same however it is reached: played, scrubbed to, or rendered.
Installing it#
Name the package in the app's header:
app [program] { pf: platform "…/server/platform/main.roc", video: "…/packages/video/main.roc" }Then import video.Player, video.Sequence and video.Timeline in a .rocx file. rocx generate (which rocx dev and rocx build run first) copies the two components into the app (Generated/video/), as it does for the charts. The package isn't a plugin: it adds nothing to the server. Rendering a file needs Chromium's headless shell and ffmpeg on the machine that renders.
A walk in eight seconds#
The video below is made of this site's own mark and the charts package's <BarChart>, fed the counts from the charts page. The bars glide by the chart's own animation; the video only changes which morning it shows. Play it, pause it, or drag the bar.
Morning 3 of 3
The composition puts two scenes on a timeline, each in a <Sequence>. The title plays for frames 0 to 71, and the chart from frame 66, so the two cross:
import pf.Html
import video.Sequence
import VideoChart
import VideoTitle
## A morning walk, in eight seconds at 30 frames a second: the title (frames 0 to 71), then the chart
## (from 66, the two crossing). A composition for packages/video's `<Player>`, made of the site's own
## components and the charts package's: the docs' video page plays it.
VideoWalk := [].{
view! : {} => Html
view! = |{}|
<div class="walk">
<Sequence frames={72}><VideoTitle /></Sequence>
<Sequence from={66}><VideoChart /></Sequence>
</div>
<style>
.walk {
display: grid;
place-items: center;
min-height: 380px;
padding: var(--space-5);
border-radius: var(--radius);
background: var(--bg-sunken);
}
/* The scenes stack, so one fades out where the next fades in. */
.walk > :global(*) {
grid-area: 1 / 1;
}
</style>
}Writing a scene#
A scene reads its clock with Player.clock!({}) in its body: the frame, as a getter, and the frames a second. It reads the frame where it is tracked, in a hole, a computed attribute or a memo, so only what depends on it changes each frame. Inside a sequence, the frame counts from the sequence's start, so a scene is written as if it starts at 0:
import pf.Html
import pf.Solid exposing [create_memo!]
import video.Player
import video.Timeline
import ../Ui/Logo
## The video's first scene: the site's own mark springs in, and the title and its line are typed.
VideoTitle := [].{
view! : {} => Html
view! = |{}| {
{ frame!, fps, .. } = Player.clock!({})
pop! = create_memo!(|| Timeline.spring(frame!(), fps))
<div class="scene" style:opacity={Timeline.keys(frame!(), [(0, 0.0), (6, 1.0), (62, 1.0), (72, 0.0)]).to_str()}>
<span class="mark" style:transform={"scale(${pop!().to_str()})"}><Logo /></span>
<p class="title">{Timeline.typed("Birds on a morning walk", Timeline.progress(frame!(), 8, 34))}</p>
<p class="line">{Timeline.typed("Three mornings, one chart", Timeline.progress(frame!(), 34, 54))}</p>
</div>
}
<style>
.scene {
display: grid;
justify-items: center;
gap: var(--space-2);
text-align: center;
}
.mark {
display: block;
width: 72px;
height: 72px;
}
.mark :global(.logo) {
width: 100%;
height: 100%;
}
.title {
min-height: 1.2em;
margin: var(--space-2) 0 0;
font: 700 var(--size-2xl) / 1.15 var(--font);
letter-spacing: -0.02em;
}
.line {
min-height: 1.4em;
margin: 0;
color: var(--text-muted);
}
</style>
}Three things are worth knowing:
- Give a pure component a memo, not the frame. A pure component given a value made from the frame is made again when that value changes. Write
ticked! = create_memo!(|| frame!() >= 14)and passticked!(): a memo compares its value with==, so the component is made once per change, and its own CSS transitions play. - A computed style doesn't multiply. A style's value may hold letters, digits, spaces and
.,%#()-+, socalc(… * …)is dropped. Pass the number as a custom property (style:--row={row!().to_str()}) and do the arithmetic in the stylesheet. - Components keep their own motion. CSS transitions,
transition:inandtransition:out, and a component's ownon:frameanimation (the chart's glide) all play: in a page in real time, and in a render on the renderer's clock.
The player#
<Player> holds the frame and plays it. The server renders its poster frame, and so does the browser as it hydrates. On its first animation frame it plays from the start, again and again, unless told otherwise.
| Prop | What it is |
|---|---|
frames | How long it is, in frames. |
fps | Frames a second. 30 unless you set it. |
width, height | The video's size, in CSS pixels: what a render films, and the stage's size when it is scaled. 1280 by 720 unless you set them. |
fit | Flow (the default) lays the composition out as any markup, at the player's width. Scale lays it out at its width and height and scales it to fit, as a video is. |
autoplay, loop | Plays from the start once the page is live, and again when it ends. Both on unless you turn them off; without loop, it stops on its last frame. |
poster | The frame shown before it plays, and where nothing plays it: Start, End (the default) or Frame(n). |
controls | Corner (the default): a play and pause button in its corner. Bar: the button, a scrubber and the time. Hidden: nothing to see. Anything that moves by itself for more than five seconds needs a way to pause it, so keep one on a page. |
label, class | Its aria-label, and classes added to video. |
| its children | The composition. |
Its element says where it is: data-frame, data-playing, and data-live once the page is live. Its scrubber, input.video-seek, is there even when hidden: set it to a frame and dispatch input, and the player goes there and pauses. The stories' catalog, the renderer and tests drive it that way.
Sequences#
<Sequence from={90} frames={60}> shows what is inside from frame 90 for 60 frames, and gives it a clock that counts from 0 there. Without frames, it lasts to the end. What is inside is made as its stretch starts and goes as it ends, so its transitions in and out play there. With hold, it shows all along instead: before its stretch as at its first frame, after it as at its last. Three steps side by side use that to wait their turn and then stay done. Sequences nest.
Timeline#
| Function | What it gives |
|---|---|
progress(frame, from, to) | How far along the span the frame is: 0 at from and before, 1 at to and after. |
eased(frame, from, to, Out) | The same, eased: Linear, In, Out, InOut or Back (past the goal and back). |
tween(frame, from, to, a, b, easing) | A value going from a to b over the span. |
keys(frame, [(0, 0.0), (10, 1.0)]) | A value through keyframes, steadily from each to the next; keys_with eases each move. |
spring(frame, fps) | A spring let go at frame 0: past 1 a little, settled in about a second. spring_with takes its stiffness, damping and mass. |
since(frame, 30) | The frames since frame 30, and 0 before it: to start a spring there. |
typed(text, share) | The first share of a text's characters, typed a character at a time. |
mix, round, frames_of | A share of the way between two values, a count as it climbs, and seconds in frames. |
Rendering a file#
rocx render http://localhost:8091/ -o walk.mp4 -o walk.webm
rocx render http://localhost:8091/__rocx/stories/Demos/VideoDemo/a-bar-to-scrub -o frames/rocx render opens the page in Chromium's headless shell, driven from Roc by packages/headless, and lets it load and hydrate. Then it takes the page's clock over: performance.now(), Date, timers and animation frames move only when it says. For each frame, it moves the clock on a frame, seeks the player there, waits for the page to settle, and puts every running animation where the clock says. That includes the platform's transitions and CSS's. Then it photographs the player's stage, and ffmpeg makes the pictures a video.
So a frame doesn't depend on how long it took to film, and two renders of one composition are the same, frame for frame. ffmpeg writes H.264 into .mp4 and VP9 into .webm. A path ending in / gets one PNG for each frame. --scale 2 (the default) films at twice the CSS size. --player picks a player by index or label, and --from and --to film part of it. Chromium is $CHROME or Playwright's headless shell. ffmpeg is $FFMPEG, the one on PATH, or Nix's.
In the stories#
A story with a player in it is a video; nothing else marks it. In the stories' catalog, a transport appears above it. It has play and pause (Space), a frame back and on (← and →; a second with Shift), a scrubber, the time and the frame, a speed and looping. Every player the catalog shows follows that one clock. A component's stories side by side therefore play in step, and so do stories you compare.
To compare stories, pin any of them in the side (or press P on one), from any component and of either kind. Then open "Compare … side by side". Render copies the command that films each video shown into an MP4 and a WebM.
Not yet#
- A player plays whatever the reader's
prefers-reduced-motionsays, since Roc can't read a media query yet. Its button pauses it. - A player starts when the page goes live, not when it scrolls into view.
- A demo is driven by its props from the frame, not by real clicks sent at given frames.
- Rendering runs on your machine, not on the server with an app's data, and videos are silent.