Building Deterministic AI-Generated Videos with Code: A Low-Cost Approach
Generative video models excel at photorealistic output but struggle with deterministic content like precise geometry, counts, and seamless loops. This article introduces a programmatic video approach where an LLM generates self-contained HTML animations, which are then captured frame by frame and assembled with ffmpeg. It covers a four-step workflow, prompt-writing techniques, a runnability checklist, cost considerations, and how programmatic video compares with generative video models.
1. What Is Programmatic Video?
Generative video models output pixel frames directly. They are good at producing images that “look plausible,” but they often deviate on deterministic content: geometric relationships, exact quantities, and loop continuity. Common failures include line segments pointing in the wrong direction, unstable counts, and first and last frames that don’t match.
Programmatic video takes a different approach. Instead of pixels, the model outputs a runnable program—typically a self-contained HTML page. When the page runs, every frame is computed by code, so results are reproducible and parameters can be adjusted one by one. A local tool then records the animation or takes screenshots and assembles them into a video file.
This route suits content that can be expressed as graphics programs: mathematical geometry, data visualization, physics demonstrations, and similar material.
2. The Four-Step Workflow
Step 1: Generate the animation program. Write the animation requirements as a prompt and give it to a general-purpose LLM with code capabilities, asking it to output an HTML file. For 3D effects, use a frontend graphics library such as Three.js; for 2D effects, plain Canvas works fine.
Step 2: Run it deterministically. Open the page in a browser and check that the animation behaves as expected. Any problems found here can be fixed by revising the prompt.
Step 3: Capture frames and assemble the video. There are two ways to do this.
The first is frame-by-frame export within the page. When the animation is based on Canvas, you can drive rendering with requestAnimationFrame and advance the animation time in fixed time steps rather than reading real elapsed time. Each time you advance one frame, export the canvas as a PNG. This keeps the frame rate stable and independent of machine performance—if export is slow, the animation won’t skip frames.
The second is screen recording or timed screenshots. If frame-by-frame export is impractical, use screen recording software, or run a script that takes a screenshot roughly every 33 milliseconds, which corresponds to 30 frames per second. The screenshot interval must match the target frame rate, or the video speed will be wrong. When screenshots take too long, frames get dropped and the video appears to stutter.
Once you have the frame sequence, assemble it with ffmpeg:
ffmpeg -framerate 30 -i frame_%04d.png -c:v libx264 -pix_fmt yuv420p output.mp4
What each parameter does: -framerate 30 sets the input frame rate. frame_%04d.png means filenames of “frame_” followed by four digits; ffmpeg reads starting from 0001 by default, so if your frames start at 0000, add -start_number 0. -c:v libx264 specifies H.264 encoding, which requires an ffmpeg build that includes libx264. -pix_fmt yuv420p ensures most players and platforms can decode the video.
A common error is “height not divisible by 2.” yuv420p requires both width and height to be even. For odd dimensions, add -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" to fix it.
Step 4: Design the loop. Short-video platforms favor content where the end connects smoothly to the beginning, and loops must be guaranteed in code. For example, make rotation angles multiples of 2π, return the camera to its starting position, and express animation parameters as periodic functions. The frame at the loop point should match the first frame—compare the last frame and the first frame side by side to verify.
3. How to Write the Prompt
Shift your prompt from describing a picture to describing program behavior. A five-part structure works well:
- Canvas and container: e.g., “3:4 portrait aspect ratio, the page fills the viewport responsively with no scrollbars.”
- Core elements: described in geometric language, e.g., “Countless thin glowing line segments rotating at constant speed around a center point, covering all directions in 3D space.”
- Motion mechanism: how things change over time, e.g., “The camera slowly zooms in for the first ten seconds, then smoothly pulls back to the full view.”
- Loop requirements: e.g., “Rotation angles have a period of 2π; the first and last frames match.”
- Dependency declaration: e.g., “Use only native Canvas APIs, no external libraries, no external network resources.”
A complete template might look like this:
Please generate a self-contained HTML file. The aspect ratio is 3:4 portrait, and the page fills the viewport responsively. The scene contains glowing line segments rotating at constant speed around a center point, covering all directions in 3D space. The camera slowly zooms in during the first half of the cycle and smoothly pulls back during the second half. All angles have a period of 2π, and the first and last frames match. Use only native Canvas APIs with no external libraries.
Runnability Checklist
After the model outputs the code, check the following four points:
- Dependencies are complete. Confirm that referenced library versions exist and that nothing relies on unreachable network addresses.
- The canvas is responsive. Resize the browser window and check for distortion or clipping.
- The loop closes. Compare the first and last frames on the timeline.
- Frame export is stable. Export several hundred frames in a row and verify the frame count and file naming.
If a check fails, don’t just say “there’s a problem, please fix it.” State the specific symptom and the expected result, for example: “The last frame is missing two line segments—ensure every segment’s phase returns to its initial value within one cycle.” The more specific the error description, the higher the model’s success rate at fixing it.
4. Cost and Limits of Applicability
The model invocation cost of this route is low—usually just a few conversations. But the full cost also includes your computer, hands-on time, and time spent on trial and error. Factor all of these into your calculation.
There have been cases of videos made this way reaching millions of views on content platforms. That number comes from individual cases, and the measurement basis and time window need to be verified. Individual successes can’t be replicated on demand and shouldn’t be treated as a promise of view counts.
The limits of applicability are equally clear. Photorealistic people, complex lighting, and natural landscapes are hard to express with simple code and are better suited to video models or live footage. For abstract ideas, visualization can’t fully convey the underlying thought, so not every step needs an explanatory animation. Leaving space for the audience to think and discuss can sometimes drive more engagement in the comments.
5. Side-by-Side Comparison
| Dimension | Generative Video Models | Programmatic Video |
|---|---|---|
| Precision | Prone to geometric and count errors | Determined by code, results are exact |
| Cost | Usually billed per generation | Low model invocation cost, plus equipment and time |
| Reproducibility | Results may differ each run | Same code always produces the same output |
| Best suited for | Realistic people, complex lighting, natural scenes | Math, geometry, data visualization, and similar |
6. Closing Thoughts
Programmatic video turns video generation into a programming problem. It has clear advantages for deterministic content and is much easier to revise. Before choosing this route, confirm your content can be expressed as a graphics program, then follow the workflow and checklist above—it will save you a lot of time.
