Creating Educational Animations with Manim: From Script to Final Video

Chan Meng

Creating educational animations is an effective way to convey complex concepts visually. This document details the complete process of turning a written transcript into a professional-quality educational animation using Manim (Mathematical Animation Engine). We’ll cover everything from initial script analysis to final video production, with a focus on best practices and solutions to common challenges.

Manim, originally created by Grant Sanderson (3Blue1Brown), is a powerful Python library designed for creating mathematical animations. However, it can be used for a wide range of educational content, as demonstrated in this project focused on machine learning concepts.

Project Overview

Our project involved creating a 10-minute educational animation about Decision Trees in Machine Learning. The animation was divided into three main scenes:

  1. Introduction to AI and Machine Learning (~2 minutes)

2. Decision Tree Fundamentals (~5 minutes) — split into two sub-scenes:

  • Basic concepts
  • Information Gain

3. Real-world Applications and Summary (~3 minutes)

The final output combined these scenes into a single cohesive video that explains decision trees from basic concepts to practical applications.

Setting Up the Environment

Prerequisites

To follow this process, you’ll need:

  • Python 3.7+
  • Manim library
  • FFmpeg for video processing
  • A text editor or IDE

Installation

  1. Python Environment Setup:
# Create a virtual environment
python -m venv manim-env

# Activate the environment
# Windows:
.\manim-env\Scripts\Activate.ps1
# Mac/Linux:
source manim-env/bin/activate

2. Installing Manim:

pip install manim

3. FFmpeg Installation:

Creating the Animation Structure

Project Structure

We organized our project with the following file structure:

manim/
├── main.py # Main file for importing and rendering all scenes
├── utils.py # Shared utility functions and components
├── scene1_introduction.py # Introduction scene
├── scene2_decision_tree_basics.py # Decision tree explanation scenes
├── scene3_applications.py # Applications and summary scene
├── render_all.ps1 # Script to automate rendering and combining
├── videos.txt # File list for video concatenation
├── README.md # Project documentation
└── final_output/ # Directory for the final merged video

Analysis of Script/Transcript

The first step was analyzing the transcript to:

  1. Identify main sections and logical breaks
  2. Note key concepts that need visualization
  3. Determine appropriate timing for each section
  4. Plan transitions between concepts

This analysis informed our division of content into separate scene files, ensuring each maintained a coherent theme while keeping files manageable.

Developing Modular Animation Code

Utility Functions

We created reusable components in utils.py to maintain consistency and simplify the animation code:

# Common colors and styles
BLUE_D = "#3C5A89"
GREEN_D = "#52876D"
RED_D = "#A64942"
BROWN = "#8B4513" # Added for fruit examples
LIGHT_GRAY = "#CCCCCC"

# Title creation function
def create_title_group(title_text, subtitle_text=None):
title = Text(title_text, font_size=48)
if subtitle_text:
subtitle = Text(subtitle_text, font_size=32)
subtitle.next_to(title, DOWN)
return VGroup(title, subtitle)
return title

# Shape creation functions
def create_apple():
# Create apple shape...

def create_orange():
# Create orange shape...

def create_banana():
# Create banana shape using Bezier curve...

# Decision tree components
def create_tree_node(text, color=WHITE, radius=0.5, fill_opacity=0.8):
# Create a node for decision trees...

def create_connector(start, end, color=WHITE):
# Create connecting lines between nodes...

Scene Structure

Each scene followed a similar pattern:

  1. Setup — Import necessary components and define the scene class
  2. Construct — The main animation method where we sequence animations
  3. Animations — Building blocks of visual elements with appropriate timing
  4. Transitions — Smooth movements between concepts
  5. Pauses — Strategic pauses for comprehension

Example from scene1_introduction.py:

from manim import *
from utils import create_title_group

class IntroductionScene(Scene):
def construct(self):
# Title animation
title_group = create_title_group("Introduction to AI", "Machine Learning & Decision Trees")
self.play(Write(title_group))
self.wait(2)

# Clear the title
self.play(FadeOut(title_group))

# AI examples - Create images and text descriptions
examples = [
"Image Recognition",
"Natural Language Processing",
"Netflix Recommendations",
"Face Recognition",
"Medical Diagnostics"
]

# Display examples with animations
for example in examples:
text = Text(example, font_size=36)
self.play(Write(text))
self.wait(1)
self.play(FadeOut(text))

# Transition to machine learning
# ... additional animations ...

Rendering Process

Individual Scene Rendering

Each scene can be rendered separately using Manim’s command-line interface:

# Render with medium quality and play after rendering
python -m manim scene1_introduction.py IntroductionScene -pqm

The flags represent:

  • -p: Play the animation once rendered
  • -q: Quality (l=low, m=medium, h=high)
  • -m: Use medium quality settings

Batch Rendering Script

We created a PowerShell script (render_all.ps1) to automate the rendering of all scenes:

# Machine Learning & Decision Trees - Automated Rendering Script

# Define quality settings
$QUALITY="-qm"

# Create output directory if it doesn't exist
if (-not (Test-Path "final_output")) {
New-Item -ItemType Directory -Path "final_output"
}

Write-Host "Starting to render all scenes..."

# Scene 1: Introduction
Write-Host "Rendering Scene 1: Introduction..."
python -m manim scene1_introduction.py IntroductionScene $QUALITY

# Scene 2a: Decision Tree Basics
Write-Host "Rendering Scene 2a: Decision Tree Basics..."
python -m manim scene2_decision_tree_basics.py DecisionTreeBasicsScene $QUALITY

# Scene 2b: Information Gain
Write-Host "Rendering Scene 2b: Information Gain..."
python -m manim scene2_decision_tree_basics.py InformationGainScene $QUALITY

# Scene 3: Applications
Write-Host "Rendering Scene 3: Applications..."
python -m manim scene3_applications.py ApplicationsScene $QUALITY

Write-Host "All scenes rendered successfully!"

# Rest of script handles merging...

Merging Animations

Creating a File List

For FFmpeg to merge videos, we need a text file listing all the video files in order:

# concat_list.txt
file 'media/videos/scene1_introduction/720p30/IntroductionScene.mp4'
file 'media/videos/scene2_decision_tree_basics/720p30/DecisionTreeBasicsScene.mp4'
file 'media/videos/scene2_decision_tree_basics/720p30/InformationGainScene.mp4'
file 'media/videos/scene3_applications/720p30/ApplicationsScene.mp4'

In PowerShell, we created this file with:

Set-Content -Path "concat_list.txt" -Value "file 'media/videos/scene1_introduction/720p30/IntroductionScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene2_decision_tree_basics/720p30/DecisionTreeBasicsScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene2_decision_tree_basics/720p30/InformationGainScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene3_applications/720p30/ApplicationsScene.mp4'"

Merging with FFmpeg

With the file list ready, we used FFmpeg’s concat function to merge the videos:

ffmpeg -f concat -safe 0 -i concat_list.txt -c copy final_output/Complete_Video_ML_Decision_Trees.mp4 -y

Parameters explained:

  • -f concat: Use the concat format
  • -safe 0: Don't check for potentially unsafe file paths
  • -i concat_list.txt: Input file list
  • -c copy: Copy streams without re-encoding (preserves quality)
  • -y: Automatically overwrite output file if it exists

Troubleshooting Common Issues

Throughout the project, we encountered and resolved several issues:

1. Missing Color Definition

Issue: NameError: name 'BROWN' is not defined

Solution: Added the missing color definition in utils.py:

BROWN = "#8B4513"

2. FFmpeg Merging Problems

Issue: Line 1: unknown keyword 'file' when using FFmpeg

Solution: Fixed the format of our concat file to ensure each entry is on a properly formatted line with no extra line breaks or spaces:

file 'path/to/video1.mp4'
file 'path/to/video2.mp4'

3. PowerShell Command Chaining

Issue: && command separator not working in PowerShell

Solution: Used PowerShell’s native command sequencing instead or separate commands:

# Instead of command1 && command2
command1
command2

4. Manim Module Not Found

Issue: No module named manim

Solution: Activated the virtual environment before running commands:

.\manim-env\Scripts\Activate.ps1

Conclusion

Creating educational animations with Manim is a powerful way to convey complex concepts visually. By following a structured approach — from script analysis to final video production — we can create compelling educational content that enhances learning.

The modular approach we used allowed for:

  • Easy maintenance and updates to individual sections
  • Parallel development of different scenes
  • Reuse of common components and styles
  • Simplified troubleshooting

This project demonstrates that with the right tools and methodology, creating professional-quality educational animations is accessible to educators, content creators, and developers.

Error and Solutions Document: Manim Educational Animation Project

  1. Missing Color Definition Error
  2. Manim Module Not Found Error
  3. FFmpeg Video Merging Error
  4. Chinese to English Content Conversion
  5. Successful Complete Workflow

Missing Color Definition Error

Error Description

NameError: name 'BROWN' is not defined

Problem Analysis

The BROWN color was used in the scene code, but this color was not defined in the utils.py file. Since our code depends on color constants defined in utils.py, any undefined color will cause rendering errors.

Solution

Add the definition of the BROWN color in the utils.py file:

# Add to the color definition section in utils.py
BROWN = "#8B4513"

Manim Module Not Found Error

Error Description

ModuleNotFoundError: No module named 'manim'

Problem Analysis

The virtual environment containing the Manim library was not properly activated when running the rendering script, causing Python to fail to find the required module.

Solution

Make sure to activate the correct Python virtual environment before running the rendering script:

# Activate the virtual environment in Windows PowerShell
.\manim-env\Scripts\Activate.ps1

# Then run the rendering script
.\render_all.ps1

FFmpeg Video Merging Error

Error Description

[concat @ 0000016741f36b80] Line 1: unknown keyword 'file'
Error opening input: Invalid data found when processing input

Problem Analysis

FFmpeg encountered a format error when processing the videos.txt file. The issue is that the format of the videos.txt file does not comply with FFmpeg's concat protocol requirements, possibly due to incorrect line format or missing line breaks.

Solution

  1. Create a new file concat_list.txt with the correct format:
# Create a new file and add the first video path
Set-Content -Path "concat_list.txt" -Value "file 'media/videos/scene1_introduction/720p30/IntroductionScene.mp4'"

# Add subsequent video paths, ensuring each path has a line break before it
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene2_decision_tree_basics/720p30/DecisionTreeBasicsScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene2_decision_tree_basics/720p30/InformationGainScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene3_applications/720p30/ApplicationsScene.mp4'"

2. Run the FFmpeg command with the new file, adding the -y parameter to automatically overwrite existing files:

ffmpeg -f concat -safe 0 -i concat_list.txt -c copy final_output/Complete_Video_ML_Decision_Trees.mp4 -y

Chinese to English Content Conversion

Problem Description

The project initially contained Chinese content that needed to be converted to English to ensure international readability.

Solution

Systematically edit each scene file to replace all Chinese content with English:

  1. Edit utils.py: Convert comments and docstrings
  2. Edit scene files:
  • Scene 1: Change “人工智能简介” to “Introduction to AI”, etc.
  • Scene 2: Change “决策树原理” to “Decision Tree Principles”, etc.
  • Scene 3: Convert application scenarios and summary sections to English

Ensure all user-visible text, variable names, and comments have been converted while keeping the code logic unchanged.

Successful Complete Workflow

The following is a successful workflow for creating and rendering Manim educational animations:

  1. Environment Setup:
# Create and activate virtual environment
python -m venv manim-env
.\manim-env\Scripts\Activate.ps1

# Install Manim
pip install manim

2. Code Structure:

  • utils.py: Shared utility functions and constants
  • Scene files: Content separated by theme (scene1_introduction.py, scene2_decision_tree_basics.py, scene3_applications.py)
  • Rendering script: render_all.ps1

3. Rendering Process:

# Activate environment
.\manim-env\Scripts\Activate.ps1

# Run rendering script
.\render_all.ps1

# If rendering script is unavailable, render scenes individually
python -m manim scene1_introduction.py IntroductionScene -qm
python -m manim scene2_decision_tree_basics.py DecisionTreeBasicsScene -qm
python -m manim scene2_decision_tree_basics.py InformationGainScene -qm
python -m manim scene3_applications.py ApplicationsScene -qm

4. Video Merging:

# Create output directory
mkdir -p final_output

# Create properly formatted file list
Set-Content -Path "concat_list.txt" -Value "file 'media/videos/scene1_introduction/720p30/IntroductionScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene2_decision_tree_basics/720p30/DecisionTreeBasicsScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene2_decision_tree_basics/720p30/InformationGainScene.mp4'"
Add-Content -Path "concat_list.txt" -Value "`nfile 'media/videos/scene3_applications/720p30/ApplicationsScene.mp4'"

# Merge videos
ffmpeg -f concat -safe 0 -i concat_list.txt -c copy final_output/Complete_Video_ML_Decision_Trees.mp4 -y

By following this workflow, you should be able to successfully create and render new educational videos while avoiding the issues we encountered in this project. Please refer to this document when developing new animations to save time and improve efficiency.

Note: This guide assumes basic familiarity with Python programming. Manim has a learning curve, but with practice, it becomes a powerful tool for creating dynamic, educational content.

No responses yet

Write a response