Skip to content

monk-io/video-hosting-example

Repository files navigation

🎬 Video Processing & Sharing Platform

A complete YouTube-like video processing and sharing platform built with modern technologies, featuring parallel video processing workers, FFmpeg transcoding, thumbnail generation, and containerized deployment.

πŸ—οΈ Architecture

This platform follows a microservices architecture with distributed video processing:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   React Frontend β”‚    β”‚   Go Backend API β”‚    β”‚  Video Workers  β”‚
β”‚   (Port: 3000)  │◄──►│   (Port: 8080)   │◄──►│  (Parallel)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                        β”‚                        β”‚
         β”‚                        β–Ό                        β”‚
         β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”‚
         β”‚              β”‚     MongoDB     β”‚                β”‚
         β”‚              β”‚  (Port: 27017)  β”‚                β”‚
         β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                β”‚
         β”‚                        β”‚                        β”‚
         β”‚                        β–Ό                        β”‚
         β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”‚
         └─────────────►│   Redis Queue   β”‚β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚   (Port: 6379)  β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                                 β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚      MinIO      β”‚
                       β”‚   (Port: 9000)  β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Technology Stack

  • Frontend: React 18 + TypeScript + Tailwind CSS
  • Backend: Go 1.21 + Gin + Domain-Driven Design (DDD)
  • Workers: Go + FFmpeg for video processing
  • Database: MongoDB 7.0 (metadata, user data)
  • Queue: Redis 7.0 (distributed job processing)
  • Storage: MinIO (S3-compatible, video files)
  • Processing: FFmpeg (video transcoding & thumbnails)
  • Deployment: Docker Compose

πŸš€ Quick Start

Prerequisites

  • Docker & Docker Compose
  • Git

Installation

  1. Clone the repository

    git clone <repository-url>
    cd youtube-example
  2. Set up environment variables

    cp .env.example .env
    # Edit .env with your preferred settings if needed
  3. Start all services

    docker-compose up -d
  4. Wait for initialization (about 30-60 seconds)

    # Check service status
    docker-compose ps
    
    # View logs
    docker-compose logs -f
  5. Access the application

🎯 Features

βœ… Fully Implemented

πŸŽ₯ Video Processing Pipeline

  • βœ… Video Upload: Multipart upload with validation
  • βœ… Distributed Processing: Redis job queue with parallel workers
  • βœ… FFmpeg Transcoding: 480p, 720p, 1080p quality options
  • βœ… Thumbnail Generation: Automatic thumbnail creation
  • βœ… Progress Tracking: Real-time job status monitoring
  • βœ… File Storage: Organized MinIO structure (original/processed/thumbnails)

πŸ”§ Backend Infrastructure

  • βœ… RESTful API: Complete CRUD operations
  • βœ… Domain-Driven Design: Clean architecture
  • βœ… Job Queue: Redis-based distributed processing
  • βœ… Database Operations: MongoDB with proper indexing
  • βœ… File Streaming: Direct video streaming with quality selection

πŸ—οΈ DevOps & Deployment

  • βœ… Docker Compose: Complete containerized stack
  • βœ… Auto-initialization: MinIO buckets and permissions
  • βœ… Health Checks: Service monitoring endpoints
  • βœ… Structured Logging: Comprehensive error tracking

🚧 Next Phase (Ready for Implementation)

  • React Frontend: Upload interface and video player
  • User Authentication: JWT-based auth system
  • Real-time Updates: WebSocket progress notifications
  • Advanced Features: Video search, playlists, comments

πŸ› οΈ API Endpoints

Videos

# Upload video
POST   /api/v1/videos/upload
curl -X POST -F "[email protected]" -F "title=My Video" http://localhost:8080/api/v1/videos/upload

# List videos (paginated)
GET    /api/v1/videos?page=1&limit=20
curl http://localhost:8080/api/v1/videos

# Get video details
GET    /api/v1/videos/:id
curl http://localhost:8080/api/v1/videos/64a7b8c9d1e2f3a4b5c6d7e8

# Stream video (original or processed)
GET    /api/v1/videos/:id/stream?quality=720p
curl http://localhost:8080/api/v1/videos/64a7b8c9d1e2f3a4b5c6d7e8/stream

# Trigger manual processing
POST   /api/v1/videos/:id/process
curl -X POST http://localhost:8080/api/v1/videos/64a7b8c9d1e2f3a4b5c6d7e8/process

Jobs

# Get job status
GET    /api/v1/jobs/:id
curl http://localhost:8080/api/v1/jobs/64a7b8c9d1e2f3a4b5c6d7e9

# Get all jobs for a video
GET    /api/v1/jobs/video/:videoId
curl http://localhost:8080/api/v1/jobs/video/64a7b8c9d1e2f3a4b5c6d7e8

# Get active processing jobs
GET    /api/v1/jobs/active
curl http://localhost:8080/api/v1/jobs/active

Health

# Service health check
GET    /health
curl http://localhost:8080/health

πŸ§ͺ Testing

Automated Test

Run the complete test pipeline:

./scripts/test-upload.sh

This script will:

  1. Create a test video (if FFmpeg is available)
  2. Upload the video via API
  3. Monitor processing jobs
  4. Show final results

Manual Testing

# 1. Check API health
curl http://localhost:8080/health

# 2. Upload a video
curl -X POST \
  -F "[email protected]" \
  -F "title=Test Video" \
  -F "description=Test upload" \
  http://localhost:8080/api/v1/videos/upload

# 3. Monitor processing
curl http://localhost:8080/api/v1/jobs/active

# 4. Check final video status
curl http://localhost:8080/api/v1/videos/{VIDEO_ID}

πŸ“Š Video Processing Flow

  1. Upload β†’ Video uploaded to MinIO (videos/original/)
  2. Job Creation β†’ 4 jobs created (1 thumbnail + 3 transcode jobs)
  3. Queue Distribution β†’ Jobs sent to Redis queue
  4. Worker Processing β†’ 2 parallel workers process jobs
  5. FFmpeg Processing β†’ Videos transcoded to multiple formats
  6. Storage β†’ Processed files saved (videos/processed/, thumbnails/)
  7. Database Update β†’ Video metadata updated with new formats
  8. Completion β†’ Video status changed to "ready"

πŸ”§ Configuration

Environment Variables

Variable Description Default
PORT Backend server port 8080
MONGODB_URI MongoDB connection string mongodb://admin:password@mongodb:27017/youtube?authSource=admin
REDIS_URI Redis connection string redis://redis:6379
MINIO_ENDPOINT MinIO server endpoint minio:9000
MINIO_ACCESS_KEY MinIO access key minioadmin
MINIO_SECRET_KEY MinIO secret key minioadmin
FRONTEND_URL Frontend URL for CORS http://localhost:3000
WORKER_ID Unique worker identifier Auto-generated

Video Processing Settings

  • Supported Input Formats: MP4, AVI, MOV, WMV, FLV, WebM, MKV
  • Output Formats:
    • 480p: H.264, 1Mbps max bitrate
    • 720p: H.264, 2.5Mbps max bitrate
    • 1080p: H.264, 4.5Mbps max bitrate
  • Audio: AAC, 128kbps
  • Max File Size: 1GB
  • Parallel Workers: 2 (configurable)

πŸ“ Project Structure

youtube-example/
β”œβ”€β”€ backend/                 # Go backend API (22 files)
β”‚   β”œβ”€β”€ internal/
β”‚   β”‚   β”œβ”€β”€ domain/         # Business logic (DDD)
β”‚   β”‚   β”‚   β”œβ”€β”€ entities/   # Video, Job, User entities
β”‚   β”‚   β”‚   β”œβ”€β”€ repositories/ # Data access interfaces
β”‚   β”‚   β”‚   └── services/   # Business services
β”‚   β”‚   β”œβ”€β”€ infrastructure/ # External dependencies
β”‚   β”‚   β”‚   β”œβ”€β”€ database/   # MongoDB client
β”‚   β”‚   β”‚   β”œβ”€β”€ storage/    # MinIO client
β”‚   β”‚   β”‚   β”œβ”€β”€ queue/      # Redis client & job publisher
β”‚   β”‚   β”‚   └── repositories/ # Repository implementations
β”‚   β”‚   β”œβ”€β”€ application/    # Application services
β”‚   β”‚   β”‚   └── handlers/   # HTTP handlers
β”‚   β”‚   └── interfaces/     # API layer
β”‚   β”‚       β”œβ”€β”€ http/       # Routes and middleware
β”‚   β”‚       └── middleware/ # CORS, logging
β”‚   └── pkg/               # Shared packages
β”œβ”€β”€ worker/                 # Video processing workers (10 files)
β”‚   β”œβ”€β”€ internal/
β”‚   β”‚   β”œβ”€β”€ processor/     # FFmpeg transcoding logic
β”‚   β”‚   β”œβ”€β”€ storage/       # MinIO client
β”‚   β”‚   └── queue/         # Redis & MongoDB clients
β”‚   └── pkg/              # Worker configuration
β”œβ”€β”€ frontend/              # React frontend (future)
β”œβ”€β”€ scripts/               # Setup and testing scripts
β”‚   β”œβ”€β”€ setup.sh          # Complete platform setup
β”‚   β”œβ”€β”€ test-upload.sh    # Video upload testing
β”‚   └── init-minio.sh     # MinIO initialization
β”œβ”€β”€ docker-compose.yml    # Container orchestration
└── .env.example         # Environment configuration

πŸƒβ€β™‚οΈ Development

Backend Development

cd backend
go mod tidy
go run main.go

Worker Development

cd worker
go mod tidy
go run main.go

Scaling Workers

# Scale to 4 workers
docker-compose up --scale worker1=2 --scale worker2=2

πŸ“Š Monitoring

Container Status

docker-compose ps

Logs

# All services
docker-compose logs -f

# Specific services
docker-compose logs -f backend worker1 worker2

MinIO Console

Database Access

# MongoDB shell
docker exec -it youtube_mongodb mongosh --username admin --password password --authenticationDatabase admin

# Redis CLI
docker exec -it youtube_redis redis-cli

πŸ”’ Security Considerations

  • βœ… File validation and size limits
  • βœ… CORS configuration
  • βœ… Error handling and logging
  • 🚧 Authentication system (planned)
  • 🚧 Rate limiting (planned)
  • 🚧 File encryption (planned)

πŸš€ Production Deployment

Recommended Setup

  1. Container Orchestration: Kubernetes or Docker Swarm
  2. Load Balancer: Nginx or HAProxy
  3. Database: MongoDB Atlas or self-hosted cluster
  4. Storage: AWS S3 or distributed MinIO
  5. Monitoring: Prometheus + Grafana
  6. Logging: ELK Stack

Performance Optimizations

  • Horizontal scaling of workers
  • CDN for video delivery
  • Database indexing
  • Connection pooling
  • Caching strategies

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License.


πŸŽ‰ Current Status: Production-Ready Backend & Workers

βœ… Complete video processing pipeline with distributed workers
βœ… FFmpeg transcoding and thumbnail generation
βœ… RESTful API with job monitoring
βœ… Containerized deployment with Docker Compose
🚧 React frontend ready for implementation

Built with ❀️ using Go, FFmpeg, MongoDB, Redis, and MinIO.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •