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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# Architecture

This guide explains how SupoClip is structured and how a task moves through the system.

## High-Level System

SupoClip is a multi-service application built around asynchronous video processing.

```text
Browser
  -> Frontend (Next.js)
  -> Frontend API routes
  -> Backend (FastAPI)
  -> Redis queue
  -> Worker (ARQ)
  -> PostgreSQL and file storage
```

The key design choice is that task creation is fast, while clip generation runs out of band in a worker.

## Runtime Components

### Frontend

Location:

- `frontend/src/app`
- `frontend/src/components`
- `frontend/src/lib`

Responsibilities:

- Authentication UI
- Task creation UI
- Task list and clip editing UI
- Admin dashboard
- Billing UI and webhooks
- Server-side API proxies to the backend

Technology:

- Next.js 15
- React 19
- Tailwind CSS
- Better Auth
- Prisma

### Backend API

Location:

- `backend/src/main.py`
- `backend/src/api/routes`
- `backend/src/services`
- `backend/src/repositories`

Responsibilities:

- Accept and validate requests
- Create and update tasks
- Manage clip editing actions
- Serve fonts, transitions, upload endpoints, and clip files
- Expose progress streams
- Handle feedback, billing support, and admin flows

Technology:

- FastAPI
- Async request handling
- Repository and service layering

### Worker

Location:

- `backend/src/workers`

Responsibilities:

- Poll jobs from Redis
- Execute long-running video processing
- Publish progress updates
- Write final clip records back to PostgreSQL

Technology:

- ARQ
- Redis

### PostgreSQL

Primary responsibilities:

- Users and sessions
- Task records
- Source records
- Generated clip metadata
- Billing metadata
- Processing cache

Schema bootstrap lives in `init.sql`.

### Redis

Primary responsibilities:

- Background queue transport
- Real-time progress plumbing
- Operational coordination for task state

## Repository Structure

Current top-level layout:

- `backend/`
- `frontend/`
- `docker-compose.yml`
- `init.sql`
- `.env.example`
- `start.sh`

## Backend Architecture

The backend follows a layered pattern.

### Routes

Directory:

- `backend/src/api/routes`

Responsibilities:

- HTTP request parsing
- Route-level authorization
- Response formatting

Main route groups:

- `tasks.py`
- `media.py`
- `billing.py`
- `feedback.py`
- `admin.py`

### Services

Directory:

- `backend/src/services`

Responsibilities:

- Orchestration
- Business logic
- Coordinating repositories and processing modules

Important services:

- `task_service.py`
- `video_service.py`
- `billing_service.py`
- `subscription_email_service.py`

### Repositories

Directory:

- `backend/src/repositories`

Responsibilities:

- Direct database access
- Raw query execution
- Encapsulated persistence logic

Important repositories:

- `task_repository.py`
- `clip_repository.py`
- `source_repository.py`
- `cache_repository.py`

### Utility and domain modules

Important backend modules:

- `ai.py`
  - Prompting and structured LLM output
- `video_utils.py`
  - Rendering, cropping, and subtitle logic
- `clip_editor.py`
  - Post-generation clip edits and exports
- `caption_templates.py`
  - Available subtitle template definitions
- `broll.py`
  - Optional Pexels integration
- `font_registry.py`
  - Font discovery and registration
- `observability.py`
  - Metrics and timing helpers

## Frontend Architecture

The frontend uses the App Router and keeps most product pages client-driven.

### App pages

Key pages:

- `/`
- `/list`
- `/tasks/[id]`
- `/settings`
- `/sign-in`
- `/sign-up`
- `/admin`

### Frontend API routes

The frontend includes server routes under `frontend/src/app/api`. They serve several purposes:

- Attach session-based auth context
- Proxy requests to the backend
- Handle Stripe callbacks and webhooks
- Expose internal user preference and feedback endpoints

This separation lets the browser talk to the frontend domain while the frontend securely talks to the backend.

### Authentication

SupoClip uses Better Auth with Prisma and PostgreSQL.

Important details:

- Email and password login is enabled
- Additional user field `is_admin` is persisted
- Trusted origins are derived from app configuration
- Session cookies are used to identify the current user

## End-to-End Task Lifecycle

### 1. Task creation

The user submits a YouTube URL or upload from the frontend.

The backend:

- Validates the request
- Creates or links a source record
- Creates a task row
- Enqueues background work
- Returns quickly to the frontend

### 2. Queueing

The task enters a queue-backed state such as `queued`.

Redis carries the job definition to the worker.

### 3. Processing

The worker:

- Pulls the job
- Downloads or reads the source media
- Creates a transcript
- Runs AI analysis
- Generates clips
- Publishes progress updates

The task status becomes `processing`.

### 4. Completion

Once clip generation succeeds:

- Files are written to storage
- clip metadata is persisted in `generated_clips`
- the task status becomes `completed`
- the frontend refetches task and clip data

If anything fails:

- the task status becomes `error`
- resumable and diagnostic information is preserved where possible

## Video Processing Pipeline

The rough pipeline is:

1. Input acquisition
   - YouTube via Apify actor, with `yt-dlp` fallback
   - Uploaded file from the frontend
2. Transcription
   - AssemblyAI for word-level timestamps
3. Segment selection
   - LLM chooses promising short moments
4. Rendering
   - Video trimming and formatting
   - Subtitle placement and styling
   - Face-aware cropping
   - Optional transitions
   - Optional B-roll
5. Persistence
   - Clip metadata in PostgreSQL
   - media files in mounted storage

### Cropping and subtitles

The rendering path includes support for:

- Vertical output
- Face-centered cropping
- Subtitle overlays
- Caption templates
- Font customization

## Progress and Realtime Updates

The task detail page subscribes to progress using Server-Sent Events.

Backend route:

- `GET /tasks/{task_id}/progress`

The worker publishes progress updates during processing, and the frontend updates its UI without polling on every step.

## Data Model Overview

Important tables from `init.sql`:

### `users`

Stores:

- Auth identity
- Admin flag
- Default font preferences
- Billing plan and subscription fields

### `sources`

Stores:

- Source type
- Title
- Original URL when applicable

### `tasks`

Stores:

- User and source relationships
- Task status
- Progress percentage and message
- Font and caption settings
- B-roll setting
- Processing mode
- Timing and cache metadata

### `generated_clips`

Stores:

- File name and path
- Clip timing
- Selected text
- AI reasoning
- Virality and scoring breakdown

### `processing_cache`

Stores reusable processing artifacts to avoid repeating expensive work when possible.

### Better Auth tables

- `session`
- `account`
- `verification`

### Billing support

- `stripe_webhook_events`

## Storage Model

In Docker, the system uses named volumes for:

- uploads
- clips
- Redis data
- PostgreSQL data
- YouTube auth state

Fonts and transitions are file-based assets mounted from the repository.

## Operational Characteristics

### Why the worker matters

Without the worker, tasks may be created successfully but never progress beyond `queued`.

### Why Redis matters

Redis is required for:

- ARQ queue delivery
- progress messaging
- coordination around task processing

### Why FastAPI docs matter

The backend exposes interactive docs at `/docs`, which is helpful for inspecting available endpoints outside the frontend.

## Backend entry point and media modules

`backend/src/main.py` is the canonical API factory and entry point. The old
`main_refactored.py` import remains an alias for existing deployments. The legacy
monolithic API implementation has been removed; clients use `/tasks/` and the
shared media routes.

Processing orchestration lives in `services/task_service.py`; clip edits and
regeneration live in `services/clip_service.py`. Media implementation is split
into `media/transcription.py`, `media/timeline.py`, `media/captions.py`,
`media/reframing.py`, and `media/ffmpeg.py`. `video_utils.py` retains pipeline
composition and compatibility exports.

Caption edits render a fresh clip from its mapped source ranges before applying
captions. The editor previews that saved file; exports do not add another caption
layer. Caption edits need the original upload or an available YouTube source.

## Related Reading

- [App Guide](./app-guide.md)
- [API Reference](./api-reference.md)
- [Development](./development.md)
- [Troubleshooting](./troubleshooting.md)