diff --git a/apps/web/__tests__/unit/custom-thumbnail.test.ts b/apps/web/__tests__/unit/custom-thumbnail.test.ts new file mode 100644 index 00000000000..786e6b21390 --- /dev/null +++ b/apps/web/__tests__/unit/custom-thumbnail.test.ts @@ -0,0 +1,189 @@ +import { describe, expect, it } from "vitest"; +import { + CUSTOM_THUMBNAIL_MAX_DIMENSION, + clampThumbnailFrameTime, + fitThumbnailSize, + isJpegData, + validateThumbnailSourceFile, +} from "@/lib/custom-thumbnail"; +import { + buildShareVideoMetadata, + getShareVideoUrls, +} from "@/lib/share-video-metadata"; +import { + customThumbnailKey, + getCustomThumbnailKey, + getCustomThumbnailMetadata, + getCustomThumbnailVersion, + isCustomThumbnailFileName, +} from "../../../../packages/web-backend/src/Storage/custom-thumbnail"; + +const video = { id: "vid123", ownerId: "owner456" }; + +describe("custom thumbnail metadata", () => { + it("resolves a key under the video's own prefix", () => { + const metadata = { + customThumbnail: { + file: "custom-lz9k2a1b2c3.jpg", + source: "frame", + time: 12.5, + updatedAt: "2026-11-16T10:00:00.000Z", + }, + }; + expect(getCustomThumbnailKey({ ...video, metadata })).toBe( + "owner456/vid123/thumbnail/custom-lz9k2a1b2c3.jpg", + ); + expect(getCustomThumbnailMetadata(metadata)).toEqual( + metadata.customThumbnail, + ); + expect(getCustomThumbnailVersion(metadata)).toBe("lz9k2a1b2c3"); + }); + + it("ignores old videos and anything that is not a plain file name", () => { + for (const metadata of [ + null, + undefined, + {}, + { customThumbnail: null }, + { customThumbnail: { file: "../other/screenshot.jpg" } }, + { customThumbnail: { file: "custom-abc.png" } }, + { customThumbnail: { file: "custom-ABC.jpg" } }, + { customThumbnail: { file: "custom-a/b.jpg" } }, + { customThumbnail: { file: "custom-.jpg" } }, + { customThumbnail: { file: 42 } }, + ]) { + expect(getCustomThumbnailKey({ ...video, metadata })).toBeUndefined(); + expect(getCustomThumbnailVersion(metadata)).toBeNull(); + } + }); + + it("defaults the source and drops a bad time", () => { + expect( + getCustomThumbnailMetadata({ + customThumbnail: { file: "custom-a1.jpg", source: "x", time: "1" }, + }), + ).toEqual({ file: "custom-a1.jpg", source: "upload", updatedAt: "" }); + }); + + it("follows the video when it is duplicated under a new id", () => { + const metadata = { customThumbnail: { file: "custom-a1.jpg" } }; + expect( + getCustomThumbnailKey({ id: "copy789", ownerId: "owner456", metadata }), + ).toBe( + customThumbnailKey({ + id: "copy789", + ownerId: "owner456", + file: "custom-a1.jpg", + }), + ); + }); + + it("validates file names", () => { + expect(isCustomThumbnailFileName("custom-0a.jpg")).toBe(true); + expect(isCustomThumbnailFileName(`custom-${"a".repeat(33)}.jpg`)).toBe( + false, + ); + }); +}); + +describe("thumbnail input checks", () => { + it("accepts PNG, JPEG and WebP sources up to 20 MB", () => { + expect( + validateThumbnailSourceFile({ type: "image/png", size: 1024 }), + ).toBeNull(); + expect( + validateThumbnailSourceFile({ type: "IMAGE/JPEG", size: 1024 }), + ).toBeNull(); + expect( + validateThumbnailSourceFile({ type: "image/webp", size: 1024 }), + ).toBeNull(); + expect( + validateThumbnailSourceFile({ type: "image/svg+xml", size: 10 }), + ).not.toBeNull(); + expect( + validateThumbnailSourceFile({ type: "image/gif", size: 10 }), + ).not.toBeNull(); + expect( + validateThumbnailSourceFile({ type: "image/png", size: 0 }), + ).not.toBeNull(); + expect( + validateThumbnailSourceFile({ + type: "image/png", + size: 20 * 1024 * 1024 + 1, + }), + ).not.toBeNull(); + }); + + it("recognises complete JPEG data only", () => { + expect( + isJpegData(new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0xff, 0xd9])), + ).toBe(true); + expect( + isJpegData(new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a])), + ).toBe(false); + expect( + isJpegData(new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x00])), + ).toBe(false); + expect(isJpegData(new Uint8Array([0xff, 0xd8]))).toBe(false); + }); + + it("fits inside the bounds without upscaling", () => { + expect(fitThumbnailSize(3840, 2160)).toEqual({ width: 1920, height: 1080 }); + expect(fitThumbnailSize(1080, 2400)).toEqual({ + width: 864, + height: CUSTOM_THUMBNAIL_MAX_DIMENSION, + }); + expect(fitThumbnailSize(1280, 720)).toEqual({ width: 1280, height: 720 }); + expect(fitThumbnailSize(0, 720)).toBeNull(); + }); + + it("clamps frame times inside the video", () => { + expect(clampThumbnailFrameTime(12.3456, 60)).toBe(12.346); + expect(clampThumbnailFrameTime(100, 60)).toBeCloseTo(59.95, 5); + expect(clampThumbnailFrameTime(5, null)).toBe(5); + expect(clampThumbnailFrameTime(0, 0)).toBe(0); + for (const time of [-1, Number.NaN, Number.POSITIVE_INFINITY, "3", null]) { + expect(clampThumbnailFrameTime(time, 60)).toBeNull(); + } + }); +}); + +describe("link previews with a custom thumbnail", () => { + const base = { + videoId: "vid123", + name: "Demo", + sourceType: "desktopMP4" as const, + webUrl: "https://cap.so", + width: 1920, + height: 1080, + }; + + it("versions the preview image and drops the animated preview", () => { + const metadata = buildShareVideoMetadata({ + ...base, + thumbnailVersion: "lz9k2a", + }); + expect(metadata.openGraph?.images).toEqual([ + { + url: "https://cap.so/api/video/og?videoId=vid123&v=lz9k2a", + width: 1200, + height: 630, + type: "image/png", + }, + ]); + expect(metadata.twitter?.images).toEqual([ + "https://cap.so/api/video/og?videoId=vid123&v=lz9k2a", + ]); + }); + + it("leaves previews unchanged for videos without one", () => { + expect(buildShareVideoMetadata(base)).toEqual( + buildShareVideoMetadata({ ...base, thumbnailVersion: null }), + ); + expect(getShareVideoUrls(base).ogImageUrl).toBe( + "https://cap.so/api/video/og?videoId=vid123", + ); + const images = buildShareVideoMetadata(base).openGraph?.images; + expect(Array.isArray(images) ? images.length : 0).toBe(2); + }); +}); diff --git a/apps/web/__tests__/unit/embed-options.test.ts b/apps/web/__tests__/unit/embed-options.test.ts new file mode 100644 index 00000000000..dc65f1c8d20 --- /dev/null +++ b/apps/web/__tests__/unit/embed-options.test.ts @@ -0,0 +1,173 @@ +import { describe, expect, it } from "vitest"; +import { buildEmbedCode } from "@/lib/embed-code"; +import { + buildEmbedQuery, + DEFAULT_EMBED_OPTIONS, + parseEmbedOptions, +} from "@/lib/embed-options"; + +const legacyBuildEmbedCode = ({ + webUrl, + videoId, + startTime, +}: { + webUrl: string; + videoId: string; + startTime?: number | null; +}): string => { + const start = + typeof startTime === "number" && Number.isFinite(startTime) && startTime > 0 + ? `?t=${Math.floor(startTime)}` + : ""; + + return ` +
+ +
+` + .trim() + .replace(/[\n\t]+/g, " ") + .replace(/>\s+<") + .replace(/"\s+>/g, '">'); +}; + +describe("parseEmbedOptions", () => { + it("defaults to how embeds have always looked", () => { + expect(parseEmbedOptions({})).toEqual(DEFAULT_EMBED_OPTIONS); + expect(parseEmbedOptions(null)).toEqual(DEFAULT_EMBED_OPTIONS); + expect(parseEmbedOptions(new URLSearchParams(""))).toEqual( + DEFAULT_EMBED_OPTIONS, + ); + }); + + it("only hides things on an explicit false", () => { + expect( + parseEmbedOptions({ title: "false", owner: "0", branding: "false" }), + ).toMatchObject({ + showTitle: false, + showOwner: false, + showBranding: false, + }); + expect( + parseEmbedOptions({ title: "no", owner: "", branding: "true" }), + ).toMatchObject({ showTitle: true, showOwner: true, showBranding: true }); + }); + + it("only autoplays and mutes on an explicit true", () => { + expect(parseEmbedOptions({ autoplay: "true", muted: "1" })).toMatchObject({ + autoplay: true, + muted: true, + }); + expect( + parseEmbedOptions({ autoplay: "yes", muted: "false" }), + ).toMatchObject({ autoplay: false, muted: false }); + }); + + it("keeps the existing start time rules", () => { + expect(parseEmbedOptions({ t: "83" }).startTime).toBe(83); + expect(parseEmbedOptions({ t: ["12", "40"] }).startTime).toBe(12); + for (const t of ["0", "-5", "abc", ""]) { + expect(parseEmbedOptions({ t }).startTime).toBeNull(); + } + }); + + it("takes the first of repeated params", () => { + expect(parseEmbedOptions({ title: ["false", "true"] }).showTitle).toBe( + false, + ); + expect( + parseEmbedOptions(new URLSearchParams("title=true&title=false")) + .showTitle, + ).toBe(true); + }); +}); + +describe("buildEmbedQuery", () => { + it("is empty for defaults", () => { + expect(buildEmbedQuery({})).toBe(""); + expect(buildEmbedQuery(DEFAULT_EMBED_OPTIONS)).toBe(""); + }); + + it("writes only changed options, in a fixed order", () => { + expect( + buildEmbedQuery({ + showBranding: false, + showTitle: false, + autoplay: true, + muted: true, + showOwner: false, + startTime: 61.9, + }), + ).toBe( + "?t=61&autoplay=true&muted=true&title=false&owner=false&branding=false", + ); + }); + + it("round trips through the parser", () => { + const options = { + ...DEFAULT_EMBED_OPTIONS, + showOwner: false, + autoplay: true, + muted: true, + startTime: 30, + }; + expect( + parseEmbedOptions(new URLSearchParams(buildEmbedQuery(options))), + ).toEqual(options); + }); +}); + +describe("buildEmbedCode", () => { + it("is byte-identical to the previous snippet with default options", () => { + const cases = [ + { webUrl: "https://cap.so", videoId: "abc123" }, + { webUrl: "https://cap.so", videoId: "abc123", startTime: 83 }, + { webUrl: "https://cap.so", videoId: "abc123", startTime: 83.7 }, + { webUrl: "https://cap.so", videoId: "abc123", startTime: null }, + { webUrl: "https://cap.example.com", videoId: "xyz", startTime: 0 }, + ]; + for (const input of cases) { + expect(buildEmbedCode(input)).toBe(legacyBuildEmbedCode(input)); + expect(buildEmbedCode({ ...input, options: {} })).toBe( + legacyBuildEmbedCode(input), + ); + } + expect( + buildEmbedCode({ webUrl: "https://cap.so", videoId: "abc123" }), + ).toBe( + '
', + ); + }); + + it("adds the options and delegates autoplay to the iframe", () => { + const code = buildEmbedCode({ + webUrl: "https://cap.so", + videoId: "abc123", + startTime: 10, + options: { autoplay: true, muted: true, showTitle: false }, + }); + expect(code).toContain( + 'src="https://cap.so/embed/abc123?t=10&autoplay=true&muted=true&title=false"', + ); + expect(code).toContain( + 'frameborder="0" allow="autoplay; fullscreen; picture-in-picture" webkitallowfullscreen', + ); + }); + + it("does not add the allow attribute without autoplay", () => { + const code = buildEmbedCode({ + webUrl: "https://cap.so", + videoId: "abc123", + options: { showBranding: false }, + }); + expect(code).not.toContain("allow="); + expect(code).toContain('src="https://cap.so/embed/abc123?branding=false"'); + }); +}); diff --git a/apps/web/__tests__/unit/organization-settings-ownership.test.ts b/apps/web/__tests__/unit/organization-settings-ownership.test.ts index ecab49c45de..ff06b1696b3 100644 --- a/apps/web/__tests__/unit/organization-settings-ownership.test.ts +++ b/apps/web/__tests__/unit/organization-settings-ownership.test.ts @@ -93,6 +93,10 @@ vi.mock( "@/app/(org)/dashboard/settings/organization/components/ShareableLinkIcon", () => ({ ShareableLinkIcon: () => null }), ); +vi.mock( + "@/app/(org)/dashboard/settings/organization/components/ShareableLinkBrandColor", + () => ({ ShareableLinkBrandColor: () => null }), +); vi.mock( "@/app/(org)/dashboard/settings/organization/components/AccessEmailDomain", () => ({ default: () => null }), diff --git a/apps/web/__tests__/unit/player-brand.test.ts b/apps/web/__tests__/unit/player-brand.test.ts new file mode 100644 index 00000000000..9e911a85755 --- /dev/null +++ b/apps/web/__tests__/unit/player-brand.test.ts @@ -0,0 +1,146 @@ +import { describe, expect, it } from "vitest"; +import { + contrastRatio, + getPlayerBrand, + MIN_CONTROL_CONTRAST, + PLAYER_BRAND_INK, + PLAYER_BRAND_WHITE, + playerBrandAttributes, + playerColorFromOrganizationMetadata, + resolvePlayerBrand, +} from "@/lib/player-brand"; + +describe("contrastRatio", () => { + it("matches the WCAG reference values", () => { + expect(contrastRatio("#000000", "#FFFFFF")).toBeCloseTo(21, 5); + expect(contrastRatio("#FFFFFF", "#FFFFFF")).toBeCloseTo(1, 5); + expect(contrastRatio("#767676", "#FFFFFF")).toBeCloseTo(4.54, 2); + }); +}); + +describe("resolvePlayerBrand", () => { + it("rejects anything that is not a hex colour", () => { + for (const value of [null, undefined, "", "blue", "#12", "#GGGGGG", 42]) { + expect(resolvePlayerBrand(value)).toBeNull(); + } + }); + + it("normalises shorthand and lowercase hex", () => { + expect(resolvePlayerBrand("#f60")?.color).toBe("#FF6600"); + expect(resolvePlayerBrand("1f6feb")?.color).toBe("#1F6FEB"); + }); + + it("picks whichever text colour reads better on the button", () => { + for (const color of [ + "#1F6FEB", + "#000000", + "#FFFFFF", + "#FFD60A", + "#7A4DFF", + "#12A150", + "#808080", + ]) { + const brand = resolvePlayerBrand(color); + if (!brand) throw new Error("expected a brand"); + const chosen = contrastRatio(color, brand.foreground); + const other = contrastRatio( + color, + brand.foreground === PLAYER_BRAND_WHITE + ? PLAYER_BRAND_INK + : PLAYER_BRAND_WHITE, + ); + expect(chosen).toBeGreaterThanOrEqual(other); + expect(chosen).toBeGreaterThanOrEqual(4.5); + } + expect(resolvePlayerBrand("#0B1F3A")?.foreground).toBe(PLAYER_BRAND_WHITE); + expect(resolvePlayerBrand("#FFD60A")?.foreground).toBe(PLAYER_BRAND_INK); + }); + + it("keeps the progress bar white when the colour vanishes on dark video", () => { + const navy = resolvePlayerBrand("#0B1F3A"); + expect(navy?.accent).toBeNull(); + expect(contrastRatio("#0B1F3A", "#000000")).toBeLessThan( + MIN_CONTROL_CONTRAST, + ); + + const orange = resolvePlayerBrand("#F26B1D"); + expect(orange?.accent).toBe("#F26B1D"); + }); + + it("darkens light-text buttons and lightens dark-text buttons on hover", () => { + expect(resolvePlayerBrand("#1F6FEB")?.hover).not.toBe("#1F6FEB"); + const blue = resolvePlayerBrand("#1F6FEB"); + const yellow = resolvePlayerBrand("#FFD60A"); + if (!blue || !yellow) throw new Error("expected brands"); + expect(contrastRatio(blue.hover, "#000000")).toBeLessThan( + contrastRatio(blue.color, "#000000"), + ); + expect(contrastRatio(yellow.hover, "#000000")).toBeGreaterThan( + contrastRatio(yellow.color, "#000000"), + ); + }); +}); + +describe("playerColorFromOrganizationMetadata", () => { + it("reads only branding.playerColor", () => { + expect( + playerColorFromOrganizationMetadata({ + branding: { playerColor: "#12a150" }, + }), + ).toBe("#12A150"); + expect( + playerColorFromOrganizationMetadata({ + branding: { colors: { primary: "#12A150" } }, + }), + ).toBeNull(); + for (const metadata of [null, undefined, "x", [], { branding: null }]) { + expect(playerColorFromOrganizationMetadata(metadata)).toBeNull(); + } + expect( + playerColorFromOrganizationMetadata({ + branding: { playerColor: "red; background: url(x)" }, + }), + ).toBeNull(); + }); +}); + +describe("getPlayerBrand", () => { + const metadata = { branding: { playerColor: "#7A4DFF" } }; + + it("applies the colour only for Pro owners", () => { + expect( + getPlayerBrand({ ownerIsPro: true, organizationMetadata: metadata }) + ?.color, + ).toBe("#7A4DFF"); + expect( + getPlayerBrand({ ownerIsPro: false, organizationMetadata: metadata }), + ).toBeNull(); + }); +}); + +describe("playerBrandAttributes", () => { + it("renders nothing without a brand", () => { + expect(playerBrandAttributes(null)).toEqual({}); + }); + + it("exposes the colour as CSS variables", () => { + const attributes = playerBrandAttributes(resolvePlayerBrand("#F26B1D")); + expect(attributes["data-player-brand"]).toBe(""); + expect(attributes["data-player-brand-accent"]).toBe(""); + expect(attributes.style).toMatchObject({ + "--player-brand": "#F26B1D", + "--player-brand-foreground": PLAYER_BRAND_INK, + "--player-brand-accent": "#F26B1D", + }); + expect( + (attributes.style as Record)["--player-brand-ring"], + ).toMatch(/^\d+(\.\d+)? \d+(\.\d+)?% \d+(\.\d+)?%$/); + }); + + it("leaves the accent off for dark colours", () => { + const attributes = playerBrandAttributes(resolvePlayerBrand("#0B1F3A")); + expect(attributes["data-player-brand"]).toBe(""); + expect(attributes["data-player-brand-accent"]).toBeUndefined(); + expect(attributes.style).not.toHaveProperty("--player-brand-accent"); + }); +}); diff --git a/apps/web/__tests__/unit/video-og-route.test.ts b/apps/web/__tests__/unit/video-og-route.test.ts index 53d3e9944aa..a6785706d2b 100644 --- a/apps/web/__tests__/unit/video-og-route.test.ts +++ b/apps/web/__tests__/unit/video-og-route.test.ts @@ -141,6 +141,67 @@ describe("/api/video/og", () => { expect(mocks.renders).toBe(0); }); + it("keeps a thumbnail version and drops everything else", async () => { + const GET = await loadRoute(); + for (const [query, location] of [ + ["?v=lz9k2a&videoId=abc123", "/api/video/og?videoId=abc123&v=lz9k2a"], + ["?videoId=abc123&v=lz9k2a&x=1", "/api/video/og?videoId=abc123&v=lz9k2a"], + ["?videoId=abc123&v=..%2Fx", "/api/video/og?videoId=abc123"], + ["?videoId=abc123&v=", "/api/video/og?videoId=abc123"], + ["?videoId=abc123&v=UPPER", "/api/video/og?videoId=abc123"], + ] as const) { + const response = await fetchOg(GET, query); + expect(response.status).toBe(308); + expect(response.headers.location).toBe(location); + } + + const versioned = await fetchOg(GET, "?videoId=abc123&v=lz9k2a"); + expect(versioned.status).toBe(200); + expect(mocks.lookups).toEqual(["abc123"]); + }); + + it("renders only the version a video actually has", async () => { + const base = { + ownerId: "owner", + name: "Quarterly update", + public: true, + password: null, + duration: 42, + }; + mocks.videos.set("abc123", { + ...base, + id: "abc123", + metadata: { customThumbnail: { file: "custom-lz9k2a.jpg" } }, + }); + mocks.videos.set("plain1", { ...base, id: "plain1", metadata: null }); + mocks.videos.set("locked1", { + ...base, + id: "locked1", + public: false, + metadata: { customThumbnail: { file: "custom-lz9k2a.jpg" } }, + }); + const GET = await loadRoute(); + + const current = await fetchOg(GET, "?videoId=abc123&v=lz9k2a"); + expect(current.status).toBe(200); + expect(current.headers["content-type"]).toBe("image/png"); + + for (const [query, location] of [ + ["?videoId=abc123&v=old1", "/api/video/og?videoId=abc123&v=lz9k2a"], + ["?videoId=plain1&v=lz9k2a", "/api/video/og?videoId=plain1"], + ] as const) { + const renders = mocks.renders; + const response = await fetchOg(GET, query); + expect(response.status).toBe(307); + expect(response.headers.location).toBe(location); + expect(mocks.renders).toBe(renders); + } + + const locked = await fetchOg(GET, "?videoId=locked1&v=old1"); + expect(locked.status).toBe(200); + expect(locked.headers.location).toBeUndefined(); + }); + it("renders the preview URL the app generates for a public video", async () => { mocks.videos.set("abc123", { id: "abc123", diff --git a/apps/web/actions/organization/shareable-link-icon.ts b/apps/web/actions/organization/shareable-link-icon.ts index c7c4495b500..ccaff5052a6 100644 --- a/apps/web/actions/organization/shareable-link-icon.ts +++ b/apps/web/actions/organization/shareable-link-icon.ts @@ -6,11 +6,12 @@ import { organizations, users } from "@cap/database/schema"; import { userIsPro } from "@cap/utils"; import { ImageUploads } from "@cap/web-backend"; import { Organisation } from "@cap/web-domain"; -import { eq } from "drizzle-orm"; +import { eq, sql } from "drizzle-orm"; import { Effect, Option } from "effect"; import { revalidatePath } from "next/cache"; import { organizationSettingsKeepingRowOwnedKeys } from "@/lib/organization-settings-write"; import { runPromise } from "@/lib/server"; +import { normalizeHexColor } from "@/lib/share-call-to-action"; import { requireOrganizationSettingsManager } from "./authorization"; const allowedImageTypes = new Set(["image/jpeg", "image/png"]); @@ -202,3 +203,30 @@ export async function selectShareableLinkBrandingOrganization( return { success: true }; } + +export async function updateShareableLinkBrandColor({ + organizationId, + color, +}: { + organizationId: Organisation.OrganisationId; + color: string | null; +}) { + const playerColor = color === null ? null : normalizeHexColor(color); + if (color !== null && !playerColor) { + throw new Error("Enter a hex color like #1F6FEB"); + } + + const organization = await getManageableProOrganization(organizationId); + const patch = JSON.stringify({ branding: { playerColor } }); + + await db() + .update(organizations) + .set({ + metadata: sql`JSON_MERGE_PATCH(COALESCE(${organizations.metadata}, JSON_OBJECT()), CAST(${patch} AS JSON))`, + }) + .where(eq(organizations.id, organization.id)); + + revalidateOrganizationBrandingPaths(); + + return { success: true, color: playerColor }; +} diff --git a/apps/web/actions/videos/custom-thumbnail.ts b/apps/web/actions/videos/custom-thumbnail.ts new file mode 100644 index 00000000000..6ad29595b35 --- /dev/null +++ b/apps/web/actions/videos/custom-thumbnail.ts @@ -0,0 +1,212 @@ +"use server"; + +import { randomBytes } from "node:crypto"; +import { db } from "@cap/database"; +import { getCurrentUser } from "@cap/database/auth/session"; +import { videos } from "@cap/database/schema"; +import { + type CustomThumbnailMetadata, + customThumbnailKey, + getCustomThumbnailMetadata, + Storage, +} from "@cap/web-backend"; +import { Video } from "@cap/web-domain"; +import { and, eq, sql } from "drizzle-orm"; +import { Effect } from "effect"; +import { getFfmpegPath } from "@/lib/audio-extract"; +import { + CUSTOM_THUMBNAIL_FRAME_FILTER, + CUSTOM_THUMBNAIL_MAX_BYTES, + clampThumbnailFrameTime, + isJpegData, +} from "@/lib/custom-thumbnail"; +import { grabFrame } from "@/lib/og/poster-frame"; +import { runPromise } from "@/lib/server"; +import { decodeStorageVideo } from "@/lib/video-storage"; + +const FRAME_TIMEOUT_MS = 20_000; + +export type CustomThumbnailResult = + | { success: true } + | { success: false; error: string }; + +type OwnedVideo = typeof videos.$inferSelect; + +async function loadOwnedVideo( + videoId: Video.VideoId, +): Promise { + const user = await getCurrentUser(); + if (!user) return "Sign in to change this thumbnail"; + + const [video] = await db() + .select() + .from(videos) + .where(eq(videos.id, videoId)) + .limit(1); + + if (!video || video.ownerId !== user.id) { + return "Only the owner can change this thumbnail"; + } + if (video.isScreenshot) { + return "Screenshots already use the image as their thumbnail"; + } + return video; +} + +const newThumbnailFileName = () => + `custom-${Date.now().toString(36)}${randomBytes(3).toString("hex")}.jpg`; + +function writeCustomThumbnailMetadata( + video: OwnedVideo, + value: CustomThumbnailMetadata | null, +) { + const patch = JSON.stringify({ customThumbnail: value }); + return db() + .update(videos) + .set({ + metadata: sql`JSON_MERGE_PATCH(JSON_MERGE_PATCH(COALESCE(${videos.metadata}, JSON_OBJECT()), CAST('{"customThumbnail":null}' AS JSON)), CAST(${patch} AS JSON))`, + }) + .where(and(eq(videos.id, video.id), eq(videos.ownerId, video.ownerId))); +} + +const deleteThumbnailObject = (video: OwnedVideo, file: string) => + Effect.gen(function* () { + const [bucket] = yield* Storage.getAccessForVideo( + decodeStorageVideo(video), + ); + yield* bucket.deleteObject( + customThumbnailKey({ id: video.id, ownerId: video.ownerId, file }), + ); + }) + .pipe(runPromise) + .catch((error) => { + console.warn( + `[custom-thumbnail] Failed to delete old thumbnail for ${video.id}`, + error, + ); + }); + +async function saveCustomThumbnail( + video: OwnedVideo, + data: Uint8Array, + details: Pick, +) { + const previous = getCustomThumbnailMetadata(video.metadata); + const file = newThumbnailFileName(); + + await Effect.gen(function* () { + const [bucket] = yield* Storage.getAccessForVideo( + decodeStorageVideo(video), + ); + yield* bucket.putObject( + customThumbnailKey({ id: video.id, ownerId: video.ownerId, file }), + data, + { contentType: "image/jpeg", contentLength: data.byteLength }, + ); + }).pipe(runPromise); + + await writeCustomThumbnailMetadata(video, { + file, + source: details.source, + ...(details.time === undefined ? {} : { time: details.time }), + updatedAt: new Date().toISOString(), + }); + + if (previous && previous.file !== file) { + await deleteThumbnailObject(video, previous.file); + } +} + +export async function uploadCustomThumbnail( + formData: FormData, +): Promise { + const videoId = formData.get("videoId"); + const image = formData.get("image"); + if (typeof videoId !== "string" || !(image instanceof Blob)) { + return { success: false, error: "Choose an image to upload" }; + } + if (image.size === 0 || image.size > CUSTOM_THUMBNAIL_MAX_BYTES) { + return { success: false, error: "That image is too large" }; + } + + const video = await loadOwnedVideo(Video.VideoId.make(videoId)); + if (typeof video === "string") return { success: false, error: video }; + + const data = new Uint8Array(await image.arrayBuffer()); + if (!isJpegData(data)) { + return { success: false, error: "That file isn't a valid image" }; + } + + try { + await saveCustomThumbnail(video, data, { source: "upload" }); + return { success: true }; + } catch (error) { + console.error(`[custom-thumbnail] Upload failed for ${video.id}`, error); + return { success: false, error: "Couldn't save the thumbnail" }; + } +} + +export async function setCustomThumbnailFromFrame( + videoId: Video.VideoId, + time: number, +): Promise { + const video = await loadOwnedVideo(videoId); + if (typeof video === "string") return { success: false, error: video }; + + const seek = clampThumbnailFrameTime(time, video.duration); + if (seek === null) return { success: false, error: "Pick a frame first" }; + + try { + const sourceUrl = await Effect.gen(function* () { + const storageVideo = decodeStorageVideo(video); + const source = Video.Video.getSource(storageVideo); + if (!(source instanceof Video.Mp4Source)) return null; + const [bucket] = yield* Storage.getAccessForVideo(storageVideo); + return yield* bucket.getInternalSignedObjectUrl(source.getFileKey()); + }).pipe(runPromise); + + if (!sourceUrl) { + return { + success: false, + error: "Frames can be picked once the video finishes processing", + }; + } + + const frame = await grabFrame( + getFfmpegPath(), + sourceUrl, + seek.toFixed(3), + FRAME_TIMEOUT_MS, + { filter: CUSTOM_THUMBNAIL_FRAME_FILTER, quality: "3" }, + ); + const data = new Uint8Array(frame); + if (!isJpegData(data) || data.byteLength > 8 * 1024 * 1024) { + return { success: false, error: "Couldn't read that frame" }; + } + + await saveCustomThumbnail(video, data, { source: "frame", time: seek }); + return { success: true }; + } catch (error) { + console.error(`[custom-thumbnail] Frame failed for ${video.id}`, error); + return { success: false, error: "Couldn't read that frame" }; + } +} + +export async function resetCustomThumbnail( + videoId: Video.VideoId, +): Promise { + const video = await loadOwnedVideo(videoId); + if (typeof video === "string") return { success: false, error: video }; + + const previous = getCustomThumbnailMetadata(video.metadata); + if (!previous) return { success: true }; + + try { + await writeCustomThumbnailMetadata(video, null); + } catch (error) { + console.error(`[custom-thumbnail] Reset failed for ${video.id}`, error); + return { success: false, error: "Couldn't reset the thumbnail" }; + } + await deleteThumbnailObject(video, previous.file); + return { success: true }; +} diff --git a/apps/web/actions/videos/get-og-image.tsx b/apps/web/actions/videos/get-og-image.tsx index dc46af1c98f..c105acd1b33 100644 --- a/apps/web/actions/videos/get-og-image.tsx +++ b/apps/web/actions/videos/get-og-image.tsx @@ -1,6 +1,10 @@ import { db } from "@cap/database"; import { users, videos } from "@cap/database/schema"; import { findScreenshotObjectKey, Storage } from "@cap/web-backend"; +import { + getCustomThumbnailKey, + getCustomThumbnailVersion, +} from "@cap/web-backend/src/Storage/custom-thumbnail"; import { getPublishedRecordingThumbnailKey } from "@cap/web-backend/src/Storage/recording-output"; import type { Video } from "@cap/web-domain"; import { eq } from "drizzle-orm"; @@ -10,7 +14,10 @@ import { renderVideoOg, renderVideoOgStatus } from "@/lib/og/video-og"; import { runPromise } from "@/lib/server"; import { decodeStorageVideo } from "@/lib/video-storage"; -export async function generateVideoOgImage(videoId: Video.VideoId) { +export async function generateVideoOgImage( + videoId: Video.VideoId, + requestedVersion: string | null = null, +) { const videoData = await getData(videoId); if (!videoData) return renderVideoOgStatus("not-found"); @@ -20,6 +27,21 @@ export async function generateVideoOgImage(videoId: Video.VideoId) { if (video.password) return renderVideoOgStatus("password"); if (video.public === false) return renderVideoOgStatus("locked"); + // A version only names an image that exists. Anything else goes to the + // current URL, so made-up versions cannot each cost a fresh render. + const currentVersion = getCustomThumbnailVersion(video.metadata); + if (requestedVersion !== null && requestedVersion !== currentVersion) { + const location = new URLSearchParams({ videoId }); + if (currentVersion) location.set("v", currentVersion); + return new Response(null, { + status: 307, + headers: { + Location: `/api/video/og?${location}`, + "Cache-Control": "public, max-age=60", + }, + }); + } + let screenshotUrl: string | undefined; try { @@ -27,7 +49,9 @@ export async function generateVideoOgImage(videoId: Video.VideoId) { const [bucket] = yield* Storage.getAccessForVideo( decodeStorageVideo(video), ); - const publishedThumbnail = getPublishedRecordingThumbnailKey(video); + const publishedThumbnail = + getCustomThumbnailKey(video) ?? + getPublishedRecordingThumbnailKey(video); if (publishedThumbnail) { screenshotUrl = yield* bucket.getSignedObjectUrl(publishedThumbnail); return; diff --git a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx index 2a802a9c2ff..e9cba262dc8 100644 --- a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx +++ b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx @@ -10,6 +10,7 @@ import { } from "@cap/ui"; import { calculateStrokeDashoffset, getProgressCircleConfig } from "@cap/utils"; import type { SpaceRuleSource, ViewerSettingKey } from "@cap/web-backend"; +import { getCustomThumbnailMetadata } from "@cap/web-backend/src/Storage/custom-thumbnail"; import type { Folder, ImageUpload, Video } from "@cap/web-domain"; import { faChartSimple } from "@fortawesome/free-solid-svg-icons/faChartSimple"; import { faCheck } from "@fortawesome/free-solid-svg-icons/faCheck"; @@ -29,6 +30,7 @@ import { useMutation, useQueryClient } from "@tanstack/react-query"; import clsx from "clsx"; import { Effect, Option } from "effect"; import { FolderInput } from "lucide-react"; +import dynamic from "next/dynamic"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { type PropsWithChildren, useEffect, useRef, useState } from "react"; @@ -65,6 +67,11 @@ import { CapCardAnalytics, capAnalyticsHref } from "./CapCardAnalytics"; import { CapCardButton } from "./CapCardButton"; import { CapCardContent } from "./CapCardContent"; +const CustomThumbnailDialog = dynamic( + () => import("@/components/custom-thumbnail/CustomThumbnailDialog"), + { ssr: false }, +); + const { circumference } = getProgressCircleConfig(); function getProgressStatusText( @@ -179,6 +186,8 @@ export const CapCard = ({ const [isDragging, setIsDragging] = useState(false); const [isSettingsDialogOpen, setIsSettingsDialogOpen] = useState(false); const [isMoveDialogOpen, setIsMoveDialogOpen] = useState(false); + const [thumbnailDialogMounted, setThumbnailDialogMounted] = useState(false); + const [isThumbnailDialogOpen, setIsThumbnailDialogOpen] = useState(false); const { user, setUpgradeModalOpen } = useDashboardContext(); const [editUpgradeModalOpen, setEditUpgradeModalOpen] = useState(false); @@ -231,6 +240,7 @@ export const CapCard = ({ }; const isOwner = userId === cap.ownerId; + const customThumbnail = getCustomThumbnailMetadata(cap.metadata); const moveEnabled = Boolean(canMove && moveLocation && moveRootLabel); const dragEnabled = moveEnabled && (!anyCapSelected || isSelected); @@ -591,6 +601,19 @@ export const CapCard = ({

Edit video

)} + {cap.isScreenshot !== true && ( + { + e.stopPropagation(); + setThumbnailDialogMounted(true); + setIsThumbnailDialogOpen(true); + }} + className="flex gap-2 items-center rounded-lg" + > + +

Thumbnail

+
+ )} { if (!user.isPro) setUpgradeModalOpen(true); @@ -623,6 +646,20 @@ export const CapCard = ({ + {thumbnailDialogMounted && ( + + )} } diff --git a/apps/web/app/(org)/dashboard/caps/components/SharingDialog.tsx b/apps/web/app/(org)/dashboard/caps/components/SharingDialog.tsx index 1c4cf40fc2c..8482342b843 100644 --- a/apps/web/app/(org)/dashboard/caps/components/SharingDialog.tsx +++ b/apps/web/app/(org)/dashboard/caps/components/SharingDialog.tsx @@ -35,6 +35,11 @@ import { import { useDashboardContext } from "@/app/(org)/dashboard/Contexts"; import type { Spaces } from "@/app/(org)/dashboard/dashboard-data"; import type { CurrentUser } from "@/app/Layout/AuthContext"; +import { + DEFAULT_EMBED_TOGGLES, + EmbedOptionsFields, + embedTogglesToOptions, +} from "@/components/embed/EmbedOptionsFields"; import { FontAwesomeIcon } from "@/components/FontAwesomeIcon"; import { SignedImageUrl } from "@/components/SignedImageUrl"; import { buildEmbedCode } from "@/lib/embed-code"; @@ -362,7 +367,15 @@ export const SharingDialog: React.FC = ({ }); }; - const embedCode = useEmbedCode(capId); + const [embedToggles, setEmbedToggles] = useState(DEFAULT_EMBED_TOGGLES); + const canHideEmbedBranding = Boolean(user?.isPro); + const embedCode = useEmbedCode( + capId, + embedTogglesToOptions({ + ...embedToggles, + showBranding: canHideEmbedBranding ? embedToggles.showBranding : true, + }), + ); const handleCopyEmbedCode = async () => { try { @@ -720,6 +733,11 @@ export const SharingDialog: React.FC = ({ {embedCode} + + + + )} + + + + + + + + ); +}; + +function Swatch({ + label, + color, + selected, + disabled, + onSelect, +}: { + label: string; + color: string; + selected: boolean; + disabled: boolean; + onSelect: () => void; +}) { + const foreground = resolvePlayerBrand(color)?.foreground; + return ( + + ); +} + +function PlayerPreview({ color }: { color: string | null }) { + const attributes = playerBrandAttributes(resolvePlayerBrand(color)); + return ( +
+
+
+ +
+
+
+
+
+
+
+
+
+
+ + 0:42 / 1:52 + +
+
+ ); +} diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts index 0c256a318ea..1ee5669f6c9 100644 --- a/apps/web/app/api/mobile/[...route]/route.ts +++ b/apps/web/app/api/mobile/[...route]/route.ts @@ -32,6 +32,7 @@ import { VideosPolicy, VideosRepo, } from "@cap/web-backend"; +import { getCustomThumbnailKey } from "@cap/web-backend/src/Storage/custom-thumbnail"; import { getPublishedRecordingThumbnailKey } from "@cap/web-backend/src/Storage/recording-output"; import { Comment, @@ -465,7 +466,12 @@ const getMobileThumbnailUrl = Effect.fn("Mobile.getThumbnailUrl")(function* ( const [video] = maybeVideo.value; const [bucket] = yield* storage.getAccessForVideo(video); - const publishedThumbnail = getPublishedRecordingThumbnailKey(video); + const publishedThumbnail = + getCustomThumbnailKey({ + id: video.id, + ownerId: video.ownerId, + metadata: Option.getOrNull(video.metadata), + }) ?? getPublishedRecordingThumbnailKey(video); if (publishedThumbnail) { return yield* bucket.getSignedObjectUrl(publishedThumbnail); } diff --git a/apps/web/app/api/thumbnail/route.ts b/apps/web/app/api/thumbnail/route.ts index 6778a1984f9..d0570ba5d63 100644 --- a/apps/web/app/api/thumbnail/route.ts +++ b/apps/web/app/api/thumbnail/route.ts @@ -6,6 +6,7 @@ import { Storage, VideosPolicy, } from "@cap/web-backend"; +import { getCustomThumbnailKey } from "@cap/web-backend/src/Storage/custom-thumbnail"; import { getPublishedRecordingThumbnailKey } from "@cap/web-backend/src/Storage/recording-output"; import { Policy, Video } from "@cap/web-domain"; import { eq } from "drizzle-orm"; @@ -72,7 +73,8 @@ export async function GET(request: NextRequest) { try { const [bucket] = await Storage.getAccessForVideo(video).pipe(runPromise); - let thumbnailKey = getPublishedRecordingThumbnailKey(video); + let thumbnailKey = + getCustomThumbnailKey(query) ?? getPublishedRecordingThumbnailKey(video); if (!thumbnailKey) { const listResponse = await bucket .listObjects({ prefix }) diff --git a/apps/web/app/api/video/og/route.tsx b/apps/web/app/api/video/og/route.tsx index 3a6735f3673..015f81371ad 100644 --- a/apps/web/app/api/video/og/route.tsx +++ b/apps/web/app/api/video/og/route.tsx @@ -7,6 +7,7 @@ import { renderVideoOgStatus } from "@/lib/og/video-og"; export const maxDuration = 30; const VIDEO_ID_PATTERN = /^[0-9A-Za-z_-]{1,64}$/; +const THUMBNAIL_VERSION_PATTERN = /^[0-9a-z]{1,32}$/; export async function GET(req: NextRequest) { const params = req.nextUrl.searchParams; @@ -15,14 +16,19 @@ export async function GET(req: NextRequest) { return renderVideoOgStatus("not-found"); } - if (Array.from(params.keys()).length > 1) { + const version = params.get("v"); + const canonical = new URLSearchParams({ videoId }); + if (version && THUMBNAIL_VERSION_PATTERN.test(version)) { + canonical.set("v", version); + } + if (params.toString() !== canonical.toString()) { return new Response(null, { status: 308, headers: { - Location: `${req.nextUrl.pathname}?${new URLSearchParams({ videoId })}`, + Location: `${req.nextUrl.pathname}?${canonical}`, }, }); } - return generateVideoOgImage(Video.VideoId.make(videoId)); + return generateVideoOgImage(Video.VideoId.make(videoId), canonical.get("v")); } diff --git a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx index 889726b9d4d..4c135b7a081 100644 --- a/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx +++ b/apps/web/app/embed/[videoId]/_components/EmbedVideo.tsx @@ -23,6 +23,7 @@ import { } from "@/app/s/[videoId]/_components/RecordingInProgress"; import type { UploadProgress } from "@/app/s/[videoId]/_components/upload-progress"; import { formatChaptersAsVTT } from "@/app/s/[videoId]/_components/utils/transcript-utils"; +import { type PlayerBrand, playerBrandAttributes } from "@/lib/player-brand"; import { formatPlayerTime } from "@/lib/player-time"; import type { SharePageBranding } from "@/lib/share-branding"; import type { ShareCallToAction } from "@/lib/share-call-to-action"; @@ -60,12 +61,17 @@ export const EmbedVideo = forwardRef< hasActiveUpload: boolean | undefined; }; branding: SharePageBranding | null; + posterUrl?: string | null; + playerBrand?: PlayerBrand | null; user: typeof userSelectProps | null; comments: CommentWithAuthor[]; chapters?: { title: string; start: number }[]; ownerName?: string | null; ownerImageUrl?: string | null; autoplay?: boolean; + muted?: boolean; + showTitle?: boolean; + showOwner?: boolean; /** Seconds to open at, from the embed URL's `?t=`. */ startTime?: number | null; minimal?: boolean; @@ -79,12 +85,17 @@ export const EmbedVideo = forwardRef< { data, branding, + posterUrl = null, + playerBrand = null, user: _user, comments: _comments, chapters = [], ownerName, ownerImageUrl, autoplay = false, + muted = false, + showTitle = true, + showOwner = true, startTime = null, minimal = false, defaultPlaybackSpeed, @@ -265,6 +276,7 @@ export const EmbedVideo = forwardRef< <>
setIsPlaying(true)} onPauseCapture={() => setIsPlaying(false)} @@ -295,10 +307,12 @@ export const EmbedVideo = forwardRef< captionsSrc={captionsDisabled ? "" : subtitleUrl || ""} videoRef={videoRef} autoplay={autoplay} + muted={muted} defaultPlaybackSpeed={defaultPlaybackSpeed} enableCrossOrigin={enableCrossOrigin} hasActiveUpload={data.hasActiveUpload} callToAction={callToAction} + posterUrl={posterUrl} /> ) : ( )}
@@ -323,52 +339,67 @@ export const EmbedVideo = forwardRef< {!isPlaying && (
- -
- {ownerName && ( - - )} -
- e.stopPropagation()} - > -

- {data.name} -

-
-
- {ownerName && ( -

- {ownerName} -

+ {(showTitle || (showOwner && ownerName)) && ( + +
+ {showOwner && ownerName && ( + + )} +
+ {showTitle && ( + e.stopPropagation()} + > +

+ {data.name} +

+
)} - {ownerName && longestDuration > 0 && ( - <> - • -

- {formatPlayerTime(longestDuration)} +

+ {showOwner && ownerName && ( +

+ {ownerName}

- - )} + )} + {(showOwner ? Boolean(ownerName) : showTitle) && + longestDuration > 0 && ( + <> + {showOwner && ( + + • + + )} +

+ {formatPlayerTime(longestDuration)} +

+ + )} +
-
- + + )} {branding?.type === "custom" ? ( 0 - ? parsedStartTime - : null; + // there too. Repeated params arrive as an array; the parser takes the first. + const embedOptions = parseEmbedOptions(searchParams); return Effect.gen(function* () { const videosPolicy = yield* VideosPolicy; @@ -160,6 +162,7 @@ export default async function EmbedVideoPage( organizationName: organizations.name, organizationIconUrl: organizations.iconUrl, shareableLinkIconUrl: organizations.shareableLinkIconUrl, + organizationMetadata: organizations.metadata, hasActiveUpload: sql`${videoUploads.videoId} IS NOT NULL AND ${videos.isScreenshot} = false`.mapWith( Boolean, @@ -192,8 +195,7 @@ export default async function EmbedVideoPage( {!data.needsPassword && ( )} @@ -209,9 +211,8 @@ export default async function EmbedVideoPage( } async function EmbedContent({ - video, - autoplay, - startTime, + video: { organizationMetadata, ...video }, + options, minimal, }: { video: Omit & { @@ -221,9 +222,11 @@ async function EmbedContent({ organizationName: (typeof organizations.$inferSelect)["name"] | null; organizationIconUrl: (typeof organizations.$inferSelect)["iconUrl"]; shareableLinkIconUrl: (typeof organizations.$inferSelect)["shareableLinkIconUrl"]; + organizationMetadata: + | (typeof organizations.$inferSelect)["metadata"] + | null; }; - autoplay: boolean; - startTime: number | null; + options: EmbedOptions; minimal: boolean; }) { const [user, sharedSpaces, [videoOwner]] = await Promise.all([ @@ -358,65 +361,81 @@ async function EmbedContent({ ); } - const [commentsQuery, ownerImageUrl, branding] = await Promise.all([ - db() - .select({ - id: comments.id, - content: comments.content, - timestamp: comments.timestamp, - type: comments.type, - authorId: comments.authorId, - videoId: comments.videoId, - createdAt: comments.createdAt, - updatedAt: comments.updatedAt, - parentCommentId: comments.parentCommentId, - mediaKey: comments.mediaKey, - mediaDuration: comments.mediaDuration, - mediaMeta: comments.mediaMeta, - authorName: users.name, - }) - .from(comments) - .leftJoin(users, eq(comments.authorId, users.id)) - .where(eq(comments.videoId, video.id)), - Effect.gen(function* () { - const imageUploads = yield* ImageUploads; - return yield* Option.fromNullable(videoOwner?.image).pipe( - Option.map(imageUploads.resolveImageUrl), - Effect.transposeOption, - Effect.map(Option.getOrNull), - ); - }).pipe(EffectRuntime.runPromise), - Effect.gen(function* () { - const brandingInput = { - owner: { isPro: ownerIsProUser }, - orgSettings: video.orgSettings, - organizationName: video.organizationName, - }; - const icon = video.orgSettings?.shareableLinkUseOrganizationIcon - ? video.organizationIconUrl - : video.shareableLinkIconUrl; + const customThumbnailKey = getCustomThumbnailKey(video); + const [commentsQuery, ownerImageUrl, branding, posterUrl] = await Promise.all( + [ + db() + .select({ + id: comments.id, + content: comments.content, + timestamp: comments.timestamp, + type: comments.type, + authorId: comments.authorId, + videoId: comments.videoId, + createdAt: comments.createdAt, + updatedAt: comments.updatedAt, + parentCommentId: comments.parentCommentId, + mediaKey: comments.mediaKey, + mediaDuration: comments.mediaDuration, + mediaMeta: comments.mediaMeta, + authorName: users.name, + }) + .from(comments) + .leftJoin(users, eq(comments.authorId, users.id)) + .where(eq(comments.videoId, video.id)), + Effect.gen(function* () { + const imageUploads = yield* ImageUploads; + return yield* Option.fromNullable(videoOwner?.image).pipe( + Option.map(imageUploads.resolveImageUrl), + Effect.transposeOption, + Effect.map(Option.getOrNull), + ); + }).pipe(EffectRuntime.runPromise), + Effect.gen(function* () { + const brandingInput = { + owner: { isPro: ownerIsProUser }, + orgSettings: video.orgSettings, + organizationName: video.organizationName, + }; + const icon = video.orgSettings?.shareableLinkUseOrganizationIcon + ? video.organizationIconUrl + : video.shareableLinkIconUrl; - if (!ownerIsProUser || !icon || minimal) { - return getSharePageBranding(brandingInput); - } + if (!ownerIsProUser || !icon || minimal || !options.showBranding) { + return getSharePageBranding(brandingInput); + } - const imageUploads = yield* ImageUploads; - const imageUrl = yield* imageUploads.resolveImageUrl(icon); + const imageUploads = yield* ImageUploads; + const imageUrl = yield* imageUploads.resolveImageUrl(icon); - return getSharePageBranding({ - ...brandingInput, - organizationIconUrl: imageUrl, - shareableLinkIconUrl: imageUrl, - }); - }).pipe(EffectRuntime.runPromise), - ]); + return getSharePageBranding({ + ...brandingInput, + organizationIconUrl: imageUrl, + shareableLinkIconUrl: imageUrl, + }); + }).pipe(EffectRuntime.runPromise), + customThumbnailKey + ? getCustomThumbnailUrl( + { ...video, owner: { id: video.ownerId } }, + customThumbnailKey, + ) + : Promise.resolve(null), + ], + ); + + const playerBrand = getPlayerBrand({ + ownerIsPro: ownerIsProUser, + organizationMetadata, + }); const { orgSettings: _orgSettings, ...embedData } = video; return ( svg { + color: var(--player-brand-foreground); +} + +[data-player-brand-accent] .player-brand-range, +[data-player-brand-accent] .player-brand-thumb { + background-color: var(--player-brand-accent); +} + +/* The control bar scopes its own `.dark`, which resets `--ring`, so it needs + the brand ring set again. Light bars (the timeline deck) keep Cap's ring. */ +[data-player-brand-accent] [data-slot="media-player"], +[data-player-brand-accent] [data-slot="media-player-controls"].dark { + --ring: var(--player-brand-ring); +} diff --git a/apps/web/app/s/[videoId]/Share.tsx b/apps/web/app/s/[videoId]/Share.tsx index bc1ffaeb827..e8c1a4d2590 100644 --- a/apps/web/app/s/[videoId]/Share.tsx +++ b/apps/web/app/s/[videoId]/Share.tsx @@ -188,6 +188,7 @@ interface ShareProps { data: VideoData; initialPlaybackUrl?: Promise; serverPlaybackUrl?: string | null; + posterUrl?: string | null; comments: MaybePromise; views: MaybePromise; screenshotImageUrl?: string | null; @@ -324,6 +325,7 @@ export const Share = ({ data, initialPlaybackUrl, serverPlaybackUrl = null, + posterUrl = null, comments, views, screenshotImageUrl, @@ -969,6 +971,7 @@ export const Share = ({ ; mediaPlayerClassName?: string; autoplay?: boolean; + muted?: boolean; enableCrossOrigin?: boolean; hasActiveUpload: boolean | undefined; blockPlaybackDuringProcessing?: boolean; @@ -147,6 +148,7 @@ interface Props { showFloatingVolumeControl?: boolean; onUploadComplete?: () => void; callToAction?: ShareCallToAction | null; + posterUrl?: string | null; } export function CapVideoPlayer({ @@ -162,6 +164,7 @@ export function CapVideoPlayer({ videoRef, mediaPlayerClassName, autoplay = false, + muted = false, enableCrossOrigin = false, hasActiveUpload, blockPlaybackDuringProcessing = false, @@ -187,6 +190,7 @@ export function CapVideoPlayer({ showFloatingVolumeControl = false, onUploadComplete, callToAction = null, + posterUrl = null, }: Props) { const [currentCue, setCurrentCue] = useState(""); const [controlsVisible, setControlsVisible] = useState(false); @@ -881,24 +885,35 @@ export function CapVideoPlayer({ )} - + {posterUrl ? ( + + ) : ( + + )} {resolvedSrc.data && ( {chaptersSrc && } {captionsSrc && ( @@ -1014,7 +1030,7 @@ export function CapVideoPlayer({ exit={{ opacity: 0, y: 10 }} transition={{ duration: 0.2 }} onClick={() => videoRef.current?.play()} - className="flex absolute inset-0 z-10 justify-center items-center m-auto bg-blue-500 rounded-full transition-colors transform cursor-pointer hover:bg-blue-600 size-12 xs:size-20 md:size-32" + className="player-brand-play flex absolute inset-0 z-10 justify-center items-center m-auto bg-blue-500 rounded-full transition-colors transform cursor-pointer hover:bg-blue-600 size-12 xs:size-20 md:size-32" > (null); const [currentCue, setCurrentCue] = useState(""); @@ -812,7 +816,7 @@ export function HLSVideoPlayer({ exit={{ opacity: 0, y: 10 }} transition={{ duration: 0.2 }} onClick={() => videoRef.current?.play()} - className="flex absolute inset-0 z-10 justify-center items-center m-auto bg-blue-500 rounded-full transition-colors transform cursor-pointer hover:bg-blue-600 size-12 xs:size-20 md:size-32" + className="player-brand-play flex absolute inset-0 z-10 justify-center items-center m-auto bg-blue-500 rounded-full transition-colors transform cursor-pointer hover:bg-blue-600 size-12 xs:size-20 md:size-32" > )} - + {posterUrl && !isLiveSegments && !isBackgroundPreview ? ( + + ) : ( + + )} diff --git a/apps/web/app/s/[videoId]/_components/ShareHeader.tsx b/apps/web/app/s/[videoId]/_components/ShareHeader.tsx index bdf68a23bea..2ee66dfb6d4 100644 --- a/apps/web/app/s/[videoId]/_components/ShareHeader.tsx +++ b/apps/web/app/s/[videoId]/_components/ShareHeader.tsx @@ -11,6 +11,7 @@ import { Logo, } from "@cap/ui"; import type { ViewerSettingKey } from "@cap/web-backend"; +import { getCustomThumbnailMetadata } from "@cap/web-backend/src/Storage/custom-thumbnail"; import { faChartSimple } from "@fortawesome/free-solid-svg-icons/faChartSimple"; import { faChevronDown } from "@fortawesome/free-solid-svg-icons/faChevronDown"; import { faEllipsis } from "@fortawesome/free-solid-svg-icons/faEllipsis"; @@ -28,6 +29,7 @@ import { Download, FolderInput, Globe2, + ImageIcon, Lock, MousePointer2, Pencil, @@ -122,6 +124,10 @@ const CallToActionDialog = dynamic( ), { ssr: false }, ); +const CustomThumbnailDialog = dynamic( + () => import("@/components/custom-thumbnail/CustomThumbnailDialog"), + { ssr: false }, +); const DuplicateCapMenuItem = dynamic(() => import("./DuplicateCapMenuItem"), { ssr: false, }); @@ -163,6 +169,7 @@ export const ShareHeader = ({ spacesData = null, branding, canManageSharePageBranding = false, + brandColor = null, canDownload = false, canMoveToFolder = false, hasEdits = false, @@ -197,6 +204,7 @@ export const ShareHeader = ({ spacesData?: Spaces[] | null; branding?: SharePageBranding | null; canManageSharePageBranding?: boolean; + brandColor?: string | null; canDownload?: boolean; canMoveToFolder?: boolean; hasEdits?: boolean; @@ -234,6 +242,8 @@ export const ShareHeader = ({ const [deleteDialogMounted, setDeleteDialogMounted] = useState(false); const [ctaDialogMounted, setCtaDialogMounted] = useState(false); const [isCtaDialogOpen, setIsCtaDialogOpenRaw] = useState(false); + const [thumbnailDialogMounted, setThumbnailDialogMounted] = useState(false); + const [isThumbnailDialogOpen, setIsThumbnailDialogOpenRaw] = useState(false); const [isSettingsDialogOpen, setIsSettingsDialogOpenRaw] = useState(false); const [isPasswordDialogOpen, setIsPasswordDialogOpenRaw] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpenRaw] = useState(false); @@ -261,6 +271,10 @@ export const ShareHeader = ({ if (open) setCtaDialogMounted(true); setIsCtaDialogOpenRaw(open); }; + const setIsThumbnailDialogOpen = (open: boolean) => { + if (open) setThumbnailDialogMounted(true); + setIsThumbnailDialogOpenRaw(open); + }; const [passwordProtected, setPasswordProtected] = useState( Boolean(data.hasPassword), ); @@ -672,6 +686,10 @@ export const ShareHeader = ({ !data.isScreenshot && !data.hasActiveUpload && (data.source.type === "desktopMP4" || data.source.type === "webMP4"); + const customThumbnail = getCustomThumbnailMetadata(data.metadata); + const canPickThumbnailFrame = + !data.hasActiveUpload && + (data.source.type === "desktopMP4" || data.source.type === "webMP4"); const handleEditVideo = () => { if (userIsOwnerAndNotPro) { setUpgradeModalOpen(true); @@ -882,6 +900,7 @@ export const ShareHeader = ({ setIsShareLinkDialogOpen(false); setIsSharingDialogOpen(true); }} + ownerIsPro={data.owner.isPro} /> )} {isOwner && ( @@ -934,6 +953,17 @@ export const ShareHeader = ({ callToAction={data.callToAction ?? null} onSaved={refresh} onUpgradeRequest={() => setUpgradeModalOpen(true)} + brandColor={brandColor} + /> + )} + {thumbnailDialogMounted && ( + )} {deleteDialogMounted && ( @@ -1301,6 +1331,20 @@ export const ShareHeader = ({ ) : null} + {!data.isScreenshot && ( + setIsThumbnailDialogOpen(true)} + className="flex items-center gap-2 rounded-lg" + > + +

Thumbnail

+ {customThumbnail && ( + + Custom + + )} +
+ )} { if (!user.isPro) setUpgradeModalOpen(true); diff --git a/apps/web/app/s/[videoId]/_components/ShareLinkDialog.tsx b/apps/web/app/s/[videoId]/_components/ShareLinkDialog.tsx index 0d3bdce6f47..3b074467d39 100644 --- a/apps/web/app/s/[videoId]/_components/ShareLinkDialog.tsx +++ b/apps/web/app/s/[videoId]/_components/ShareLinkDialog.tsx @@ -5,6 +5,11 @@ import clsx from "clsx"; import { Check, CodeXml, Link2, Lock, Megaphone, X } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { toast } from "sonner"; +import { + DEFAULT_EMBED_TOGGLES, + EmbedOptionsFields, + embedTogglesToOptions, +} from "@/components/embed/EmbedOptionsFields"; import { FacebookIcon, GoogleIcon, @@ -90,6 +95,7 @@ export const ShareLinkDialog = ({ isPublic, canManageAccess = false, onManageAccess, + ownerIsPro = false, }: { open: boolean; onOpenChange: (open: boolean) => void; @@ -100,6 +106,7 @@ export const ShareLinkDialog = ({ isPublic: boolean; canManageAccess?: boolean; onManageAccess?: () => void; + ownerIsPro?: boolean; }) => { const { webUrl } = usePublicEnv(); const playback = useOptionalPlayback(); @@ -107,6 +114,7 @@ export const ShareLinkDialog = ({ const [withTimestamp, setWithTimestamp] = useState(false); const [startTime, setStartTime] = useState(0); const [copied, setCopied] = useState(false); + const [embedToggles, setEmbedToggles] = useState(DEFAULT_EMBED_TOGGLES); // The offset is sampled once, when the dialog opens. Subscribing would // re-render the dialog at frame rate while the video plays on behind it, and @@ -117,6 +125,7 @@ export const ShareLinkDialog = ({ setTab("social"); setWithTimestamp(false); setCopied(false); + setEmbedToggles(DEFAULT_EMBED_TOGGLES); }, [open, playback]); useEffect(() => { @@ -133,8 +142,17 @@ export const ShareLinkDialog = ({ ); const embedCode = useMemo( - () => buildEmbedCode({ webUrl, videoId, startTime: timestamp }), - [webUrl, videoId, timestamp], + () => + buildEmbedCode({ + webUrl, + videoId, + startTime: timestamp, + options: embedTogglesToOptions({ + ...embedToggles, + showBranding: ownerIsPro ? embedToggles.showBranding : true, + }), + }), + [webUrl, videoId, timestamp, embedToggles, ownerIsPro], ); const handleCopy = useCallback(async () => { @@ -252,6 +270,11 @@ export const ShareLinkDialog = ({ {embedCode}
+

Paste this wherever HTML is allowed. The player keeps its aspect ratio and fills the width it's given. diff --git a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx index 3fa1d947380..ec9cb8acb87 100644 --- a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx +++ b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx @@ -85,6 +85,7 @@ export const ShareVideo = forwardRef< }; initialPlaybackUrl?: Promise; serverPlaybackUrl?: string | null; + posterUrl?: string | null; comments: MaybePromise; chapters?: { title: string; start: number }[]; areChaptersDisabled?: boolean; @@ -113,6 +114,7 @@ export const ShareVideo = forwardRef< data, initialPlaybackUrl, serverPlaybackUrl = null, + posterUrl = null, comments, chapters = NO_CHAPTERS, areCaptionsDisabled, @@ -592,6 +594,7 @@ export const ShareVideo = forwardRef< } canRetryProcessing={canRetryProcessing} callToAction={callToAction} + posterUrl={posterUrl} /> ) : ( )} {showFinalizeRecordingControl && ( diff --git a/apps/web/app/s/[videoId]/_components/VideoPreviewGif.tsx b/apps/web/app/s/[videoId]/_components/VideoPreviewGif.tsx index 5333cef45c9..a6e5c968990 100644 --- a/apps/web/app/s/[videoId]/_components/VideoPreviewGif.tsx +++ b/apps/web/app/s/[videoId]/_components/VideoPreviewGif.tsx @@ -66,3 +66,29 @@ function PreviewGifImage({ /> ); } + +export function VideoPoster({ + src, + visible, +}: { + src: string; + visible: boolean; +}) { + return ( + + ); +} diff --git a/apps/web/app/s/[videoId]/_components/call-to-action/CallToActionDialog.tsx b/apps/web/app/s/[videoId]/_components/call-to-action/CallToActionDialog.tsx index 92a1bb1be11..bb376f4072e 100644 --- a/apps/web/app/s/[videoId]/_components/call-to-action/CallToActionDialog.tsx +++ b/apps/web/app/s/[videoId]/_components/call-to-action/CallToActionDialog.tsx @@ -69,8 +69,12 @@ const EMPTY_FORM: FormState = { const PREVIEW_PLACEHOLDER_LABEL = "Book a call"; -function formFromCallToAction(cta: ShareCallToAction | null): FormState { - if (!cta) return EMPTY_FORM; +function formFromCallToAction( + cta: ShareCallToAction | null, + brandColor: string | null, +): FormState { + if (!cta) + return brandColor ? { ...EMPTY_FORM, color: brandColor } : EMPTY_FORM; return { label: cta.label, url: cta.url, @@ -114,6 +118,7 @@ export function CallToActionDialog({ callToAction, onSaved, onUpgradeRequest, + brandColor = null, }: { open: boolean; onOpenChange: (open: boolean) => void; @@ -121,6 +126,7 @@ export function CallToActionDialog({ callToAction: ShareCallToAction | null; onSaved?: (callToAction: ShareCallToAction | null) => void; onUpgradeRequest: () => void; + brandColor?: string | null; }) { const isWorkspace = target.kind === "workspace"; const videoSource = target.kind === "video" ? target.source : null; @@ -129,7 +135,7 @@ export function CallToActionDialog({ const initialIncludeExisting = target.kind === "workspace" ? target.includeExisting : false; const [form, setForm] = useState(() => - formFromCallToAction(callToAction), + formFromCallToAction(callToAction, brandColor), ); const [includeExisting, setIncludeExisting] = useState( initialIncludeExisting, @@ -157,13 +163,13 @@ export function CallToActionDialog({ const opening = open && !wasOpenRef.current; wasOpenRef.current = open; if (!opening) return; - setForm(formFromCallToAction(callToAction)); + setForm(formFromCallToAction(callToAction, brandColor)); setIncludeExisting(initialIncludeExisting); setErrors({}); setTouched({}); setPreviewMode("end"); setPending(null); - }, [open, callToAction, initialIncludeExisting]); + }, [open, callToAction, brandColor, initialIncludeExisting]); const liveErrors = useMemo(() => { const result = validateCallToAction(form); @@ -515,6 +521,7 @@ export function CallToActionDialog({ update("color", color)} + brandColor={brandColor} />

@@ -839,18 +846,23 @@ function TextInput({ function ColorPicker({ value, onChange, + brandColor, }: { value: string; onChange: (color: string) => void; + brandColor: string | null; }) { const normalized = normalizeHexColor(value) ?? DEFAULT_CTA_COLOR; - const isCustom = !CTA_COLOR_PRESETS.some( - (preset) => preset.value === normalized, - ); + const presets = + brandColor && + !CTA_COLOR_PRESETS.some((preset) => preset.value === brandColor) + ? [{ name: "Brand", value: brandColor }, ...CTA_COLOR_PRESETS] + : CTA_COLOR_PRESETS; + const isCustom = !presets.some((preset) => preset.value === normalized); return (
- {CTA_COLOR_PRESETS.map((preset) => { + {presets.map((preset) => { const selected = preset.value === normalized; return ( + + +
+
+ {( + [ + { id: "frame", label: "Pick a frame" }, + { id: "upload", label: "Upload image" }, + ] as const + ).map((tab) => ( + + ))} +
+ + {canPickFrame && ( +
+
+
+
+ + + seekTo(Number(event.currentTarget.value)) + } + className="h-1.5 flex-1 cursor-pointer appearance-none rounded-full bg-gray-4 accent-blue-9 disabled:cursor-not-allowed disabled:opacity-50" + /> + + {formatClockWithHundredths(frameTime)} + +
+
+ )} + {mode === "upload" && ( +
+ + {image && ( +

+ Click the image to choose another. +

+ )} + {imageError && ( +

+ {imageError} +

+ )} +
+ )} + {!canPickFrame && ( +

+ Frames can be picked once the video finishes processing. +

+ )} +
+ +
+ {hasCustomThumbnail && ( + + )} +
+ + +
+
+ + + ); +} + +export default CustomThumbnailDialog; diff --git a/apps/web/components/embed/EmbedOptionsFields.tsx b/apps/web/components/embed/EmbedOptionsFields.tsx new file mode 100644 index 00000000000..2737dbf0b1e --- /dev/null +++ b/apps/web/components/embed/EmbedOptionsFields.tsx @@ -0,0 +1,92 @@ +"use client"; + +import { Switch } from "@cap/ui"; +import { useId } from "react"; +import type { EmbedOptions } from "@/lib/embed-options"; + +export type EmbedToggleOptions = Pick< + EmbedOptions, + "showTitle" | "showOwner" | "showBranding" | "autoplay" +>; + +export const DEFAULT_EMBED_TOGGLES: EmbedToggleOptions = { + showTitle: true, + showOwner: true, + showBranding: true, + autoplay: false, +}; + +/** + * Autoplay only works muted in browsers, so the toggle sets both. + */ +export const embedTogglesToOptions = ( + toggles: EmbedToggleOptions, +): Partial> => ({ + ...toggles, + muted: toggles.autoplay, +}); + +const ROWS: { + key: keyof EmbedToggleOptions; + label: string; + description?: string; + pro?: boolean; +}[] = [ + { key: "showTitle", label: "Title" }, + { key: "showOwner", label: "Owner" }, + { key: "showBranding", label: "Logo", pro: true }, + { + key: "autoplay", + label: "Autoplay", + description: "Starts muted, as browsers require", + }, +]; + +export function EmbedOptionsFields({ + value, + onChange, + canHideBranding, +}: { + value: EmbedToggleOptions; + onChange: (value: EmbedToggleOptions) => void; + canHideBranding: boolean; +}) { + const id = useId(); + return ( +
+ Embed options + {ROWS.map((row) => { + const locked = row.pro === true && !canHideBranding; + const inputId = `${id}-${row.key}`; + return ( +
+ + onChange({ ...value, [row.key]: checked }) + } + /> + +
+ ); + })} +
+ ); +} diff --git a/apps/web/lib/custom-thumbnail.ts b/apps/web/lib/custom-thumbnail.ts new file mode 100644 index 00000000000..cef951f7976 --- /dev/null +++ b/apps/web/lib/custom-thumbnail.ts @@ -0,0 +1,74 @@ +export const CUSTOM_THUMBNAIL_MAX_DIMENSION = 1920; + +/** + * Server actions accept 1 MB bodies. Images are re-encoded in the browser to + * a JPEG under this, which leaves room for the multipart envelope. + */ +export const CUSTOM_THUMBNAIL_MAX_BYTES = 900 * 1024; + +export const CUSTOM_THUMBNAIL_MAX_SOURCE_BYTES = 20 * 1024 * 1024; + +export const CUSTOM_THUMBNAIL_ACCEPT = "image/png,image/jpeg,image/webp"; + +const ACCEPTED_SOURCE_TYPES = new Set(CUSTOM_THUMBNAIL_ACCEPT.split(",")); + +export function validateThumbnailSourceFile(file: { + type: string; + size: number; +}): string | null { + if (!ACCEPTED_SOURCE_TYPES.has(file.type.toLowerCase())) { + return "Choose a PNG, JPEG or WebP image"; + } + if (file.size === 0) return "That image is empty"; + if (file.size > CUSTOM_THUMBNAIL_MAX_SOURCE_BYTES) { + return "Choose an image under 20 MB"; + } + return null; +} + +export function isJpegData(data: Uint8Array): boolean { + return ( + data.length > 4 && + data[0] === 0xff && + data[1] === 0xd8 && + data[2] === 0xff && + data[data.length - 2] === 0xff && + data[data.length - 1] === 0xd9 + ); +} + +export function fitThumbnailSize(width: number, height: number) { + if (!(width > 0) || !(height > 0)) return null; + const scale = Math.min( + 1, + CUSTOM_THUMBNAIL_MAX_DIMENSION / Math.max(width, height), + ); + return { + width: Math.max(1, Math.round(width * scale)), + height: Math.max(1, Math.round(height * scale)), + }; +} + +/** + * Seconds into the video to take a frame from, kept just inside its end so + * ffmpeg always has a frame to decode. Null for anything that is not a time. + */ +export function clampThumbnailFrameTime( + time: unknown, + duration: number | null | undefined, +): number | null { + if (typeof time !== "number" || !Number.isFinite(time) || time < 0) { + return null; + } + const rounded = Math.round(time * 1000) / 1000; + if ( + typeof duration === "number" && + Number.isFinite(duration) && + duration > 0 + ) { + return Math.min(rounded, Math.max(duration - 0.05, 0)); + } + return rounded; +} + +export const CUSTOM_THUMBNAIL_FRAME_FILTER = `scale='min(iw,${CUSTOM_THUMBNAIL_MAX_DIMENSION})':'min(ih,${CUSTOM_THUMBNAIL_MAX_DIMENSION})':force_original_aspect_ratio=decrease:force_divisible_by=2`; diff --git a/apps/web/lib/embed-code.ts b/apps/web/lib/embed-code.ts index 1b9bc4112ae..5eff204053c 100644 --- a/apps/web/lib/embed-code.ts +++ b/apps/web/lib/embed-code.ts @@ -1,3 +1,5 @@ +import { buildEmbedQuery, type EmbedOptions } from "@/lib/embed-options"; + /** * The ``; return { @@ -138,7 +142,7 @@ export const buildOEmbedVideo = ({ ...((parsedMaxWidth === undefined || parsedMaxWidth >= 1200) && (parsedMaxHeight === undefined || parsedMaxHeight >= 630) ? { - thumbnail_url: thumbnailUrl.toString(), + thumbnail_url: thumbnailUrl, thumbnail_width: 1200, thumbnail_height: 630, } diff --git a/apps/web/lib/og/poster-frame.ts b/apps/web/lib/og/poster-frame.ts index 80d4566592c..4e450f4469d 100644 --- a/apps/web/lib/og/poster-frame.ts +++ b/apps/web/lib/og/poster-frame.ts @@ -24,11 +24,18 @@ const SOURCE_LADDER = [ { videoType: "raw-preview", seek: "0" }, ]; -const grabFrame = ( +const OG_FRAME_FILTER = "scale=1120:-2"; +const OG_FRAME_QUALITY = "4"; + +export const grabFrame = ( ffmpeg: string, url: string, seek: string, timeoutMs: number, + { + filter = OG_FRAME_FILTER, + quality = OG_FRAME_QUALITY, + }: { filter?: string; quality?: string } = {}, ) => new Promise((resolve, reject) => { const proc = spawn( @@ -45,9 +52,9 @@ const grabFrame = ( "-frames:v", "1", "-vf", - "scale=1120:-2", + filter, "-q:v", - "4", + quality, "-f", "image2pipe", "-vcodec", diff --git a/apps/web/lib/player-brand.ts b/apps/web/lib/player-brand.ts new file mode 100644 index 00000000000..f2b56a7edab --- /dev/null +++ b/apps/web/lib/player-brand.ts @@ -0,0 +1,147 @@ +import type { CSSProperties } from "react"; +import { normalizeHexColor } from "@/lib/share-call-to-action"; + +export const PLAYER_BRAND_INK = "#111113"; +export const PLAYER_BRAND_WHITE = "#FFFFFF"; + +/** + * The player's controls sit on a dark scrim over the video. Below this + * contrast against black a coloured progress bar or focus ring disappears, so + * those keep Cap's white instead (WCAG 2.2 non-text contrast is 3:1). + */ +export const MIN_CONTROL_CONTRAST = 3; + +export type PlayerBrand = { + color: string; + foreground: typeof PLAYER_BRAND_INK | typeof PLAYER_BRAND_WHITE; + hover: string; + accent: string | null; +}; + +const channelToLinear = (channel: number) => { + const value = channel / 255; + return value <= 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4; +}; + +const hexToRgb = (hex: string) => { + const value = Number.parseInt(hex.slice(1), 16); + return [(value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff] as const; +}; + +export function relativeLuminance(hex: string): number { + const [r, g, b] = hexToRgb(hex); + return ( + 0.2126 * channelToLinear(r) + + 0.7152 * channelToLinear(g) + + 0.0722 * channelToLinear(b) + ); +} + +export function contrastRatio(a: string, b: string): number { + const la = relativeLuminance(a); + const lb = relativeLuminance(b); + const [light, dark] = la > lb ? [la, lb] : [lb, la]; + return (light + 0.05) / (dark + 0.05); +} + +const mixHex = (hex: string, target: number, amount: number) => { + const toHex = (channel: number) => + Math.round(channel + (target - channel) * amount) + .toString(16) + .padStart(2, "0"); + const [r, g, b] = hexToRgb(hex); + return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase(); +}; + +export function resolvePlayerBrand(input: unknown): PlayerBrand | null { + const color = normalizeHexColor(input); + if (!color) return null; + const foreground = + contrastRatio(color, PLAYER_BRAND_WHITE) >= + contrastRatio(color, PLAYER_BRAND_INK) + ? PLAYER_BRAND_WHITE + : PLAYER_BRAND_INK; + return { + color, + foreground, + hover: + foreground === PLAYER_BRAND_WHITE + ? mixHex(color, 0, 0.12) + : mixHex(color, 255, 0.12), + accent: + contrastRatio(color, "#000000") >= MIN_CONTROL_CONTRAST ? color : null, + }; +} + +function hexToHslComponents(hex: string) { + const [r, g, b] = hexToRgb(hex).map((channel) => channel / 255) as [ + number, + number, + number, + ]; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + const lightness = (max + min) / 2; + const delta = max - min; + if (delta === 0) return `0 0% ${Math.round(lightness * 1000) / 10}%`; + const saturation = delta / (1 - Math.abs(2 * lightness - 1)); + const hue = + max === r + ? ((g - b) / delta) % 6 + : max === g + ? (b - r) / delta + 2 + : (r - g) / delta + 4; + const degrees = Math.round(((hue * 60 + 360) % 360) * 10) / 10; + return `${degrees} ${Math.round(saturation * 1000) / 10}% ${Math.round(lightness * 1000) / 10}%`; +} + +/** + * Props for the element that wraps a branded player. The CSS in + * `globals.css` keys off `data-player-brand`, so pages without a brand render + * exactly the classes they always have. + */ +export function playerBrandAttributes(brand: PlayerBrand | null): { + "data-player-brand"?: ""; + "data-player-brand-accent"?: ""; + style?: CSSProperties; +} { + if (!brand) return {}; + const style: Record = { + "--player-brand": brand.color, + "--player-brand-hover": brand.hover, + "--player-brand-foreground": brand.foreground, + }; + if (brand.accent) { + style["--player-brand-accent"] = brand.accent; + style["--player-brand-ring"] = hexToHslComponents(brand.accent); + } + return { + "data-player-brand": "", + ...(brand.accent ? { "data-player-brand-accent": "" as const } : {}), + style: style as CSSProperties, + }; +} + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +export function playerColorFromOrganizationMetadata( + metadata: unknown, +): string | null { + if (!isRecord(metadata) || !isRecord(metadata.branding)) return null; + return normalizeHexColor(metadata.branding.playerColor); +} + +export function getPlayerBrand({ + ownerIsPro, + organizationMetadata, +}: { + ownerIsPro: boolean; + organizationMetadata: unknown; +}): PlayerBrand | null { + if (!ownerIsPro) return null; + return resolvePlayerBrand( + playerColorFromOrganizationMetadata(organizationMetadata), + ); +} diff --git a/apps/web/lib/public-share-video.ts b/apps/web/lib/public-share-video.ts index 5ae904e8abf..c4104a5cb79 100644 --- a/apps/web/lib/public-share-video.ts +++ b/apps/web/lib/public-share-video.ts @@ -7,6 +7,7 @@ import { videos, videoUploads, } from "@cap/database/schema"; +import { getCustomThumbnailVersion } from "@cap/web-backend/src/Storage/custom-thumbnail"; import { Video } from "@cap/web-domain"; import { and, eq, isNotNull, isNull, sql } from "drizzle-orm"; @@ -33,6 +34,7 @@ export type PublicShareVideoCandidate = { width: number | null; height: number | null; duration: number | null; + thumbnailVersion?: string | null; }; export type PublicShareVideo = Omit< @@ -91,6 +93,9 @@ export async function getPublicShareVideo( width: videos.width, height: videos.height, duration: videos.duration, + customThumbnailFile: sql< + string | null + >`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.customThumbnail.file'))`, }) .from(videos) .innerJoin( @@ -114,7 +119,8 @@ export async function getPublicShareVideo( .where(and(eq(spaceVideos.videoId, videoId), isNotNull(spaces.password))) .limit(1); - const { videoSettings, organizationSettings, ...rest } = video; + const { videoSettings, organizationSettings, customThumbnailFile, ...rest } = + video; const candidate: PublicShareVideoCandidate = { ...rest, linkOpenToAnyone: Video.isLinkOpenToAnyone( @@ -122,6 +128,9 @@ export async function getPublicShareVideo( organizationSettings, ), hasInheritedPassword: Boolean(inheritedPassword), + thumbnailVersion: getCustomThumbnailVersion({ + customThumbnail: { file: customThumbnailFile }, + }), }; if (!isPublicShareVideoCandidateEligible(candidate)) return null; @@ -135,5 +144,6 @@ export async function getPublicShareVideo( width: candidate.width, height: candidate.height, duration: candidate.duration, + thumbnailVersion: candidate.thumbnailVersion, }; } diff --git a/apps/web/lib/share-playback.ts b/apps/web/lib/share-playback.ts index 50f947f9d89..4a704c54844 100644 --- a/apps/web/lib/share-playback.ts +++ b/apps/web/lib/share-playback.ts @@ -42,6 +42,25 @@ export const getSharePlaybackSource = ( .pipe(runPromise) .catch(() => null); +export const getCustomThumbnailUrl = ( + video: SharePlaybackVideo, + key: string, +): Promise => + Effect.gen(function* () { + const loadedVideo = yield* Schema.decodeUnknown(Video.Video)({ + ...video, + ownerId: video.owner.id, + bucketId: video.bucket, + folderId: null, + createdAt: video.createdAt.toISOString(), + updatedAt: video.updatedAt.toISOString(), + }); + const [bucket] = yield* Storage.getAccessForVideo(loadedVideo); + return yield* bucket.getSignedObjectUrl(key); + }) + .pipe(runPromise) + .catch(() => null); + export const getSharePlaybackUrl = (video: SharePlaybackVideo) => getSharePlaybackSource(video).then((source) => source?.url ?? null); diff --git a/apps/web/lib/share-video-metadata.ts b/apps/web/lib/share-video-metadata.ts index 73fb96cd29b..b782e22d10e 100644 --- a/apps/web/lib/share-video-metadata.ts +++ b/apps/web/lib/share-video-metadata.ts @@ -25,6 +25,22 @@ export type ShareVideoMetadataInput = { */ canonicalWebUrl?: string; advertiseIframelyPlayer?: boolean; + thumbnailVersion?: string | null; +}; + +export const buildVideoOgImageUrl = ({ + videoId, + webUrl, + thumbnailVersion, +}: { + videoId: string; + webUrl: string; + thumbnailVersion?: string | null; +}) => { + const url = new URL("/api/video/og", webUrl); + url.searchParams.set("videoId", videoId); + if (thumbnailVersion) url.searchParams.set("v", thumbnailVersion); + return url.toString(); }; export const getShareVideoUrls = ({ @@ -32,9 +48,10 @@ export const getShareVideoUrls = ({ sourceType, webUrl, canonicalWebUrl = webUrl, + thumbnailVersion, }: Pick< ShareVideoMetadataInput, - "videoId" | "sourceType" | "webUrl" | "canonicalWebUrl" + "videoId" | "sourceType" | "webUrl" | "canonicalWebUrl" | "thumbnailVersion" >) => { const shareUrl = new URL(`/s/${videoId}`, webUrl).toString(); const canonicalShareUrl = new URL( @@ -57,8 +74,11 @@ export const getShareVideoUrls = ({ const previewImageUrl = new URL("/api/video/preview", webUrl); previewImageUrl.searchParams.set("videoId", videoId); previewImageUrl.searchParams.set("fallback", "og"); - const ogImageUrl = new URL("/api/video/og", webUrl); - ogImageUrl.searchParams.set("videoId", videoId); + const ogImageUrl = buildVideoOgImageUrl({ + videoId, + webUrl, + thumbnailVersion, + }); const oEmbedUrl = new URL("/api/oembed", canonicalWebUrl); oEmbedUrl.searchParams.set("url", canonicalShareUrl); oEmbedUrl.searchParams.set("format", "json"); @@ -69,7 +89,7 @@ export const getShareVideoUrls = ({ streamUrl: streamUrl.toString(), streamContentType, previewImageUrl: previewImageUrl.toString(), - ogImageUrl: ogImageUrl.toString(), + ogImageUrl, oEmbedUrl: oEmbedUrl.toString(), }; }; @@ -83,13 +103,21 @@ export const buildShareVideoMetadata = ({ advertiseIframelyPlayer = false, width, height, + thumbnailVersion, }: ShareVideoMetadataInput): Metadata => { const urls = getShareVideoUrls({ videoId, sourceType, webUrl, canonicalWebUrl, + thumbnailVersion, }); + const ogImage = { + url: urls.ogImageUrl, + width: 1200, + height: 630, + type: "image/png", + }; const title = `${name} | Cap Recording`; const description = "Watch this video on Cap"; const hasDimensions = @@ -132,20 +160,17 @@ export const buildShareVideoMetadata = ({ title, description, ttl: 300, - images: [ - { - url: urls.previewImageUrl, - width: 480, - height: 270, - type: "image/gif", - }, - { - url: urls.ogImageUrl, - width: 1200, - height: 630, - type: "image/png", - }, - ], + images: thumbnailVersion + ? [ogImage] + : [ + { + url: urls.previewImageUrl, + width: 480, + height: 270, + type: "image/gif", + }, + ogImage, + ], videos: [ { url: urls.streamUrl, @@ -160,7 +185,9 @@ export const buildShareVideoMetadata = ({ card: "player", title, description, - images: [urls.previewImageUrl, urls.ogImageUrl], + images: thumbnailVersion + ? [urls.ogImageUrl] + : [urls.previewImageUrl, urls.ogImageUrl], players: { playerUrl: urls.playerUrl, streamUrl: urls.streamUrl, diff --git a/apps/web/lib/slack/unfurl.ts b/apps/web/lib/slack/unfurl.ts index 70d902f0d5a..d3984d556af 100644 --- a/apps/web/lib/slack/unfurl.ts +++ b/apps/web/lib/slack/unfurl.ts @@ -5,6 +5,7 @@ import { getPublicShareVideo, type PublicShareVideo, } from "@/lib/public-share-video"; +import { buildVideoOgImageUrl } from "@/lib/share-video-metadata"; import { sendSlackUnfurl } from "./client"; import { deleteSlackInstallationByTeamId, @@ -58,8 +59,11 @@ export const buildSlackVideoBlock = ({ const embedUrl = new URL(`/embed/${video.id}`, webUrl); embedUrl.searchParams.set("autoplay", "true"); embedUrl.searchParams.set("slack", "true"); - const thumbnailUrl = new URL("/api/video/og", webUrl); - thumbnailUrl.searchParams.set("videoId", video.id); + const thumbnailUrl = buildVideoOgImageUrl({ + videoId: video.id, + webUrl, + thumbnailVersion: video.thumbnailVersion, + }); return { type: "video", @@ -76,7 +80,7 @@ export const buildSlackVideoBlock = ({ }, video_url: embedUrl.toString(), alt_text: truncate(`Watch ${video.name}`, 200), - thumbnail_url: thumbnailUrl.toString(), + thumbnail_url: thumbnailUrl, provider_name: "Cap", ...(video.ownerName ? { author_name: truncate(video.ownerName, 49) } : {}), }; diff --git a/packages/database/types/metadata.ts b/packages/database/types/metadata.ts index 4b37a10454a..83533c9a8ee 100644 --- a/packages/database/types/metadata.ts +++ b/packages/database/types/metadata.ts @@ -132,6 +132,16 @@ export interface VideoMetadata { state: "pending" | "accepted" | "rejected"; rawFileKey?: string; }; + /** + * Thumbnail the owner picked. `file` lives under + * `//thumbnail/` and changes with every pick. + */ + customThumbnail?: { + file: string; + source: "upload" | "frame"; + time?: number; + updatedAt: string; + }; } export interface VideoCallToAction { diff --git a/packages/web-backend/src/Storage/custom-thumbnail.ts b/packages/web-backend/src/Storage/custom-thumbnail.ts new file mode 100644 index 00000000000..c526a70f88f --- /dev/null +++ b/packages/web-backend/src/Storage/custom-thumbnail.ts @@ -0,0 +1,68 @@ +export const CUSTOM_THUMBNAIL_DIRECTORY = "thumbnail"; + +const CUSTOM_THUMBNAIL_FILE_PATTERN = /^custom-[0-9a-z]{1,32}\.jpg$/; + +export type CustomThumbnailMetadata = { + file: string; + source: "upload" | "frame"; + time?: number; + updatedAt: string; +}; + +type CustomThumbnailVideo = { + id: string; + ownerId: string; + metadata: unknown; +}; + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +export function isCustomThumbnailFileName(file: unknown): file is string { + return typeof file === "string" && CUSTOM_THUMBNAIL_FILE_PATTERN.test(file); +} + +export function getCustomThumbnailMetadata( + metadata: unknown, +): CustomThumbnailMetadata | null { + if (!isRecord(metadata)) return null; + const value = metadata.customThumbnail; + if (!isRecord(value) || !isCustomThumbnailFileName(value.file)) return null; + return { + file: value.file, + source: value.source === "frame" ? "frame" : "upload", + ...(typeof value.time === "number" && Number.isFinite(value.time) + ? { time: value.time } + : {}), + updatedAt: typeof value.updatedAt === "string" ? value.updatedAt : "", + }; +} + +/** + * The thumbnail the owner picked, stored under the video's own prefix so + * deleting or duplicating the video carries it along like every other object. + * The metadata only holds the file name, never a full key. + */ +export function customThumbnailKey(video: { + id: string; + ownerId: string; + file: string; +}) { + return `${video.ownerId}/${video.id}/${CUSTOM_THUMBNAIL_DIRECTORY}/${video.file}`; +} + +export function getCustomThumbnailKey(video: CustomThumbnailVideo) { + const custom = getCustomThumbnailMetadata(video.metadata); + if (!custom) return undefined; + return customThumbnailKey({ + id: video.id, + ownerId: video.ownerId, + file: custom.file, + }); +} + +export function getCustomThumbnailVersion(metadata: unknown) { + const custom = getCustomThumbnailMetadata(metadata); + return custom ? custom.file.slice("custom-".length, -".jpg".length) : null; +} diff --git a/packages/web-backend/src/Videos/index.ts b/packages/web-backend/src/Videos/index.ts index 9a04261344e..20c09544688 100644 --- a/packages/web-backend/src/Videos/index.ts +++ b/packages/web-backend/src/Videos/index.ts @@ -16,6 +16,7 @@ import { Array as Arr, Cause, Effect, Exit, Option } from "effect"; import type { Schema } from "effect/Schema"; import { Database } from "../Database.ts"; +import { getCustomThumbnailKey } from "../Storage/custom-thumbnail.ts"; import { Storage as StorageService } from "../Storage/index.ts"; import { getPublishedRecordingThumbnailKey } from "../Storage/recording-output.ts"; import { Tinybird } from "../Tinybird/index.ts"; @@ -258,6 +259,14 @@ export class Videos extends Effect.Service()("Videos", { const getThumbnailURLForVideo = Effect.fn("Videos.getThumbnailURLForVideo")( function* (video: Video.Video) { const [bucket] = yield* storage.getAccessForVideo(video); + const customThumbnail = getCustomThumbnailKey({ + id: video.id, + ownerId: video.ownerId, + metadata: Option.getOrNull(video.metadata), + }); + if (customThumbnail) { + return Option.some(yield* bucket.getSignedObjectUrl(customThumbnail)); + } const publishedThumbnail = getPublishedRecordingThumbnailKey(video); if (publishedThumbnail) { return Option.some( diff --git a/packages/web-backend/src/index.ts b/packages/web-backend/src/index.ts index 9cbf5176975..742ad3c7ae1 100644 --- a/packages/web-backend/src/index.ts +++ b/packages/web-backend/src/index.ts @@ -15,6 +15,7 @@ export { S3Buckets } from "./S3Buckets/index.ts"; export { Spaces } from "./Spaces/index.ts"; export { SpacesPolicy } from "./Spaces/SpacesPolicy.ts"; export { attachmentContentDisposition } from "./Storage/content-disposition.ts"; +export * from "./Storage/custom-thumbnail.ts"; export * from "./Storage/GoogleDrive.ts"; export { Storage } from "./Storage/index.ts"; export {