Building an AI-Powered YouTube Shorts Generator: A Complete Technical Deep Dive

I am a Computer Science undergrad from The National Institute of Engineering, Mysore. I freelance while working at Twilio as Software Developer Engineer (l1).
Search for a command to run...

I am a Computer Science undergrad from The National Institute of Engineering, Mysore. I freelance while working at Twilio as Software Developer Engineer (l1).
No comments yet. Be the first to comment.
Lately, I've been diving deep into the world of my SaaS product, Reach. I've been thoroughly immersed in figuring out how to calculate basic site analytics—focusing on the usual suspects like views, unique views, clicks, and a range of other interest...

In a world where communication is key, email templates play a crucial role in delivering messages effectively. Explore the transformative power of React in simplifying the intricate art of crafting email templates. From streamlined development workfl...

Tech stack breakdown: Reach | Building my SAAS

A small story of me learning micro services, gRPC and a multitude of things.

In the era of short-form content dominance, creating engaging YouTube Shorts consistently can be a time-consuming challenge. Today, I'm excited to share a comprehensive technical breakdown of an open-source YouTube Shorts generator that automates the entire video creation pipeline—from text-to-speech generation to final video composition.
The YouTube Shorts Generator is a Python-based automation tool designed to create professional-quality short videos with zero manual intervention. What makes this project unique is its "local-first" approach, prioritizing CPU processing and minimal API dependencies while maintaining high output quality.
🚀 Fast & Efficient: Optimized for batch processing multiple videos
🏠 Local-First: Primary processing happens on your machine
💰 Cost-Effective: Only requires Pexels API (free tier available)
🎤 Human-Sounding: Multiple TTS engines with neural voice synthesis
📱 YouTube Shorts Optimized: 9:16 aspect ratio, perfect timing
The system follows a modular, component-based architecture that ensures maintainability and extensibility:
videoOrchestrator.py (Main Controller)
├── config/config_manager.py (Configuration)
├── components/
│ ├── topic_manager.py (Content Management)
│ ├── tts_generator.py (Audio Generation)
│ ├── pexels_fetcher.py (Image Fetching)
│ └── video_composer.py (Video Assembly)
config_manager.py)The configuration system uses environment variables and .env files for flexible deployment:
class ConfigManager:
def __init__(self, env_file: str = ".env", pexels_api_key: str = None):
self._load_environment()
self._setup_directories()
self._setup_logging()
Key Configuration Areas:
TTS Settings: Rate, volume, voice selection
Video Parameters: Resolution (1080x1920), FPS, duration
Pexels Integration: API key, image quality, search terms
File Paths: Output directories, temp storage, topic files
tts_generator.py)One of the project's standout features is its sophisticated TTS engine fallback system:
def _initialize_engine(self):
tts_methods = [
("coqui_tts", self._init_coqui_tts), # Neural, local
("elevenlabs", self._init_elevenlabs), # Premium, cloud
("pyttsx3", self._init_pyttsx3), # Cross-platform
("system_say", self._init_system_say), # macOS native
("espeak", self._init_espeak) # Linux fallback
]
TTS Engine Hierarchy:
Coqui TTS (Preferred): Neural synthesis, completely local
ElevenLabs: Premium cloud-based, requires API key
pyttsx3: System TTS, cross-platform compatibility
macOS Say: Native macOS voice synthesis
espeak: Linux/Unix fallback option
pexels_fetcher.py)The image fetching system includes intelligent caching and search query generation:
def _generate_search_queries(self, topic_title: str) -> List[str]:
# Default tech/coding related queries
tech_queries = [
"technology abstract",
"computer programming",
"digital technology",
"coding screen",
"dark technology"
]
# Combine and randomize for variety
return self._combine_and_shuffle_queries(tech_queries)
Image Processing Features:
Intelligent Caching: 1-hour cache for API responses
Rate Limiting: Respects Pexels API constraints
Auto-Scaling: Resizes images to 9:16 aspect ratio
Validation: Ensures image quality and accessibility
video_composer.py)The video composer handles the complex task of synchronizing audio, images, and text overlays:
def _create_background_slideshow(self, image_paths: List[str], duration: float):
# Calculate precise timing with NO transitions
base_time_per_image = duration / num_images
for i, image_path in enumerate(image_paths):
if i == len(image_paths) - 1:
# Last image gets ALL remaining time
clip_duration = duration - cumulative_time
else:
clip_duration = base_time_per_image
Video Composition Features:
Perfect Timing Sync: Audio and video durations match exactly
Visual Effects: Subtle zoom/pan effects for engagement
Text Overlays: Title integration with fallback support
Quality Optimization: YouTube Shorts specifications
The system supports two distinct workflows:
# Uses topics.json for automated progression
orchestrator = VideoOrchestrator()
result = orchestrator.run_single_generation()
# Direct topic data input
topic_data = {
"title": "Machine Learning",
"description": "ML algorithms learn from data..."
}
orchestrator = VideoOrchestrator.from_topic_data(topic_data)
result = orchestrator.generate()
The system provides detailed performance metrics:
result = {
"timing": {
"validation": 0.05,
"audio_generation": 4.68,
"image_fetching": 2.34,
"video_creation": 15.23,
"file_update": 0.12,
"total": 22.42
}
}
Clip Cleanup: Automatic MoviePy clip disposal
Temp File Management: Automatic cleanup with age-based purging
Cache Management: Intelligent image cache with size limits
The project includes automated setup scripts for different platforms:
chmod +x setup_scripts/CoquiSetup.sh
./setup_scripts/CoquiSetup.sh
./setup_scripts/macOs_engines_setup.sh
./setup_scripts/MoviePyImageMagickFix.sh
python videoOrchestrator.py --mode single --verbose
python videoOrchestrator.py --mode continuous --max-iterations 5
python videoOrchestrator.py --mode status
from videoOrchestrator import VideoOrchestrator
topic_data = {
"title": "API Design",
"description": "Creating effective APIs..."
}
mo = VideoOrchestrator.from_topic_data(topic_data)
result = mo.generate()
if result["success"]:
print(f"Video created: {result['video_path']}")
from flask import Flask, request, jsonify
@app.route('/generate', methods=['POST'])
def generate_video():
data = request.json
mo = VideoOrchestrator.from_topic_data(data)
result = mo.generate()
return jsonify(result)
The modular design allows easy integration with job queues like Celery for scalable video processing.
The system ensures perfect timing alignment by calculating exact frame durations and handling audio extension when needed.
Multiple TTS engine support ensures the system works across different operating systems and hardware configurations.
Intelligent cleanup and caching prevent memory leaks during batch processing.
Comprehensive error handling with graceful degradation ensures the system continues working even if individual components fail.
The modular architecture enables several exciting possibilities:
Multi-language Support: Additional TTS engines for different languages
Custom Voice Training: Integration with voice cloning technologies
Advanced Visual Effects: More sophisticated image processing and transitions
Content Intelligence: AI-powered topic generation and optimization
Analytics Integration: YouTube Analytics integration for performance tracking
This YouTube Shorts generator demonstrates how thoughtful architecture and component design can create powerful automation tools. By prioritizing local processing, providing multiple fallback options, and maintaining detailed performance tracking, the system achieves both reliability and efficiency.
The project serves as an excellent example of:
Modular Python Architecture: Clean separation of concerns
Graceful Degradation: Multiple fallback options for each component
Performance Optimization: Detailed timing analysis and resource management
Developer Experience: Comprehensive setup scripts and documentation
Whether you're looking to automate content creation, learn about video processing pipelines, or explore text-to-speech integration, this project provides a solid foundation and demonstrates best practices in Python automation development.
GitHub Repository License: MIT (Open Source)
The complete source code, setup instructions, and detailed documentation are available in the GitHub repository. Feel free to contribute, fork, or adapt the project for your specific needs!