import React, { useCallback } from "react"; import { WEBCAM_PREFIX } from "../../config/cameras"; import { RecordingStatus } from "../RecordButton"; import { cancelTranscribeOnServer } from "../helpers/cancel-transcribe"; import { convertInBrowser } from "../helpers/convert-in-browser"; import { downloadVideo } from "../helpers/download-video"; import { getExtension } from "../helpers/find-good-supported-codec"; import { Prefix } from "../helpers/prefixes"; import { transcribeVideoOnServer } from "../helpers/transcribe-video"; import { uploadFileToServer } from "../helpers/upload-file"; import { ProcessStatus } from "./ProcessingStatus"; import { Button } from "./ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "./ui/dropdown-menu"; let currentProcessing = Promise.resolve(); export const UseThisTake: React.FC<{ readonly selectedFolder: string | null; recordingStatus: RecordingStatus; setRecordingStatus: React.Dispatch>; setStatus: React.Dispatch>; }> = ({ selectedFolder, recordingStatus, setRecordingStatus, setStatus }) => { const keepVideoOnServer = useCallback(async () => { if (recordingStatus.type !== "recording-finished") { throw new Error("Recording not finished"); } if (selectedFolder === null) { alert("Please select a folder first."); return Promise.resolve(); } let aborted = false; for (const blob of recordingStatus.blobs) { const extension = getExtension(blob.mimeType); currentProcessing = currentProcessing .then(() => { setStatus({ title: `Converting ${blob.prefix}${blob.endDate}.${extension}`, description: "Starting...", abort: null, }); return blob.data(); }) .then((src) => { return convertInBrowser({ src: src, mimeType: blob.mimeType, onProgress: (progress, abortFn) => { setStatus({ title: `Converting ${blob.prefix}${blob.endDate}.${extension}`, description: `${Math.round(progress * 100)}% progress`, abort: abortFn, }); }, }); }) .then((convertedBlob) => { setStatus({ title: `Copying to public folder ${blob.prefix}${blob.endDate}.${extension}`, description: "Copying in progress", abort: null, }); return uploadFileToServer({ blob: convertedBlob, endDate: recordingStatus.endDate, prefix: blob.prefix, selectedFolder, expectedFrames: recordingStatus.expectedFrames, mimeType: blob.mimeType, }); }) .then(() => { setStatus(null); }) .catch((err) => { // ⚠️ might come from @remotion/webcodecs or from transcribe-video.ts if ((err as Error).message.includes("aborted by user")) { aborted = true; return; } alert( "Failed to convert video. Downloaded original video for backup.", ); console.log(err); return blob.data(); }) .then((buffer) => { if (buffer) { const blobToDownload = new Blob([buffer], { type: blob.mimeType.includes("webm") ? "video/webm" : "video/mp4", }); const a = document.createElement("a"); const url = URL.createObjectURL(blobToDownload); a.href = url; a.download = `${blob.prefix}${blob.endDate}.${extension}`; a.click(); blob.releaseData(); } }); } if (aborted) { return; } currentProcessing = currentProcessing .then(() => { setStatus({ title: `Transcribing ${WEBCAM_PREFIX}${recordingStatus.endDate}.${recordingStatus.blobs.find((b) => b.prefix === WEBCAM_PREFIX)?.mimeType.includes("webm") ? "webm" : "mp4"}`, description: "See Terminal for progress", abort: () => cancelTranscribeOnServer(), }); return transcribeVideoOnServer({ endDate: recordingStatus.endDate, selectedFolder, onProgress: (stat) => { setStatus(stat); }, }); }) .catch((err) => { // ⚠️ might come from @remotion/webcodecs or from transcribe-video.ts if ((err as Error).message.includes("aborted by user")) { return; } console.log(err); }) .then(() => { setStatus(null); }); setRecordingStatus({ type: "idle" }); }, [recordingStatus, selectedFolder, setRecordingStatus, setStatus]); const keepVideosWithoutConverting = useCallback(async () => { if (recordingStatus.type !== "recording-finished") { throw new Error("Recording not finished"); } for (const blob of recordingStatus.blobs) { const buffer = await blob.data(); const extension = getExtension(blob.mimeType); const blobToDownload = new Blob([buffer], { type: blob.mimeType.includes("webm") ? "video/webm" : "video/mp4", }); const a = document.createElement("a"); const url = URL.createObjectURL(blobToDownload); a.href = url; a.download = `${blob.prefix}${blob.endDate}-unprocessed.${extension}`; a.click(); } }, [recordingStatus]); const keepVideoOnClient = useCallback(async () => { if (recordingStatus.type !== "recording-finished") { throw new Error("Recording not finished"); } for (const blob of recordingStatus.blobs) { await downloadVideo({ data: await blob.data(), endDate: recordingStatus.endDate, prefix: blob.prefix, mimeType: blob.mimeType, setStatus, }); } }, [recordingStatus, setStatus]); const previewWebcam = useCallback( async (prefix: Prefix) => { if (recordingStatus.type !== "recording-finished") { throw new Error("Recording not finished"); } const webcamBlob = recordingStatus.blobs.find((b) => b.prefix === prefix); if (!webcamBlob) { throw new Error("No webcam blob"); } // turn into blob url and open in new tab const blob = new Blob([await webcamBlob.data()], { type: webcamBlob.mimeType, }); const url = URL.createObjectURL(blob); window.open(url, "_blank"); }, [recordingStatus], ); return ( <>
{window.remotionServerEnabled ? ( ) : ( )} {window.remotionServerEnabled ? ( Download as file ) : null} Download without conversion {recordingStatus.type === "recording-finished" && recordingStatus.blobs.map((blob) => ( { previewWebcam(blob.prefix as Prefix); }} > Preview {blob.prefix} ))}
); };