How does text-to-video generation work?

Authors
How does text-to-video generation work?

Text-to-video generation is a way for an AI model to create a short video from a written sentence. The model starts from random noise and slowly cleans it, step by step, into moving frames that match our words.

In this blog, we will learn about how text-to-video generation works. We will also see what a video really is for a computer, how the model understands our sentence, why it works in a small compressed space instead of on raw pixels, how diffusion turns noise into a video, how the model keeps the frames consistent over time, and where it works well and where it fails.

We will cover the following:

  • What is text-to-video generation?
  • What is a video for a computer?
  • Why is video harder than an image?
  • The big picture: the main parts
  • Part 1: Understanding the text
  • Part 2: Compressing the video into a latent space
  • Part 3: Diffusion - turning noise into a video
  • Part 4: Keeping the frames consistent over time
  • Part 5: Decoding back to real frames
  • How is such a model trained?
  • A simple example in code
  • Where it works well and where it fails

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

What is text-to-video generation?

Text-to-video generation is the task where we give an AI model a sentence, and the model creates a video that matches that sentence.

Let's say we type: "A golden retriever running on a beach at sunset."

After some time, the model gives us a short video clip. In that clip, a dog runs on the sand, the waves move, and the sky is orange.

No camera was used. The model created every frame by itself.

Models like OpenAI Sora and the open model Wan do this task.

Now, the question is, how can a computer do this? Before jumping into the answer, we must know what a video looks like to a computer.

What is a video for a computer?

A video is just a series of pictures shown very fast, one after another. Each picture is called a frame.

When we see 24 frames in one second, our eyes feel that things are moving smoothly.

And, each frame is made of tiny colored dots called pixels. Each pixel is stored as three numbers: how much red, how much green, and how much blue it has.

So, for a computer, a video is a big block of numbers as below:

   Video = Frames x Height x Width x Colors

   Frame 1      Frame 2      Frame 3      ...   Frame 120
  +--------+   +--------+   +--------+         +--------+
  | pixels |   | pixels |   | pixels |   ...   | pixels |
  +--------+   +--------+   +--------+         +--------+

   time ------------------------------------------------>

Here, we can see that a video is a stack of frames placed along the time direction. Each frame is a grid of pixels.

Let's do a small calculation for the sake of understanding. A 5-second video at 24 frames per second has 120 frames. If each frame is 1280 x 720 pixels, and each pixel has 3 numbers, then:

120 x 1280 x 720 x 3 = 331,776,000 numbers

That is more than 330 million numbers for just 5 seconds. Generating a video means the model must produce all these numbers correctly.

Why is video harder than an image?

Many of us must be knowing about text-to-image models that create a picture from a sentence. Text-to-video is much harder because of the following reasons:

  • Much more data: One video has hundreds of frames, so it needs far more numbers and far more computing power.
  • Consistency over time: The dog in frame 1 must be the same dog in frame 100. It cannot suddenly change color or get a fifth leg.
  • Real motion: Things must move in a natural way. Waves must flow, a ball must fall down, not up.
  • Less training data: Good videos with good written descriptions are harder to collect than images with descriptions.

So, the model must not only draw well, it must also understand time and motion.

Now, let's see the main parts that solve these problems.

The big picture: the main parts

A modern text-to-video model has a few main parts working together. Let's see the whole flow first, and then we will learn each part one by one.

+------------------------------------+
| Prompt: "A dog running on a beach" |
+------------------------------------+
                  ↓
+------------------------------------+
| Text Encoder                       |
| Understands the sentence           |
| Output: meaning (numbers)          |
+------------------------------------+
                  ↓
+------------------------------------+
| Denoiser (many steps)              |
| Inputs: meaning + random noise     |
| (small, compressed)                |
| Output: clean compressed video     |
+------------------------------------+
                  ↓
+------------------------------------+
| Video Decoder                      |
| Expands to real frames             |
+------------------------------------+
                  ↓
+------------------------------------+
| Final video                        |
+------------------------------------+

Here, we can see four ideas:

  • The text encoder turns our sentence into numbers that carry its meaning.
  • The model starts with random noise, like the static on an old TV.
  • The denoiser cleans that noise step by step, guided by the meaning of our sentence.
  • The video decoder turns the clean result into real frames that we can watch.

We will learn about each of them in detail.

Part 1: Understanding the text

First, the model must understand what we want.

Computers do not understand words directly. They understand numbers. So, we use a text encoder, which is a model that reads our sentence and turns it into a list of numbers. These numbers are called an embedding, and they carry the meaning of the sentence.

For example, the sentences "a dog running on a beach" and "a puppy jogging on the sand" will get very similar numbers, because their meaning is similar.

Common text encoders used here are models like CLIP and T5, and newer systems also use large language models for this job.

Note: Many systems also rewrite our short prompt into a longer and more detailed one before using it. "A dog on a beach" becomes "A golden retriever with wet fur running along a sandy beach, waves in the background, warm sunset light". A detailed prompt gives the model more to work with.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

Now, let's understand the small space where the video is actually created.

Part 2: Compressing the video into a latent space

We saw that a 5-second video has more than 330 million numbers. Working directly on so many numbers is very slow and very expensive.

So, here comes the Video Autoencoder to the rescue.

An autoencoder has two halves:

  • An encoder that squeezes a video into a much smaller form.
  • A decoder that expands the small form back into the full video.

This small form is called the latent, and the space where it lives is called the latent space.

Let's take an analogy. Think of a big book summarized into short notes. The notes are much smaller, but a good writer can rebuild the full story from them. The latent is like those short notes for a video.

+--------------------------------+
| Full video (huge)              |
| 120 frames x 1280 x 720 x 3    |
+--------------------------------+
                ↓
+--------------------------------+
| Encoder                        |
+--------------------------------+
                ↓
+--------------------------------+
| Latent (small notes)           |
| 30 x 160 x 90 x 16             |
+--------------------------------+

Here, the encoder shrinks the video in both space and time. Every 8 x 8 block of pixels becomes one point, and roughly every 4 frames become one step in time. Each point keeps 16 numbers that describe what was there. So, about 331 million numbers become about 7 million numbers, which is nearly 48 times smaller. These exact numbers change from model to model, and we are using them just for the sake of understanding.

The model creates the video in this small latent space, not on raw pixels. This makes generation many times cheaper and faster.

The decoder is used only at the very end to turn the latent back into a real video. We will come back to it in Part 5.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

Part 3: Diffusion - turning noise into a video

Now, let's understand where the video is actually generated.

Most text-to-video models use an idea called diffusion.

Diffusion is a method where a model learns to remove noise from data, step by step, until clean data appears.

Let's understand this with an example.

Suppose we take a clear photo and add a little random noise to it. It becomes slightly grainy. We add more noise. It becomes more grainy. We keep doing this until the photo becomes pure random static. Nothing of the original photo is visible now.

Now, think in the reverse direction. If a model learns how to remove a little noise at each step, then we can start from pure static and remove noise again and again. In the end, a clean picture appears.

Training (adding noise)     Generation (removing noise)

clean video                 pure noise
     ↓                           ↓
bit noisy                   less noisy
     ↓                           ↓
more noisy                  almost clean
     ↓                           ↓
pure noise                  clean video

Here, we can see that during training, we add noise to real videos and ask the model to tell how to clean it. Classic diffusion models do this by predicting the noise that was added. During generation, the model uses this skill to clean pure noise into a brand new video.

If we start from random noise, how does the model know that it must create a dog on a beach and not a car on a road?

The answer is: the text embedding guides every cleaning step. At each step, the denoiser looks at two things:

  • The current noisy latent video.
  • The meaning of our sentence.

Then, it removes the noise in a direction that makes the video look more like our sentence. Slowly, step after step, the dog and the beach appear.

Note: Models also use a trick called classifier-free guidance. The model makes two predictions at each step, one with our sentence and one without any sentence, and then pushes the result further in the direction of the sentence. This makes the video follow our prompt more strongly.

Many newer models use a closely related method called flow matching. The idea is the same: start from noise and move step by step towards a clean video. Flow matching just learns a straighter path from noise to video, so it usually needs fewer cleaning steps.

If we want to go deep into Generative AI, Diffusion Models, and Multimodal Generative Models, we can check out our AI and Machine Learning Program at Outcome School.

Part 4: Keeping the frames consistent over time

Now, it's time to learn how all the frames stay consistent with each other.

Let's say the model creates each frame separately, like 120 separate images. What happens?

Frame 1 has a golden dog. Frame 2 has a slightly brown dog. Frame 3 has the dog facing a different side. When we play them together, the video flickers and jumps. It looks broken.

The issue with this approach is that no frame knows about the other frames. We needed a solution for that.

So, here comes the idea of spacetime patches and attention into the picture.

First, the latent video is cut into small pieces called patches. Each patch covers a small area of the picture and a short slice of time. Think of a video as a big loaf of bread, and we cut it into small cubes. Each cube is a patch.

+----+----+----+
| p1 | p2 | p3 |
+----+----+----+
| p4 | p5 | p6 |
+----+----+----+

Here, each small cube is one patch, which covers a little area and a little time.

The denoiser is usually a Transformer, the same architecture used in LLMs. A diffusion model built on a Transformer is called a Diffusion Transformer, or DiT. Sora popularized this design for video.

A Transformer has a mechanism called attention. Attention lets every patch look at every other patch and ask, "What is happening there, and how does it affect me?"

The patches also look at the words of our sentence in the same way. This is how the text guides each cleaning step. We have a detailed blog on Cross Attention in Transformers that explains how this works.

So, a patch showing the dog's ear in frame 50 can look at the dog's ear in frame 10 and in frame 90. Hence, the ear keeps the same shape and color across the whole video.

There are two common ways to do this:

  • Full spacetime attention: Every patch looks at all patches across all frames at once. It gives the best consistency but costs a lot of computing power.
  • Separate space and time attention: First, patches look at others in the same frame (space). Then, each patch looks at the same position in other frames (time). It is cheaper, but slightly weaker.

Because all the frames are cleaned together, at the same time, in one big process, the model plans the whole clip as one piece. This is why motion usually looks smooth and the dog stays the same dog.

This is how the model keeps frames consistent over time.

To master the Transformer Architecture, the Attention Mechanism, and Cross Attention from the ground up, we have our AI and Machine Learning Program at Outcome School.

Part 5: Decoding back to real frames

After all the cleaning steps are done, we have a clean latent video. But, it is still in the small "notes" form. We cannot watch it yet.

So, we pass it through the video decoder from Part 2. The decoder expands the small latent back into full-size frames with real pixels.

+------------------------+
| Clean latent (small)   |
+------------------------+
            ↓
+------------------------+
| Decoder                |
+------------------------+
            ↓
+------------------------+
| 120 real frames        |
+------------------------+
            ↓
+------------------------+
| Video file             |
+------------------------+

Many systems then add a final polish, like upscaling to a higher resolution. Some newer models also generate sound along with the video.

How is such a model trained?

Now, the next big question is: how does the model learn all this?

The model is trained on a very large number of video clips, each paired with a text description of what happens in it.

The training loop works as below:

  • Take a real video and its description.
  • Compress the video into a latent using the encoder.
  • Add a random amount of noise to the latent.
  • Ask the denoiser to predict how to clean it, while showing it the description. A classic diffusion model predicts the added noise. A flow matching model, like the Wan model we will use in the code, predicts the direction from the noise to the clean video.
  • Compare the prediction with the correct answer and update the model parameters a little.
  • Repeat this millions of times.

Note: Many videos on the internet do not have good descriptions. So, teams often use another AI model to watch each video and write a detailed caption for it. This is called re-captioning, and it helps the model connect words to what it sees much better.

Also, models are usually trained on images first, then on short low-resolution videos, and then on longer and sharper videos. Learning from easy to hard makes training more stable.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

A simple example in code

Let's see how we can use an open text-to-video model in Python with the diffusers library. We can write the code as below:

import torch
from diffusers import DiffusionPipeline
from diffusers.utils import export_to_video

# load an open text-to-video model
pipe = DiffusionPipeline.from_pretrained(
    "Wan-AI/Wan2.1-T2V-1.3B-Diffusers",
    torch_dtype=torch.bfloat16
)
pipe.to("cuda")

prompt = "A golden retriever running on a beach at sunset"

frames = pipe(
    prompt=prompt,
    num_frames=81,            # how many frames to create
    num_inference_steps=30,   # how many cleaning steps
    guidance_scale=5.0        # how strongly to follow the prompt
).frames[0]

export_to_video(frames, "dog.mp4", fps=16)

Here, we have:

  • Loaded a pipeline, which bundles the text encoder, the denoiser, and the video autoencoder together.
  • Moved it to the GPU using cuda, because video generation needs a lot of computing power.
  • Passed our prompt, which goes to the text encoder.
  • Set num_frames, which decides the length of the video.
  • Set num_inference_steps, which is the number of cleaning steps. More steps usually give better quality but take more time.
  • Set guidance_scale, which is the classifier-free guidance strength. A higher value follows the prompt more strictly.
  • Saved the frames as an mp4 file using export_to_video.

Note: The exact model name and the best settings change with time and with the model we use. We must check the model page for the recommended values.

Where it works well and where it fails

Text-to-video works well for:

  • Short clips, usually a few seconds to a few tens of seconds.
  • Nature scenes, landscapes, and slow camera movements.
  • Concept videos, ads, storyboards, and styles like animation or cinematic looks.

It still fails in some cases:

  • Physics mistakes: A glass breaks in a strange way, or a person walks through a chair.
  • Hands and fine details: Fingers and small text often come out wrong.
  • Long videos: Keeping a character the same over a long time is still hard.
  • Counting and exact actions: "Exactly three birds flying left" often goes wrong.
  • High cost: Generating even a short video needs powerful GPUs and a lot of time.

Now we must have understood how text-to-video generation works.

Frequently Asked Questions

What is a Diffusion Transformer (DiT)?

A Diffusion Transformer, or DiT, is a diffusion model whose denoiser is built on a Transformer, the same architecture used in LLMs. Sora popularized this design for video. Its attention lets every patch of the video look at every other patch, across all frames, which keeps the frames consistent over time.

What does the guidance scale do in text-to-video generation?

The guidance scale sets the strength of classifier-free guidance. At each cleaning step, the model makes one prediction with our sentence and one without any sentence, and then pushes the result further in the direction of the sentence. A higher value makes the video follow the prompt more strictly.

Do more cleaning steps always give a better video?

Not always, but more steps usually give better quality and take more time. Models that use flow matching learn a straighter path from noise to video, so they usually need fewer cleaning steps.

Can text-to-video models create long videos?

Not well yet. Text-to-video works best for short clips, usually a few seconds to a few tens of seconds. Keeping a character the same over a long time is still hard, and generating even a short video needs powerful GPUs and a lot of time.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.