# Cloudflare R2 Video Migration Cost Projection

## 📊 Current Video Infrastructure Analysis

### Video Configuration
- **Max video size per episode**: 500 MB (configurable limit)
- **Video formats**: MP4 with multiple quality options
- **Quality tiers**: 1080p, 720p, 540p, 360p, 144p
- **CDN sources**: 2+ redundant sources per episode
- **Storage structure**: `{language}/{bookId}/{episode}.mp4`

### Content Volume Estimates

#### Per Episode Breakdown
Based on the codebase analysis and typical video compression patterns:

| Quality | Estimated Size | Target Use |
|---------|---------------|------------|
| 1080p   | 150-300 MB    | Premium streaming |
| 720p    | 80-150 MB     | Standard streaming |
| 540p    | 50-100 MB     | Mobile/bandwidth-limited |
| 360p    | 30-60 MB      | Low bandwidth |
| 144p    | 15-30 MB      | Ultra-low bandwidth |

**Average episode size** (storing 720p only): ~100 MB
**Average episode size** (storing all qualities): ~325 MB

#### Drama Content Estimates
- **Average episodes per drama**: 20-81 episodes (test drama has 81)
- **Typical drama length**: 40-60 episodes
- **Active drama catalog**: Estimated 1,000-5,000 dramas

## 💰 Cloudflare R2 Pricing (Current 2025 Rates)

### Storage Costs
- **Storage**: $0.015 per GB per month
- **No egress fees** (major advantage over AWS S3)
- **Class A Operations** (PUT, POST): $4.50 per million requests
- **Class B Operations** (GET, HEAD): $0.36 per million requests

### Cost Calculation Scenarios

#### Scenario 1: Conservative (1,000 dramas, 720p only)
```
• Dramas: 1,000
• Avg episodes per drama: 50
• Total episodes: 50,000
• Avg size per episode: 100 MB
• Total storage: 5,000 GB (5 TB)

Monthly Costs:
• Storage: 5,000 GB × $0.015 = $75/month
• Upload operations: 50,000 × $4.50/1M = $0.23
• Monthly bandwidth: FREE (R2 advantage)

Annual Cost: $900 + one-time upload fees
```

#### Scenario 2: Moderate (2,500 dramas, 720p only)
```
• Dramas: 2,500  
• Avg episodes per drama: 50
• Total episodes: 125,000
• Avg size per episode: 100 MB
• Total storage: 12,500 GB (12.5 TB)

Monthly Costs:
• Storage: 12,500 GB × $0.015 = $187.50/month
• Upload operations: 125,000 × $4.50/1M = $0.56

Annual Cost: $2,250 + one-time upload fees
```

#### Scenario 3: Large (5,000 dramas, 720p only)
```
• Dramas: 5,000
• Avg episodes per drama: 50  
• Total episodes: 250,000
• Avg size per episode: 100 MB
• Total storage: 25,000 GB (25 TB)

Monthly Costs:
• Storage: 25,000 GB × $0.015 = $375/month
• Upload operations: 250,000 × $4.50/1M = $1.13

Annual Cost: $4,500 + one-time upload fees
```

#### Scenario 4: Premium Multi-Quality (2,500 dramas, all qualities)
```
• Dramas: 2,500
• Avg episodes per drama: 50
• Total episodes: 125,000  
• Avg size per episode: 325 MB (all quality tiers)
• Total storage: 40,625 GB (40.6 TB)

Monthly Costs:
• Storage: 40,625 GB × $0.015 = $609.38/month
• Upload operations: 625,000 files × $4.50/1M = $2.81

Annual Cost: $7,312 + one-time upload fees
```

## 🔄 Migration Process Costs

### One-Time Setup Costs
- **Data transfer from current CDN**: Varies by current provider
- **Upload to R2**: Calculated above in scenarios
- **Development time**: ~40-80 hours for migration tooling
- **Testing & validation**: ~20-40 hours

### Bandwidth During Migration
- **Downloading from source**: Depends on current CDN rates
- **Uploading to R2**: No ingress fees for Cloudflare R2
- **Total transfer time**: ~1-4 weeks depending on content volume

## 📈 Ongoing Operational Costs

### Monthly R2 Costs by Scale

| Content Scale | Storage (TB) | Monthly Cost | Annual Cost |
|---------------|--------------|--------------|-------------|
| Small (1K dramas) | 5 TB | $75 | $900 |
| Medium (2.5K dramas) | 12.5 TB | $188 | $2,250 |
| Large (5K dramas) | 25 TB | $375 | $4,500 |
| Premium Multi-Quality | 40.6 TB | $609 | $7,312 |

### API Operations
- **Regular usage**: ~$5-20/month for typical read operations
- **Heavy traffic periods**: Could increase to $50-100/month

## 🎯 ROI Analysis

### Cost Savings vs Traditional CDN
Cloudflare R2 advantages:
- **No egress fees**: Saves $50-500+/month vs AWS S3
- **Integrated CDN**: Reduces separate CDN costs
- **Global edge locations**: Built-in worldwide distribution

### Break-Even Scenarios
- **Current CDN costs > $75/month**: R2 profitable immediately for small scale
- **Current CDN costs > $375/month**: R2 profitable for large scale
- **Current bandwidth costs > $100/month**: R2 egress savings significant

## 🛠️ Technical Implementation

### Migration Strategy
1. **Phase 1**: Migrate metadata and establish R2 integration
2. **Phase 2**: Bulk transfer existing videos using parallel uploads
3. **Phase 3**: Update application to serve from R2
4. **Phase 4**: Decommission old CDN

### Code Changes Required
- Update `ScrapeDramaBoxDatabase.php` S3 configuration
- Modify `Episode.php` model for R2 URLs
- Update video serving in `episode-page.blade.php`
- Configure R2 credentials in `.env`

## 📋 Recommendations

### Recommended Starting Point: **Scenario 2 (Moderate)**
- **2,500 dramas with 720p quality**
- **Annual cost**: ~$2,250
- **Storage**: 12.5 TB
- **Monthly**: $188

### Migration Priority
1. **Start with most popular content** (optimize cost/benefit)
2. **Use 720p as primary quality** (good balance of quality/cost)
3. **Add higher qualities incrementally** based on user demand
4. **Implement lazy loading** for less popular content

### Cost Optimization Tips
- **Smart quality selection**: Store only 720p initially, add others on-demand
- **Compression optimization**: Re-encode videos for better compression ratios
- **Analytics-driven storage**: Keep popular content in R2, others in cheaper storage
- **Batch operations**: Minimize API operation costs through bulk processing

## 🎊 Final Cost Projection

**For a typical deployment (2,500 dramas, 720p):**
- **Year 1**: $2,250 + $500 migration costs = **$2,750**
- **Year 2+**: $2,250 annually
- **Cost per drama**: ~$0.90/year
- **Cost per episode**: ~$0.018/year

**ROI**: If current CDN costs exceed $188/month, R2 migration pays for itself immediately plus provides zero egress fees advantage.

---

*Cost estimates based on October 2025 Cloudflare R2 pricing. Actual costs may vary based on usage patterns, content compression, and pricing changes.*