Captions for uploaded videos (migration 066)

Video blocks get a Captions field and the hero a Video captions field,
each a WebVTT upload drawn as <track kind="captions" default> on a
video file. The media library takes .vtt (sniffed by its WEBVTT
header, kept as is, always in /uploads/captions/) with its own tab.
Newsletter parts leave the field out. nginx needs text/vtt in
mime.types: docs/media-uploads.md, step 5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-10-04 21:55:05 -05:00
parent aaab1a945c
commit 33be618331
18 changed files with 123 additions and 43 deletions

View file

@ -351,7 +351,7 @@ function Block({
return (
<figure>
<div className="overflow-hidden">
<VideoPlayer url={block.href} still={mediaUrl(block.media)} title={text || null} />
<VideoPlayer url={block.href} still={mediaUrl(block.media)} captions={mediaUrl(block.captions)} title={text || null} />
</div>
{!reading && <Caption text={text} />}
</figure>

View file

@ -33,11 +33,14 @@ export default function VideoPlayer({
url,
still,
title,
captions,
className = '',
}: {
url?: string | null
/** An image URL to show before it plays. */
still?: string | null
/** A WebVTT file's URL, for an uploaded video; embeds bring their own. */
captions?: string | null
title?: string | null
className?: string
}) {
@ -66,7 +69,9 @@ export default function VideoPlayer({
playsInline
aria-label={title || 'Video'}
className={`aspect-video w-full rounded-md bg-black ${className}`}
/>
>
{captions && <track kind="captions" src={captions} srcLang="en" label="Captions" default />}
</video>
) : (
<iframe
ref={focusOnMount}

View file

@ -37,7 +37,7 @@
cached list would lie.
═══════════════════════════════════════════════════════════════ */
import { FileAudio, X } from "@phosphor-icons/react";
import { ClosedCaptioning, FileAudio, X } from "@phosphor-icons/react";
import { useCallback, useEffect, useId, useRef, useState, type ReactNode } from "react";
import { useLocation } from "react-router-dom";
@ -59,7 +59,7 @@ export type MediaItem = {
processed: boolean;
uploaded_by: string | null;
created_at: string;
/** logos, images or thumbnails; null for clips and older files in the root. */
/** logos, images, thumbnails or captions; null for clips and older files in the root. */
folder: string | null;
};
@ -69,6 +69,7 @@ export const IMAGE_FOLDERS = [
["logos", "Logos"],
["images", "Images"],
["thumbnails", "Previews"],
["captions", "Captions"],
] as const;
export type ImageFolder = (typeof IMAGE_FOLDERS)[number][0];
@ -198,6 +199,8 @@ export function UploadButton({
const [keepOriginal, setKeepOriginal] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
// Captions are kept as they are, so there's no original to keep.
const captions = folder === "captions";
async function send(files: FileList | null) {
if (!files || files.length === 0) return;
@ -235,21 +238,23 @@ export function UploadButton({
<input
ref={inputRef}
type="file"
accept="image/jpeg,image/png,image/gif,image/webp,image/svg+xml"
accept={captions ? ".vtt,text/vtt" : "image/jpeg,image/png,image/gif,image/webp,image/svg+xml"}
multiple={multiple}
className="hidden"
onChange={(e) => send(e.target.files)}
/>
<label htmlFor={keepId} className="flex items-center gap-1.5 text-xs text-muted">
<input
id={keepId}
type="checkbox"
checked={keepOriginal}
onChange={(e) => setKeepOriginal(e.target.checked)}
className="h-3.5 w-3.5 border-muted/40 text-link"
/>
Keep original
</label>
{!captions && (
<label htmlFor={keepId} className="flex items-center gap-1.5 text-xs text-muted">
<input
id={keepId}
type="checkbox"
checked={keepOriginal}
onChange={(e) => setKeepOriginal(e.target.checked)}
className="h-3.5 w-3.5 border-muted/40 text-link"
/>
Keep original
</label>
)}
</div>
{error && <p className="mt-1 text-xs text-danger">{error}</p>}
</div>
@ -293,6 +298,10 @@ export function MediaGrid({
<div className="flex h-full w-full items-center justify-center bg-tint" aria-hidden="true">
<FileAudio weight="bold" className="h-10 w-10 text-link" />
</div>
) : item.content_type === "text/vtt" ? (
<div className="flex h-full w-full items-center justify-center bg-tint" aria-hidden="true">
<ClosedCaptioning weight="bold" className="h-10 w-10 text-link" />
</div>
) : (
<Thumb url={item.url} />
)}
@ -429,7 +438,7 @@ export function MediaPicker({
const [query, setQuery] = useState("");
const [tab, setTab] = useState<string>(folder);
const fromRequests = tab === REQUESTS_TAB;
const { items, error, setItems } = useMediaLibrary(query, "image", fromRequests ? undefined : tab);
const { items, error, setItems } = useMediaLibrary(query, tab === "captions" ? undefined : "image", fromRequests ? undefined : tab);
const dialogRef = useRef<HTMLDialogElement>(null);
// showModal gives focus trapping, Escape and the backdrop for free.
@ -556,11 +565,13 @@ export function ImageInput({
className="flex h-20 w-20 shrink-0 items-center justify-center overflow-hidden border border-muted/20"
style={CHECKER}
>
{src && !broken ? (
{folder === "captions" && src ? (
<ClosedCaptioning weight="bold" className="h-8 w-8 text-link" aria-label="Captions file" />
) : src && !broken ? (
<img src={src} alt="" className="h-full w-full object-contain" onError={() => setBroken(true)} />
) : (
<span className="px-1 text-center text-[10px] text-muted/70">
{src ? "Not found" : "No image"}
{src ? "Not found" : folder === "captions" ? "No file" : "No image"}
</span>
)}
</div>

View file

@ -107,6 +107,15 @@ export const frontPage = {
"A YouTube, Vimeo or Facebook link, or a video file: a filename in " +
"the uploads folder, or a link ending in .mp4 or .webm",
},
{
path: "video_captions",
label: "Video captions",
widget: "image",
folder: "captions",
resolve: mediaUrl,
full: true,
help: "A WebVTT (.vtt) file, for a video file; YouTube, Vimeo and Facebook show their own. Default: none.",
},
{ path: "video_title", label: "Video title", full: true, help: "Shown after \"Featured video\" above the headline" },
{
path: "hero_event_id",

View file

@ -396,6 +396,16 @@ export const blocksChild = {
help: (row) =>
row?.type === "video" ? "Still shown before it plays. Default: YouTube's own thumbnail." : undefined,
},
{
path: "captions",
label: "Captions",
widget: "image",
folder: "captions",
resolve: mediaUrl,
full: true,
when: { path: "type", value: "video" },
help: "A WebVTT (.vtt) file, for an uploaded video; YouTube, Vimeo and Facebook show their own. Default: none.",
},
{
path: "alt",
label: "Alt text",

View file

@ -48,6 +48,8 @@ export type ContentBlock = {
team_id?: string | null
/** Who a quote is by. */
cite?: string | null
/** A video block's WebVTT file, for an uploaded video (066). */
captions?: string | null
items?: ContentBlockItem[]
}

View file

@ -51,7 +51,7 @@ export type Hero = {
slides: HeroSlide[]
livestream: { url: string; title?: string | null } | null
/** A link to embed or a video file; see videoSource in embeds.ts. */
video: { url: string; title?: string | null } | null
video: { url: string; title?: string | null; captions?: string | null } | null
/** With the 'event' feature: the pinned event, else the countdown's. */
event: HeroEvent | null
}

View file

@ -107,7 +107,7 @@ const sourceOf = (part: NewsletterPart): PickSource => (SOURCES.includes(part.so
/* Every part is a row: one editor for blocks and the email's own
parts, so any of them can be moved, copied or removed. */
const blockFields = blocksChild.fields.filter((field) => !['slot', 'team_id', 'type'].includes(field.path))
const blockFields = blocksChild.fields.filter((field) => !['slot', 'team_id', 'type', 'captions'].includes(field.path))
const PARTS_SPEC = {
key: 'parts',
label: 'Parts',

View file

@ -113,7 +113,7 @@ export default function HeroStage({ hero }: { hero: Hero | null }) {
{feature === 'video' && hero?.video && (
<div className="lg:col-span-7">
<VideoFrame source={video} url={hero.video.url} title={hero.video.title} />
<VideoFrame source={video} url={hero.video.url} title={hero.video.title} captions={mediaUrl(hero.video.captions)} />
</div>
)}
@ -498,10 +498,13 @@ function VideoFrame({
source,
url,
title,
captions,
}: {
source: VideoSource | null
url: string
title?: string | null
/** A WebVTT file's URL; only a file plays it, an embed has its own. */
captions?: string | null
}) {
return (
<div
@ -517,7 +520,9 @@ function VideoFrame({
controls
playsInline
preload="metadata"
/>
>
{captions && <track kind="captions" src={captions} srcLang="en" label="Captions" default />}
</video>
) : source?.kind === 'embed' ? (
<VideoPlayer url={url} title={title || 'Featured video'} className="h-full" />
) : (