Skip to content

crazycazy444/Douglas

Repository files navigation

AI Video Automation System

A complete end-to-end serverless automation system that generates video ideas, creates realistic AI talking head videos, and automatically posts them to YouTube multiple times per day.

Features

  • 🤖 Automated Idea Generation: Uses GPT-4 to generate engaging video ideas based on customizable topics
  • 🎥 AI Video Creation: Integrates with HeyGen or D-ID to create realistic talking head videos
  • 📺 YouTube Auto-Posting: Automatically uploads videos to YouTube with metadata and scheduling
  • Configurable Scheduling: Post videos multiple times per day at specified times
  • 📊 Status Tracking: Complete visibility into idea generation, video creation, and upload status
  • 🔄 Error Handling: Automatic retries and comprehensive error logging
  • 🛠️ API Control: REST API for manual triggering and configuration
  • ☁️ Serverless: Built on AWS Lambda, DynamoDB, and EventBridge for scalability

Architecture

Components

  1. Idea Generator Service (src/idea_generator/)

    • Uses OpenAI GPT-4 to generate creative video ideas
    • Stores ideas with scripts, titles, descriptions, and tags in DynamoDB
  2. Video Generator Service (src/video_generator/)

    • Integrates with HeyGen or D-ID APIs
    • Creates realistic talking head videos from scripts
    • Tracks video generation status asynchronously
  3. YouTube Uploader Service (src/youtube_uploader/)

    • Uploads completed videos to YouTube
    • Handles OAuth authentication
    • Sets video metadata (title, description, tags)
  4. Orchestrator/Scheduler (src/scheduler/)

    • Coordinates all services
    • Manages the end-to-end workflow
  5. API Gateway (src/api/)

    • REST API for configuration and manual triggering
    • Status monitoring endpoints

Data Flow

1. EventBridge triggers Orchestrator (daily)
   ↓
2. Orchestrator → Idea Generator
   ↓ (Generates 5-8 video ideas)
3. Ideas stored in DynamoDB
   ↓
4. EventBridge triggers Video Generator (every 2 hours)
   ↓
5. Video Generator picks pending ideas → sends to HeyGen/D-ID
   ↓ (Async video generation)
6. Status Check (every 15 minutes) → Updates video status
   ↓
7. EventBridge triggers Upload (at scheduled times: 9am, 1pm, 5pm, 9pm)
   ↓
8. YouTube Uploader → Downloads video → Uploads to YouTube
   ↓
9. Video published ✅

Prerequisites

Required API Keys

  1. OpenAI API Key: For idea generation

  2. Video API Provider (choose one):

  3. YouTube OAuth Credentials:

    • Create a project in Google Cloud Console
    • Enable YouTube Data API v3
    • Create OAuth 2.0 credentials
    • Get Client ID and Client Secret
    • Generate refresh token (see YouTube Setup)

AWS Requirements

  • AWS Account with appropriate permissions
  • AWS CLI configured
  • Terraform >= 1.0

Installation

1. Clone and Install Dependencies

git clone <repository-url>
cd <repository>
pip install -r requirements.txt

2. YouTube OAuth Setup

Before deploying, you need to obtain a YouTube refresh token:

# Install required package
pip install google-auth-oauthlib

# Run the OAuth flow helper script
python scripts/get_youtube_token.py

This will:

  1. Open your browser for YouTube authorization
  2. Generate a refresh token
  3. Display the token to use in Terraform variables

3. Configure Terraform Variables

Create terraform/terraform.tfvars:

# AWS Configuration
aws_region   = "us-east-1"
project_name = "video-automation"

# API Keys
openai_api_key = "sk-..."

# Choose ONE video provider
video_api_provider = "heygen"  # or "did"
heygen_api_key     = "..."     # If using HeyGen
# did_api_key      = "..."     # If using D-ID

# YouTube OAuth
youtube_client_id     = "....apps.googleusercontent.com"
youtube_client_secret = "..."
youtube_refresh_token = "..."

# Configuration
default_topic  = "technology trends"
posting_times  = "09:00,13:00,17:00,21:00"  # UTC times

4. Deploy Infrastructure

cd terraform
terraform init
terraform plan
terraform apply

This will create:

  • 4 DynamoDB tables
  • 5 Lambda functions
  • API Gateway with HTTP API
  • EventBridge schedules for automation
  • IAM roles and permissions
  • CloudWatch log groups

5. Verify Deployment

# Get the API endpoint
terraform output api_endpoint

# Test health check
curl $(terraform output -raw api_endpoint)/health

# Check system status
curl $(terraform output -raw api_endpoint)/status

Usage

Automatic Operation

Once deployed, the system runs automatically:

  1. Daily at 8:00 AM UTC: Generates 8 new video ideas
  2. Every 2 hours: Initiates video generation for pending ideas
  3. Every 15 minutes: Checks status of in-progress videos
  4. At scheduled times (9am, 1pm, 5pm, 9pm UTC): Uploads completed videos to YouTube

Manual API Control

Set Topic

curl -X POST https://your-api-endpoint/config/topic \
  -H "Content-Type: application/json" \
  -d '{"topic": "artificial intelligence breakthroughs"}'

Get Current Topic

curl https://your-api-endpoint/config/topic

Trigger Idea Generation

curl -X POST https://your-api-endpoint/trigger/ideas \
  -H "Content-Type: application/json" \
  -d '{"topic": "space exploration", "count": 5}'

Trigger Video Generation

curl -X POST https://your-api-endpoint/trigger/videos

Trigger YouTube Upload

curl -X POST https://your-api-endpoint/trigger/upload

Check System Status

curl https://your-api-endpoint/status

Response:

{
  "current_topic": "technology trends",
  "pending_ideas": 3,
  "generating_videos": 2,
  "completed_videos": 1,
  "pending_uploads": 0,
  "published_videos": 5
}

Configuration

Posting Schedule

Modify posting_times in terraform.tfvars:

posting_times = "06:00,10:00,14:00,18:00,22:00"  # UTC times

After changing, run:

terraform apply

Video Provider Settings

HeyGen Configuration

Default avatar and voice can be customized in src/video_generator/providers/heygen.py:

"avatar_id": "Angela-inblackskirt-20220820",
"voice_id": "1bd001e7e50f421d891986aad5158bc8"

D-ID Configuration

Default presenter and voice in src/video_generator/providers/did.py:

"presenter_image": "https://...",
"voice_id": "en-US-JennyNeural"

Retry Configuration

Modify max_retries in src/common/config.py (default: 3)

Monitoring

CloudWatch Logs

View logs for each component:

# Idea Generator
aws logs tail /aws/lambda/video-automation-idea-generator --follow

# Video Generator
aws logs tail /aws/lambda/video-automation-video-generator --follow

# YouTube Uploader
aws logs tail /aws/lambda/video-automation-youtube-uploader --follow

# Orchestrator
aws logs tail /aws/lambda/video-automation-orchestrator --follow

# API
aws logs tail /aws/lambda/video-automation-api --follow

DynamoDB Tables

Query tables directly:

# List all ideas
aws dynamodb scan --table-name video-automation-ideas

# Check video status
aws dynamodb scan --table-name video-automation-videos

# View upload history
aws dynamodb scan --table-name video-automation-uploads

Troubleshooting

Videos Not Generating

  1. Check video generation logs:

    aws logs tail /aws/lambda/video-automation-video-generator --follow
  2. Verify API key is valid:

    # For HeyGen
    curl -H "X-Api-Key: YOUR_KEY" https://api.heygen.com/v2/avatars
    
    # For D-ID
    curl -H "Authorization: Basic YOUR_KEY" https://api.d-id.com/talks
  3. Check DynamoDB for failed videos:

    aws dynamodb scan --table-name video-automation-videos \
      --filter-expression "status = :status" \
      --expression-attribute-values '{":status":{"S":"failed"}}'

YouTube Upload Failures

  1. Verify OAuth token is valid:

    • Check Lambda environment variables
    • Regenerate refresh token if needed
  2. Check upload logs:

    aws logs tail /aws/lambda/video-automation-youtube-uploader --follow
  3. Verify YouTube API quota:

    • Visit Google Cloud Console → APIs & Services → YouTube Data API v3
    • Check quota usage

Ideas Not Being Generated

  1. Check OpenAI API key and quota
  2. Review idea generator logs
  3. Verify EventBridge rule is enabled:
    aws events describe-rule --name video-automation-idea-generation

Cost Estimation

AWS Costs (approximate)

  • DynamoDB: ~$1-5/month (on-demand pricing)
  • Lambda: ~$5-10/month (depending on video count)
  • API Gateway: ~$1/month
  • CloudWatch Logs: ~$1-2/month

Total AWS: ~$10-20/month

API Costs

  • OpenAI GPT-4: ~$0.03-0.06 per video idea
  • HeyGen: ~$0.50-2.00 per video (depending on plan)
  • D-ID: ~$0.30-1.00 per video (depending on plan)
  • YouTube API: Free (within quota)

For 4 videos/day: ~$60-240/month in API costs

Security Best Practices

  1. Never commit API keys - Use Terraform variables and AWS Secrets Manager
  2. Limit YouTube OAuth scope - Only grant necessary permissions
  3. Enable CloudWatch alarms - Monitor for unusual activity
  4. Rotate credentials regularly - Update API keys and OAuth tokens
  5. Use IAM least privilege - Lambda roles only have necessary permissions

Development

Local Testing

# Set environment variables
export OPENAI_API_KEY="..."
export HEYGEN_API_KEY="..."
# ... other variables

# Test idea generation
python -c "from src.idea_generator.generator import IdeaGenerator; g = IdeaGenerator(); print(g.generate_ideas('AI trends', 2))"

# Test video generation (requires existing idea ID)
python -c "from src.video_generator.generator import VideoGenerator; # ..."

Running Tests

# Install dev dependencies
pip install pytest pytest-mock moto

# Run tests
pytest tests/

Extending the System

Adding New Video Providers

  1. Create new provider class in src/video_generator/providers/
  2. Inherit from VideoProvider base class
  3. Implement required methods
  4. Update VideoGenerator._get_provider() to support new provider

Custom Idea Generation

Modify the prompt in src/idea_generator/generator.py to customize:

  • Video length
  • Content style
  • Target audience
  • Script format

Custom Scheduling

Modify EventBridge rules in terraform/eventbridge.tf:

  • Change cron expressions
  • Add new schedules
  • Create conditional triggers

License

MIT

Support

For issues, questions, or contributions, please open an issue or pull request.

Acknowledgments

  • OpenAI for GPT-4 API
  • HeyGen/D-ID for realistic avatar generation
  • AWS for serverless infrastructure
  • YouTube Data API for video hosting

About

Test

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •