DEV Community

fu-_ry17
fu-_ry17

Posted on

Building a Video-to-MP3 Converter with FastAPI Microservices

🎡 Video to MP3 Converter

A modern, scalable microservices-based video to MP3 conversion platform built with FastAPI, featuring user authentication, file processing, and email notifications.

πŸ—οΈ Architecture

This project follows a microservices architecture pattern with the following services:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Gateway       β”‚    β”‚   Auth Service  β”‚    β”‚  Media Service  β”‚
β”‚   (Port 8000)   │◄──►│   (Port 5000)   β”‚    β”‚   (Port 7000)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Convertor       β”‚    β”‚ Notification    β”‚    β”‚   RabbitMQ      β”‚
β”‚ Service         β”‚    β”‚ Service         β”‚    β”‚   Message       β”‚
β”‚ (Port 4000)     β”‚    β”‚ (Port 6000)     β”‚    β”‚   Broker        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MongoDB       β”‚    β”‚   PostgreSQL    β”‚    β”‚   Mongo Express β”‚
β”‚   (Port 27017)  β”‚    β”‚   (Port 5433)   β”‚    β”‚   (Port 8081)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

πŸš€ Features

  • User Authentication: JWT-based authentication with registration and login
  • Video Upload: Secure file upload with GridFS storage
  • Video Processing: Asynchronous video to MP3 conversion using FFmpeg
  • Email Notifications: Automated welcome and conversion completion emails
  • API Gateway: Centralized routing and request handling
  • Microservices: Scalable, independent service architecture
  • Message Queuing: RabbitMQ for asynchronous communication
  • Database Support: PostgreSQL for user data, MongoDB for file storage

πŸ› οΈ Tech Stack

Backend Services

  • FastAPI - Modern, fast web framework for building APIs
  • Python 3.13 - Programming language
  • SQLModel - Database ORM with type safety
  • Pydantic - Data validation and settings management

Databases

  • PostgreSQL 17 - Relational database for user management
  • MongoDB 6.0 - Document database with GridFS for file storage

Message Queue

  • RabbitMQ 3.12 - Message broker for asynchronous communication

Media Processing

  • FFmpeg - Video/audio processing and conversion

Email Service

  • FastAPI-Mail - Email sending capabilities

Infrastructure

  • Docker & Docker Compose - Containerization and orchestration
  • Uvicorn - ASGI server for FastAPI applications

πŸ“ Project Structure

video-mp3-convertor/
β”œβ”€β”€ docker-compose.dev.yml          # Development environment setup
└── services/
    β”œβ”€β”€ auth/                       # Authentication service
    β”‚   β”œβ”€β”€ src/
    β”‚   β”‚   β”œβ”€β”€ auth/              # Auth module
    β”‚   β”‚   β”‚   β”œβ”€β”€ routes.py      # API endpoints
    β”‚   β”‚   β”‚   β”œβ”€β”€ service.py     # Business logic
    β”‚   β”‚   β”‚   β”œβ”€β”€ schema.py      # Data models
    β”‚   β”‚   β”‚   └── utils.py       # Utilities (JWT, hashing)
    β”‚   β”‚   └── db/                # Database configuration
    β”‚   └── requirements.txt
    β”œβ”€β”€ convertor/                  # Video conversion service
    β”‚   β”œβ”€β”€ main.py                # Service entry point
    β”‚   β”œβ”€β”€ mp3_consumer.py        # RabbitMQ consumer
    β”‚   └── requirements.txt
    β”œβ”€β”€ gateway/                    # API Gateway
    β”‚   β”œβ”€β”€ main.py                # Proxy service
    β”‚   └── requirements.txt
    β”œβ”€β”€ media/                      # Media handling service
    β”‚   β”œβ”€β”€ src/
    β”‚   β”‚   β”œβ”€β”€ media/             # Media module
    β”‚   β”‚   β”‚   β”œβ”€β”€ routes.py      # Upload/download endpoints
    β”‚   β”‚   β”‚   └── service.py     # File operations
    β”‚   β”‚   └── auth.py            # Authentication middleware
    β”‚   └── requirements.txt
    └── notification/               # Email notification service
        β”œβ”€β”€ src/
        β”‚   β”œβ”€β”€ consumer/          # Message consumers
        β”‚   β”‚   β”œβ”€β”€ mp3_consumer.py    # Conversion notifications
        β”‚   β”‚   └── user_consumer.py   # Welcome emails
        β”‚   └── mail.py            # Email configuration
        └── requirements.txt
Enter fullscreen mode Exit fullscreen mode

πŸš€ Quick Start

Prerequisites

  • Docker and Docker Compose
  • Git

Installation

  1. Clone the repository
   git clone <repository-url>
   cd video-mp3-convertor
Enter fullscreen mode Exit fullscreen mode
  1. Start the services
   docker-compose -f docker-compose.dev.yml up --build
Enter fullscreen mode Exit fullscreen mode
  1. Access the services

πŸ“š API Documentation

Authentication Endpoints

Register User

POST /auth/sign-up
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "securepassword"
}
Enter fullscreen mode Exit fullscreen mode

Login User

POST /auth/sign-in
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "securepassword"
}
Enter fullscreen mode Exit fullscreen mode

Verify Token

GET /auth/verify
Authorization: Bearer <access_token>
Enter fullscreen mode Exit fullscreen mode

Media Endpoints

Upload Video

POST /media/upload
Authorization: Bearer <access_token>
Content-Type: multipart/form-data

file: <video_file>
Enter fullscreen mode Exit fullscreen mode

Download MP3

GET /media/download/{file_id}
Authorization: Bearer <access_token>
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Workflow

  1. User Registration/Login: Users authenticate through the auth service
  2. Video Upload: Users upload videos through the media service
  3. File Storage: Videos are stored in MongoDB using GridFS
  4. Message Queue: Upload event triggers RabbitMQ message
  5. Video Processing: Convertor service processes videos to MP3 using FFmpeg
  6. Notification: Email notification sent when conversion is complete
  7. Download: Users can download converted MP3 files

🐳 Docker Services

Service Port Description
Gateway 8000 API Gateway and routing
Auth 5000 User authentication
Media 7000 File upload/download
Convertor 4000 Video processing
Notification 6000 Email notifications
PostgreSQL 5433 User database
MongoDB 27017 File storage
RabbitMQ 5672 Message broker
RabbitMQ Management 15672 Message queue UI
Mongo Express 8081 MongoDB UI

πŸ”§ Environment Variables

Auth Service

  • JWT_SECRET: Secret key for JWT token generation
  • DATABASE_URL: PostgreSQL connection string
  • RABBITMQ_URL: RabbitMQ connection string

Media Service

  • RABBITMQ_URL: RabbitMQ connection string
  • MONGODB_URL: MongoDB connection string
  • AUTH_URL: Auth service URL for token validation

Convertor Service

  • RABBITMQ_URL: RabbitMQ connection string
  • MONGODB_URL: MongoDB connection string

Gateway Service

  • AUTH_URL: Auth service URL
  • MEDIA_URL: Media service URL

πŸ§ͺ Development

Running Individual Services

Each service can be run independently:

# Auth Service
cd services/auth
python -m uvicorn src.main:app --host 0.0.0.0 --port 5000

# Media Service
cd services/media
python -m uvicorn src.main:app --host 0.0.0.0 --port 7000

# Gateway Service
cd services/gateway
python -m uvicorn main:app --host 0.0.0.0 --port 8000
Enter fullscreen mode Exit fullscreen mode

Database Migrations

The auth service uses SQLModel with automatic table creation. For production, consider using Alembic for migrations.

πŸ”’ Security Features

  • JWT-based authentication with access and refresh tokens
  • Password hashing using bcrypt
  • Token-based authorization for protected endpoints
  • Secure file upload with validation
  • Environment-based configuration

πŸ“ˆ Scalability

  • Microservices architecture allows independent scaling
  • Message queue enables asynchronous processing
  • Stateless services for horizontal scaling
  • Database separation for different data types

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

🎯 Future Enhancements

  • [ ] WebSocket support for real-time conversion progress
  • [ ] Multiple output formats (WAV, AAC, etc.)
  • [ ] Batch processing capabilities
  • [ ] User dashboard with conversion history
  • [ ] File size and format validation
  • [ ] Rate limiting and usage quotas
  • [ ] Cloud storage integration (AWS S3, Google Cloud)
  • [ ] Kubernetes deployment configuration
  • [ ] Monitoring and logging with Prometheus/Grafana
  • [ ] CI/CD pipeline setup

Built with ❀️ using FastAPI and modern Python practices

Top comments (0)