Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix(video-analytics): read playback events on every stats surface
Marc Seitze6b2a4ac6575
diff --git a/components/documents/video-analytics.tsx b/components/documents/video-analytics.tsxindex 9f23876f..5bdc22a6 100644--- a/components/documents/video-analytics.tsx+++ b/components/documents/video-analytics.tsx@@ -1,19 +1,11 @@import { DocumentVersion } from "@prisma/client";-import { formatDistanceToNow } from "date-fns";-import {- Area,- AreaChart,- ResponsiveContainer,- Tooltip,- XAxis,- YAxis,-} from "recharts";import useSWR from "swr";import { fetcher } from "@/lib/utils";import StatsElement from "@/components/documents/stats-element";import VideoChartPlaceholder from "@/components/documents/video-chart-placeholder";+import { VideoRetentionChart } from "@/components/documents/video-retention-chart";import { Card, CardContent } from "@/components/ui/card";import LoadingSpinner from "@/components/ui/loading-spinner";@@ -101,38 +93,6 @@ export default function VideoAnalytics({return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`;};- const formatTooltipTime = (value: number) => {- return formatTime(value);- };-- const CustomTooltip = ({ active, payload, label }: any) => {- if (active && payload && payload.length) {- const uniqueViews = payload[1].value;- const playbackCount = payload[0].value;- const intensity = playbackCount / uniqueViews || 1;-- return (- <div className="space-y-1 rounded-md border bg-background p-2 text-sm">- <p className="font-medium">{formatTooltipTime(label)}</p>- <div className="space-y-0.5 text-muted-foreground">- <p className="flex items-center gap-2">- <span className="h-2 w-2 rounded-full bg-emerald-500" />- {uniqueViews} unique viewer{uniqueViews !== 1 ? "s" : ""}- </p>- <p className="flex items-center gap-2">- <span className="h-2 w-2 rounded-full bg-[#3B82F6]" />- {playbackCount} playback{playbackCount !== 1 ? "s" : ""}- </p>- <p className="pt-1 text-xs">- Replayed {(intensity - 1).toFixed(1)}x on average- </p>- </div>- </div>- );- }- return null;- };-const stats = [{name: "Total views",@@ -158,75 +118,7 @@ export default function VideoAnalytics({return (<div className="space-y-4">- <div className="mt-2 flex items-center justify-center gap-4 text-sm text-muted-foreground">- <div className="flex items-center gap-2">- <div className="h-3 w-3 rounded-full bg-emerald-500" />- <span>Unique Viewers</span>- </div>- <div className="flex items-center gap-2">- <div className="h-3 w-3 rounded-full bg-[#3B82F6]" />- <span>Playback Count</span>- </div>- </div>- <div className="-mx-9 h-[300px]">- <ResponsiveContainer width="100%" height="100%">- <AreaChart- data={data.overall.view_distribution}- margin={{ top: 10, right: 10, left: 0, bottom: 0 }}- >- <XAxis- dataKey="start_time"- tickFormatter={formatTooltipTime}- stroke="#888888"- fontSize={12}- />- <YAxis- stroke="#888888"- fontSize={12}- tickFormatter={(value) => Math.floor(value).toString()}- domain={[0, (dataMax: number) => dataMax + 1]}- padding={{ top: 20 }}- allowDecimals={false}- />- <Tooltip content={<CustomTooltip />} />- <defs>- <linearGradient- id="uniqueViewsGradient"- x1="0"- y1="0"- x2="0"- y2="1"- >- <stop offset="5%" stopColor="#10b981" stopOpacity={0.3} />- <stop offset="95%" stopColor="#10b981" stopOpacity={0} />- </linearGradient>- <linearGradient id="playbackGradient" x1="0" y1="0" x2="0" y2="1">- <stop offset="5%" stopColor="#3B82F6" stopOpacity={0.3} />- <stop offset="95%" stopColor="#3B82F6" stopOpacity={0} />- </linearGradient>- </defs>- <Area- type="monotone"- dataKey="total_views"- stroke="#3B82F6"- strokeWidth={2}- fill="url(#playbackGradient)"- dot={false}- name="Playback Count"- />- <Area- type="monotone"- dataKey="unique_views"- stroke="#10b981"- strokeWidth={2}- fill="url(#uniqueViewsGradient)"- dot={false}- name="Unique Viewers"- />- </AreaChart>- </ResponsiveContainer>- </div>-+ <VideoRetentionChart data={data.overall.view_distribution} /><div className="grid grid-cols-3 gap-1.5 sm:gap-4 lg:gap-4">{stats.map((stat, index) => (<StatsElement key={stat.name} stat={stat} statIdx={index} />diff --git a/components/documents/video-retention-chart.tsx b/components/documents/video-retention-chart.tsxnew file mode 100644index 00000000..04f02228--- /dev/null+++ b/components/documents/video-retention-chart.tsx@@ -0,0 +1,136 @@+import {+ Area,+ AreaChart,+ ResponsiveContainer,+ Tooltip,+ XAxis,+ YAxis,+} from "recharts";++export type VideoRetentionPoint = {+ start_time: number;+ unique_views: number;+ total_views: number;+};++function formatTime(seconds: number) {+ const minutes = Math.floor(seconds / 60);+ const remainingSeconds = Math.floor(seconds % 60);+ return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`;+}++function RetentionTooltip({+ active,+ payload,+ label,+}: {+ active?: boolean;+ payload?: Array<{ value: number }>;+ label?: number;+}) {+ if (!active || !payload || payload.length < 2) return null;++ const uniqueViews = payload[1].value;+ const playbackCount = payload[0].value;+ const intensity = playbackCount / uniqueViews || 1;++ return (+ <div className="space-y-1 rounded-md border bg-background p-2 text-sm">+ <p className="font-medium">{formatTime(label ?? 0)}</p>+ <div className="space-y-0.5 text-muted-foreground">+ <p className="flex items-center gap-2">+ <span className="h-2 w-2 rounded-full bg-emerald-500" />+ {uniqueViews} unique viewer{uniqueViews !== 1 ? "s" : ""}+ </p>+ <p className="flex items-center gap-2">+ <span className="h-2 w-2 rounded-full bg-[#3B82F6]" />+ {playbackCount} playback{playbackCount !== 1 ? "s" : ""}+ </p>+ <p className="pt-1 text-xs">+ Replayed {(intensity - 1).toFixed(1)}x on average+ </p>+ </div>+ </div>+ );+}++export function VideoRetentionChart({+ data,+ height = 300,+}: {+ data: VideoRetentionPoint[];+ height?: number;+}) {+ return (+ <div className="space-y-4">+ <div className="mt-2 flex items-center justify-center gap-4 text-sm text-muted-foreground">+ <div className="flex items-center gap-2">+ <div className="h-3 w-3 rounded-full bg-emerald-500" />+ <span>Unique Viewers</span>+ </div>+ <div className="flex items-center gap-2">+ <div className="h-3 w-3 rounded-full bg-[#3B82F6]" />+ <span>Playback Count</span>+ </div>+ </div>+ <div className="-mx-9" style={{ height }}>+ <ResponsiveContainer width="100%" height="100%">+ <AreaChart+ data={data}+ margin={{ top: 10, right: 10, left: 0, bottom: 0 }}+ >+ <XAxis+ dataKey="start_time"+ tickFormatter={formatTime}+ stroke="#888888"+ fontSize={12}+ />+ <YAxis+ stroke="#888888"+ fontSize={12}+ tickFormatter={(value) => Math.floor(value).toString()}+ domain={[0, (dataMax: number) => dataMax + 1]}+ padding={{ top: 20 }}+ allowDecimals={false}+ />+ <Tooltip content={<RetentionTooltip />} />+ <defs>+ <linearGradient+ id="uniqueViewsGradient"+ x1="0"+ y1="0"+ x2="0"+ y2="1"+ >+ <stop offset="5%" stopColor="#10b981" stopOpacity={0.3} />+ <stop offset="95%" stopColor="#10b981" stopOpacity={0} />+ </linearGradient>+ <linearGradient id="playbackGradient" x1="0" y1="0" x2="0" y2="1">+ <stop offset="5%" stopColor="#3B82F6" stopOpacity={0.3} />+ <stop offset="95%" stopColor="#3B82F6" stopOpacity={0} />+ </linearGradient>+ </defs>+ <Area+ type="monotone"+ dataKey="total_views"+ stroke="#3B82F6"+ strokeWidth={2}+ fill="url(#playbackGradient)"+ dot={false}+ name="Playback Count"+ />+ <Area+ type="monotone"+ dataKey="unique_views"+ stroke="#10b981"+ strokeWidth={2}+ fill="url(#uniqueViewsGradient)"+ dot={false}+ name="Unique Viewers"+ />+ </AreaChart>+ </ResponsiveContainer>+ </div>+ </div>+ );+}diff --git a/components/visitors/dataroom-view-stats.tsx b/components/visitors/dataroom-view-stats.tsxindex a96651d0..ded3b1f8 100644--- a/components/visitors/dataroom-view-stats.tsx+++ b/components/visitors/dataroom-view-stats.tsx@@ -11,6 +11,7 @@ import {UploadCloudIcon,} from "lucide-react";+import { useTeam } from "@/context/team-context";import { useDataroomVisitHistory } from "@/lib/swr/use-dataroom";import {DocumentViewStats,@@ -30,6 +31,7 @@ import { TableCell, TableRow } from "@/components/ui/table";import { TimestampTooltip } from "@/components/ui/timestamp-tooltip";import { DocumentPageChart } from "./document-view-stats";+import VisitorVideoChart from "./visitor-video-chart";export function DataroomViewStats({viewId,@@ -241,8 +243,16 @@ function DocumentViewRow({dataroomViewId: string;timestamp: Date;}) {- const [showPageByPage, setShowPageByPage] = useState(false);- const hasPages = stats && stats.totalPages > 0;+ const [showDetail, setShowDetail] = useState(false);+ const teamId = useTeam()?.currentTeam?.id;+ const documentType =+ stats?.documentType ??+ view.document?.type ??+ view.document?.versions?.[0]?.type ??+ null;+ const isVideo = documentType === "video";+ const hasPages = Boolean(stats && stats.totalPages > 0);+ const canExpand = isVideo || hasPages;return (<>@@ -260,18 +270,20 @@ function DocumentViewRow({<ArrowUpRightIcon className="h-4 w-4" /></Link></div>- {hasPages && (+ {canExpand && (<button- onClick={() => setShowPageByPage((prev) => !prev)}+ onClick={() => setShowDetail((prev) => !prev)}className="flex shrink-0 items-center gap-0.5 text-xs text-muted-foreground transition-colors hover:text-foreground"><ChevronRightIconclassName={cn("h-3 w-3 transition-transform",- showPageByPage && "rotate-90",+ showDetail && "rotate-90",)}/>- <span className="hidden sm:inline">page-by-page</span>+ <span className="hidden sm:inline">+ {isVideo ? "playback" : "page-by-page"}+ </span></button>)}</div>@@ -308,7 +320,18 @@ function DocumentViewRow({</TableCell><TableCell /></TableRow>- {showPageByPage && hasPages && (+ {showDetail && isVideo && view.document?.id && teamId ? (+ <TableRow>+ <TableCell colSpan={5} className="p-0 px-4 pb-3 pt-0">+ <VisitorVideoChart+ documentId={view.document.id}+ viewId={view.id}+ teamId={teamId}+ />+ </TableCell>+ </TableRow>+ ) : null}+ {showDetail && hasPages && !isVideo && stats ? (<TableRow><TableCell colSpan={5} className="p-0 px-4 pb-3 pt-0"><DocumentPageChart@@ -322,7 +345,7 @@ function DocumentViewRow({/></TableCell></TableRow>- )}+ ) : null}</>);}diff --git a/components/visitors/visitor-video-chart.tsx b/components/visitors/visitor-video-chart.tsxindex 7758dcc3..8a5d336f 100644--- a/components/visitors/visitor-video-chart.tsx+++ b/components/visitors/visitor-video-chart.tsx@@ -8,6 +8,8 @@ import {} from "recharts";import useSWR from "swr";+import { useTeam } from "@/context/team-context";+import { Card } from "@/components/ui/card";import LoadingSpinner from "@/components/ui/loading-spinner";@@ -20,24 +22,28 @@ export default function VisitorVideoChart({}: {documentId: string;viewId: string;- teamId: string;+ teamId?: string;}) {- // Fetch video events if this is a video document+ const teamInfo = useTeam();+ const resolvedTeamId = teamId || teamInfo?.currentTeam?.id;const { data: videoEvents, error: videoError } = useSWR<{data: Array<{start_time: number;views: number;}>;}>(- teamId- ? `/api/teams/${teamId}/documents/${documentId}/views/${viewId}/video-stats`+ resolvedTeamId+ ? `/api/teams/${resolvedTeamId}/documents/${documentId}/views/${viewId}/video-stats`: null,fetcher,);if (videoError) {- console.error("Error loading video events:", videoError);- return null;+ return (+ <p className="py-6 text-sm text-muted-foreground">+ Playback chart could not be loaded.+ </p>+ );}if (!videoEvents) {diff --git a/components/visitors/visitors-table.tsx b/components/visitors/visitors-table.tsxindex a08ab211..4acb9a12 100644--- a/components/visitors/visitors-table.tsx+++ b/components/visitors/visitors-table.tsx@@ -731,7 +731,7 @@ export default function VisitorsTable({<VisitorVideoChartdocumentId={view.documentId!}viewId={view.id}- teamId={view.teamId!}+ teamId={view.teamId ?? teamId}/>) : (<VisitorChartdiff --git a/lib/api/documents/process-document.ts b/lib/api/documents/process-document.tsindex abbae95d..e0349dbf 100644--- a/lib/api/documents/process-document.ts+++ b/lib/api/documents/process-document.ts@@ -13,6 +13,7 @@ import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";import { getExtension } from "@/lib/utils";import { isMarkdownFile } from "@/lib/utils/get-content-type";import { conversionQueueName } from "@/lib/utils/trigger-utils";+import { videoProcessingMode } from "@/lib/video/processing-plan";import { sendDocumentCreatedWebhook } from "@/lib/webhook/triggers/document-created";import { sendLinkCreatedWebhook } from "@/lib/webhook/triggers/link-created";@@ -191,21 +192,15 @@ export const processDocument = async ({);}- if (- type === "video" &&- contentType !== "video/mp4" &&- contentType?.startsWith("video/")- ) {+ const videoMode = videoProcessingMode({ type, contentType });+ if (videoMode) {await processVideo.trigger({- videoUrl: key,- teamId,- docId: key.split("/")[1], // Extract doc_xxxx from teamId/doc_xxxx/filenamedocumentVersionId: document.versions[0].id,- fileSize: fileSize || 0,+ mode: videoMode,},{- idempotencyKey: `${teamId}-${document.versions[0].id}`,+ idempotencyKey: `${teamId}-${document.versions[0].id}-${videoMode}`,tags: [`team_${teamId}`,`document_${document.id}`,diff --git a/lib/swr/use-dataroom-view-document-stats.ts b/lib/swr/use-dataroom-view-document-stats.tsindex 925ff326..67f7ea59 100644--- a/lib/swr/use-dataroom-view-document-stats.ts+++ b/lib/swr/use-dataroom-view-document-stats.ts@@ -10,6 +10,7 @@ export type DocumentViewStats = {pagesViewed: number;totalPages: number;completionRate: number;+ documentType?: string | null;};export function useDataroomViewDocumentStats({diff --git a/lib/trigger/backfill-video-lengths.ts b/lib/trigger/backfill-video-lengths.tsnew file mode 100644index 00000000..69bfac97--- /dev/null+++ b/lib/trigger/backfill-video-lengths.ts@@ -0,0 +1,83 @@+import { logger, task } from "@trigger.dev/sdk";++import prisma from "@/lib/prisma";+import { processVideo } from "@/lib/trigger/optimize-video-files";++type BackfillVideoLengthsPayload = {+ dryRun?: boolean;+};++type BackfillVideoLengthsResult = {+ enqueued: number;+ dryRun: boolean;+};++const PAGE_SIZE = 500;++export const backfillVideoLengths = task({+ id: "backfill-video-lengths",+ run: async ({+ dryRun = false,+ }: BackfillVideoLengthsPayload): Promise<BackfillVideoLengthsResult> => {+ let lastId: string | undefined;+ let page = 0;+ let total = 0;++ while (true) {+ const versions = await prisma.documentVersion.findMany({+ where: {+ type: "video",+ OR: [{ length: null }, { length: { lte: 0 } }],+ ...(lastId ? { id: { gt: lastId } } : {}),+ },+ take: PAGE_SIZE,+ orderBy: { id: "asc" },+ select: {+ id: true,+ document: {+ select: {+ teamId: true,+ },+ },+ },+ });++ page += 1;+ total += versions.length;++ if (!dryRun && versions.length > 0) {+ await processVideo.batchTrigger(+ versions.map((version) => ({+ payload: {+ documentVersionId: version.id,+ mode: "probe",+ },+ options: {+ idempotencyKey: `backfill-video-length-${version.id}`,+ queue: "video-length-backfill",+ concurrencyKey: version.document.teamId,+ tags: [+ `team_${version.document.teamId}`,+ `version:${version.id}`,+ "video-length-backfill",+ ],+ },+ })),+ );+ }++ logger.info("Processed video length backfill page", {+ page,+ total,+ dryRun,+ });++ const lastVersion = versions.at(-1);+ if (!lastVersion || versions.length < PAGE_SIZE) {+ return { enqueued: total, dryRun };+ }++ lastId = lastVersion.id;+ }+ },+});diff --git a/lib/trigger/optimize-video-files.ts b/lib/trigger/optimize-video-files.tsindex e7cf9437..b5358938 100644--- a/lib/trigger/optimize-video-files.ts+++ b/lib/trigger/optimize-video-files.ts@@ -6,225 +6,266 @@ import os from "os";import path from "path";import { Readable } from "stream";import { pipeline } from "stream/promises";-import { ReadableStream } from "stream/web";+import type { ReadableStream } from "stream/web";+import { ONE_HOUR } from "@/lib/constants";import { getFile } from "@/lib/files/get-file";import { streamFileServer } from "@/lib/files/stream-file-server";import prisma from "@/lib/prisma";+import { probeVideo } from "@/lib/video/probe";+import type { VideoProcessingMode } from "@/lib/video/processing-plan";++type ProcessVideoPayload = {+ documentVersionId: string;+ mode: VideoProcessingMode;+};++type ProcessVideoResult =+ | { status: "skipped"; reason: "version-missing" }+ | { status: "probed"; duration: number; lengthWritten: boolean }+ | {+ status: "optimization-skipped";+ duration: number;+ lengthWritten: boolean;+ }+ | { status: "optimized"; duration: number; lengthWritten: boolean };++const MAX_OPTIMIZATION_FILE_SIZE = BigInt(500 * 1024 * 1024);++// The upload path segment is minted by putFileInS3 before the Document row+// exists, so it is not Document.id. The optimized file must reuse it to stay+// under the same `teamId/docId/` prefix as the original.+function storageDocId(key: string): string {+ const docId = key.split("/")[1];+ if (!docId) {+ throw new Error(`Cannot derive storage docId from key ${key}`);+ }+ return docId;+}export const processVideo = task({id: "process-video",machine: {preset: "medium-1x",},- run: async (payload: {- videoUrl: string;- teamId: string;- docId: string;- documentVersionId: string;- fileSize: number;- }) => {- const { videoUrl, teamId, docId, documentVersionId, fileSize } = payload;+ run: async ({+ documentVersionId,+ mode,+ }: ProcessVideoPayload): Promise<ProcessVideoResult> => {+ const version = await prisma.documentVersion.findUnique({+ where: { id: documentVersionId },+ select: {+ id: true,+ file: true,+ storageType: true,+ fileSize: true,+ length: true,+ document: {+ select: {+ teamId: true,+ },+ },+ },+ });- try {- const fileUrl = await getFile({- data: videoUrl,- type: "S3_PATH",+ if (!version) {+ logger.warn("Skipping video processing because version is missing", {+ documentVersionId,+ mode,});+ return { status: "skipped", reason: "version-missing" };+ }- logger.info("Starting video optimization", { fileUrl });-- // Create temp directory for input and output- const tempDirectory = path.join(os.tmpdir(), `video_${Date.now()}`);- await fs.mkdir(tempDirectory, { recursive: true });- const inputPath = path.join(tempDirectory, "input.mp4");- const outputPath = path.join(tempDirectory, "output.mp4");+ try {+ const signedUrl = await getFile({+ data: version.file,+ type: version.storageType,+ expiresIn: ONE_HOUR,+ });- // Stream video to temporary file- const response = await fetch(fileUrl);- if (!response.body) {- throw new Error("Failed to fetch video stream");+ const metadata = await probeVideo(signedUrl);+ if (!Number.isFinite(metadata.duration) || metadata.duration <= 0) {+ throw new Error(+ `Invalid video duration for document version ${documentVersionId}`,+ );}- logger.info("Streaming video to temporary file");- await pipeline(- Readable.fromWeb(response.body as ReadableStream),- createWriteStream(inputPath),- );-- // Get input metadata first- const metadata = await new Promise<{- width: number;- height: number;- fps: number;- duration: number;- }>((resolve, reject) => {- ffmpeg.ffprobe(inputPath, (err, metadata) => {- if (err) {- logger.error("Probe error:", { error: err.message });- reject(err);- return;- }- const videoStream = metadata.streams.find(- (s) => s.codec_type === "video",- );- if (!videoStream) {- reject(new Error("No video stream found"));- return;- }-- const fps = (() => {- const fpsStr =- videoStream.r_frame_rate || videoStream.avg_frame_rate;- const [num, den] = fpsStr?.split("/").map(Number) || [0, 1];- return num / (den || 1);- })();-- resolve({- width: videoStream.width || 1920,- height: videoStream.height || 1080,- fps,- duration: Math.round(metadata.format.duration || 0),- });- });+ logger.info("Video probed", {+ documentVersionId,+ mode,+ duration: metadata.duration,+ width: metadata.width,+ fps: metadata.fps,});- // Update document version with metadata- await prisma.documentVersion.update({- where: { id: documentVersionId },- data: {- length: metadata.duration,+ const written = await prisma.documentVersion.updateMany({+ where: {+ id: documentVersionId,+ OR: [{ length: null }, { length: { lte: 0 } }],},+ data: { length: metadata.duration },});+ const lengthWritten = written.count > 0;- if (fileSize > 500 * 1024 * 1024) {- // if file size is greater than 500MB, skip optimization- logger.info(- `File size is ${fileSize / 1024 / 1024} MB, skipping optimization`,- );-- // Clean up temporary directory- await fs.rm(tempDirectory, { recursive: true });- logger.info("Temporary directory cleaned up", { tempDirectory });- return {- success: true,- message: "File size is too large, skipping optimization",- };- }-- // Calculate encoding parameters- const keyframeInterval = Math.round(metadata.fps * 2);- const bitrate = "6000k";- const maxBitrate = parseInt(bitrate.replace("k", "")) * 2;+ switch (mode) {+ case "probe":+ return {+ status: "probed",+ duration: metadata.duration,+ lengthWritten,+ };+ case "optimize": {+ if (+ version.fileSize !== null &&+ version.fileSize > MAX_OPTIMIZATION_FILE_SIZE+ ) {+ logger.info("Skipping optimization because the file is too large", {+ documentVersionId,+ mode,+ duration: metadata.duration,+ width: metadata.width,+ fps: metadata.fps,+ fileSize: version.fileSize?.toString(),+ });+ return {+ status: "optimization-skipped",+ duration: metadata.duration,+ lengthWritten,+ };+ }- // Only scale if the video is larger than 1080p- const scaleFilter = metadata.width > 1920 ? "-vf scale=1920:-2" : null;+ const tempDirectory = await fs.mkdtemp(+ path.join(os.tmpdir(), "video_"),+ );- logger.info("Video metadata:", {- originalWidth: metadata.width,- originalHeight: metadata.height,- fps: metadata.fps,- duration: metadata.duration,- willScale: !!scaleFilter,- });+ try {+ const inputPath = path.join(tempDirectory, "input.mp4");+ const outputPath = path.join(tempDirectory, "output.mp4");+ const response = await fetch(signedUrl);+ if (!response.body) {+ throw new Error("Failed to fetch video stream");+ }- // Process video to temporary file first- await new Promise<void>((resolve, reject) => {- const ffmpegCommand = ffmpeg(inputPath)- .inputOptions(["-y"])- .outputOptions([- ...(scaleFilter ? [scaleFilter] : []), // Only include scale if needed- "-c:v libx264",- "-profile:v high",- "-level:v 4.1",- "-c:a aac",- "-ar 48000",- "-b:a 128k",- `-b:v ${bitrate}`,- `-maxrate ${maxBitrate}k`,- `-bufsize ${maxBitrate}k`,- "-preset medium",- `-g ${keyframeInterval}`,- `-keyint_min ${keyframeInterval}`,- "-sc_threshold 0",- "-movflags +faststart",- ])- .output(outputPath)- .on("start", (cmd) => {- logger.info("FFmpeg started:", {- cmd,- originalSize: `${metadata.width}x${metadata.height}`,- scaling: !!scaleFilter,- fps: metadata.fps,- keyframeInterval,- });- })- .on("error", (err, stdout, stderr) => {- logger.error("FFmpeg error:", {- error: err.message,- stdout,- stderr,+ logger.info("Streaming video to temporary file", {+ documentVersionId,+ mode,});- reject(err);- })- .on("end", () => {- logger.info("FFmpeg completed");- resolve();- });-- ffmpegCommand.run();- });+ await pipeline(+ Readable.fromWeb(response.body as ReadableStream),+ createWriteStream(inputPath),+ );- // Create read stream from output file- const fileStream = createReadStream(outputPath);+ const keyframeInterval = Math.round(metadata.fps * 2);+ const bitrate = "6000k";+ const maxBitrate = parseInt(bitrate.replace("k", "")) * 2;+ const scaleFilter =+ metadata.width > 1920 ? "-vf scale=1920:-2" : null;- // Add error handling for the file stream- fileStream.on("error", (err) => {- logger.error("Stream error:", {- error: err.message,- stack: err.stack,- });- });+ await new Promise<void>((resolve, reject) => {+ const ffmpegCommand = ffmpeg(inputPath)+ .inputOptions(["-y"])+ .outputOptions([+ ...(scaleFilter ? [scaleFilter] : []),+ "-c:v libx264",+ "-profile:v high",+ "-level:v 4.1",+ "-c:a aac",+ "-ar 48000",+ "-b:a 128k",+ `-b:v ${bitrate}`,+ `-maxrate ${maxBitrate}k`,+ `-bufsize ${maxBitrate}k`,+ "-preset medium",+ `-g ${keyframeInterval}`,+ `-keyint_min ${keyframeInterval}`,+ "-sc_threshold 0",+ "-movflags +faststart",+ ])+ .output(outputPath)+ .on("start", (cmd) => {+ logger.info("FFmpeg started", {+ cmd,+ documentVersionId,+ originalSize: `${metadata.width}x${metadata.height}`,+ scaling: !!scaleFilter,+ fps: metadata.fps,+ keyframeInterval,+ });+ })+ .on("error", (err, stdout, stderr) => {+ logger.error("FFmpeg error", {+ documentVersionId,+ error: err.message,+ stdout,+ stderr,+ });+ reject(err);+ })+ .on("end", () => {+ logger.info("FFmpeg completed", { documentVersionId });+ resolve();+ });- // Start the upload process using streamFileServer- const uploadPromise = streamFileServer({- file: {- name: "optimized.mp4",- type: "video/mp4",- stream: fileStream,- },- teamId,- docId,- });+ ffmpegCommand.run();+ });- // Wait for the upload to complete- const { type, data } = await uploadPromise;- logger.info("Upload completed", { type, data });+ const fileStream = createReadStream(outputPath);+ fileStream.on("error", (err) => {+ logger.error("Stream error", {+ documentVersionId,+ error: err.message,+ stack: err.stack,+ });+ });- if (!data) {- throw new Error("Upload failed: No file path returned");- }+ const { type, data } = await streamFileServer({+ file: {+ name: "optimized.mp4",+ type: "video/mp4",+ stream: fileStream,+ },+ teamId: version.document.teamId,+ docId: storageDocId(version.file),+ });+ logger.info("Upload completed", {+ documentVersionId,+ type,+ data,+ });- // Update the document version with the new file and length- await prisma.documentVersion.update({- where: {- id: documentVersionId,- },- data: {- file: data,- },- });+ if (!data) {+ throw new Error("Upload failed: No file path returned");+ }- // Clean up temporary directory- await fs.rm(tempDirectory, { recursive: true });- logger.info("Temporary directory cleaned up", { tempDirectory });+ await prisma.documentVersion.update({+ where: { id: documentVersionId },+ data: { file: data },+ });- return {- success: true,- message: "Successfully optimized video",- };+ return {+ status: "optimized",+ duration: metadata.duration,+ lengthWritten,+ };+ } finally {+ await fs.rm(tempDirectory, { recursive: true, force: true });+ logger.info("Temporary directory cleaned up", {+ documentVersionId,+ tempDirectory,+ });+ }+ }+ default: {+ const exhaustiveMode: never = mode;+ throw new Error(+ `Unsupported video processing mode: ${exhaustiveMode}`,+ );+ }+ }} catch (error) {- logger.error("Failed to optimize video:", {+ logger.error("Failed to process video", {+ documentVersionId,+ mode,error: error instanceof Error ? error.message : String(error),stack: error instanceof Error ? error.stack : undefined,});diff --git a/lib/trigger/queues.ts b/lib/trigger/queues.tsindex 9ba3e0d6..81eb65b2 100644--- a/lib/trigger/queues.ts+++ b/lib/trigger/queues.ts@@ -18,6 +18,11 @@ export const convertKeynoteToPdfQueue = queue({concurrencyLimit: 2,});+export const videoLengthBackfillQueue = queue({+ name: "video-length-backfill",+ concurrencyLimit: 5,+});+// AI processing queuesexport const processPdfForAIQueue = queue({name: "process-pdf-for-ai",diff --git a/lib/video-analytics/playback.ts b/lib/video-analytics/playback.tsnew file mode 100644index 00000000..e0c812ae--- /dev/null+++ b/lib/video-analytics/playback.ts@@ -0,0 +1,161 @@+export const COUNTABLE_VIDEO_EVENTS = [+ "played",+ "muted",+ "unmuted",+ "rate_changed",+] as const;++export type CountableVideoEventType = (typeof COUNTABLE_VIDEO_EVENTS)[number];++export type VideoPlaybackEvent = {+ view_id?: string;+ event_type: string;+ start_time: number;+ end_time: number;+};++const COUNTABLE = new Set<string>(COUNTABLE_VIDEO_EVENTS);++export function isCountablePlaybackEvent(+ event: VideoPlaybackEvent,+): boolean {+ return (+ COUNTABLE.has(event.event_type) &&+ typeof event.start_time === "number" &&+ typeof event.end_time === "number" &&+ event.end_time > event.start_time &&+ event.end_time - event.start_time >= 1+ );+}++export function countablePlaybackEvents<T extends VideoPlaybackEvent>(+ events: T[] | null | undefined,+): T[] {+ return (events ?? []).filter(isCountablePlaybackEvent);+}++export function playbackSeconds(event: VideoPlaybackEvent): number[] {+ const start = Math.floor(event.start_time);+ const end = Math.ceil(event.end_time);+ const seconds: number[] = [];+ for (let t = start; t < end; t++) {+ seconds.push(t);+ }+ return seconds;+}++export function resolveVideoLength(+ stored: number | null | undefined,+ events: VideoPlaybackEvent[],+): number {+ let maxEnd = 0;+ for (const event of events) {+ if (event.end_time > maxEnd) maxEnd = event.end_time;+ }+ return Math.max(stored && stored > 0 ? stored : 0, Math.ceil(maxEnd));+}++export function watchTimeSeconds(events: VideoPlaybackEvent[]): {+ total: number;+ unique: number;+} {+ const counts = new Map<number, number>();+ for (const event of events) {+ for (const second of playbackSeconds(event)) {+ counts.set(second, (counts.get(second) ?? 0) + 1);+ }+ }+ let total = 0;+ counts.forEach((count) => {+ total += count;+ });+ return { total, unique: counts.size };+}++export function completionRate(+ uniqueSeconds: number,+ videoLength: number,+): number {+ if (videoLength <= 0) return 0;+ return Math.min(100, (uniqueSeconds / videoLength) * 100);+}++export type DocumentSecondStats = {+ start_time: number;+ unique_views: number;+ total_views: number;+};++export function documentViewDistribution(+ events: Array<VideoPlaybackEvent & { view_id: string }>,+ videoLength: number,+): DocumentSecondStats[] {+ const buckets = new Map<+ number,+ { uniqueViewers: Set<string>; viewDurations: Map<string, number> }+ >();+ for (let t = 0; t <= videoLength; t++) {+ buckets.set(t, { uniqueViewers: new Set(), viewDurations: new Map() });+ }++ for (const event of events) {+ for (const second of playbackSeconds(event)) {+ let stats = buckets.get(second);+ if (!stats) {+ stats = { uniqueViewers: new Set(), viewDurations: new Map() };+ buckets.set(second, stats);+ }+ stats.uniqueViewers.add(event.view_id);+ stats.viewDurations.set(+ event.view_id,+ (stats.viewDurations.get(event.view_id) ?? 0) + 1,+ );+ }+ }++ return Array.from(buckets.entries())+ .map(([start_time, stats]) => {+ let total_views = 0;+ stats.viewDurations.forEach((count) => {+ total_views += count;+ });+ return {+ start_time,+ unique_views: stats.uniqueViewers.size,+ total_views,+ };+ })+ .sort((a, b) => a.start_time - b.start_time);+}++export type ViewSecondStats = {+ start_time: number;+ views: number;+};++export function viewPlaybackTimeline(+ events: VideoPlaybackEvent[],+ videoLength: number,+): ViewSecondStats[] {+ const buckets = new Map<number, number>();+ for (let t = 0; t <= videoLength; t++) {+ buckets.set(t, 0);+ }++ for (const event of events) {+ for (const second of playbackSeconds(event)) {+ buckets.set(second, (buckets.get(second) ?? 0) + 1);+ }+ }++ return Array.from(buckets.entries())+ .map(([start_time, views]) => ({ start_time, views }))+ .sort((a, b) => a.start_time - b.start_time);+}++export function eventsForView<T extends VideoPlaybackEvent & { view_id: string }>(+ events: T[],+ viewId: string,+): T[] {+ return events.filter((event) => event.view_id === viewId);+}diff --git a/lib/video-analytics/playback.verify.ts b/lib/video-analytics/playback.verify.tsnew file mode 100644index 00000000..e898aa7d--- /dev/null+++ b/lib/video-analytics/playback.verify.ts@@ -0,0 +1,66 @@+import {+ documentViewDistribution,+ resolveVideoLength,+ viewPlaybackTimeline,+} from "./playback";++function assert(condition: unknown, message: string): asserts condition {+ if (!condition) {+ throw new Error(message);+ }+}++const playbackEvent = {+ event_type: "played",+ start_time: 0,+ end_time: 10,+};++assert(+ resolveVideoLength(210, [playbackEvent]) === 210,+ "stored duration should determine the video length",+);+assert(+ resolveVideoLength(null, [playbackEvent]) === 10,+ "event duration should remain the fallback video length",+);++const timeline = viewPlaybackTimeline([playbackEvent], 210);+assert(+ timeline.length === 211,+ "timeline should contain seconds 0 through 210",+);+assert(+ timeline.at(-1)?.start_time === 210,+ "timeline should end at the stored duration",+);+assert(+ timeline+ .filter(({ start_time }) => start_time >= 11)+ .every(({ views }) => views === 0),+ "timeline should have zero views after playback ends",+);++const distribution = documentViewDistribution(+ [{ ...playbackEvent, view_id: "view_1" }],+ 210,+);+assert(+ distribution.length === 211,+ "distribution should contain seconds 0 through 210",+);+assert(+ distribution.at(-1)?.start_time === 210,+ "distribution should end at the stored duration",+);+assert(+ distribution+ .filter(({ start_time }) => start_time >= 11)+ .every(+ ({ unique_views, total_views }) =>+ unique_views === 0 && total_views === 0,+ ),+ "distribution should have zero views after playback ends",+);++console.log("playback.verify: all checks passed");diff --git a/lib/video/probe.ts b/lib/video/probe.tsnew file mode 100644index 00000000..d0a9dc79--- /dev/null+++ b/lib/video/probe.ts@@ -0,0 +1,58 @@+import ffmpeg from "fluent-ffmpeg";++export type VideoMetadata = {+ width: number;+ height: number;+ duration: number;+ fps: number;+};++export function probeVideo(+ source: string,+ timeoutMs: number = 60_000,+): Promise<VideoMetadata> {+ return new Promise((resolve, reject) => {+ let settled = false;++ // Rejecting cannot kill the ffprobe child: fluent-ffmpeg's ffprobe returns+ // no process handle, unlike FfmpegCommand.+ const fail = (error: Error) => {+ if (settled) return;+ settled = true;+ clearTimeout(timer);+ reject(error);+ };++ const succeed = (value: VideoMetadata) => {+ if (settled) return;+ settled = true;+ clearTimeout(timer);+ resolve(value);+ };++ const timer = setTimeout(+ () => fail(new Error(`ffprobe timed out after ${timeoutMs}ms`)),+ timeoutMs,+ );++ ffmpeg.ffprobe(source, (err, metadata) => {+ if (err) {+ fail(err);+ return;+ }+ const stream = metadata.streams.find((s) => s.codec_type === "video");+ if (!stream) {+ fail(new Error("No video stream"));+ return;+ }+ const fpsStr = stream.r_frame_rate || stream.avg_frame_rate || "0/1";+ const [num, den] = fpsStr.split("/").map(Number);+ succeed({+ width: stream.width || 0,+ height: stream.height || 0,+ duration: Math.round(metadata.format.duration || 0),+ fps: num / (den || 1),+ });+ });+ });+}diff --git a/lib/video/processing-plan.ts b/lib/video/processing-plan.tsnew file mode 100644index 00000000..84c2502d--- /dev/null+++ b/lib/video/processing-plan.ts@@ -0,0 +1,18 @@+export type VideoProcessingMode = "probe" | "optimize";++export function videoProcessingMode(input: {+ type: string | null | undefined;+ contentType: string | null | undefined;+}): VideoProcessingMode | null {+ const contentType = input.contentType?.split(";")[0].trim().toLowerCase();++ if (input.type !== "video" || !contentType?.startsWith("video/")) {+ return null;+ }++ if (contentType === "video/mp4") {+ return "probe";+ }++ return "optimize";+}diff --git a/lib/video/processing-plan.verify.ts b/lib/video/processing-plan.verify.tsnew file mode 100644index 00000000..ad103d1c--- /dev/null+++ b/lib/video/processing-plan.verify.ts@@ -0,0 +1,52 @@+import { videoProcessingMode } from "./processing-plan";++function assert(condition: unknown, message: string): asserts condition {+ if (!condition) {+ throw new Error(message);+ }+}++assert(+ videoProcessingMode({ type: "pdf", contentType: "application/pdf" }) === null,+ "non-video type should return null",+);+assert(+ videoProcessingMode({ type: "video", contentType: "video/mp4" }) === "probe",+ "mp4 should return probe",+);+assert(+ videoProcessingMode({+ type: "video",+ contentType: "Video/MP4; codecs=avc1",+ }) === "probe",+ "mp4 with charset/codecs should normalize to probe",+);+assert(+ videoProcessingMode({+ type: "video",+ contentType: " video/mp4 ; codecs=avc1 ",+ }) === "probe",+ "mp4 with whitespace around the parameter separator should normalize to probe",+);+assert(+ videoProcessingMode({ type: "video", contentType: "video/quicktime" }) ===+ "optimize",+ "non-mp4 video/* should return optimize",+);+assert(+ videoProcessingMode({ type: "video", contentType: null }) === null,+ "video with null content type should return null",+);+assert(+ videoProcessingMode({+ type: "video",+ contentType: "application/octet-stream",+ }) === null,+ "video with non-video content type should return null",+);+assert(+ videoProcessingMode({ type: "document", contentType: "video/mp4" }) === null,+ "non-video type with video content type should return null",+);++console.log("processing-plan.verify: all checks passed");diff --git a/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/document-stats.ts b/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/document-stats.tsindex 6be7cf77..25080e56 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/document-stats.ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/document-stats.ts@@ -6,7 +6,15 @@ import { getServerSession } from "next-auth/next";import { errorhandler } from "@/lib/errorHandler";import prisma from "@/lib/prisma";import { getDataroomViewDocumentStats, getViewPageDuration } from "@/lib/tinybird";+import { getVideoEventsByDocument } from "@/lib/tinybird/pipes";import { CustomUser } from "@/lib/types";+import {+ countablePlaybackEvents,+ completionRate,+ eventsForView,+ resolveVideoLength,+ watchTimeSeconds,+} from "@/lib/video-analytics/playback";export default async function handle(req: NextApiRequest,@@ -108,10 +116,11 @@ export default async function handle(select: {id: true,name: true,+ type: true,versions: {where: { isPrimary: true },take: 1,- select: { numPages: true },+ select: { numPages: true, length: true, type: true },},},},@@ -124,17 +133,62 @@ export default async function handle(const viewIds = documentViews.map((v) => v.id).join(",");- const tinybirdStats = await getDataroomViewDocumentStats({ viewIds });+ const videoDocumentIds = [+ ...new Set(+ documentViews+ .filter(+ (dv) =>+ (dv.document?.type ?? dv.document?.versions?.[0]?.type) ===+ "video",+ )+ .map((dv) => dv.documentId)+ .filter((id): id is string => Boolean(id)),+ ),+ ];++ const [tinybirdStats, videoEventsByDocument] = await Promise.all([+ getDataroomViewDocumentStats({ viewIds }),+ Promise.all(+ videoDocumentIds.map(async (id) => {+ const response = await getVideoEventsByDocument({+ document_id: id,+ });+ return [id, countablePlaybackEvents(response.data)] as const;+ }),+ ),+ ]);const statsMap = new Map(tinybirdStats.data.map((s) => [`${s.viewId}:${s.documentId}`, s]),);+ const videoEvents = new Map(videoEventsByDocument);const documentStats = documentViews.map((dv) => {+ const documentType =+ dv.document?.type ?? dv.document?.versions?.[0]?.type ?? null;+ if (documentType === "video") {+ const events = videoEvents.get(dv.documentId ?? "") ?? [];+ const viewEvents = eventsForView(events, dv.id);+ const { total, unique } = watchTimeSeconds(viewEvents);+ const videoLength = resolveVideoLength(+ dv.document?.versions?.[0]?.length,+ events,+ );+ return {+ viewId: dv.id,+ documentId: dv.documentId,+ totalDuration: total * 1000,+ pagesViewed: unique,+ totalPages: 0,+ completionRate: Math.round(completionRate(unique, videoLength)),+ documentType: "video",+ };+ }+const stats = statsMap.get(`${dv.id}:${dv.documentId}`);const totalPages = dv.document?.versions?.[0]?.numPages ?? 0;const pagesViewed = stats?.pages_viewed ?? 0;- const completionRate =+ const pageCompletion =totalPages > 0 ? Math.round((pagesViewed / totalPages) * 100) : 0;return {@@ -143,7 +197,8 @@ export default async function handle(totalDuration: stats?.sum_duration ?? 0,pagesViewed,totalPages,- completionRate,+ completionRate: pageCompletion,+ documentType,};});diff --git a/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/history.ts b/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/history.tsindex b26399d4..e9b42e2c 100644--- a/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/history.ts+++ b/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/history.ts@@ -69,6 +69,12 @@ export default async function handle(select: {id: true,name: true,+ type: true,+ versions: {+ where: { isPrimary: true },+ take: 1,+ select: { type: true },+ },},},},diff --git a/pages/api/teams/[teamId]/documents/[id]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/stats.tsindex 0e0b7fcc..21ddae16 100644--- a/pages/api/teams/[teamId]/documents/[id]/stats.ts+++ b/pages/api/teams/[teamId]/documents/[id]/stats.ts@@ -16,6 +16,13 @@ import {getViewCompletionStats,} from "@/lib/tinybird/pipes";import { CustomUser } from "@/lib/types";+import {+ countablePlaybackEvents,+ completionRate,+ eventsForView,+ resolveVideoLength,+ watchTimeSeconds,+} from "@/lib/video-analytics/playback";export default async function handle(req: NextApiRequest,@@ -194,33 +201,19 @@ export default async function handle(document_id: docId,});- const completionRates = await Promise.all(- filteredViews.map(async (view) => {- const viewEvents =- videoEvents?.data.filter(- (event: any) =>- event.view_id === view.id &&- ["played", "muted", "unmuted", "rate_changed"].includes(- event.event_type,- ) &&- event.end_time > event.start_time &&- event.end_time - event.start_time >= 1,- ) || [];-- const uniqueTimestamps = new Set<number>();- viewEvents.forEach((event: any) => {- for (let t = event.start_time; t < event.end_time; t++) {- uniqueTimestamps.add(Math.floor(t));- }- });-- const videoLength = document.versions[0]?.length || 0;- return videoLength > 0- ? Math.min(100, (uniqueTimestamps.size / videoLength) * 100)- : 0;- }),+ const countable = countablePlaybackEvents(videoEvents?.data);+ const videoLength = resolveVideoLength(+ document.versions[0]?.length,+ countable,);+ const completionRates = filteredViews.map((view) => {+ const { unique } = watchTimeSeconds(+ eventsForView(countable, view.id),+ );+ return completionRate(unique, videoLength);+ });+avgCompletionRate =completionRates.reduce((sum, rate) => sum + rate, 0) /completionRates.length;diff --git a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts b/pages/api/teams/[teamId]/documents/[id]/versions/index.tsindex fb5aaa68..8a622d84 100644--- a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts@@ -16,6 +16,7 @@ import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";import { isMarkdownFile } from "@/lib/utils/get-content-type";import { conversionQueueName } from "@/lib/utils/trigger-utils";+import { videoProcessingMode } from "@/lib/video/processing-plan";import { documentUploadSchema } from "@/lib/zod/url-validation";export default async function handle(@@ -201,21 +202,15 @@ export default async function handle();}- if (- type === "video" &&- contentType !== "video/mp4" &&- contentType?.startsWith("video/")- ) {+ const videoMode = videoProcessingMode({ type, contentType });+ if (videoMode) {await processVideo.trigger({- videoUrl: url,- teamId,- docId: url.split("/")[1], // Extract doc_xxxx from teamId/doc_xxxx/filenamedocumentVersionId: version.id,- fileSize: fileSize || 0,+ mode: videoMode,},{- idempotencyKey: `${teamId}-${version.id}`,+ idempotencyKey: `${teamId}-${version.id}-${videoMode}`,tags: [`team_${teamId}`,`document_${documentId}`,diff --git a/pages/api/teams/[teamId]/documents/[id]/video-analytics.ts b/pages/api/teams/[teamId]/documents/[id]/video-analytics.tsindex 45adc8dd..49de87b6 100644--- a/pages/api/teams/[teamId]/documents/[id]/video-analytics.ts+++ b/pages/api/teams/[teamId]/documents/[id]/video-analytics.ts@@ -7,6 +7,12 @@ import { enforceDocumentMemberScope } from "@/lib/api/rbac/guard";import prisma from "@/lib/prisma";import { getVideoEventsByDocument } from "@/lib/tinybird/pipes";import { CustomUser } from "@/lib/types";+import {+ countablePlaybackEvents,+ documentViewDistribution,+ resolveVideoLength,+ watchTimeSeconds,+} from "@/lib/video-analytics/playback";interface AnalyticsResponse {overall: {@@ -46,101 +52,18 @@ function calculateAnalytics(}try {- // Filter for valid events and ensure valid time ranges > 1 second- const validEvents = events.filter((event) => {- // Check if event has required properties- if (!event || typeof event.event_type !== "string" || !event.view_id) {- console.warn("Invalid event structure:", event);- return false;- }-- // Check if event has valid time properties- if (- typeof event.start_time !== "number" ||- typeof event.end_time !== "number"- ) {- console.warn("Invalid time properties:", event);- return false;- }-- return (- (event.event_type === "played" ||- event.event_type === "muted" ||- event.event_type === "unmuted" ||- event.event_type === "rate_changed") &&- event.end_time > event.start_time &&- event.end_time - event.start_time >= 1 &&- event.start_time >= 0 &&- event.end_time <= videoLength + 10- ); // Allow some buffer- });-- // Get all unique view_ids from any event type+ const validEvents = countablePlaybackEvents(+ events.filter((event) => event?.view_id && event.start_time >= 0),+ );+ const timelineLength = resolveVideoLength(videoLength, validEvents);const uniqueViewIds = new Set(events.map((e) => e.view_id));+ const { total: totalWatchTime } = watchTimeSeconds(validEvents);- // Calculate total watch time- let totalWatchTime = 0;- validEvents.forEach((event) => {- const duration = event.end_time - event.start_time;- totalWatchTime += duration;- });-- // Create a baseline array with zeros for every second- const viewDistributionMap = new Map<- number,- { uniqueViewers: Set<string>; viewDurations: Map<string, number> }- >();- for (let t = 0; t <= videoLength; t++) {- viewDistributionMap.set(t, {- uniqueViewers: new Set(),- viewDurations: new Map(), // Map of view_id to number of times this second was viewed- });- }-- // Fill in the actual playback periods- validEvents.forEach((event) => {- // For each second in the duration, track the view- for (- let t = Math.floor(event.start_time);- t < Math.ceil(event.end_time);- t++- ) {- const stats = viewDistributionMap.get(t);- if (!stats) {- console.warn(`No stats found for time ${t}, skipping`);- continue;- }- stats.uniqueViewers.add(event.view_id);-- // Increment the count for this view_id at this second- const currentCount = stats.viewDurations.get(event.view_id) || 0;- stats.viewDurations.set(event.view_id, currentCount + 1);- }- });-- // Sort events by timestamp to find first and last viewconst sortedEvents = [...validEvents].sort((a, b) =>new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),);- // Convert view distribution to sorted array with both metrics- const distributionArray = Array.from(viewDistributionMap.entries())- .map(([start_time, stats]) => {- // Sum up all view durations for this second- let totalViews = 0;- stats.viewDurations.forEach((count) => {- totalViews += count;- });-- return {- start_time,- unique_views: stats.uniqueViewers.size,- total_views: totalViews,- };- })- .sort((a, b) => a.start_time - b.start_time);-return {overall: {unique_views: uniqueViewIds.size,@@ -154,7 +77,10 @@ function calculateAnalytics(sortedEvents.length > 0? sortedEvents[sortedEvents.length - 1].timestamp: "",- view_distribution: distributionArray,+ view_distribution: documentViewDistribution(+ validEvents,+ timelineLength,+ ),},};} catch (error) {@@ -226,10 +152,7 @@ export default async function handle(return res.status(404).json({ message: "Document not found" });}- const videoLength = document.versions[0]?.length || 51;- if (!videoLength) {- return res.status(400).json({ message: "Video length not found" });- }+ const videoLength = document.versions[0]?.length ?? 0;try {// Fetch video events from Tinybirddiff --git a/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/video-stats.ts b/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/video-stats.tsindex 469ea7e2..ab5a7b9a 100644--- a/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/video-stats.ts+++ b/pages/api/teams/[teamId]/documents/[id]/views/[viewId]/video-stats.ts@@ -7,6 +7,11 @@ import { enforceDocumentMemberScope } from "@/lib/api/rbac/guard";import prisma from "@/lib/prisma";import { getVideoEventsByView } from "@/lib/tinybird/pipes";import { CustomUser } from "@/lib/types";+import {+ countablePlaybackEvents,+ resolveVideoLength,+ viewPlaybackTimeline,+} from "@/lib/video-analytics/playback";export default async function handler(req: NextApiRequest,@@ -74,12 +79,8 @@ export default async function handler(return res.status(404).json({ error: "Document not found" });}- const videoLength = doc.versions[0]?.length;- if (!videoLength) {- return res.status(400).json({ error: "Video length not found" });- }+ const storedLength = doc.versions[0]?.length ?? 0;- // Fetch video events from Tinybirdconst response = await getVideoEventsByView({view_id: viewId,document_id: documentId,@@ -89,41 +90,11 @@ export default async function handler(return res.status(200).json({ data: [] });}- // Filter for valid events and ensure valid time ranges > 1 second- const validEvents = response.data.filter(- (event) =>- (event.event_type === "played" ||- event.event_type === "muted" ||- event.event_type === "unmuted" ||- event.event_type === "rate_changed") &&- event.end_time > event.start_time &&- event.end_time - event.start_time >= 1,- );-- // Create a baseline array with zeros for every second- const viewDistributionMap = new Map<number, number>();- for (let t = 0; t <= videoLength; t++) {- viewDistributionMap.set(t, 0);- }-- // Fill in the actual playback periods- validEvents.forEach((event) => {- // For each second in the duration, increment the view count- for (let t = event.start_time; t < event.end_time; t++) {- viewDistributionMap.set(t, (viewDistributionMap.get(t) || 0) + 1);- }- });-- // Convert to sorted array- const distributionArray = Array.from(viewDistributionMap.entries())- .map(([start_time, views]) => ({- start_time,- views,- }))- .sort((a, b) => a.start_time - b.start_time);+ const validEvents = countablePlaybackEvents(response.data);+ const videoLength = resolveVideoLength(storedLength, validEvents);return res.status(200).json({- data: distributionArray,+ data: viewPlaybackTimeline(validEvents, videoLength),});} catch (error) {console.error("Error fetching video stats:", error);diff --git a/pages/api/teams/[teamId]/documents/[id]/views/index.ts b/pages/api/teams/[teamId]/documents/[id]/views/index.tsindex 96bdaa93..ea7433bb 100644--- a/pages/api/teams/[teamId]/documents/[id]/views/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/views/index.ts@@ -15,6 +15,13 @@ import { getViewPageDuration } from "@/lib/tinybird";import { getVideoEventsByDocument } from "@/lib/tinybird/pipes";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";+import {+ countablePlaybackEvents,+ completionRate,+ eventsForView,+ resolveVideoLength,+ watchTimeSeconds,+} from "@/lib/video-analytics/playback";type DocumentVersion = {versionNumber: number;@@ -67,52 +74,22 @@ async function getVideoViews(document: Document,videoEvents: { data: VideoEvent[] },) {- const durationsPromises = views.map((view) => {- const viewEvents =- videoEvents?.data.filter(- (event) =>- event.view_id === view.id &&- ["played", "muted", "unmuted", "rate_changed"].includes(- event.event_type,- ) &&- event.end_time > event.start_time &&- event.end_time - event.start_time >= 1,- ) || [];-- // Track timestamps and their frequency for total watch time- const timestampCounts = new Map<number, number>();- // Track unique timestamps for completion calculation- const uniqueTimestamps = new Set<number>();-- // Calculate total watch time- // let totalWatchTime = 0;- viewEvents.forEach((event) => {- for (let t = event.start_time; t < event.end_time; t++) {- const timestamp = Math.floor(t);- // Count total occurrences including replays- timestampCounts.set(- timestamp,- (timestampCounts.get(timestamp) || 0) + 1,- );- // Track unique timestamps- uniqueTimestamps.add(timestamp);- }- });-- // Sum up all timestamps including duplicates for total watch time- let totalWatchTime = 0;- timestampCounts.forEach((count) => {- totalWatchTime += count;- });+ const countable = countablePlaybackEvents(videoEvents?.data);+ const videoLength = resolveVideoLength(+ document.versions[0]?.length,+ countable,+ );- // Get the number of unique timestamps watched- const uniqueWatchTime = uniqueTimestamps.size;+ const durationsPromises = views.map((view) => {+ const { total, unique } = watchTimeSeconds(+ eventsForView(countable, view.id),+ );return {data: [],- totalWatchTime,- uniqueWatchTime,- videoLength: document.versions[0]?.length || 0,+ totalWatchTime: total,+ uniqueWatchTime: unique,+ videoLength,};});@@ -124,16 +101,15 @@ async function getVideoViews();const duration = durations[index];- const completionRate =- duration.videoLength > 0- ? Math.min(100, (duration.uniqueWatchTime / duration.videoLength) * 100)- : 0;return {...view,duration: durations[index],- totalDuration: duration.totalWatchTime * 1000, // convert to milliseconds- completionRate: completionRate.toFixed(),+ totalDuration: duration.totalWatchTime * 1000,+ completionRate: completionRate(+ duration.uniqueWatchTime,+ duration.videoLength,+ ).toFixed(),versionNumber: relevantDocumentVersion?.versionNumber || 1,versionNumPages: 0,};diff --git a/pages/api/teams/[teamId]/links/[id]/visits.ts b/pages/api/teams/[teamId]/links/[id]/visits.tsindex 8e8a459c..24b87ad2 100644--- a/pages/api/teams/[teamId]/links/[id]/visits.ts+++ b/pages/api/teams/[teamId]/links/[id]/visits.ts@@ -6,9 +6,16 @@ import { LIMITS } from "@/lib/constants";import { errorhandler } from "@/lib/errorHandler";import prisma from "@/lib/prisma";import { getDocumentWithTeamAndUser } from "@/lib/team/helper";-import { getViewPageDuration } from "@/lib/tinybird";+import { getViewPageDuration, getVideoEventsByDocument } from "@/lib/tinybird";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";+import {+ countablePlaybackEvents,+ completionRate,+ eventsForView,+ resolveVideoLength,+ watchTimeSeconds,+} from "@/lib/video-analytics/playback";import { authOptions } from "../../../../auth/[...nextauth]";@@ -52,11 +59,12 @@ export default async function handle(select: {id: true,numPages: true,+ type: true,versions: {where: { isPrimary: true },orderBy: { createdAt: "desc" },take: 1,- select: { numPages: true },+ select: { numPages: true, length: true },},team: {select: {@@ -177,40 +185,61 @@ export default async function handle(}}- const durationsPromises = limitedViews.map((view) => {- return getViewPageDuration({- documentId: view.documentId!,- viewId: view.id,- since: 0,- });- });+ const isVideo = result.document.type === "video";+ let viewsWithDuration;- const durations = await Promise.all(durationsPromises);+ if (isVideo) {+ const videoEvents = await getVideoEventsByDocument({+ document_id: docId,+ });+ const countable = countablePlaybackEvents(videoEvents?.data);+ const videoLength = resolveVideoLength(+ result.document.versions[0]?.length,+ countable,+ );- // Sum up durations for each view- const summedDurations = durations.map((duration) => {- return duration.data.reduce(- (totalDuration, data) => totalDuration + data.sum_duration,- 0,+ viewsWithDuration = limitedViews.map((view) => {+ const { total, unique } = watchTimeSeconds(+ eventsForView(countable, view.id),+ );+ return {+ ...view,+ duration: { data: [] },+ totalDuration: total * 1000,+ completionRate: completionRate(unique, videoLength).toFixed(),+ };+ });+ } else {+ const durations = await Promise.all(+ limitedViews.map((view) =>+ getViewPageDuration({+ documentId: view.documentId!,+ viewId: view.id,+ since: 0,+ }),+ ),);- });- // Construct the response combining views and their respective durations- const viewsWithDuration = limitedViews.map((view, index) => {- const viewNumPages = view.documentId- ? (numPagesByDocumentId.get(view.documentId) ?? currentDocNumPages)- : currentDocNumPages;- const completionRate = viewNumPages- ? (durations[index].data.length / viewNumPages) * 100- : 0;-- return {- ...view,- duration: durations[index],- totalDuration: summedDurations[index],- completionRate: completionRate.toFixed(),- };- });+ viewsWithDuration = limitedViews.map((view, index) => {+ const viewNumPages = view.documentId+ ? (numPagesByDocumentId.get(view.documentId) ?? currentDocNumPages)+ : currentDocNumPages;+ const viewCompletion = viewNumPages+ ? (durations[index].data.length / viewNumPages) * 100+ : 0;+ const totalDuration = durations[index].data.reduce(+ (sum, data) => sum + data.sum_duration,+ 0,+ );++ return {+ ...view,+ duration: durations[index],+ totalDuration,+ completionRate: viewCompletion.toFixed(),+ };+ });+ }// TODO: Check that the user is owner of the links, otherwise return 401