Generating Videos with Code: A Practical Guide to Automated Video Production
This article explains how to treat video as a structured artifact described by data and code, turning manual editing work into an engineering pipeline. It covers the declarative rendering model where a frame is a function of time, the key engineering elements such as timelines and frame rates, data and view separation, rendering and encoding, and asset management. It also discusses how AI fits in to generate production data, common pitfalls and how to avoid them, and a step-by-step path for adopting a code-driven rendering workflow.
1. Background: The Cost Structure of Video Production Is Changing
In the past, the cost of taking a video from idea to final cut was concentrated in three areas: shooting footage, manual editing, and repeated rounds of revision and communication. As generative AI capabilities have become more accessible, the marginal cost of script writing, voice synthesis, and image and asset generation has dropped significantly. Editing and composition have instead become the new bottleneck, because traditional editing relies on graphical interfaces and manual operation and is hard to absorb into an automated pipeline.
This has led to a clear direction of evolution in the industry: abstracting video from a “manually operated timeline” into a “structured artifact described by data and code.” Once a video becomes a function of code and data, it can be version controlled, automatically tested, and executed in batches. Production cost shifts from “human effort per video” to a “one-time engineering investment.”
2. Core Idea: A Declarative Video Rendering Model
The key to code-driven video is breaking the visuals down into “a function of frames.” At any point in time t, the frame is determined by the component tree participating in rendering at that moment, the style parameters, and the assets. This is closely aligned with how front-end UI rendering works: a UI is a function of state, and a video is a function of time.
A minimal illustrative structure is shown below (pseudocode, expressing the abstract model only):
// Video = a function of time tfunction Scene({ frame, fps }) {const t = frame / fps; // frame number -> secondsconst progress = clamp(t / 4, 0, 1); // normalized progressreturn (<Frame><Title text="Quarterly Data Overview"opacity={progress}translateY={(1 - progress) * 40} /><Chart data={payload.series} width={1920} /></Frame>);}
This model brings three direct benefits:
- Determinism: the same input data and parameters always render the same frame, which makes regression comparison easy;
- Composability: subtitles, charts, transitions, and watermarks are all components that can be freely assembled into different templates;
- Programmability: animation curves, layout rules, and color schemes all reduce to parameters rather than manual dragging.
3. Key Engineering Elements
3.1 Timeline and Frame Rate
The timeline is the core of a video. You need to define the total duration, the frame rate, and the start and end frames of each segment. Once the frame rate is set, all animations should be expressed in a consistent unit of frames or seconds to avoid drift from mixing units. For audio-visual synchronization scenarios, derive the timeline from the audio duration rather than fixing the visuals first and forcing the sound to fit.
3.2 Separating Data from View
The template only describes structure and style, while all content comes from structured data (such as JSON). This is the prerequisite for batch production: build the template once, feed in a batch of data.
{"template": "product-card","duration": 12,"items": [{ "title": "Capability 1", "desc": "Automated rendering", "accent": "#2b6cff" },{ "title": "Capability 2", "desc": "Data binding", "accent": "#00b578" }]}
3.3 Rendering and Encoding
Rendering is usually split into two steps: “generating images frame by frame” and “encoding them into a video container.” In engineering terms, pay attention to frame-level parallelism, GPU memory or RAM usage, retry on failure, and resumable rendering. For batch jobs, introduce a task queue and treat rendering as stateless job scheduling, which naturally gives you horizontal scalability.
3.4 Asset and Output Management
Static assets such as fonts, images, and audio should be centrally hosted on object storage and referenced through a unified path; the finished videos are written back to the same storage and given access URLs. Combined with logging services and monitoring alerts, you can observe task success rates, average duration, and the distribution of failure causes.
4. Where AI Capabilities Fit In
Once video is code-driven, the value of AI is amplified into “producing data” rather than “producing frames”:
- Text generation produces scripts and storyboard structures, output as JSON;
- Speech synthesis produces narration audio tracks, whose duration is fed back into the timeline;
- Image generation fills in missing assets with consistent dimensions and style;
- In the quality check stage, a model samples frames from the finished video to detect issues such as layout overflow and subtitle occlusion.
If you use AI coding tools to help write template code, focus the prompts on “component interface conventions, animation curves, and boundary conditions,” and apply manual review and automated testing to the generated results to avoid inventing parameters that do not exist.
5. Common Pitfalls and How to Avoid Them
- Missing fonts cause layout misalignment: version fonts together with the assets and validate their existence before rendering;
- Long text overflow: apply automatic shrinking and truncation strategies at the component level rather than relying on visual adjustment by eye;
- Numeric precision issues: use integer frames for all time calculations to avoid floating-point accumulation errors;
- Occasional asset unavailability: pre-fetch and validate assets before rendering, and fail fast with an alert on failure;
- Rework caused by lack of preview: first render a low-resolution, low-frame-rate proxy for review, and produce the high-definition final cut only after confirmation.
6. Recommended Adoption Path
- Start with a “parameterizable” template that covers a three-part structure: intro, body, and outro;
- Fix the data contract to establish a version mapping between templates and data;
- Add queued rendering and object storage, and run batch jobs at a scale of ten to a hundred videos;
- Add automated validation and monitoring to reduce manual involvement to spot checks;
- Finally, embed generative capabilities into the data production end to form a closed loop.
7. Conclusion
Writing video as code is essentially turning a craft into an engineering discipline. What it brings is not just cost savings, but a reusable, testable, and scalable production capability. For teams that need to continuously produce large volumes of structured video (such as data reports, product explainers, and course segments), the return on investment of this path is usually far higher than pure manual editing. The combination of general capabilities on Baidu AI Cloud, such as container platforms, object storage, and logging and monitoring, can provide a stable operational foundation for this kind of rendering pipeline.
