Converting Web Pages to MP4 with Open-Source LLMs: A Practical Guide

    This article explains a reproducible workflow for using open-source large language models to generate web-page animations and render them as MP4 videos. It compares three video-production approaches, covers environment setup with Node.js, Playwright, Chromium, and FFmpeg, and discusses dependency cleanup, baseline rendering checks, vertical video generation, deterministic frame synchronization, and frame-number verification. The goal is to help developers validate model output in their own projects while avoiding common rendering and safety pitfalls.

    1. Introduction

    Large language models have improved noticeably in their ability to generate front-end code. Capabilities such as code generation, long-context understanding, and tool calling can now support many real engineering tasks. However, model performance depends on the specific version, prompt, and engineering validation, all of which need to be verified in your own project. This article uses “letting a large model write a web animation and render it to MP4” as an example and provides reproducible steps plus details that need to be checked one by one.

    2. Solution Selection: Why Use Web Rendering for Video

    There are three common approaches to producing promotional videos or data-visualization videos.

    The first is to use traditional editing software and adjust the timeline frame by frame. When data changes, the work must be redone manually, and binding dynamic data is labor-intensive.

    The second is to use a React-based video rendering library. It offers strong componentization, but requires more dependencies, and slightly complex code can easily cause runtime errors.

    The third is HTML-to-MP4. A large model writes ordinary HTML, CSS, and JavaScript pages; a headless browser takes screenshots frame by frame according to time; and FFmpeg encodes the screenshots into MP4. This approach has fewer dependencies, and during debugging you can open the page directly in a browser for preview.

    Dimension Traditional editing software React-based video rendering library HTML plus headless browser
    Number of dependencies The software itself More, such as React and bundling tools Fewer: Node.js, a browser, and FFmpeg
    Data binding Manual Convenient Convenient, relying on page scripts
    Debugging cost Medium Medium to high Low; can preview directly
    Suitable scenarios Manual fine-tuning Complex videos with componentization Web-style promotional videos and data motion effects

    3. Environment Setup

    1. Node.js and npm

    Use the current LTS version. This article uses Node.js 22 as an example. After installation, run the following commands to confirm the versions:

    1. node -v
    2. npm -v

    2. Headless browser

    This article uses Playwright to drive Chromium. Run the following in the project directory:

    1. npm init -y
    2. npm install playwright
    3. npx playwright install chromium

    3. FFmpeg

    Install according to your operating system:

    • macOS: brew install ffmpeg
    • Ubuntu or Debian: sudo apt install ffmpeg
    • Windows: winget install Gyan.FFmpeg

    After installation, run ffmpeg -version to confirm availability. For specific version numbers and installation methods, refer to the official documentation.

    4. Directory structure

    The recommended initial structure is:

    1. video-project/
    2. ├── index.html
    3. ├── render.mjs
    4. ├── frames/
    5. └── out/

    4. Dependency Cleanup and Safety Interception

    When reinstalling dependencies, a large model may delete node_modules in bulk. Before deletion, take the following precautions:

    • Commit the project to Git first, or make a full backup.
    • Limit the deletion scope to node_modules under the project root. Do not perform recursive deletion on the user directory or system directories.
    • Keep package-lock.json so dependency versions can be traced.

    When a tool shows an interception prompt, check the path first. If the target is node_modules inside the project, you can allow this deletion. If the target points elsewhere, reject it and require the model to explain why. The “intercept when more than 50 items” mentioned in this article is only an example; the actual threshold is determined by the configuration of the tool you use.

    5. Baseline Rendering Validation

    First ask the model to generate a 10-second test page, complete the rendering, and record the elapsed time. During the test, also record the following information: CPU model, memory size, video resolution, frame rate, encoder, screenshot format, and concurrency. Rendering time is heavily affected by hardware and parameters, so do not directly use someone else’s numbers as your own baseline.

    6. Generating a 20-Second Vertical Video

    The specifications in this example are 1080×1920 resolution, 25 fps, and 500 frames in total. The page needs to include scene transitions, text animations, and an ending logo. Below is an example of a scene entrance:

    1. .scene-1 {
    2. animation: slideIn 1.2s cubic-bezier(.2, .7, .2, 1) 0s both;
    3. }
    4. @keyframes slideIn {
    5. from { transform: translateY(60px); opacity: 0; }
    6. to { transform: none; opacity: 1; }
    7. }

    The source of design assets needs to be confirmed first. This article recommends using only your own pages, pages you are authorized to use, or locally provided design files. Directly scraping another company’s website copy, logo, and color scheme for public release may involve copyright and trademark issues.

    7. Frame Synchronization Validation

    When converting a web page to video, the most common problems are dropped frames and animation desynchronization. Recording the screen directly with real time will cause screenshot time consumption to disrupt the timeline. A more reliable approach is deterministic rendering: manually set the animation time for each frame, then take a screenshot.

    1. import { chromium } from 'playwright';
    2. const fps = 25;
    3. const total = 500;
    4. const browser = await chromium.launch();
    5. const page = await browser.newPage({ viewport: { width: 1080, height: 1920 } });
    6. await page.goto('file:///path/to/video-project/index.html');
    7. await page.evaluate(() => document.fonts.ready);
    8. for (let i = 0; i < total; i++) {
    9. const t = (i / fps) * 1000; // milliseconds
    10. await page.evaluate((t) => {
    11. document.getAnimations().forEach((a) => {
    12. a.pause();
    13. a.currentTime = t;
    14. });
    15. }, t);
    16. const name = `frame_${String(i).padStart(4, '0')}.png`;
    17. await page.screenshot({ path: `frames/${name}` });
    18. }
    19. await browser.close();

    After the screenshots are complete, use FFmpeg to encode them as MP4. Odd dimensions will cause yuv420p encoding to fail, so make sure the width and height are even:

    1. ffmpeg -framerate 25 -i frames/frame_%04d.png -c:v libx264 -pix_fmt yuv420p -crf 18 out/video.mp4

    Frame-number convention needs to be clarified first. In this article, frame numbers start at 0, and the timestamp of frame n is n÷25 seconds. Therefore, “frame 150” means index 150, corresponding to 6.00 seconds. If counting starts from 1, frame 150 has index 149 and a timestamp of 5.96 seconds. Mixing the two conventions will cause comparison misalignment.

    During validation, extract the same frame from the MP4:

    1. ffmpeg -i out/video.mp4 -vf "select=eq(n\,150)" -vframes 1 out/check_150.png

    In the Windows command line, the escaping of commas may be different and needs to be adjusted according to the terminal type. Then use ImageMagick to compare the two images.