Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Zai org CogVideo DDIM Export Latents To Video

From Leeroopedia


Attribute Value
Implementation Name DDIM Export Latents To Video
Workflow Video Editing DDIM Inversion
Step 6 of 6
Type API Doc
Source File inference/ddim_inversion.py:L312-317
Repository zai-org/CogVideo
External Dependencies diffusers (CogVideoXPipeline)
Last Updated 2026-02-10 00:00 GMT

Overview

Implementation of the latent-to-video export function for the DDIM inversion pipeline. The export_latents_to_video function decodes latent tensors through the pipeline's VAE and saves the result as an MP4 video file.

Description

The export_latents_to_video function:

  1. Calls pipeline.decode_latents(latents) to convert latent-space tensors to pixel-space video
  2. Calls pipeline.video_processor.postprocess_video(video, output_type="pil") to convert to PIL format
  3. Exports the PIL frames as an MP4 video file at the specified FPS using export_to_video

This function is called twice in the typical DDIM editing workflow: once for the inversion reconstruction (to verify quality) and once for the edited reconstruction.

Usage

from inference.ddim_inversion import export_latents_to_video

# Export the final trajectory step as video
export_latents_to_video(pipe, trajectory[-1], "output.mp4", fps=8)

Code Reference

Source Location

File Lines Description
inference/ddim_inversion.py L312-317 export_latents_to_video function

Signature

def export_latents_to_video(
    pipeline: CogVideoXPipeline,
    latents: torch.FloatTensor,
    video_path: str,
    fps: int = 8
) -> None:

Import

from inference.ddim_inversion import export_latents_to_video

I/O Contract

Inputs

Parameter Type Default Description
pipeline CogVideoXPipeline Required Loaded CogVideoX pipeline with VAE decoder and video processor
latents torch.FloatTensor Required Latent tensor of shape [B, T, C, H', W'] (typically the final step of a trajectory)
video_path str Required Output file path for the MP4 video
fps int 8 Frames per second for the output video

Outputs

Output Type Description
Side effect MP4 file Video file written to video_path

Usage Examples

Example 1: Export inversion reconstruction for verification

from inference.ddim_inversion import export_latents_to_video

# After DDIM inversion
inversion_trajectory = sample(pipe, latents, inverse_scheduler, prompt="")

# Export the inversion reconstruction (should match source video)
export_latents_to_video(
    pipeline=pipe,
    latents=inversion_trajectory[0],  # First step = clean latents
    video_path="inversion_verification.mp4",
    fps=8
)

Example 2: Export edited reconstruction

# After prompted reconstruction
with OverrideAttnProcessors(pipe.transformer):
    reconstruction = sample(
        pipe, noise, forward_scheduler,
        prompt="A cat running through autumn leaves",
        reference_latents=reversed_trajectory,
    )

# Export the edited video
export_latents_to_video(
    pipeline=pipe,
    latents=reconstruction[-1],  # Last step = denoised result
    video_path="edited_video.mp4",
    fps=8
)

Example 3: Side-by-side comparison

# Export both for comparison
export_latents_to_video(pipe, inversion_trajectory[0],
                        "original_reconstruction.mp4", fps=8)
export_latents_to_video(pipe, reconstruction[-1],
                        "edited_result.mp4", fps=8)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment