Skip to content

lorettarehm/mermaid2gif

Β 
Β 

Repository files navigation

mermaid2gif

Autonomous Mermaid to Flow-Animated GIF Converter

Python 3.11+ License: MIT

Overview

mermaid2gif is a fully autonomous, headless system that converts Mermaid diagrams into flow-animated GIFs. The system uses LangGraph for orchestration, LiteLLM for LLM interactions, Playwright for browser automation, and FFmpeg for high-quality media processing.

Demo: .mmd files to animated diagram

assets/Mermaid2GIF1.gif


Demo: Natural language to animated diagram

assets/Mermaid2GIF2.gif

Key Features:

  • πŸ€– Fully Autonomous: Zero manual interaction required
  • 🎯 Headless Execution: CI/CD safe
  • πŸ”„ Seamless Loops: "Buffered Recording" technology eliminates blank frames
  • 🎬 Flow Animation: Path-based animation for all supported diagram types
  • πŸ“ Smart Viewport: Auto-crops to diagram size (no excess white space)
  • πŸ“Š High Quality: Sharp output with floyd_steinberg dithering and full palette generation
  • πŸ” HD Sequence Diagrams: Specialized rendering configuration for crisp, readable text

Supported Diagram Types:

  • βœ… Flowcharts (TD/LR)
  • βœ… Sequence Diagrams
  • βœ… Class Diagrams
  • βœ… State Diagrams
  • βœ… ER Diagrams (Entity-Relationship)

Architecture

The system is implemented as a Directed Cyclic Graph (DCG) using LangGraph:

assets/Full-orchestration-graph-TD.gif ⚑ Powered by Mermaid2GIF

Technology Stack

  • Orchestration: LangGraph
  • LLM Interface: LiteLLM (Groq / OpenRouter)
  • Rendering: Native Mermaid.js (via Playwright)
  • Animation: JavaScript Path-Based Calculation
  • Browser Control: Playwright (Chromium)
  • Media Processing: FFmpeg (Palette-based encoding)
  • Configuration: Pydantic Settings

Installation

Prerequisites

  • Python 3.11+
  • FFmpeg (must be in system PATH)

Setup

  1. Clone the repository:
git clone https://github.com/rsrini7/mermaid2gif.git
cd mermaid2gif
  1. Install dependencies:
pip install -e .
playwright install chromium
  1. Configure environment:
cp .env.example .env
# Edit .env and add either GROQ_API_KEY (Recommended) or OPENROUTER_API_KEY

Usage

Basic Usage

# Convert natural language to GIF
python -m src.main "Create a flowchart showing user authentication flow"

# Convert Mermaid code directly
python -m src.main "graph TD; A[Start] --> B{Check}; B -->|Yes| C[OK];"

CLI Options

# Read from file
python -m src.main --input-file diagram.mmd
python -m src.main -i diagram.mmd

# Specify output path
python -m src.main "graph TD; A-->B" --output my-diagram.gif
python -m src.main -i diagram.mmd -o output/result.gif

# Enable verbose logging
python -m src.main "Create a sequence diagram" --verbose
python -m src.main -i diagram.mmd -v

# Combine options
python -m src.main -i input.mmd -o output.gif -v

Available Options:

  • -i, --input-file: Path to file containing Mermaid diagram code
  • -o, --output: Output path for the generated GIF (default: auto-generated in ./output/)
  • -v, --verbose: Enable verbose logging for debugging

Configuration

All configuration is managed through environment variables or .env file.

Credentials (At least one required)

  • GROQ_API_KEY: Groq API Key (Recommended for speed/cost). Must start with gsk_.
  • OPENROUTER_API_KEY: OpenRouter API Key. Must start with sk-or-.

Optional Configuration

Variable Default Value Description
LITELLM_MODEL groq/llama-3.3-70b-versatile LLM model via LiteLLM
DEFAULT_ANIMATION_DURATION 5.0 Animation duration in seconds
DEFAULT_FPS 30 Frame rate for GIF output
VIEWPORT_WIDTH 1920 Browser viewport width
VIEWPORT_HEIGHT 1080 Browser viewport height
LOG_LEVEL INFO Logging level (DEBUG, INFO, WARNING, ERROR)

Project Structure

mermaid2gif/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ agents/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ fixer.py            # Mermaid syntax repair agent
β”‚   β”‚   └── intent.py           # Text-to-Mermaid conversion agent
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ config.py           # Pydantic configuration
β”‚   β”‚   β”œβ”€β”€ exceptions.py       # Custom exception hierarchy
β”‚   β”‚   β”œβ”€β”€ graph.py            # LangGraph orchestration
β”‚   β”‚   └── state.py            # Shared graph state
β”‚   β”œβ”€β”€ engine/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ animation_applicator.py # JS path-based animation
β”‚   β”‚   β”œβ”€β”€ capture_controller.py   # Playwright video capture
β”‚   β”‚   β”œβ”€β”€ ffmpeg_processor.py     # FFmpeg transcoding & optimization
β”‚   β”‚   β”œβ”€β”€ mermaid_renderer.py     # Native Mermaid.js rendering
β”‚   β”‚   β”œβ”€β”€ mermaid_validator.py    # Syntax validation
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── logger.py           # Structured logging
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── main.py                 # CLI entry point
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ mocks/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── test_smoke.py           # Mock-based end-to-end test
β”œβ”€β”€ .env.example
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ README.md
└── REQUIREMENTS.md

Critical Constraints & Design

  1. No External Dependencies: Bypassed Draw.io completely. Rendering is pure local Mermaid.js.
  2. Headless Only: No manual interaction required.
  3. Deterministic: Same input always produces same output.
  4. Buffered Capture: Recording time is Duration + 2s to ensure clean loops.
  5. Smart Viewport: Two-phase capture (measure β†’ record) auto-crops to diagram size with 40px padding.

Contributors

This project was developed with contributions from:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.


License

MIT License - see LICENSE file for details

About

Natural Language or Mermaid Code to Animated Flow Gif Generation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 91.2%
  • Mermaid 7.2%
  • Dockerfile 1.6%