Pure Code Video Generation: A Technical Practice for Stunning Short Videos

    This article walks through a complete technical approach to generating a 30–60 second, 1920×1080, 30fps short video using code only, with no external image, video, or audio assets and no generative media APIs. It covers visual concept design, signed distance fields with fractal noise, gravitational lensing and Doppler disk approximations, instanced particle rendering with curl noise, anti-flicker bloom and tone mapping, procedural audio with audio-driven visuals, project structure for reproducibility, a two-stage render-then-composite workflow, performance budgets with tiered fallback plans, and hard acceptance criteria for evaluating the result.

    1. Background: Why “Pure Code Video Generation” Is an Engineering Challenge

    As intelligent coding tools and code generation models improve, a demanding task has come back into focus for developers: producing a 30–60 second short video at 1920×1080 and 30fps using code alone, without any external image, video, or audio assets and without calling any generative media API.

    The difficulty is not writing code that runs. It is satisfying three groups of conflicting constraints at the same time:

    • Visuals must be stunning: the piece needs a clear visual concept and narrative pacing, not scattered technical showoffs.
    • Engineering must be reproducible: source code, dependencies, run commands, and the export flow are all required.
    • Runtime must be stable: a performance budget and fallback plan are needed to avoid “it only runs on the author’s machine.”

    The rest of this article unfolds layer by layer around one complete technical solution.

    2. Concept and Visual Language Design (Deliverable A)

    The most common mistake in procedural visuals is doing the technology first and finding a theme later. A safer order is to lock the visual concept in one sentence, then work backward to the technical approach.

    Example concept: an accretion disk under gravitational lensing as the narrative core. The camera slowly pushes in from deep space, light is bent, the disk surface is stretched into light and dark differences by the Doppler effect, and the sequence finally resolves into a silhouette within a low-frequency wall of sound.

    The visual language needs to define three things:

    1. Color strategy: use a high-dynamic-range linear workflow, and converge highlights at the end with an industry-standard filmic tone mapping curve to avoid overexposure and color shifts.
    2. Composition and camera: design 3–4 shot segments (setup, development, turn, resolution), using slow dolly moves and slight roll to create a sense of breathing.
    3. Pacing: restrained for the first 10 seconds, higher information density in the middle, and negative space at the end, avoiding full load throughout that causes visual fatigue.

    3. Technical Approach and Key Difficulties (Deliverable B)

    3.1 Scene Representation: Signed Distance Fields and Mixed Fractals

    Signed distance fields (SDF) describe geometry and naturally support boolean operations and smooth blending, which suits procedural generation of complex forms. Layering fractal noise onto an SDF yields “surfaces with detail” without introducing any model files.

    1. float map(vec3 p){
    2. float sphere = length(p) - 1.0;
    3. float fractal = fbm(p * 3.0) * 0.15;
    4. return mix(sphere, fractal, 0.5);
    5. }

    The difficulty is balancing stepping cost against detail frequency: too high a frequency causes aliasing and flickering, so it must be paired with distance-adaptive step sizes and a reasonable maximum iteration count.

    3.2 Physics Approximation: Gravitational Lensing and the Doppler Disk

    The essence of gravitational lensing is that light paths are bent. In engineering, it is commonly approximated by “screen-space offset sampling of the background”: the closer to the center, the larger the offset, with a capped maximum offset radius to control sampling cost. The accretion disk uses radial temperature falloff in polar coordinates plus angular velocity differences to approximate the brightness asymmetry caused by the Doppler effect.

    The value of these approximations is trading a controllable compute budget for a “physically believable” look, rather than pursuing strict numerical solutions.

    3.3 Particle System: Instanced Rendering and Curl Fields

    Particles in the tens of thousands should use instanced rendering, moving the “update position every frame” work into the shader to avoid the bandwidth bottleneck of transferring data back each frame. Curl noise keeps particle motion both random and locally continuous, avoiding a mechanical feel.

    1. Per frame: sample curl field -> update velocity -> update position -> write instance buffer
    2. Budget: 12000 instances, per-frame update < 4ms (leaving headroom for 60fps)

    3.4 Post-Processing and Stability: Anti-Flicker Bloom and Tone Mapping

    Small, high-contrast light sources flicker during fast motion. A common approach is bloom extraction with a stability constraint (downsample first, then blend threshold and mean), followed by unified tone mapping and a slight vignette. The order of the post-processing chain must be fixed, otherwise reproduced results will drift.

    3.5 Procedural Audio and Audio-Driven Visuals

    Audio must also be “pure code”: synthesize a 48kHz track from layered basic waveforms, envelope modulation, and simple filtering, then map low-frequency energy as a driving signal onto visual parameters (such as bloom intensity and particle speed) to achieve audio-visual sync.

    1. # Pseudocode: audio energy drives visual parameters
    2. energy = low_pass(signal, cutoff=120)
    3. glow_strength = base + gain * normalize(energy)

    4. Engineering Structure and Reproducibility (Deliverables C/D)

    A reproducible project should contain at least an entry file, a shader directory, an audio synthesis script, a dependency list, and run and export instructions. A suggested layout:

    1. project/
    2. index.html # single-page entry, runs offline
    3. shaders/ # vertex/fragment shaders
    4. audio/synth.py # procedural audio generation
    5. tools/render.md # rendering and compositing commands
    6. README.md # dependencies and reproduction steps

    Run and export should follow a two-stage “frames first, then composite” flow: first render offline to a lossless frame sequence (named by the frame_00001.png convention), then use a command-line tool to composite into MP4 or GIF. This way, even if the process fails midway, rendering can resume from a breakpoint.

    5. Performance Budget and Fallback Plan (Deliverables E/F)

    The performance budget must be written into the documentation, not promised verbally. Three tiers are suggested:

    • High: full particles plus high-resolution bloom, targeting a stable 30fps.
    • Medium: half the particles and downsampled bloom, accepting a slight quality loss.
    • Low: disable particle post-processing and lower the render resolution before upscaling, ensuring a video can still be produced.

    The self-assessment dimensions should be fixed at five: visual impact, technical difficulty, code quality, reproducibility, and performance stability, with a reason for each deduction to avoid an invalid “full marks across the board” self-review.

    6. How to Judge Whether the Generated Result Is Actually Acceptable

    For automatically generated solutions, acceptance should follow four hard criteria:

    1. Truly zero external assets: check the dependency list and network requests.
    2. Reproducible: run once in a clean environment per the documentation and compare frame sequence hashes.
    3. Has narrative: do the shots and colors serve the theme rather than piling up parameters?
    4. Has a fallback path: can a low-spec environment still produce a video stably?

    If more compute and storage are needed to handle batch rendering, a general cloud capability combination can be used: a container platform hosts stateless rendering tasks, object storage holds frame sequences and final videos, a message queue distributes tasks, and logging plus monitoring with alerts track failure rates and durations. This general combination is vendor-agnostic and can serve as an infrastructure foundation for long-term engineering.

    7. Conclusion

    “Generating stunning short videos with pure code” is essentially a stress test of engineering completeness: it simultaneously tests mathematical modeling, graphics rendering, audio synthesis, and delivery standards. Writing the concept, budget, fallback, and acceptance criteria up front, then letting the code implement them, is the correct way to make this kind of task truly reproducible and evaluable.