355 lines
7.0 KiB
Markdown
355 lines
7.0 KiB
Markdown
# Private Studio - Video Transcoding Web Service
|
|
|
|
A FastAPI-based video transcoding management service with S3-compatible file uploads, API key authentication, and per-user resource isolation.
|
|
|
|
## Features
|
|
|
|
- **S3-Compatible Interface**: Upload, download, delete, and list files using S3-compatible HTTP endpoints
|
|
- **API Key Authentication**: Secure bearer token authentication with per-user isolation
|
|
- **YAML-Based Transcode API**: Request transcode jobs and manage encoding profiles via YAML
|
|
- **SQLite Database**: Lightweight file-based database with SQLAlchemy ORM
|
|
- **User Management**: Create users and API keys via CLI
|
|
|
|
## Installation
|
|
|
|
Install the package with all dependencies:
|
|
|
|
```bash
|
|
pip install -e .
|
|
```
|
|
|
|
Or install development dependencies:
|
|
|
|
```bash
|
|
pip install -e ".[dev]"
|
|
```
|
|
|
|
## Quick Start
|
|
|
|
### 1. Initialize Database
|
|
|
|
```bash
|
|
ps-serve init
|
|
```
|
|
|
|
This creates a `transcoding.db` file in the current directory.
|
|
|
|
### 2. Create a User
|
|
|
|
```bash
|
|
ps-serve create-user myuser
|
|
```
|
|
|
|
### 3. Create an API Key
|
|
|
|
```bash
|
|
ps-serve create-key 1
|
|
```
|
|
|
|
This outputs an API key that should be kept secret:
|
|
|
|
```
|
|
API key created for user 1
|
|
Key: 4YXDJoXf26auwGFQPKB6aB-kY6HxbkKPJzmWFbs7cnM
|
|
Keep this key safe - you won't be able to see it again!
|
|
```
|
|
|
|
### 4. Start the Server
|
|
|
|
```bash
|
|
ps-serve serve
|
|
```
|
|
|
|
The server will start at `http://localhost:8000`
|
|
|
|
## Configuration
|
|
|
|
Environment variables:
|
|
|
|
- `DATABASE_URL`: SQLite database path (default: `./transcoding.db`)
|
|
- `STORAGE_PATH`: File storage directory (default: `./storage`)
|
|
- `SERVER_HOST`: Server host address (default: `0.0.0.0`)
|
|
- `SERVER_PORT`: Server port (default: `8000`)
|
|
- `SQL_ECHO`: Enable SQL query logging (default: `false`)
|
|
|
|
Example:
|
|
|
|
```bash
|
|
export DATABASE_URL="sqlite:///./my_transcoding.db"
|
|
export STORAGE_PATH="./media"
|
|
ps-serve serve --host 127.0.0.1 --port 8080
|
|
```
|
|
|
|
## API Usage
|
|
|
|
### Authentication
|
|
|
|
All endpoints require an API key via Bearer token:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" http://localhost:8000/api/keys
|
|
```
|
|
|
|
### S3-Compatible File Operations
|
|
|
|
Upload a file:
|
|
|
|
```bash
|
|
curl -X PUT \
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
-F "file=@video.mp4" \
|
|
http://localhost:8000/s3/1/videos/video.mp4
|
|
```
|
|
|
|
Download a file:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/s3/1/videos/video.mp4 \
|
|
-o video.mp4
|
|
```
|
|
|
|
List files:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/s3/1
|
|
```
|
|
|
|
Delete a file:
|
|
|
|
```bash
|
|
curl -X DELETE \
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/s3/1/videos/video.mp4
|
|
```
|
|
|
|
### Transcode Profile Management
|
|
|
|
Create a profile:
|
|
|
|
```bash
|
|
curl -X POST \
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
-H "Content-Type: application/yaml" \
|
|
-d @profile.yaml \
|
|
http://localhost:8000/api/profiles?name=my-profile
|
|
```
|
|
|
|
Where `profile.yaml`:
|
|
|
|
```yaml
|
|
profiles:
|
|
hd:
|
|
resolution: "1280x720"
|
|
bitrate: "2500k"
|
|
fps: 30
|
|
sd:
|
|
resolution: "854x480"
|
|
bitrate: "1000k"
|
|
fps: 30
|
|
```
|
|
|
|
List profiles:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/api/profiles
|
|
```
|
|
|
|
### Transcode Job Requests
|
|
|
|
Request a transcode job with a stored profile:
|
|
|
|
```bash
|
|
curl -X POST \
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
-H "Content-Type: application/yaml" \
|
|
-d 'asset: video.mp4
|
|
profile: my-profile' \
|
|
http://localhost:8000/api/transcode
|
|
```
|
|
|
|
Or with an inline profile:
|
|
|
|
```bash
|
|
curl -X POST \
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
-H "Content-Type: application/yaml" \
|
|
-d 'asset: video.mp4
|
|
profile:
|
|
profiles:
|
|
hd:
|
|
resolution: "1280x720"
|
|
bitrate: "2500k"' \
|
|
http://localhost:8000/api/transcode
|
|
```
|
|
|
|
Get job status:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/api/transcode/1
|
|
```
|
|
|
|
List jobs:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/api/transcode
|
|
```
|
|
|
|
List jobs by status:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/api/transcode?status=pending
|
|
```
|
|
|
|
### API Key Management
|
|
|
|
Create an API key:
|
|
|
|
```bash
|
|
curl -X POST \
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/api/keys
|
|
```
|
|
|
|
List keys:
|
|
|
|
```bash
|
|
curl -H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/api/keys
|
|
```
|
|
|
|
Revoke a key:
|
|
|
|
```bash
|
|
curl -X DELETE \
|
|
-H "Authorization: Bearer YOUR_API_KEY" \
|
|
http://localhost:8000/api/keys/KEY_ID
|
|
```
|
|
|
|
## Testing
|
|
|
|
Run the test suite:
|
|
|
|
```bash
|
|
pytest tests/test_web.py -v
|
|
```
|
|
|
|
## Database Schema
|
|
|
|
### Users
|
|
|
|
- `id`: Primary key
|
|
- `username`: Unique username
|
|
- `created_at`: Creation timestamp
|
|
|
|
### API Keys
|
|
|
|
- `id`: Primary key
|
|
- `key_hash`: SHA256 hash of the API key
|
|
- `user_id`: Foreign key to users
|
|
- `created_at`: Creation timestamp
|
|
- `is_active`: Whether the key is active
|
|
|
|
### Uploaded Assets
|
|
|
|
- `id`: Primary key
|
|
- `user_id`: Foreign key to users
|
|
- `filename`: Original filename
|
|
- `size`: File size in bytes
|
|
- `s3_path`: Path relative to storage root
|
|
- `upload_timestamp`: Upload time
|
|
|
|
### Transcode Profiles
|
|
|
|
- `id`: Primary key
|
|
- `user_id`: Foreign key to users
|
|
- `name`: Profile name
|
|
- `profile_definition`: YAML definition as string
|
|
- `created_at`: Creation timestamp
|
|
|
|
### Transcode Jobs
|
|
|
|
- `id`: Primary key
|
|
- `user_id`: Foreign key to users
|
|
- `asset_id`: Foreign key to uploaded_assets
|
|
- `profile_id`: Foreign key to transcode_profiles (nullable for inline profiles)
|
|
- `status`: Job status (pending, in_progress, completed, failed)
|
|
- `requested_at`: Request timestamp
|
|
- `completed_at`: Completion timestamp (nullable)
|
|
|
|
### Packaging Jobs
|
|
|
|
- `id`: Primary key
|
|
- `user_id`: Foreign key to users
|
|
- `transcode_job_id`: Foreign key to transcode_jobs
|
|
- `status`: Job status (pending, in_progress, completed, failed)
|
|
- `requested_at`: Request timestamp
|
|
- `completed_at`: Completion timestamp (nullable)
|
|
|
|
### Transcoded Assets
|
|
|
|
- `id`: Primary key
|
|
- `user_id`: Foreign key to users
|
|
- `transcode_job_id`: Foreign key to transcode_jobs
|
|
- `filename`: Output filename
|
|
- `s3_path`: Path relative to storage root
|
|
- `format`: Output format (e.g., "mp4", "audio")
|
|
- `created_at`: Creation timestamp
|
|
|
|
## Architecture
|
|
|
|
### Authentication
|
|
|
|
API keys are stored as SHA256 hashes in the database and validated via FastAPI's HTTPBearer security scheme. Each authenticated endpoint receives the user context automatically.
|
|
|
|
### Per-User Isolation
|
|
|
|
All resources (assets, profiles, jobs) are scoped to the authenticated user. Users can only access their own resources, enforced at the database query level.
|
|
|
|
### File Storage
|
|
|
|
Files are stored on disk in user-specific directories under `STORAGE_PATH`:
|
|
|
|
```
|
|
storage/
|
|
├── 1/ (user 1)
|
|
│ ├── video1.mp4
|
|
│ └── video2.mp4
|
|
└── 2/ (user 2)
|
|
└── presentation.mp4
|
|
```
|
|
|
|
Symlink traversal attacks are prevented by validating file paths.
|
|
|
|
## Development
|
|
|
|
### Enable Auto-Reload
|
|
|
|
```bash
|
|
ps-serve serve --reload
|
|
```
|
|
|
|
### View SQL Queries
|
|
|
|
```bash
|
|
export SQL_ECHO=true
|
|
ps-serve serve
|
|
```
|
|
|
|
### Run Tests
|
|
|
|
```bash
|
|
pytest tests/test_web.py -v
|
|
```
|
|
|
|
With coverage:
|
|
|
|
```bash
|
|
pytest tests/test_web.py --cov=privatestudio.web --cov-report=html
|
|
```
|