Generating Videos from Web Code: An Engineering Practice and Validation with Open-Source LLMs

    This article explores a practical approach to video generation: using large language models to write HTML, CSS, and JavaScript, capturing frames with a headless browser, and assembling them into a video with FFmpeg. It covers why video generation is a strong test scenario for open-source LLMs, how the pipeline works, environment setup and risky-operation review, and three test cases with acceptance criteria covering code generation, tool use, and instruction following.

    Introduction

    Short videos are a common requirement in scenarios such as product launches and data presentations. Traditional editing software requires frame-by-frame timeline adjustments, which is time-consuming. This article tests an alternative approach: have a large language model generate HTML, CSS, and JavaScript, use a headless browser to capture the page frame by frame, and finally assemble the frames into a video. This process is also a practical way to evaluate how well open-source LLMs perform at code generation, tool use, and instruction following.

    Why Use Video Generation as a Test Scenario

    Video generation simultaneously involves long-context understanding, style reproduction, animation timing control, and command-line operations. A single simpler task is unlikely to cover all of these. The resulting video is also easy to verify: whether a given frame in the browser matches the same frame in the exported video can be compared directly.

    How the Approach Works

    The approach converts a video problem into a web front-end problem. Animations on the page are defined with CSS or JavaScript, and the renderer retrieves each frame at a specified timestamp. This method does not depend on frameworks like React, so it has few dependencies and is easy to debug. Its prerequisite is that the page must support seeking to an arbitrary time; otherwise, the renderer cannot control playback progress.

    Environment Setup and Risky-Operation Review

    First, install Node.js and npm, and record their versions. During testing, it is recommended to save the following information: the output of node -v, npm -v, and ffmpeg -version, as well as the Chromium version.

    1. mkdir hyperframes-demo
    2. cd hyperframes-demo
    3. npm init -y
    4. npm install puppeteer

    Puppeteer downloads Chromium during installation. The script below reads index.html from the same directory and outputs a 20-second screenshot sequence at 30 fps with a resolution of 1080×1920:

    1. // render.js
    2. const puppeteer = require('puppeteer');
    3. const fs = require('fs');
    4. const path = require('path');
    5. (async () => {
    6. const FPS = 30;
    7. const DURATION = 20;
    8. const WIDTH = 1080;
    9. const HEIGHT = 1920;
    10. const TOTAL = FPS * DURATION;
    11. const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] });
    12. const page = await browser.newPage();
    13. await page.setViewport({ width: WIDTH, height: HEIGHT, deviceScaleFactor: 1 });
    14. await page.goto('file://' + path.resolve('index.html'), { waitUntil: 'networkidle0' });
    15. fs.mkdirSync('frames', { recursive: true });
    16. for (let i = 0; i < TOTAL; i++) {
    17. const t = i / FPS;
    18. await page.evaluate((time) => window.seekTo(time), t);
    19. await page.screenshot({ path: `frames/frame_${String(i).padStart(5, '0')}.png` });
    20. }
    21. await browser.close();
    22. })();

    The page must expose a seekTo function that positions all CSS animations at the specified time:

    1. window.seekTo = (t) => {
    2. document.getAnimations().forEach((a) => {
    3. a.pause();
    4. a.currentTime = t * 1000;
    5. });
    6. };

    Animations written with requestAnimationFrame will not respond to this function. Such animations need to be rewritten so that the visual state is computed directly from a time parameter.

    After the screenshots are captured, use FFmpeg to assemble the video:

    1. ffmpeg -framerate 30 -i frames/frame_%05d.png -c:v libx264 -pix_fmt yuv420p -crf 18 output.mp4

    When cleaning up old dependencies, the model may attempt batch deletion. Tools typically block such a command because the cumulative number of deletions exceeds a threshold. Before approving it, verify each of the following: the target path contains only the node_modules directory inside the project; it does not involve the user’s home directory, system directories, or other projects; and the project is under version control with package-lock.json preserved. Only approve the command when all these conditions are met. After deleting node_modules, dependencies can be restored with npm install. Requests to delete any other path should be rejected.

    Three Use Cases and Acceptance Criteria

    For each use case, record the input, constraints, acceptance criteria, and actual results, so that different models can be compared.

    Use case 1: Vertical product launch video
    Input: the URL of a public official website. The model reads the page HTML and extracts the primary color, fonts, and logo.
    Constraints: output a single index.html; canvas 1080×1920 (9:16); duration 20 seconds; frame rate 30 fps; use only standard DOM and CSS.
    Acceptance: primary color values match the original site, with color deviations recorded item by item; the logo is fully displayed; no text is truncated; no blank frames appear in the video.
    Results to record: color value deviations, the number of truncated text items, and the positions of any blank frames.

    Use case 2: Data growth animation
    Input: a set of 12 months of business data in JSON format.
    Constraints: use only CSS @keyframes, or time functions driven by seekTo; bar heights must be proportional to the values.
    Acceptance: all bar heights are 0 at second 0; the animation completes by the end of second 3; the proportional error between final bar heights and values stays within the tolerance set by the team. A tolerance of 1% is recommended and can be adjusted as needed.
    Results to record: the actual proportional error of each bar.

    Use case 3: Voiceover subtitle layout
    Input: a voiceover script, plus a start-and-end time schedule (in seconds) for each subtitle line.
    Constraints: subtitle appearance times are computed from animation-delay; frame numbers must not be hard-coded manually.
    Acceptance: the appearance time of each subtitle matches the schedule.