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

    This article presents a general technical workflow for generating a reproducible 30–60 second, 1920×1080, 30fps short video using code only, with no external images, video, audio assets, or generative media APIs. It covers measurable acceptance criteria, a four-part narrative structure, and key techniques including signed distance fields with ray marching, gravitational lensing and Doppler accretion disk approximations, curl-noise particle systems, anti-flicker bloom and tone mapping, and procedural audio with audio-driven visuals. It also provides a reproducible project layout, run and render commands, performance budgets with quality degradation tiers, and common pitfalls to avoid.

    1. Problem Definition: Why “Cinematic Visuals from Pure Code” Is Worth Doing

    As intelligent coding tools and code generation models evolve quickly, a common challenge prompt is to ask a model to produce a 30–60 second, 1920×1080, 30fps short video using code only, without any external image, video, or audio assets and without calling generative media APIs. The value of this task is not about showing off. It tests three things at once: visual storytelling, mathematics and graphics fundamentals, and engineering reproducibility. For developers, it is a hands-on exercise in wiring shaders, particles, physics approximations, a post-processing pipeline, and audio synthesis into one complete pipeline.

    This article does not focus on any specific product or platform. Instead, it abstracts the original experience into a general technical problem: how to design and implement an automated rendering pipeline that takes a prompt as input and outputs a reproducible video.

    2. Define Acceptance Criteria First: Turning “Stunning” into Measurable Metrics

    “Stunning enough” is subjective, but engineering delivery must be objective. Before starting, fix the scoring dimensions into six items, each with a verifiable definition:

    1. Pure code: the repository contains no binary media assets, and the asset list is empty.
    2. Reproducibility: fixed random seed, locked dependency versions, and a one-command run.
    3. Visual impact: a clear focal point in the composition, contrast and unity in color, and rhythm in motion.
    4. Technical complexity: at least one advanced technique (procedural materials, lighting and shadows, fluid approximation, fractals, post-processing, or audio-driven effects).
    5. Code engineering quality: modular design, externalized parameters, and solid logging and error handling.
    6. Performance and stability: a frame time budget, a VRAM limit, and a degradation switch.

    3. Visual Concept and Narrative Pacing

    No matter how strong the technology is, without narrative it is just a demo. A four-part structure of setup, development, turn, and resolution is recommended. Using 45 seconds as an example:

    • Setup (0–8s): a single light source in darkness, with the camera slowly pushing in to establish spatial scale.
    • Development (8–22s): an accretion-disk-like structure emerges around the light source, particles begin to rotate in an orderly way, and colors shift from cool to warm.
    • Turn (22–36s): the gravitational lensing effect strengthens, the background starfield is distorted, the pace speeds up, and Doppler brightening appears.
    • Resolution (36–45s): the camera pulls back, the structure converges into a ring of light, colors return to cool tones, and the scene fades out.

    For the visual language, limit the palette to a single primary color plus one complementary color to avoid color noise. Use easing curves (such as cubic ease-in-out) for camera movement to avoid the mechanical feel of linear motion.

    4. Technical Approach and Key Challenges

    4.1 Procedural Geometry: Mixing Fractals and Signed Distance Fields

    The main subject can be described with a signed distance field (SDF), with multiple layers of fractal detail added through domain repetition and domain rotation, then rendered with ray marching. The key challenge is the step count and convergence threshold: too few steps cause gaps, while too many make frame time spike. In practice, externalize the maximum step count, minimum hit distance, and maximum travel distance as parameters.

    1. float map(vec3 p){
    2. float d = sdTorus(p, vec2(1.0, 0.35));
    3. for(int i=0;i<5;i++){
    4. p = rotY(0.6) * p;
    5. p = mod(p, 1.2) - 0.6;
    6. d = min(d, sdSphere(p, 0.18));
    7. }
    8. return d;
    9. }

    4.2 Physics Approximation: Gravitational Lensing and a Doppler Accretion Disk

    Gravitational lensing does not require solving the full general relativity equations. An approximation model where “light rays bend near a gravitational source” is enough for a believable look: accumulate the deflection angle along the ray path based on distance, and apply the offset direction when sampling the background starfield. For the accretion disk, use noise textures in polar coordinates to simulate turbulence, and add a Doppler factor based on orbital velocity so that the side facing the observer is brighter. The challenge is numerical stability; clamp the deflection coefficient to an upper limit to prevent rays from diverging.

    4.3 Particle System: Instanced Rendering and Curl Fields

    Use tens of thousands of instanced particles to represent dust and plasma, with a velocity field based on curl noise to keep it divergence-free and the motion natural. The key implementation points are instanced drawing with a single submission to avoid per-particle calls, and packing particle attributes (position, lifetime, scale) into textures or buffers for the vertex shader to parse.

    4.4 Post-Processing Pipeline: Anti-Flicker Bloom and Tone Mapping

    High-contrast small light sources flicker during motion. Use bloom extraction with a stability constraint (temporal smoothing of brightness), then combine it through multi-level downsampled blur. Finally, apply an industry-standard cinematic tone mapping curve (such as an ACES approximation) with slight vignetting and grain to unify the look. Note that post-processing must run in linear space and convert to sRGB only at the end; otherwise colors look washed out.

    4.5 Procedural Audio and Audio-Driven Visuals

    Audio is also synthesized in code: use sine wave stacking and envelopes to generate a low-frequency bed, use filtered noise to generate texture, and write the waveform file at a 48kHz sample rate. Visuals can be driven by the audio envelope, for example using low-frequency energy to control bloom intensity and particle brightness for audio-visual sync. The challenge is avoiding clipping, which requires normalization and limiting.

    5. Reproducible Project Structure

    A recommended repository structure keeps parameters centrally managed for easy reproduction and tuning:

    1. project/
    2. src/ # rendering and compositing source code
    3. shaders/ # vertex and fragment shaders
    4. config.json # resolution, frame rate, seed, performance budget
    5. package.json# dependencies and version locking
    6. README.md # run and render commands
    7. out/frames/ # frame sequence output directory

    6. Run and Render Commands (Examples)

    If using a browser graphics approach, make sure it runs offline: the entry is a single-page file, dependencies are bundled locally, and no external network is accessed.

    1. # Install dependencies (offline cache mode)
    2. npm ci
    3. # Start a local static server
    4. npx serve -s . -l 8080
    5. # Capture frame by frame with a headless browser, output to out/frames
    6. node render.js --config config.json --out out/frames

    If using a scripted approach, specify the environment and compositing command:

    1. # Render frames to a PNG sequence named frame_00001.png
    2. python render.py --seed 42 --frames 1350
    3. # Composite MP4
    4. ffmpeg -framerate 30 -i out/frames/frame_%05d.png -c:v libx264 -pix_fmt yuv420p out/final.mp4

    7. Performance Budget and Degradation Plan

    Targeting 1080p at 30fps, keep the per-frame budget under 33ms. Define three quality tiers: high (full resolution, full steps, full particles), medium (render at half resolution then upscale, halve the steps), and low (disable lensing, reduce particles to three thousand). Also provide an “offline high-quality mode” that allows per-frame time to extend to several seconds for final export. Be sure to state the VRAM limit and automatic degradation trigger conditions in the config, for example switching tiers automatically when VRAM usage exceeds a threshold.

    8. Self-Assessment and Common Pitfalls

    On delivery, provide a five-dimension self-assessment: visual impact, technical difficulty, code quality, reproducibility, and performance, with evidence (screenshots, frame time logs, and an empty asset list). Common pitfalls include doing post-processing in non-linear space, which causes washed-out colors; not fixing the random seed, which causes result drift; per-instance particle submission, which collapses the frame rate; and un-limited audio, which causes clipping. Explicitly listing these issues in the README with workarounds is key to scoring well on engineering quality.