Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): improve player handling for edit processing

Richie McIlroy7911c024f72b
diff --git a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
index 970f237aa..e2d2b50e1 100644
--- a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
@@ -88,8 +88,10 @@ interface Props {
autoplay?: boolean;
enableCrossOrigin?: boolean;
hasActiveUpload: boolean | undefined;
+ blockPlaybackDuringProcessing?: boolean;
disableCommentStamps?: boolean;
disableReactionStamps?: boolean;
+ disablePreviewGif?: boolean;
comments?: Array<{
id: string;
timestamp: number | null;
@@ -108,6 +110,7 @@ interface Props {
canRetryProcessing?: boolean;
duration?: number | null;
showPlaybackStatusBadge?: boolean;
+ onUploadComplete?: () => void;
}
export function CapVideoPlayer({
@@ -122,9 +125,11 @@ export function CapVideoPlayer({
autoplay = false,
enableCrossOrigin = false,
hasActiveUpload,
+ blockPlaybackDuringProcessing = false,
comments = [],
disableCommentStamps = false,
disableReactionStamps = false,
+ disablePreviewGif = false,
onSeek,
enhancedAudioUrl: _enhancedAudioUrl,
enhancedAudioStatus: _enhancedAudioStatus,
@@ -136,6 +141,7 @@ export function CapVideoPlayer({
canRetryProcessing = false,
duration: fallbackDuration,
showPlaybackStatusBadge = false,
+ onUploadComplete,
}: Props) {
const [currentCue, setCurrentCue] = useState<string>("");
const [controlsVisible, setControlsVisible] = useState(false);
@@ -170,7 +176,11 @@ export function CapVideoPlayer({
videoId,
hasActiveUpload || false,
);
- const uploadProgress = videoLoaded ? null : uploadProgressRaw;
+ const uploadProgress = blockPlaybackDuringProcessing
+ ? uploadProgressRaw
+ : videoLoaded
+ ? null
+ : uploadProgressRaw;
const isUploading = uploadProgress?.status === "uploading";
const isProcessing = uploadProgress?.status === "processing";
const isGeneratingThumbnail =
@@ -524,15 +534,16 @@ export function CapVideoPlayer({
}
}, [canRetryUploadProcessing, isRetryingProcessing, queryClient, videoId]);
- const prevUploadProgress = useRef<typeof uploadProgress>(uploadProgress);
+ const prevUploadProgress =
+ useRef<typeof uploadProgressRaw>(uploadProgressRaw);
useEffect(() => {
if (
shouldReloadPlaybackAfterUploadCompletes(
prevUploadProgress.current,
- uploadProgress,
- videoLoaded,
+ uploadProgressRaw,
)
) {
+ setVideoLoaded(false);
setHasError(false);
void queryClient.invalidateQueries({
queryKey: [
@@ -543,18 +554,37 @@ export function CapVideoPlayer({
preferredSource,
],
});
+ onUploadComplete?.();
}
- prevUploadProgress.current = uploadProgress;
+ prevUploadProgress.current = uploadProgressRaw;
}, [
enableCrossOrigin,
+ onUploadComplete,
preferredSource,
queryClient,
rawFallbackSrc,
- uploadProgress,
- videoLoaded,
+ uploadProgressRaw,
videoSrc,
]);
+ const editRecoveryRefreshTriggeredRef = useRef(false);
+ useEffect(() => {
+ if (
+ !blockPlaybackDuringProcessing ||
+ uploadProgressRaw?.status !== "error" ||
+ editRecoveryRefreshTriggeredRef.current
+ ) {
+ return;
+ }
+
+ editRecoveryRefreshTriggeredRef.current = true;
+ onUploadComplete?.();
+ }, [
+ blockPlaybackDuringProcessing,
+ onUploadComplete,
+ uploadProgressRaw?.status,
+ ]);
+
const showPreparingOverlay =
!videoLoaded &&
!uploadProgress &&
@@ -573,7 +603,8 @@ export function CapVideoPlayer({
? "The processed version is unavailable right now, so this page is playing the original uploaded file instead."
: "This page is temporarily playing the original uploaded file while Cap finishes processing the optimized version for smoother playback and broader compatibility.";
const blockPlaybackControls =
- (!videoLoaded && hasActiveProgress) || showUploadFailureOverlay;
+ ((blockPlaybackDuringProcessing || !videoLoaded) && hasActiveProgress) ||
+ showUploadFailureOverlay;
return (
<MediaPlayer
@@ -649,6 +680,7 @@ export function CapVideoPlayer({
<VideoPreviewGif
videoId={videoId}
visible={
+ !disablePreviewGif &&
videoLoaded &&
!hasPlayedOnce &&
!showUploadFailureOverlay &&
@@ -685,63 +717,68 @@ export function CapVideoPlayer({
</MediaPlayerVideo>
)}
<AnimatePresence>
- {!videoLoaded && hasActiveProgress && !showUploadFailureOverlay && (
- <>
- <motion.div
- initial={{ opacity: 0 }}
- animate={{ opacity: 1 }}
- exit={{ opacity: 0 }}
- transition={{ duration: 0.2 }}
- className="absolute inset-0 z-10 transition-all duration-300 bg-black/60 rounded-xl"
- />
- <motion.div
- initial={{ opacity: 0, y: 10 }}
- animate={{ opacity: 1, y: 0 }}
- exit={{ opacity: 0, y: 10 }}
- transition={{ duration: 0.2 }}
- className="flex absolute bottom-3 left-3 gap-2 items-center z-20"
- >
- <span className="text-sm font-semibold text-white">
- {getProgressStatusText(
- isProcessing
- ? "processing"
- : isGeneratingThumbnail
- ? "generating_thumbnail"
- : "uploading",
- )}
- {uploadProgress?.progress != null &&
- uploadProgress.progress > 0 &&
- ` ${Math.round(uploadProgress.progress)}%`}
- </span>
- <svg className="w-4 h-4 transform -rotate-90" viewBox="0 0 20 20">
- <title>Progress</title>
- <circle
- cx="10"
- cy="10"
- r="8"
- stroke="currentColor"
- strokeWidth="3"
- fill="none"
- className="text-white/30"
- />
- <circle
- cx="10"
- cy="10"
- r="8"
- stroke="currentColor"
- strokeWidth="3"
- fill="none"
- strokeLinecap="round"
- className="text-white transition-all duration-200 ease-out"
- style={{
- strokeDasharray: `${circumference} ${circumference}`,
- strokeDashoffset: `${calculateStrokeDashoffset(uploadProgress?.progress ?? 0, circumference)}`,
- }}
- />
- </svg>
- </motion.div>
- </>
- )}
+ {(blockPlaybackDuringProcessing || !videoLoaded) &&
+ hasActiveProgress &&
+ !showUploadFailureOverlay && (
+ <>
+ <motion.div
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{ duration: 0.2 }}
+ className="absolute inset-0 z-10 transition-all duration-300 bg-black/60 rounded-xl"
+ />
+ <motion.div
+ initial={{ opacity: 0, y: 10 }}
+ animate={{ opacity: 1, y: 0 }}
+ exit={{ opacity: 0, y: 10 }}
+ transition={{ duration: 0.2 }}
+ className="flex absolute bottom-3 left-3 gap-2 items-center z-20"
+ >
+ <span className="text-sm font-semibold text-white">
+ {getProgressStatusText(
+ isProcessing
+ ? "processing"
+ : isGeneratingThumbnail
+ ? "generating_thumbnail"
+ : "uploading",
+ )}
+ {uploadProgress?.progress != null &&
+ uploadProgress.progress > 0 &&
+ ` ${Math.round(uploadProgress.progress)}%`}
+ </span>
+ <svg
+ className="w-4 h-4 transform -rotate-90"
+ viewBox="0 0 20 20"
+ >
+ <title>Progress</title>
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ className="text-white/30"
+ />
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ strokeLinecap="round"
+ className="text-white transition-all duration-200 ease-out"
+ style={{
+ strokeDasharray: `${circumference} ${circumference}`,
+ strokeDashoffset: `${calculateStrokeDashoffset(uploadProgress?.progress ?? 0, circumference)}`,
+ }}
+ />
+ </svg>
+ </motion.div>
+ </>
+ )}
{showPlayButton &&
videoLoaded &&
!hasPlayedOnce &&
diff --git a/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
index e9085b88a..f5fd76862 100644
--- a/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
@@ -580,14 +580,13 @@ export function HLSVideoPlayer({
shouldReloadPlaybackAfterUploadCompletes(
prevUploadProgress.current,
uploadProgress,
- videoLoaded,
)
) {
reloadPlayback();
setTimeout(reloadPlayback, 1000);
}
prevUploadProgress.current = uploadProgress;
- }, [uploadProgress, videoLoaded, reloadPlayback]);
+ }, [uploadProgress, reloadPlayback]);
return (
<MediaPlayer
diff --git a/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx b/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx
index 19b5cae78..1642a5abd 100644
--- a/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx
+++ b/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx
@@ -46,13 +46,11 @@ export function shouldDeferPlaybackSource(
export function shouldReloadPlaybackAfterUploadCompletes(
previousUploadProgress: UploadProgress | null,
uploadProgress: UploadProgress | null,
- videoLoaded: boolean,
): boolean {
return (
previousUploadProgress !== null &&
previousUploadProgress.status !== "fetching" &&
- uploadProgress === null &&
- !videoLoaded
+ uploadProgress === null
);
}
@@ -115,6 +113,10 @@ export function getStalledProcessingMessage(input: {
return "Video finishing stalled. Retry processing.";
}
+ if (input.phase === "complete" && ageMs > STALE_THUMBNAIL_MS) {
+ return "Video finishing stalled. Retry processing.";
+ }
+
return null;
}
@@ -154,7 +156,13 @@ export function useUploadProgress(
processingProgress: query.data.processingProgress,
});
- if (phase === "complete") return null;
+ if (phase === "complete") {
+ return {
+ status: "generating_thumbnail",
+ lastUpdated,
+ progress: 100,
+ };
+ }
if (phase === "error") {
return {
diff --git a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
index f31f5c086..a6096d3e9 100644
--- a/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
+++ b/apps/web/app/s/[videoId]/_components/ShareVideo.tsx
@@ -6,6 +6,7 @@ import { useTranscript } from "hooks/use-transcript";
import { useRouter } from "next/navigation";
import {
forwardRef,
+ useCallback,
useEffect,
useImperativeHandle,
useRef,
@@ -69,6 +70,9 @@ export const ShareVideo = forwardRef<
const videoRef = useRef<HTMLVideoElement | null>(null);
useImperativeHandle(ref, () => videoRef.current as HTMLVideoElement, []);
const router = useRouter();
+ const handleUploadComplete = useCallback(() => {
+ router.refresh();
+ }, [router]);
const captionContext = useCaptionContext();
@@ -190,6 +194,9 @@ export const ShareVideo = forwardRef<
const isMp4Source =
data.source.type === "desktopMP4" || data.source.type === "webMP4";
const isSegmentsSource = data.source.type === "desktopSegments";
+ const isEditProcessing =
+ data.activeUploadRawFileKey ===
+ `${data.owner.id}/${data.id}/source/original.mp4`;
const isActivelyRecording =
isSegmentsSource &&
(data.hasActiveUpload ?? false) &&
@@ -256,7 +263,10 @@ export const ShareVideo = forwardRef<
return (
<>
- <div className="relative h-full">
+ <div
+ className="relative h-full"
+ style={{ viewTransitionName: "cap-edit-video" }}
+ >
{isActivelyRecording ? (
<RecordingInProgressOverlay
onConfirmStopped={() => setUserConfirmedStopped(true)}
@@ -280,6 +290,8 @@ export const ShareVideo = forwardRef<
videoRef={videoRef}
enableCrossOrigin={enableCrossOrigin}
hasActiveUpload={data.hasActiveUpload}
+ blockPlaybackDuringProcessing={isEditProcessing}
+ onUploadComplete={handleUploadComplete}
comments={commentsData.map((comment) => ({
id: comment.id,
type: comment.type,
diff --git a/apps/web/app/s/[videoId]/_components/video-frame-thumbnail.ts b/apps/web/app/s/[videoId]/_components/video-frame-thumbnail.ts
index 848c80064..06495e03e 100644
--- a/apps/web/app/s/[videoId]/_components/video-frame-thumbnail.ts
+++ b/apps/web/app/s/[videoId]/_components/video-frame-thumbnail.ts
@@ -3,12 +3,13 @@ export interface CaptureVideoFrameDeps {
createCanvas?: () => HTMLCanvasElement;
width?: number;
height?: number;
+ quality?: number;
}
export function captureVideoFrameDataUrl(
deps: CaptureVideoFrameDeps,
): string | undefined {
- const { video, width = 224, height = 128 } = deps;
+ const { video, width = 224, height = 128, quality = 0.8 } = deps;
if (!video) return undefined;
if (video.readyState < 2) return undefined;
const createCanvas =
@@ -20,7 +21,7 @@ export function captureVideoFrameDataUrl(
if (!ctx) return undefined;
try {
ctx.drawImage(video, 0, 0, width, height);
- return canvas.toDataURL("image/jpeg", 0.8);
+ return canvas.toDataURL("image/jpeg", quality);
} catch {
return undefined;
}