Pure Code Video Generation: A Technical Practice for Stunning Short Videos
This article explains how to generate a visually striking short video using code alone, without any external images, video, audio, or generative media APIs. It covers turning requirements into engineering constraints, planning storyboards before implementation, and key techniques such as procedural modeling with signed distance functions, GPU particle systems driven by curl noise, gravitational lensing post-processing, anti-flicker bloom and tone mapping, and procedural audio with audio-driven visuals. It also provides a reproducible project structure, run commands, a performance budget with tiered fallback strategies, and a five-dimension acceptance checklist.
1. Why Pure Code Video Generation Is Worth Taking Seriously
In creative coding, a common challenge is producing a short video with narrative and visual impact using code alone, without relying on any external images, video, or audio, and without calling any generative media API. These constraints may seem harsh, but they bring three benefits.
First, reproducibility. Every frame and sound comes from deterministic mathematical processes. With a fixed random seed and runtime environment, outputs can be aligned at the bit level. Second, maintainability. The visual style lives in parameters and shader code, so adjusting pacing, color, and camera movement does not require reshooting or redrawing assets. Third, scalability. The same pipeline can move from 1080p to higher resolutions, and rendering tasks can be split across container platforms and object storage for batch execution.
The point is not to build a show-off demo. It is to put the visual concept, narrative pacing, and engineering constraints on the same design map, and use code to complete the entire workflow from storyboard to final cut.
2. Translating Requirements into Executable Engineering Constraints
A rigorous task description usually includes the following hard conditions, which directly determine technology choices:
- Asset constraints: no external images, video, or audio, and no text-to-image, text-to-video, or generative audio APIs.
- Generation method: visuals must be generated by mathematical functions, fractals, particle systems, shaders, and procedural textures; sound must be produced by procedural synthesis.
- Delivery completeness: provide source code, dependency notes, run commands, and rendering and export commands, and actually produce a video file or a complete frame sequence.
- Runtime form: a browser-based approach needs a single-page entry and an offline run mode; a scripted rendering approach needs a clear environment and commands.
- Engineering quality: provide a performance budget and fallback plan, avoiding “runs only on high-end machines.”
Formalizing these conditions gives a simple acceptance checklist:
Input: fixed random seed, duration T, resolution W×H, frame rate fpsOutput: frame sequence frame_%05d.png or final video.mp4Constraints: no external assets, no generative media APIs, fully reproducible workflow
3. Visual Concept and Narrative Structure: Storyboard First, Code Second
“Stunning” does not mean piling up effects. A more reliable approach is to settle on a clear visual concept first, then break it into four beats: setup, development, turn, and resolution. Take an abstract cosmic theme as an example:
- Setup: a point of light appears in darkness, and the camera slowly pushes in to establish spatial scale.
- Development: an accretion-disk-like structure gradually forms, particles move along a curl field, and colors shift from cool to warm.
- Turn: the gravitational lensing effect intensifies, background stars are stretched into arcs, and the pacing speeds up.
- Resolution: after a brightness pulse, the frame closes in, the soundtrack ends in sync, and the loop is complete.
Once the storyboard is set, map each beat to a concrete technical module: the background starfield is generated with hash noise, the main structure is modeled with signed distance functions (SDF), particle motion is driven by a curl noise field, camera movement is controlled by keyframe interpolation, and color stays in linear space with tone mapping at the end.
4. Technical Approach and Key Challenges
1. Procedural Modeling and Materials
Signed distance functions are good at describing composable geometry. Union, difference, and smooth union can assemble complex outlines at low cost. Combined with normal estimation and a physically based lighting model, they produce surfaces with a sense of volume. For materials, stacking multiple layers of noise can simulate roughness and emissive regions, without any texture files.
2. Particle Systems and Field-Driven Motion
Tens of thousands of instanced particles are suitable for GPU instanced drawing. Motion direction can come from a curl noise field, giving particles a turbulent, layered look; lifetimes are offset in time to avoid synchronized flickering. If a fluid approximation is needed, a simplified incompressible step can run on a 2D grid, and the result can be sampled into particle velocity.
3. Gravitational Lensing and Relativistic Visuals
Gravitational lensing is essentially an approximation of bending light paths. In engineering, it is commonly implemented as a screen-space post-process: for each pixel, compute an offset based on the mass parameter of the central body, resample the background texture, and form a ring-shaped distortion. The Doppler effect can be approximated by applying frequency shifts and brightness gains at different angles of the accretion disk, which visually appears as one side being brighter and bluer.
4. Anti-Flicker Bloom and Tone Mapping
High-frequency particles and thin lines tend to flicker in high-contrast scenes. A common approach is highlight extraction with a soft-knee threshold, multi-scale blur accumulation, and slight accumulation over time to suppress frame-to-frame jitter. At the end, everything enters linear space for exposure and tone mapping, using a common cinematic mapping curve, and is then output to a standard display space.
5. Procedural Audio and Audio-Visual Sync
Sound is also synthesized in code: sine and sawtooth waves are layered to form a sustained bass, filtered noise simulates wind, and envelopes control pulses. Feeding the audio energy envelope back as a parameter to the visual layer drives brightness and slight camera zoom, achieving “audio-driven visuals.” The sample rate is 48kHz, exported to a lossless intermediate format before packaging.
5. Complete Project Structure and How to Run
A reproducible project usually has the following structure:
project/├── index.html # single-page entry (browser approach)├── src/│ ├── main.js # timeline and render loop│ ├── shaders/ # vertex/fragment shaders│ ├── particles.js # instanced particles and fields│ └── audio.js # procedural audio synthesis├── package.json└── README.md
The browser approach emphasizes offline runnability: all dependencies are localized, and the entry page can be opened directly or served through a local static server. The scripted approach specifies the environment and commands, for example pinning the runtime version, installing dependencies, and rendering frame by frame:
# Install dependenciesinstall_dependencies# Render frames offline into frames/render_frames --seed 42 --duration 45 --fps 30 --width 1920 --height 1080# Encode videoencode_video --input frames/frame_%05d.png --output out/video.mp4 --fps 30
Frame sequences should use zero-padded incrementing numbers, which makes it easy to resume rendering and detect missing frames. The final video is packaged in a common MP4 container, and a GIF can also be exported as a quick preview.
6. Performance Budget and Fallback Strategy
The performance budget should be written down before starting, for example: target 30fps at 1080p, with a per-frame budget of 33ms; geometry and particles take 12ms, post-processing takes 10ms, and audio and encoding run asynchronously without blocking the main loop. The fallback strategy has three tiers:
- High-end: full particles, the complete post-processing chain, and more noise layers.
- Mid-range: half the particles, bloom reduced to two scales, and fewer noise layers.
- Low-end: disable screen-space lensing and use simplified background displacement; frame rate can drop to 24fps with frame interpolation.
Making the fallback switches parameters rather than scattered conditional branches is key to keeping the code readable.
7. Self-Assessment Dimensions and Acceptance Suggestions
At delivery, self-assess across five dimensions and provide evidence: visual impact, judged by whether the storyboard and color are unified; technical difficulty, judged by whether modeling, field-driven motion, and post-processing are truly implemented; code quality, judged by module separation and parameter centralization; reproducibility, judged by whether two runs with a fixed seed produce identical output; and performance, judged by whether it stably meets the target on the target machine.
If running in the cloud, rendering tasks can be containerized, frame sequences written to object storage, and log services with monitoring alerts used to track failure rates and durations, turning a one-off creative experiment into a repeatable pipeline.
