Shivam Anil Jha 全程以程式碼製作 Orbit OS 兩分鐘宣傳影片
I made my app's 2-minute launch film entirely in code
Shivam Anil Jha 為其應用程式 Orbit OS 全程以程式碼製作兩分鐘宣傳影片,以單一 HTML 頁面按時間計算畫面狀態,再由 Playwright 擷取每格影像。1080p、30 fps 的 4,050 格影像在手提電腦上約需渲染 45 秒;最終版則以 4K、60 fps 渲染 8,100 格。配樂亦由程式以 numpy 生成,並與畫面共用時間提示表。
My app, Orbit OS, needed a film: two minutes that show every feature of a TV home screen and walk through the whole setup. I don't use editing software, so my AI pair programmer and I wrote the film instead.
Here's how it works, and why I'd do it this way again.
Every frame is a function of time
The film is one HTML page. It has the TV, the phone, the captions and the setup guide as plain DOM, and one function:
function seek(t) { apply(compute(t), t); }
compute(t) builds a state object for the time t: where the TV sits, which screen layer is visible, where the home row's focus is, which phone screenshot is showing, how far each caption has risen. apply() writes that state onto the DOM. Nothing animates by itself: there are no CSS transitions and no requestAnimationFrame. The same t always gives the same frame.
That one rule makes everything else easy. A scene is just a time range:
if (t >= 31.2 && t < 40.8) { // Live TV
S.L.live = t >= 31.75 ? 1 : 0;
S.live.open = E.out5(lin(t, 31.75, 32.3)); // the card opens into the picture
S.live.guide = win(t, 33.0, 36.5, 0.35, 0.35); // the guide slides in, then out
if (t >= 35.4) { S.live.ch = [19, 'Aastha']; S.live.zap = t - 35.4; }
}
The "launcher" in the film is the real one's design rebuilt in DOM: a row of cards on the inside of a cylinder (translate3d plus rotateY from the card's distance to the focus), a glow that takes the focused app's colour, and a streak of light across the focused card. The phone screens are real screenshots of the app, captured with Playwright from its own demo mode.
Rendering: Playwright as a frame grabber
for (let f = first; f <= last; f++) {
await page.evaluate((t) => window.seek(t), f / FPS);
await page.screenshot({ path: `frames/f${String(f).padStart(5, '0')}.jpg`, type: 'jpeg', quality: 95 });
}
Six pages render in parallel. At 1080p and 30 fps the 4,050 frames take about 45 seconds on a laptop. With deviceScaleFactor: 2 the same page renders at 4K, and at 60 fps that's 8,100 frames for the final version. ffmpeg joins them with the sound.
One gotcha: anything random has to be random per frame, not per call, or a re-render changes the picture. The TV static uses a hash of the time instead of Math.random().
The music is code too
No stock music and no samples: score.py writes the soundtrack with numpy, on the same cue list as the picture. Plucked notes are Karplus-Strong strings. The bells use FM synthesis. Pads are detuned saws through a spectral low-pass. Every UI click in the film has a tick at the same timestamp. A noise-burst reverb glues it together, a sidechain makes the pad breathe with the kick, and a two-pass loudnorm brings it to -14 LUFS for YouTube and X.
def pluck(f, dur=0.5, level=0.18):
P = int(SR / f) # one period of the string
out = np.zeros(int(dur * SR) + P)
out[:P] = rng.uniform(-1, 1, P) # pluck: a burst of noise
for i in range(P, len(out) - P, P): # each period is the last one, averaged and damped
prev = out[i - P:i]
shifted = np.concatenate(([out[i - P - 1]], prev[:-1]))
out[i:i + P] = 0.997 * 0.5 * (prev + shifted)
...
Why bother
-
Fixes are one line. "The phone covers the toast in step 3" became
left: 510px, then a 45-second re-render. - The film can't drift from the app. The screens are the app's own, and the captions sit in an array next to their times.
- It's reviewable. Every scene was checked by rendering stills at chosen times and looking at contact sheets, the same way you'd review a UI.
The source for Orbit OS is GPL-3.0 on GitHub, and the app is free on Google Play. If you've rendered video from code another way, I'd love to hear how.
Written with my AI assistant, from the film's real code.
來源:DEV Community · dev.to