feat(player): let editors upload subtitles for a version

Subtitle tracks hang off a version rather than off a video, because re-editing a cut shifts every cue. The file
always lands in our own S3-compatible storage whatever hosts the video, so a Bunny-hosted cut and an R2 one take the
same path: both already play through our own video element, so a track element is all it takes.

Uploads are normalised before they are stored. Whatever arrives, SRT or WebVTT, is parsed into cues and
re-serialised as a canonical WebVTT file, and anything we did not understand is dropped rather than passed through.
That is what makes it safe to serve a user-supplied text file from our own origin. Files saved out of Windows
editors are decoded as windows-1254 or windows-1252 when they are not valid UTF-8, rather than refused.

A YouTube version cannot carry an uploaded track, so the same CC menu drives YouTube's own captions through the
iframe module API. The embed hides YouTube's controls, so until now those captions were unreachable even when the
video had them.

Uploading and deleting take the editor permission rather than the commenter one: a subtitle is part of the
delivered cut, not a comment attachment.
This commit is contained in:
2026-08-22 07:51:46 +03:00
parent 1f3c6b3f1e
commit d981d98cf5
26 changed files with 2529 additions and 25 deletions
@@ -0,0 +1,39 @@
-- Subtitle tracks hang off a version, not off the video: re-editing a cut shifts
-- every cue, so a track attached to the parent would be wrong for every version
-- but the one it was written against.
CREATE TABLE "video_subtitles" (
"id" TEXT NOT NULL,
"versionId" TEXT NOT NULL,
"language" TEXT NOT NULL,
"label" TEXT NOT NULL,
"sourceUrl" TEXT NOT NULL,
"size_bytes" BIGINT NOT NULL DEFAULT 0,
"billedUserId" TEXT NOT NULL,
"uploadedByUserId" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "video_subtitles_pkey" PRIMARY KEY ("id")
);
-- One stored object belongs to exactly one row, so the reference check that runs
-- before an object delete cannot be fooled by a second row pointing at the file.
CREATE UNIQUE INDEX "video_subtitles_sourceUrl_key" ON "video_subtitles"("sourceUrl");
-- Re-uploading a language replaces the track rather than stacking a second one,
-- which would leave the player with two tracks labelled the same.
CREATE UNIQUE INDEX "video_subtitles_versionId_language_key" ON "video_subtitles"("versionId", "language");
CREATE INDEX "video_subtitles_versionId_idx" ON "video_subtitles"("versionId");
-- The storage quota sums this column per billed user on every upload.
CREATE INDEX "video_subtitles_billedUserId_idx" ON "video_subtitles"("billedUserId");
ALTER TABLE "video_subtitles" ADD CONSTRAINT "video_subtitles_versionId_fkey"
FOREIGN KEY ("versionId") REFERENCES "video_versions"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "video_subtitles" ADD CONSTRAINT "video_subtitles_billedUserId_fkey"
FOREIGN KEY ("billedUserId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "video_subtitles" ADD CONSTRAINT "video_subtitles_uploadedByUserId_fkey"
FOREIGN KEY ("uploadedByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;