Skip to content

firdavsDev/fastapi-video-streamer-saas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎬 Video Streaming Backend with FastAPI

A secure, scalable video streaming service built with FastAPI, Celery, and MinIO for custom video hosting and streaming.

πŸ“ Project Structure

.
β”œβ”€β”€ .env
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ api
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ auth.py
β”‚   β”‚   └── videos.py
β”‚   β”œβ”€β”€ core
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ config.py
β”‚   β”‚   β”œβ”€β”€ database.py
β”‚   β”‚   └── security.py
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ models
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── video.py
β”‚   β”œβ”€β”€ services
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ minio_service.py
β”‚   β”‚   └── video_service.py
β”‚   β”œβ”€β”€ storage
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── local_storage.p
β”‚   └── workers
β”‚       β”œβ”€β”€ __init__.py
β”‚       └── video_worker.py
β”œβ”€β”€ celery_worker
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ tasks.py
β”‚   └── worker.py
β”œβ”€β”€ docker
β”‚   β”œβ”€β”€ docker-compose.yml
β”‚   └── Dockerfile
β”œβ”€β”€ frontend
β”‚   β”œβ”€β”€ video-player.html
β”‚   └── video-player.js
β”œβ”€β”€ README.md
└── requirements.txt

πŸš€ Quick Start

1. Environment Setup

Copy the example environment file and configure your settings:

cp .env.example .env
# Edit .env with your specific configuration

2. Install Dependencies

pip install -r requirements.txt

3. Start Services with Docker

# Start all services (FastAPI, Celery, MinIO, Redis)
docker-compose -f docker/docker-compose.yml up --build --remove-orphans

# Or run locally for development
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

4. Access Services

πŸ”Œ API Endpoints

Authentication

  • POST /auth/login - Admin login
  • POST /auth/refresh - Refresh JWT token

Video Management

  • GET /videos/ - List all videos (admin only)
  • POST /videos/upload - Upload new video (admin only)
  • GET /videos/{video_id}/status - Check upload status
  • GET /videos/{video_id}/stream - Stream video (blob response)
  • DELETE /videos/{video_id} - Delete video (admin only)

Video Streaming

  • GET /videos/{video_id}/stream - Secure video streaming endpoint
  • GET /videos/{video_id}/thumbnail - Get video thumbnail
  • POST /videos/{video_id}/progress - Save video progress

πŸ“Š Video Upload Flow

  1. Admin uploads video via /videos/upload
  2. Celery task processes upload asynchronously
  3. Status tracking via /videos/{video_id}/status
  4. Secure streaming via /videos/{video_id}/stream

πŸ”’ Security Features

  • JWT Authentication for admin operations
  • Blob streaming prevents direct file access
  • Presigned URLs for temporary access
  • Rate limiting on streaming endpoints
  • File validation (size, format, duration)

🎯 Video Resume Feature

Videos automatically resume from the last watched position using:

  • Frontend localStorage for position tracking
  • Server-side progress API for cross-device sync
  • Blob streaming with range requests

πŸ“± Frontend Integration

// Example: Streaming video in React
const VideoPlayer = ({ videoId }) => {
  const videoRef = useRef(null);
  
  useEffect(() => {
    const streamVideo = async () => {
      const response = await fetch(`/videos/${videoId}/stream`, {
        headers: { 'Authorization': `Bearer ${token}` }
      });
      const blob = await response.blob();
      const videoUrl = URL.createObjectURL(blob);
      
      if (videoRef.current) {
        videoRef.current.src = videoUrl;
        // Resume from saved position
        const savedTime = localStorage.getItem(`video_${videoId}_time`);
        if (savedTime) {
          videoRef.current.currentTime = parseFloat(savedTime);
        }
      }
    };
    
    streamVideo();
  }, [videoId]);
  
  return (
    <video 
      ref={videoRef}
      controls
      onTimeUpdate={(e) => {
        // Save progress
        localStorage.setItem(`video_${videoId}_time`, e.target.currentTime);
      }}
    />
  );
};

🐳 Docker Services

The docker-compose setup includes:

  • FastAPI app (port 8000)
  • Celery worker for background tasks
  • Redis for Celery broker
  • MinIO for object storage (port 9000/9001)
  • PostgreSQL for production database

πŸ› οΈ Development

Running Tests

pytest tests/

Database Migrations

alembic upgrade head

Monitoring

πŸ“ˆ Production Considerations

  • Replace SQLite with PostgreSQL
  • Use AWS S3 instead of MinIO
  • Implement CDN for video delivery
  • Add video transcoding pipeline
  • Set up monitoring and logging
  • Configure SSL/TLS certificates

🀝 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.