Local AI Video Generation with CogVideoX and Wan2.1: Text-to-Video on Linux Without Cloud APIs

AI tutorial - IT technology blog
AI tutorial - IT technology blog

Cloud video generation APIs are great until you see the invoice. Last month I was prototyping an automated content pipeline — generating short explainer videos from blog posts — and the costs from commercial services hit $300 in testing alone. Not a typo. That’s when I decided to go fully local.

Running text-to-video models on your own hardware gives you unlimited generation, complete privacy, and the ability to wire video creation into automated workflows without rate limits or per-minute pricing. Two open-source models stand out for Linux deployments: CogVideoX from Tsinghua University and Wan2.1 from Alibaba. Both are genuinely production-capable. Here’s what I’ve learned from running them in real workloads.

Why Cloud Video APIs Break Automated Workflows

The fundamental problem with cloud video APIs isn’t quality — it’s predictability. Rate limits kick in exactly when your batch job is running. Pricing changes with a blog post announcement. Creative content gets flagged by automated moderation. And every frame you generate goes through someone else’s servers.

For automated content production — think tutorial videos generated from documentation, product demos from feature descriptions, or social media clips from articles — you need consistent throughput at predictable cost. In my real-world experience, this is one of the essential skills to master: getting open-source video models running reliably in production without babysitting a cloud dashboard.

Local deployment solves all of this. The upfront cost is GPU hardware (or a rented GPU instance by the hour), but the marginal cost per video drops to electricity. For any serious automation use case, the math works out within weeks.

CogVideoX and Wan2.1: What They Are and What They Need

CogVideoX

CogVideoX comes in two sizes: a 2B parameter model and a 5B parameter model. The 5B version produces noticeably better quality — more coherent motion, better scene consistency, more cinematic results. Both generate roughly 6-second clips at 8fps by default, adjustable through the pipeline parameters.

VRAM requirements are significant:

  • CogVideoX-2B: ~14GB VRAM minimum (RTX 3090 / 4080 tier)
  • CogVideoX-5B: ~24GB VRAM minimum (RTX 3090 Ti / 4090 / A100)

With CPU offloading enabled, you can run CogVideoX-2B on cards with less VRAM at the cost of generation speed. The diffusers library handles the offloading transparently.

Wan2.1

Wan2.1 is my daily driver for batch jobs. The 1.3B model runs comfortably on an 8GB card — RTX 3070, 4060 Ti, and similar — making local video generation accessible without enterprise hardware. Quality is genuinely impressive for the parameter count: it handles motion physics well and prompt adherence is solid.

Available sizes:

  • Wan2.1-T2V-1.3B: ~8GB VRAM, fast generation, great for batch automation
  • Wan2.1-T2V-14B: ~40GB+ VRAM, higher fidelity output

For most automation workflows, the 1.3B model hits the right balance between resource usage and output quality.

System Prerequisites

Before installing either model, confirm your Linux system meets these requirements:

  • NVIDIA GPU with CUDA support (driver 525+)
  • CUDA 11.8 or 12.1+
  • Python 3.10+
  • At least 50GB free disk space for model weights
  • 16GB+ system RAM

Verify your GPU setup before touching anything else:

nvidia-smi
nvcc --version

Setting Up and Running Both Models

Environment Setup

Create a dedicated virtual environment — these models have specific dependency requirements, and you don’t want version conflicts with other Python projects:

python3 -m venv video-ai-env
source video-ai-env/bin/activate

# Install PyTorch with CUDA 12.1 support
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121

# Install diffusers ecosystem
pip install diffusers transformers accelerate
pip install imageio imageio-ffmpeg sentencepiece

Running CogVideoX

The HuggingFace diffusers library makes CogVideoX straightforward. Models download automatically on first run — about 14GB for 2B and 20GB for 5B, so run this when you have time and bandwidth:

from diffusers import CogVideoXPipeline
import torch
from diffusers.utils import export_to_video

pipe = CogVideoXPipeline.from_pretrained(
    "THUDM/CogVideoX-2b",
    torch_dtype=torch.float16
)

# Critical for cards near VRAM limit
pipe.enable_model_cpu_offload()
pipe.vae.enable_tiling()

prompt = """
A Linux terminal screen with green text scrolling,
close-up shot, dark background, cinematic lighting,
professional tech aesthetic, smooth animation
"""

video = pipe(
    prompt=prompt.strip(),
    num_videos_per_prompt=1,
    num_inference_steps=50,
    num_frames=49,
    guidance_scale=6.0,
    generator=torch.Generator(device="cuda").manual_seed(42),
).frames[0]

export_to_video(video, "cogvideox_output.mp4", fps=8)
print("Saved: cogvideox_output.mp4")

The enable_model_cpu_offload() and vae.enable_tiling() calls are not optional if you’re near your VRAM ceiling. They add roughly 20–30% to generation time but prevent out-of-memory crashes mid-run.

Running Wan2.1

Wan2.1 follows the same pattern through diffusers. The 1.3B model downloads in under 5GB and generates faster than CogVideoX on equivalent hardware:

from diffusers import WanPipeline
import torch
from diffusers.utils import export_to_video

pipe = WanPipeline.from_pretrained(
    "Wan-AI/Wan2.1-T2V-1.3B-Diffusers",
    torch_dtype=torch.bfloat16
)
pipe.to("cuda")

prompt = "A developer typing code on a mechanical keyboard, overhead shot, warm office lighting, cinematic"
negative_prompt = "blurry, distorted, low quality, watermark, static"

output = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    num_frames=81,
    num_inference_steps=50,
    guidance_scale=5.0,
    generator=torch.Generator(device="cuda").manual_seed(99),
).frames[0]

export_to_video(output, "wan21_output.mp4", fps=16)
print("Saved: wan21_output.mp4")

Wan2.1 defaults to 81 frames at 16fps (~5 seconds) while CogVideoX defaults to 49 frames at 8fps (~6 seconds). Both are adjustable. One thing to get right from the start: use bfloat16 for Wan2.1 and float16 for CogVideoX — swapping them introduces generation artifacts that look like the model is broken when it’s actually a dtype mismatch.

Batch Automation Script

Single video generation is fine for testing. The real payoff comes from batch automation. Here’s a script that reads prompts from a JSON file and processes them as a queue, skipping already-completed videos on restart:

import gc
import json
import torch
from pathlib import Path
from diffusers import WanPipeline
from diffusers.utils import export_to_video

OUTPUT_DIR = Path("videos")
OUTPUT_DIR.mkdir(exist_ok=True)

pipe = WanPipeline.from_pretrained(
    "Wan-AI/Wan2.1-T2V-1.3B-Diffusers",
    torch_dtype=torch.bfloat16
).to("cuda")

with open("prompts.json") as f:
    prompts = json.load(f)

for i, item in enumerate(prompts):
    output_path = OUTPUT_DIR / f"{item['id']}.mp4"
    if output_path.exists():
        print(f"Skip {i}: {item['id']} (already done)")
        continue

    print(f"[{i+1}/{len(prompts)}] Generating: {item['id']}")
    frames = pipe(
        prompt=item["prompt"],
        num_frames=81,
        num_inference_steps=50,
        guidance_scale=5.0,
    ).frames[0]

    export_to_video(frames, str(output_path), fps=16)
    print(f"Saved: {output_path}")

    # Prevent memory fragmentation across long runs
    gc.collect()
    torch.cuda.empty_cache()

The prompts file format:

[
  {
    "id": "linux-intro",
    "prompt": "Tux the Linux penguin in a data center, animated, friendly, blue ambient lighting"
  },
  {
    "id": "docker-containers",
    "prompt": "Shipping containers transforming into server racks, digital art style, time-lapse"
  },
  {
    "id": "python-code",
    "prompt": "Python code appearing line by line on a screen, macro close-up, green syntax highlighting"
  }
]

Memory Optimization Tips That Actually Matter

A few things I’ve learned the hard way from running these in production:

  • Clear CUDA cache between batch items: Memory fragmentation accumulates over long runs. Call torch.cuda.empty_cache() and gc.collect() between generations — it adds less than a second per item and prevents OOM crashes on video 47 of 50.
  • Lower inference steps for draft passes: 20–25 steps gives acceptable quality for review. Only go to 50 for final output. This cuts generation time roughly in half for draft workflows.
  • Pin your random seed during prompt development: Fix manual_seed while iterating on prompts so composition changes reflect the prompt, not randomness. Remove the seed for production batch runs to get variety.
  • Negative prompts make a real difference in Wan2.1: CogVideoX doesn’t use a negative prompt parameter, but Wan2.1 does. Even a simple "blurry, distorted, low quality" visibly improves sharpness.

Picking the Right Model for the Job

After running both models across different use cases, the decision comes down to hardware and priority:

  • Choose Wan2.1-1.3B when throughput matters over absolute quality, you have 8–12GB VRAM, or you’re running overnight batch jobs. It’s faster, lighter on memory, and the output is genuinely usable for most content automation.
  • Choose CogVideoX-5B when each video needs to look polished — presentations, professional content, cases where you’re generating fewer videos but quality is non-negotiable.

Both models improve dramatically with specific prompts. Include shot type (close-up, wide shot, overhead), lighting conditions, camera movement description (static, slow pan left, dolly zoom), and visual style (cinematic, documentary, 3D animated). Vague prompts produce mediocre results regardless of model size — that’s true with every generative AI model I’ve used.

From here, the natural extensions are compositing generated clips with ffmpeg, adding voiceover via a local TTS model, and wiring the pipeline into a content automation system triggered by new blog posts or documentation commits. The infrastructure for fully automated video production without touching a cloud API is all available in open-source form — CogVideoX and Wan2.1 are the hardest piece, and they’re now solved.

Share: