A comprehensive Python-based AI video generation system that processes movie scripts and storyboards to generate videos with consistent characters. Built as a modular, production-ready system with advanced character consistency techniques.
- Script Analysis: Advanced NLP-based character extraction from scripts using spaCy
- Character Generation: Text-to-image generation for character reference sheets
- Storyboard Analysis: Computer vision for character identification in storyboards
- Video Generation: Text-to-video with character consistency using multiple AI models
- Database Management: SQLite-based persistence for projects, characters, and videos
- CLI Interface: Easy-to-use command-line interface for all operations
- Character Consistency: Advanced techniques including reference image injection and LoRA fine-tuning
- ScriptParser - NLP-based character extraction from scripts
- CharacterGenerator - Text-to-image generation for character references
- StoryboardAnalyzer - Computer vision for character identification in storyboards
- VideoGenerator - Text-to-video with character consistency
- DatabaseManager - Character and scene data management
- APIManager - External API integrations
- Python 3.9+
- CUDA-compatible GPU (recommended for faster processing)
- 8GB+ RAM (16GB+ recommended)
- Clone the repository
git clone <repository-url>
cd ai_video_generator- Install Python dependencies
pip install -r requirements.txt- Download spaCy model
python -m spacy download en_core_web_sm- Download NLTK data
python -c "import nltk; nltk.download('punkt'); nltk.download('vader_lexicon')"- Set up environment variables
cp env.example .env
# Edit .env with your API keys- Create a new project
python src/main.py create-project --name "My Movie" --script "script.txt"- Extract characters from script
python src/main.py extract-characters --project-id 1- Generate character reference images
python src/main.py generate-references --project-id 1- Analyze storyboard
python src/main.py analyze-storyboard --project-id 1 --storyboard "storyboard.png"- Generate video
python src/main.py generate-video --project-id 1Extracts characters, scenes, and dialogue from screenplay formats.
from src.script_parser import ScriptParser
parser = ScriptParser()
characters, scenes = parser.parse_script("script.txt")
# Get character information
for char in characters:
print(f"Character: {char.name}")
print(f"Importance: {char.importance_score}")
print(f"Traits: {char.traits}")Features:
- Supports Final Draft, PDF, and TXT formats
- Extracts character descriptions and traits using spaCy NER
- Ranks characters by importance based on dialogue frequency
- Identifies character relationships and emotions
Generates consistent character reference images using text-to-image models.
from src.character_generator import CharacterGenerator
generator = CharacterGenerator()
generator.load_models()
character_info = {
'id': 'char_001',
'name': 'John Smith',
'description': 'A confident businessman in his 40s',
'traits': {'confident': 3, 'intelligent': 2}
}
reference = generator.generate_character_views(character_info, "./output")Features:
- Multiple character views (front, side, 3/4, expressions)
- Character consistency techniques (ControlNet, LoRA)
- Batch processing for multiple characters
- Reference sheet generation
Analyzes storyboard images to identify characters using computer vision.
from src.storyboard_analyzer import StoryboardAnalyzer
analyzer = StoryboardAnalyzer()
analyzer.load_character_database(characters)
frames = analyzer.analyze_storyboard("storyboard.png")
character_matches = analyzer.identify_characters_in_scene(frames)Features:
- Automatic frame detection in storyboard sheets
- Face recognition using DeepFace and face_recognition
- Character matching with confidence scores
- Multiple detection methods (contours, grid, text-based)
Generates videos from storyboard scenes with character consistency.
from src.video_generator import VideoGenerator
generator = VideoGenerator()
generator.load_models()
video = generator.generate_scene_video(scene_info, character_matches, "./output")Features:
- Multiple video generation backends (Open-Sora, RunwayML, HuggingFace)
- Character consistency maintenance
- Batch video generation
- Quality assessment
Create a .env file with the following variables:
# API Keys
OPENAI_API_KEY=your_openai_api_key_here
HUGGINGFACE_API_KEY=your_huggingface_token_here
RUNWAYML_API_KEY=your_runwayml_api_key_here
# Model Configuration
DEFAULT_VIDEO_MODEL=open-sora
DEFAULT_IMAGE_MODEL=stabilityai/stable-diffusion-xl-base-1.0
DEFAULT_FACE_MODEL=Facenet
# Processing Configuration
MAX_CONCURRENT_VIDEOS=2
MAX_CONCURRENT_IMAGES=4
VIDEO_QUALITY=high
IMAGE_RESOLUTION=1024The system supports multiple AI models:
Image Generation:
- Stable Diffusion XL
- DALL-E 3 (via API)
- Midjourney (via API)
Video Generation:
- Open-Sora (local)
- OpenAI Sora (API)
- RunwayML (API)
- HuggingFace models (API)
Face Recognition:
- DeepFace (Facenet, VGG-Face, ArcFace)
- face_recognition library
The system uses SQLite for data persistence with the following tables:
- projects: Project information and status
- characters: Character data and reference images
- scenes: Scene information from scripts
- scene_characters: Character-scene relationships
- videos: Generated video metadata
- Reference Image Injection: Pass character reference images to video generation
- LoRA Fine-tuning: Train character-specific adapters for Stable Diffusion
- Embedding Consistency: Use consistent text embeddings for characters
- Multi-shot Character Generation: Advanced character consistency across scenes
- Multi-modal Matching: Combine face recognition with clothing/pose matching
- Confidence Thresholding: Only proceed with high-confidence character matches
- Manual Override: UI for correcting mis-identified characters
- Batch Processing: Generate multiple scenes in parallel
- Quality Control: Automatic filtering of poor-quality outputs
- Style Consistency: Maintain visual style across scenes
Run the test suite:
# Run all tests
pytest tests/
# Run with coverage
pytest --cov=src tests/
# Run specific test file
pytest tests/test_script_parser.pyai_video_generator/
βββ src/
β βββ script_parser.py # Script analysis and character extraction
β βββ character_generator.py # Character reference image generation
β βββ storyboard_analyzer.py # Storyboard analysis and character identification
β βββ video_generator.py # Video generation with character consistency
β βββ database.py # Database management and models
β βββ config.py # Configuration management
β βββ main.py # CLI application
βββ data/
β βββ projects/ # Project files
β βββ characters/ # Character reference images
β βββ videos/ # Generated videos
βββ models/ # Local AI models
βββ tests/ # Test suite
βββ requirements.txt # Python dependencies
βββ env.example # Environment variables template
βββ README.md # This file
-
Model Loading Errors
- Ensure you have sufficient RAM (8GB+)
- Check CUDA installation for GPU acceleration
- Verify model downloads completed successfully
-
API Rate Limits
- Adjust
MAX_CONCURRENT_VIDEOSandMAX_CONCURRENT_IMAGESin config - Implement exponential backoff for API calls
- Adjust
-
Character Recognition Issues
- Ensure character reference images are high quality
- Adjust confidence thresholds in storyboard analyzer
- Use manual override for difficult cases
-
Video Generation Failures
- Check API key validity
- Verify sufficient disk space
- Monitor GPU memory usage
-
GPU Memory Management
- Use smaller batch sizes
- Enable gradient checkpointing
- Clear CUDA cache between operations
-
API Optimization
- Implement request caching
- Use connection pooling
- Batch API requests when possible
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenAI for Sora and DALL-E APIs
- HuggingFace for model hosting and APIs
- Stability AI for Stable Diffusion models
- The open-source community for various AI libraries
For support and questions:
- Create an issue on GitHub
- Check the troubleshooting section
- Review the documentation
Note: This system requires significant computational resources and API access. Ensure you have appropriate hardware and API keys before running.