Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(web): use shared renderer for video OG images

Richie McIlroy4dc01e5615d2
diff --git a/apps/web/actions/videos/get-og-image.tsx b/apps/web/actions/videos/get-og-image.tsx
index d7e7b4263..3117bd610 100644
--- a/apps/web/actions/videos/get-og-image.tsx
+++ b/apps/web/actions/videos/get-og-image.tsx
@@ -1,67 +1,25 @@
import { db } from "@cap/database";
-import { videos } from "@cap/database/schema";
+import { users, videos } from "@cap/database/schema";
import { findScreenshotObjectKey, Storage } from "@cap/web-backend";
import type { Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect } from "effect";
-import { ImageResponse } from "next/og";
+import { extractPosterFrameDataUri } from "@/lib/og/poster-frame";
+import { renderVideoOg } from "@/lib/og/video-og";
import { runPromise } from "@/lib/server";
import { decodeStorageVideo } from "@/lib/video-storage";
export async function generateVideoOgImage(videoId: Video.VideoId) {
const videoData = await getData(videoId);
- if (!videoData) {
- return new ImageResponse(
- <div
- style={{
- width: "100%",
- height: "100%",
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- background:
- "radial-gradient(90.01% 80.01% at 53.53% 49.99%,#d3e5ff 30.65%,#4785ff 88.48%,#fff 100%)",
- }}
- >
- <h1 style={{ fontSize: "60px" }}>Cap not found</h1>
- <p style={{ fontSize: "30px" }}>
- The video you are looking for does not exist or has moved.
- </p>
- </div>,
- {
- width: 1200,
- height: 630,
- },
- );
- }
+ if (!videoData) return renderVideoOg({ kind: "not-found" });
- const { video } = videoData;
+ const { video, ownerName } = videoData;
- if (!video || video.public === false) {
- return new ImageResponse(
- <div
- style={{
- width: "100%",
- height: "100%",
- display: "flex",
- alignItems: "center",
- justifyContent: "center",
- background:
- "radial-gradient(90.01% 80.01% at 53.53% 49.99%,#d3e5ff 30.65%,#4785ff 88.48%,#fff 100%)",
- }}
- >
- <h1 style={{ fontSize: "40px" }}>Video or bucket not found</h1>
- </div>,
- {
- width: 1200,
- height: 630,
- },
- );
- }
+ if (video.password) return renderVideoOg({ kind: "password" });
+ if (video.public === false) return renderVideoOg({ kind: "locked" });
- let screenshotUrl = null;
+ let screenshotUrl: string | undefined;
try {
await Effect.gen(function* () {
@@ -82,96 +40,36 @@ export async function generateVideoOgImage(videoId: Video.VideoId) {
console.error("Error generating URL for screenshot:", error);
}
- return new ImageResponse(
- <div
- style={{
- width: "100%",
- height: "100%",
- display: "flex",
- alignItems: "center",
- justifyContent: "center",
- background:
- "radial-gradient(90.01% 80.01% at 53.53% 49.99%,#d3e5ff 30.65%,#4785ff 88.48%,#fff 100%)",
- }}
- >
- <div
- style={{
- width: "85%",
- height: "85%",
- display: "flex",
- borderRadius: "10px",
- overflow: "hidden",
- position: "relative",
- background: "#000",
- }}
- >
- <div
- style={{
- display: "flex",
- alignItems: "center",
- justifyContent: "center",
- width: "200px",
- height: "200px",
- position: "absolute",
- top: "50%",
- left: "50%",
- transform: "translate(-50%, -50%)",
- zIndex: 10,
- }}
- >
- <svg
- xmlns="http://www.w3.org/2000/svg"
- style={{
- width: "100%",
- height: "100%",
- color: "#ffffff",
- display: "flex",
- }}
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- strokeWidth="2"
- strokeLinecap="round"
- strokeLinejoin="round"
- >
- <title>Play</title>
- <polygon points="6 3 20 12 6 21 6 3"></polygon>
- </svg>
- </div>
- {screenshotUrl && (
- <div
- style={{
- width: "100%",
- height: "100%",
- position: "absolute",
- backgroundImage: `url(${screenshotUrl})`,
- backgroundPosition: "center",
- backgroundSize: "cover",
- opacity: 0.4,
- zIndex: 1,
- }}
- />
- )}
- </div>
- </div>,
- {
- width: 1200,
- height: 630,
+ // The media pipeline writes the screenshot asynchronously — fall back to
+ // grabbing a frame from the playable source so fresh uploads still get a
+ // real thumbnail.
+ if (!screenshotUrl) {
+ screenshotUrl = await extractPosterFrameDataUri(video.id).catch(
+ () => undefined,
+ );
+ }
+
+ return renderVideoOg({
+ kind: "video",
+ video: {
+ title: video.name,
+ ownerName: ownerName ?? undefined,
+ duration: video.duration ?? undefined,
+ screenshotUrl,
},
- );
+ });
}
async function getData(videoId: Video.VideoId) {
const query = await db()
- .select({ video: videos })
+ .select({ video: videos, ownerName: users.name })
.from(videos)
+ .leftJoin(users, eq(videos.ownerId, users.id))
.where(eq(videos.id, videoId));
const result = query[0];
if (!result) return;
- return {
- video: result.video,
- };
+ return result;
}
diff --git a/apps/web/app/api/video/og/route.tsx b/apps/web/app/api/video/og/route.tsx
index 5223a23ae..e20d41a0b 100644
--- a/apps/web/app/api/video/og/route.tsx
+++ b/apps/web/app/api/video/og/route.tsx
@@ -2,6 +2,9 @@ import { Video } from "@cap/web-domain";
import type { NextRequest } from "next/server";
import { generateVideoOgImage } from "@/actions/videos/get-og-image";
+// Headroom for the ffmpeg poster-frame fallback on unprocessed videos.
+export const maxDuration = 30;
+
export async function GET(req: NextRequest) {
const videoId = req.nextUrl.searchParams.get("videoId") as string;
return generateVideoOgImage(Video.VideoId.make(videoId));