:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(replay): say when a recording is too long to export as one video (#83410)
Kim Svatos Dugan0d7cdfce4cea
diff --git a/frontend/src/lib/components/ExportButton/exportStatus.test.ts b/frontend/src/lib/components/ExportButton/exportStatus.test.tsindex 0e1d63171e06..ad42de676925 100644--- a/frontend/src/lib/components/ExportButton/exportStatus.test.ts+++ b/frontend/src/lib/components/ExportButton/exportStatus.test.ts@@ -1,9 +1,11 @@import { ExportedAssetType, ExporterFormat } from '~/types'import {+ MAX_EXPORTABLE_RECORDING_SECONDS,getExportDisabledReason,getExportPendingLabel,getExportPendingStatus,+ getVideoExportDisabledReason,isLongRunningExportFormat,} from './exportStatus'@@ -35,6 +37,29 @@ describe('exportStatus', () => {})})+ describe('getVideoExportDisabledReason', () => {+ // Past this length no capture rate produces a watchable video, so the API refuses the export.+ // Disabling the option first is what stops someone spending a click to find that out.+ it.each([+ ['a short recording', 5 * 60 * 1000],+ ['a recording at the limit', MAX_EXPORTABLE_RECORDING_SECONDS * 1000],+ ])('allows %s', (_label, durationMs) => {+ expect(getVideoExportDisabledReason(durationMs)).toBeUndefined()+ })++ it('gives a reason past the limit, and says what to do instead', () => {+ const reason = getVideoExportDisabledReason((MAX_EXPORTABLE_RECORDING_SECONDS + 1) * 1000)++ expect(reason).toContain('3 hours')+ expect(reason).toContain('clip')+ })++ it('allows the export while the duration is unknown', () => {+ // Loading is not the same as too long, and guessing wrong here hides a usable export.+ expect(getVideoExportDisabledReason(undefined)).toBeUndefined()+ })+ })+describe('getExportPendingStatus', () => {it('returns null when the asset has content', () => {expect(getExportPendingStatus(baseAsset({ has_content: true }))).toBeNull()diff --git a/frontend/src/lib/components/ExportButton/exportStatus.ts b/frontend/src/lib/components/ExportButton/exportStatus.tsindex 76c5287b7ea1..16388633bae0 100644--- a/frontend/src/lib/components/ExportButton/exportStatus.ts+++ b/frontend/src/lib/components/ExportButton/exportStatus.ts@@ -29,6 +29,21 @@ export function getExportPendingLabel(asset: ExportedAssetType): string | null {return null}+// A whole-session video longer than this cannot render inside the rasterizer's time budget. The+// backend does not refuse such exports, so this check is what stops a doomed render from starting.+export const MAX_EXPORTABLE_RECORDING_SECONDS = 3 * 60 * 60++export function getVideoExportDisabledReason(recordingDurationMs: number | undefined): string | undefined {+ if (recordingDurationMs === undefined) {+ return undefined+ }+ if (recordingDurationMs / 1000 <= MAX_EXPORTABLE_RECORDING_SECONDS) {+ return undefined+ }+ const hours = MAX_EXPORTABLE_RECORDING_SECONDS / 3600+ return `This recording is longer than ${hours} hours, which is too long to export as one video. Use the clip button to export part of it.`+}+export function getExportDisabledReason(asset: ExportedAssetType): string | undefined {if (asset.exception) {return asset.exceptiondiff --git a/frontend/src/lib/components/ExportButton/exportsLogic.ts b/frontend/src/lib/components/ExportButton/exportsLogic.tsindex 14e014e7f770..d2f461983c30 100644--- a/frontend/src/lib/components/ExportButton/exportsLogic.ts+++ b/frontend/src/lib/components/ExportButton/exportsLogic.ts@@ -444,9 +444,11 @@ export const exportsLogic = kea<exportsLogicType>([export_context: exportData.export_context,})} catch (error) {- // Preserve the export-limit error so the caller can show the upsell;- // give everything else a friendly message for the failure toast.- if ((error as { data?: APIErrorType })?.data?.attr === 'export_limit_exceeded') {+ // Preserve the errors the caller renders itself — the limit upsell and the+ // reason a recording is too long — and give everything else a friendly+ // message for the failure toast.+ const attr = (error as { data?: APIErrorType })?.data?.attr+ if (attr === 'export_limit_exceeded' || attr === 'export_duration_unsupported') {throw error}const message = error instanceof Error ? error.message : String(error)@@ -532,6 +534,16 @@ export const exportsLogic = kea<exportsLogicType>([},})}+ // The recording is longer than the renderer can turn into one video. The+ // API is what decides that, so its reason is shown rather than a generic+ // failure: it names the limit and what to do instead.+ if (apiError?.data?.attr === 'export_duration_unsupported') {+ lemonToast.dismiss(exportToastId)+ lemonToast.error(+ apiError?.data?.detail ||+ 'This recording is too long to export as one video. Export part of it instead.'+ )+ }}})()diff --git a/frontend/src/scenes/session-recordings/player/controller/ClipRecording.tsx b/frontend/src/scenes/session-recordings/player/controller/ClipRecording.tsxindex 393b53b34115..50b39ab1b438 100644--- a/frontend/src/scenes/session-recordings/player/controller/ClipRecording.tsx+++ b/frontend/src/scenes/session-recordings/player/controller/ClipRecording.tsx@@ -12,6 +12,8 @@ import { sessionRecordingPlayerLogic } from 'scenes/session-recordings/player/seimport { ExporterFormat } from '~/types'+import { MIN_CLIP_DURATION_SECONDS, clipDurationOptions, clipWindowSeconds } from './clipRange'+interface ClipTimes {current: stringstartClip: string@@ -25,24 +27,10 @@ function calculateClipTimes(currentTimeMs: number | null, sessionDurationMs: numconst current = colonDelimitedDuration(startTimeSeconds, fixedUnits)- // Calculate ideal start/end centered around current time- let idealStart = startTimeSeconds - clipDuration / 2- let idealEnd = startTimeSeconds + clipDuration / 2-- // Adjust if we hit the beginning boundary- if (idealStart < 0) {- idealStart = 0- idealEnd = Math.min(clipDuration, endTimeSeconds)- }-- // Adjust if we hit the end boundary- if (idealEnd > endTimeSeconds) {- idealEnd = endTimeSeconds- idealStart = Math.max(0, endTimeSeconds - clipDuration)- }+ const window = clipWindowSeconds(startTimeSeconds, endTimeSeconds, clipDuration)- const startClip = colonDelimitedDuration(idealStart, fixedUnits)- const endClip = colonDelimitedDuration(idealEnd, fixedUnits)+ const startClip = colonDelimitedDuration(window.startSeconds, fixedUnits)+ const endClip = colonDelimitedDuration(window.endSeconds, fixedUnits)return { current, startClip, endClip }}@@ -51,9 +39,14 @@ export function ClipOverlay(): JSX.Element | null {const { currentPlayerTime, sessionPlayerData, showingClipParams, sessionRecordingId } =useValues(sessionRecordingPlayerLogic)const { getClip, setShowingClipParams } = useActions(sessionRecordingPlayerLogic)- const [duration, setDuration] = useState(5)+ const [duration, setDuration] = useState(MIN_CLIP_DURATION_SECONDS)const [format, setFormat] = useState(ExporterFormat.MP4)+ const durationOptions = useMemo(+ () => clipDurationOptions(sessionPlayerData.durationMs),+ [sessionPlayerData.durationMs]+ )+const { current, startClip, endClip } = calculateClipTimes(currentPlayerTime,sessionPlayerData.durationMs,@@ -99,15 +92,11 @@ export function ClipOverlay(): JSX.Element | null {</div><div className="space-y-1">- <label className="block text-sm font-medium text-default">Duration (seconds)</label>+ <label className="block text-sm font-medium text-default">Duration</label><LemonSegmentedSelectfullWidthsize="xsmall"- options={[- { value: 5, label: '5', 'data-attr': 'replay-clip-duration-5' },- { value: 10, label: '10', 'data-attr': 'replay-clip-duration-10' },- { value: 15, label: '15', 'data-attr': 'replay-clip-duration-15' },- ]}+ options={durationOptions}value={duration}onChange={(value) => setDuration(value)}/>@@ -121,7 +110,9 @@ export function ClipOverlay(): JSX.Element | null {type="primary"className="mt-3 mx-auto"disabledReason={- !duration || duration < 5 || duration > 15 ? 'Duration must be between 5 and 15 seconds' : undefined+ durationOptions.some((option) => option.value === duration)+ ? undefined+ : 'Pick one of the durations above'}data-attr="replay-clip-create">@@ -168,7 +159,7 @@ function ClipRecording_({ current, className }: { current: string; className?: sexport function ClipRecording({ className }: { className?: string }): JSX.Element {const { currentPlayerTime, sessionPlayerData } = useValues(sessionRecordingPlayerLogic)- const { current } = calculateClipTimes(currentPlayerTime, sessionPlayerData.durationMs, 5)+ const { current } = calculateClipTimes(currentPlayerTime, sessionPlayerData.durationMs, MIN_CLIP_DURATION_SECONDS)return <ClipRecording_ current={current} className={className} />}diff --git a/frontend/src/scenes/session-recordings/player/controller/clipRange.test.ts b/frontend/src/scenes/session-recordings/player/controller/clipRange.test.tsnew file mode 100644index 000000000000..8d328ae94116--- /dev/null+++ b/frontend/src/scenes/session-recordings/player/controller/clipRange.test.ts@@ -0,0 +1,44 @@+import { clipDurationOptions, clipWindowSeconds } from './clipRange'++describe('clipRange', () => {+ describe('clipDurationOptions', () => {+ const values = (durationMs: number): number[] => clipDurationOptions(durationMs).map((option) => option.value)++ it('offers only the short durations for a short recording', () => {+ expect(values(30 * 1000)).toEqual([5, 10, 15])+ })++ it('offers minute-long clips once the recording is long enough to need them', () => {+ // A recording too long to export whole is reviewed in parts, and seconds are not a useful part.+ expect(values(2 * 60 * 60 * 1000)).toEqual([5, 10, 15, 60, 300, 900])+ })++ it('leaves out durations longer than the recording itself', () => {+ // A clip that runs past the end would render an empty tail.+ expect(values(3 * 60 * 1000)).toEqual([5, 10, 15, 60])+ })+ })++ describe('clipWindowSeconds', () => {+ it('centers the window on the playhead', () => {+ expect(clipWindowSeconds(600, 3600, 900)).toEqual({ startSeconds: 150, endSeconds: 1050 })+ })++ it.each([+ ['near the end', 3595, { startSeconds: 2700, endSeconds: 3600 }],+ ['at the very end', 3600, { startSeconds: 2700, endSeconds: 3600 }],+ ])('pulls the window back inside the recording %s', (_label, playhead, expected) => {+ // Both the overlay and the export read this. While only the overlay clamped, the exported+ // file started up to half a clip length later than the range shown on screen.+ expect(clipWindowSeconds(playhead as number, 3600, 900)).toEqual(expected)+ })++ it('pushes the window forward at the start of a recording', () => {+ expect(clipWindowSeconds(2, 3600, 900)).toEqual({ startSeconds: 0, endSeconds: 900 })+ })++ it('covers the whole recording when the clip is longer than it', () => {+ expect(clipWindowSeconds(10, 60, 900)).toEqual({ startSeconds: 0, endSeconds: 60 })+ })+ })+})diff --git a/frontend/src/scenes/session-recordings/player/controller/clipRange.ts b/frontend/src/scenes/session-recordings/player/controller/clipRange.tsnew file mode 100644index 000000000000..26c5c0109705--- /dev/null+++ b/frontend/src/scenes/session-recordings/player/controller/clipRange.ts@@ -0,0 +1,64 @@+export interface ClipDurationOption {+ value: number+ label: string+ 'data-attr': string+}++export interface ClipWindow {+ startSeconds: number+ endSeconds: number+}++// Seconds for a moment worth sharing. Anything past a couple of minutes is a different job, and a+// recording long enough to need one is served by the longer options below.+const SHORT_CLIP_SECONDS = [5, 10, 15]+// Minutes, for reviewing part of a recording too long to export whole.+const LONG_CLIP_MINUTES = [1, 5, 15]++export const MIN_CLIP_DURATION_SECONDS = SHORT_CLIP_SECONDS[0]++export function clipDurationOptions(sessionDurationMs: number): ClipDurationOption[] {+ const sessionSeconds = sessionDurationMs / 1000+ const shortOptions = SHORT_CLIP_SECONDS.map((seconds) => ({+ value: seconds,+ label: `${seconds}s`,+ 'data-attr': `replay-clip-duration-${seconds}`,+ }))++ // Only offered where there is that much recording to clip, so no option produces an empty tail.+ const longOptions = LONG_CLIP_MINUTES.filter((minutes) => minutes * 60 < sessionSeconds).map((minutes) => ({+ value: minutes * 60,+ label: `${minutes}m`,+ 'data-attr': `replay-clip-duration-${minutes}m`,+ }))++ return [...shortOptions, ...longOptions]+}++/**+ * The window a clip covers, centered on the playhead and pulled inside the recording at both ends.+ *+ * Shared by the overlay that shows the range and the export that renders it. Computing it in two+ * places let them disagree near the end of a recording by half the clip length, so the file people+ * got back started later than the range they were shown.+ */+export function clipWindowSeconds(+ currentTimeSeconds: number,+ sessionDurationSeconds: number,+ clipDurationSeconds: number+): ClipWindow {+ let startSeconds = currentTimeSeconds - clipDurationSeconds / 2+ let endSeconds = currentTimeSeconds + clipDurationSeconds / 2++ if (startSeconds < 0) {+ startSeconds = 0+ endSeconds = Math.min(clipDurationSeconds, sessionDurationSeconds)+ }++ if (endSeconds > sessionDurationSeconds) {+ endSeconds = sessionDurationSeconds+ startSeconds = Math.max(0, sessionDurationSeconds - clipDurationSeconds)+ }++ return { startSeconds, endSeconds }+}diff --git a/frontend/src/scenes/session-recordings/player/player-meta/PlayerMetaLinks.tsx b/frontend/src/scenes/session-recordings/player/player-meta/PlayerMetaLinks.tsxindex 76c3267a85e0..b0a53d314ace 100644--- a/frontend/src/scenes/session-recordings/player/player-meta/PlayerMetaLinks.tsx+++ b/frontend/src/scenes/session-recordings/player/player-meta/PlayerMetaLinks.tsx@@ -13,6 +13,7 @@ import {import { LemonButton, LemonButtonProps, LemonDialog, LemonMenu, LemonMenuItems } from '@posthog/lemon-ui'import { AccessControlAction } from 'lib/components/AccessControlAction'+import { getVideoExportDisabledReason } from 'lib/components/ExportButton/exportStatus'import { IconBlank } from 'lib/lemon-ui/icons'import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'import { useNotebookNode } from 'scenes/notebooks/Nodes/NotebookNodeContext'@@ -132,7 +133,8 @@ const AddToNotebookButton = ({ fullWidth = false }: Pick<LemonButtonProps, 'full}const MenuActions = ({ size }: { size: PlayerMetaBreakpoints }): JSX.Element => {- const { logicProps, isMuted, hasReachedExportFullVideoLimit } = useValues(sessionRecordingPlayerLogic)+ const { logicProps, isMuted, hasReachedExportFullVideoLimit, sessionPlayerData } =+ useValues(sessionRecordingPlayerLogic)const { deleteRecording, setIsFullScreen, exportRecordingToFile, exportRecordingToVideoFile, setMuted } =useActions(sessionRecordingPlayerLogic)const { skipInactivitySetting } = useValues(playerSettingsLogic)@@ -147,6 +149,8 @@ const MenuActions = ({ size }: { size: PlayerMetaBreakpoints }): JSX.Element =>const isStandardMode =(logicProps.mode ?? SessionRecordingPlayerMode.Standard) === SessionRecordingPlayerMode.Standard+ const tooLongToExportReason = getVideoExportDisabledReason(sessionPlayerData?.durationMs)+const onDelete = useMemo(() => () => {setIsFullScreen(false)@@ -210,6 +214,7 @@ const MenuActions = ({ size }: { size: PlayerMetaBreakpoints }): JSX.Element =>: 'Export PostHog recording data to MP4 video file.',disabledReason:(hasReachedExportFullVideoLimit ? 'You have reached your export limit.' : undefined) ??+ tooLongToExportReason ??exportAccessControlDisabledReason ??undefined,'data-attr': 'replay-export-mp4',@@ -243,6 +248,7 @@ const MenuActions = ({ size }: { size: PlayerMetaBreakpoints }): JSX.Element =>isMuted,setMuted,hasReachedExportFullVideoLimit,+ tooLongToExportReason,exportAccessControlDisabledReason,])diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.tsindex 3657c87deaaa..f35bfba652cf 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts@@ -71,6 +71,7 @@ import {playerCommentOverlayLogic,type playerCommentOverlayLogicType,} from './commenting/playerFrameCommentOverlayLogic'+import { clipWindowSeconds } from './controller/clipRange'import { playerSettingsLogic } from './playerSettingsLogic'import { snapshotDataLogic } from './snapshotDataLogic'import {@@ -3102,11 +3103,14 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(actions.exportRecording(ExporterFormat.PNG, timestamp, SessionRecordingPlayerMode.Screenshot)},getClip: async ({ format, duration = 5, filename }) => {- // Center the clip around current time, minus 1 second offset for player start- const timestamp = Math.max(- 0,- Math.floor(getCurrentPlayerTime(values.logicProps) - 1 - Math.floor(duration / 2))+ // The window the overlay showed, so the exported file covers the range that was on screen.+ const window = clipWindowSeconds(+ getCurrentPlayerTime(values.logicProps),+ Math.floor((values.sessionPlayerData?.durationMs ?? 0) / 1000),+ duration)+ // Minus 1 second offset for player start+ const timestamp = Math.max(0, Math.floor(window.startSeconds - 1))actions.exportRecording(format, timestamp, SessionRecordingPlayerMode.Screenshot, duration, filename)},exportRecordingToVideoFile: async () => {