3.4 KiB
HLS (m3u8) Implementation Plan
Objective
Transition internal video processing and serving from single MP4 files to HLS (HTTP Live Streaming) to improve playback performance, seekability, and support adaptive bitrate streaming.
Technical Changes
1. Video Processing (Worker)
File: server/src/products/videos/worker.ts
Current: Generates single jobId.mp4.
New: Generate HLS playlist and segments.
FFmpeg Command:
ffmpeg -i input.mp4 \
-b:v 1M \
-g 60 \
-hls_time 2 \
-hls_list_size 0 \
-hls_segment_size 500000 \
output.m3u8
Storage Structure:
Instead of flat files in videos/, use a directory per job:
videos/
├── [jobId]/
│ ├── playlist.m3u8
│ ├── output0.ts
│ ├── output1.ts
│ └── ...
2. API Endpoints (Product)
File: server/src/products/videos/routes.ts & index.ts
Create new endpoints to serve HLS assets. We need to serve both the playlist (.m3u8) and the segment files (.ts).
Endpoints:
-
Playlist:
GET /api/videos/jobs/:id/hls/playlist.m3u8- Handler: Looks up directory
videos/:id/, servesplaylist.m3u8. - Content-Type:
application/vnd.apple.mpegurlorapplication/x-mpegURL.
- Handler: Looks up directory
-
Segments:
GET /api/videos/jobs/:id/hls/:segment- Handler: Looks up directory
videos/:id/, serves requested.tsfile. - Content-Type:
video/MP2T.
- Handler: Looks up directory
Note: The playlist generated by ffmpeg usually contains relative paths (e.g., output0.ts). If the browser loads the playlist from .../hls/playlist.m3u8, it will resolve segments relative to that base, .../hls/output0.ts, which fits the proposed endpoint structure perfectly.
3. Frontend (Playground & Components)
File: src/pages/VideoPlayerPlaygroundIntern.tsx
- Update
pollJoborhandleUploadcompletion logic. - Instead of setting
videoUrlto the MP4 download link, set it to the HLS playlist endpoint:http://localhost:3333/api/videos/jobs/[jobId]/hls/playlist.m3u8
- Pass type
application/x-mpegurl(orapplication/vnd.apple.mpegurl) toVideoCard.
File: src/components/VideoCard.tsx
- Ensure
Vidstack/MediaPlayerhandles the HLS mime type correctly. Vidstack has built-in HLS support (often requireshls.jsbut it's usually bundled or easily added). - Verify
srcprop handling for HLS.
4. Database / Metadata
- Update
resultUrlin the job completion data (inpg-bossor returned to frontend) to point to the.m3u8file. - Supabase
picturestable: Updateimage_urlto store the HLS playlist URL instead of the MP4 download URL.
Migration Strategy
- Implement new Worker logic: Create a new job name or update existing
video-processingto output HLS. - Implement new Endpoints: Add HLS routes to
VideosProduct. - Update Frontend: Point playground to new endpoints.
- Verify: Test playback.
- Cleanup: Remove MP4 download endpoint and old worker logic once verified.
Outstanding Questions
- Storage Cleanup: Deleting a video now means deleting a directory. Ensure
handleDeleteis updated. - Legacy Content: Old MP4s will stop working if we aggressively remove the old endpoint.
- Decision: Keep the old
downloadendpoint for backward compatibility if needed, or migration script to convert old videos (out of scope for now).
- Decision: Keep the old