Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

feat: enhance audio detection in video processing and update layers types for video support

CristianOlivera165cab77826ed
diff --git a/lib/videos-library.ts b/lib/videos-library.ts
index efde2ce..94f9bbd 100644
--- a/lib/videos-library.ts
+++ b/lib/videos-library.ts
@@ -142,34 +142,47 @@ async function getVideoMetadata(file: File): Promise<{
async function detectVideoHasAudio(blob: Blob): Promise<boolean> {
return new Promise((resolve) => {
+ const url = URL.createObjectURL(blob);
const video = document.createElement("video");
- video.preload = "metadata";
-
- video.onloadedmetadata = () => {
- let hasAudio = false;
-
- const extVideo = video as ExtendedVideoElement;
-
- if (extVideo.audioTracks !== undefined && extVideo.audioTracks.length > 0) {
- hasAudio = true;
- }
- else if (extVideo.mozHasAudio !== undefined) {
- hasAudio = extVideo.mozHasAudio;
- }
- else {
- hasAudio = true;
+ video.muted = true;
+ video.preload = "auto";
+ let settled = false;
+
+ const cleanup = (result: boolean) => {
+ if (settled) return;
+ settled = true;
+ clearTimeout(timeoutId);
+ URL.revokeObjectURL(url);
+ video.src = "";
+ resolve(result);
+ };
+
+ const timeoutId = setTimeout(() => cleanup(false), 8000);
+
+ const checkAudio = (afterData: boolean) => {
+ const v = video as ExtendedVideoElement & { webkitAudioDecodedByteCount?: number };
+
+ if (afterData && typeof v.webkitAudioDecodedByteCount === "number") {
+ return cleanup(v.webkitAudioDecodedByteCount > 0);
}
- URL.revokeObjectURL(video.src);
- resolve(hasAudio);
- };
+ if (v.audioTracks !== undefined && v.audioTracks.length > 0) {
+ return cleanup(true);
+ }
- video.onerror = () => {
- URL.revokeObjectURL(video.src);
- resolve(true);
+ if (v.mozHasAudio !== undefined) {
+ return cleanup(Boolean(v.mozHasAudio));
+ }
+
+ if (!afterData) return;
+
+ cleanup(false);
};
- video.src = URL.createObjectURL(blob);
+ video.addEventListener("loadedmetadata", () => checkAudio(false));
+ video.addEventListener("loadeddata", () => checkAudio(true));
+ video.addEventListener("error", () => cleanup(false));
+ video.src = url;
});
}
@@ -226,7 +239,7 @@ export async function addVideoToLibrary(file: File): Promise<LibraryVideo> {
console.warn("Failed to generate thumbnail:", e);
}
- let hasAudio = true;
+ let hasAudio = false;
try {
hasAudio = await detectVideoHasAudio(file);
} catch (e) {
@@ -244,7 +257,7 @@ export async function addVideoToLibrary(file: File): Promise<LibraryVideo> {
aspectRatio: metadata.aspectRatio,
uploadedAt: Date.now(),
thumbnailUrl,
- hasAudio,
+ hasAudio: false,
originalHasAudio: hasAudio,
};
@@ -283,7 +296,7 @@ export async function addVideoToLibraryWithMetadata(options: AddVideoWithMetadat
hasAudio = await detectVideoHasAudio(options.blob);
} catch (e) {
console.warn("Failed to detect audio:", e);
- hasAudio = true;
+ hasAudio = false;
}
}
@@ -298,7 +311,7 @@ export async function addVideoToLibraryWithMetadata(options: AddVideoWithMetadat
aspectRatio: "auto",
uploadedAt: Date.now(),
thumbnailUrl,
- hasAudio,
+ hasAudio: false,
originalHasAudio: hasAudio,
};
diff --git a/types/audio.types.ts b/types/audio.types.ts
index cc51f4e..8adb5eb 100644
--- a/types/audio.types.ts
+++ b/types/audio.types.ts
@@ -57,14 +57,10 @@ export const MAX_AUDIO_TRACKS = 5;
export interface AudioMenuProps {
audioTracks: AudioTrack[];
uploadedAudios: UploadedAudio[];
- muteOriginalAudio: boolean;
- masterVolume: number;
videoDuration: number;
onAudioUpload: (file: File) => void;
onUpdateAudioTrack: (trackId: string, updates: Partial<AudioTrack>) => void;
onDeleteAudioTrack: (trackId: string) => void;
- onToggleMuteOriginalAudio: () => void;
- onMasterVolumeChange: (volume: number) => void;
}
export const MIN_FRAGMENT_DURATION = 0.1;
diff --git a/types/editor.types.ts b/types/editor.types.ts
index 3c46870..019590e 100644
--- a/types/editor.types.ts
+++ b/types/editor.types.ts
@@ -75,7 +75,6 @@ export interface VideoCanvasProps {
imageMaskConfig?: import("@/types/photo.types").ImageMaskConfig;
videoMaskConfig?: import("@/types/photo.types").ImageMaskConfig;
onVideoMaskConfigChange?: (config: import("@/types/photo.types").ImageMaskConfig) => void;
- // Video-specific props
videoRef: React.RefObject<HTMLVideoElement | null>;
videoUrl: string | null;
padding: number;
@@ -98,83 +97,77 @@ export interface VideoCanvasProps {
getThumbnailForTime?: (time: number) => VideoThumbnail | null;
zoomFragments?: ZoomFragment[];
currentTime?: number;
- // Mockup props
mockupId?: string;
mockupConfig?: import("./mockup.types").MockupConfig;
- // Video upload props
onVideoUpload?: (file: File) => void;
onImageUpload?: (file: File) => void;
onImageDrop?: (files: FileList | File[]) => void;
isUploading?: boolean;
- // Transform props
videoTransform?: VideoTransform;
onVideoTransformChange?: (transform: VideoTransform) => void;
- // Canvas elements props
canvasElements?: CanvasElement[];
selectedElementId?: string | null;
onElementUpdate?: (id: string, updates: Partial<CanvasElement>) => void;
onElementSelect?: (id: string | null) => void;
onElementDelete?: (id: string | string[]) => void;
- // Cursor overlay props
cursorConfig?: CursorConfig;
cursorData?: CursorRecordingData;
- // Camera overlay props
cameraUrl?: string | null;
cameraConfig?: import("./camera.types").CameraConfig | null;
onCameraConfigChange?: (partial: Partial<import("./camera.types").CameraConfig>) => void;
onCameraClick?: () => void;
- // Layers panel customization
layersPanelToolbar?: React.ReactNode;
- // Text placement tool (Figma-style T key)
textToolActive?: boolean;
onTextToolDeactivate?: () => void;
onAddElement?: (element: CanvasElement) => void;
}
export async function detectVideoHasAudio(blob: Blob): Promise<boolean> {
- try {
+ return new Promise<boolean>((resolve) => {
const url = URL.createObjectURL(blob);
-
- return new Promise<boolean>((resolve) => {
- const audioCtx = new AudioContext();
- const reader = new FileReader();
-
- reader.onload = async (e) => {
- try {
- const arrayBuffer = e.target?.result as ArrayBuffer;
- const audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
-
- let hasSignal = false;
- for (let ch = 0; ch < audioBuffer.numberOfChannels; ch++) {
- const data = audioBuffer.getChannelData(ch);
- for (let i = 0; i < Math.min(data.length, 10000); i++) {
- if (Math.abs(data[i]) > 0.001) {
- hasSignal = true;
- break;
- }
- }
- if (hasSignal) break;
- }
-
- await audioCtx.close();
- URL.revokeObjectURL(url);
- resolve(hasSignal);
- } catch {
- await audioCtx.close();
- URL.revokeObjectURL(url);
- resolve(false);
- }
+ const video = document.createElement("video");
+ video.muted = true;
+ video.preload = "auto";
+ let settled = false;
+
+ const cleanup = (result: boolean) => {
+ if (settled) return;
+ settled = true;
+ clearTimeout(timeoutId);
+ URL.revokeObjectURL(url);
+ video.src = "";
+ resolve(result);
+ };
+
+ const timeoutId = setTimeout(() => cleanup(false), 8000);
+
+ const checkAudio = (afterData: boolean) => {
+ const v = video as HTMLVideoElement & {
+ audioTracks?: { length: number };
+ mozHasAudio?: boolean;
+ webkitAudioDecodedByteCount?: number;
};
- reader.onerror = () => {
- audioCtx.close();
- URL.revokeObjectURL(url);
- resolve(false);
- };
+ if (afterData && typeof v.webkitAudioDecodedByteCount === "number") {
+ return cleanup(v.webkitAudioDecodedByteCount > 0);
+ }
+
+ if (v.audioTracks !== undefined && v.audioTracks.length > 0) {
+ return cleanup(true);
+ }
+
+ if (v.mozHasAudio !== undefined) {
+ return cleanup(Boolean(v.mozHasAudio));
+ }
+
+ if (!afterData) return;
+
+ cleanup(false);
+ };
- reader.readAsArrayBuffer(blob);
- });
- } catch {
- return true;
- }
+ video.addEventListener("loadedmetadata", () => checkAudio(false));
+ video.addEventListener("loadeddata", () => checkAudio(true));
+ video.addEventListener("error", () => cleanup(false));
+ video.src = url;
+ });
}
diff --git a/types/layers.types.ts b/types/layers.types.ts
index b182ab7..a1a2b00 100644
--- a/types/layers.types.ts
+++ b/types/layers.types.ts
@@ -1,9 +1,10 @@
import { CanvasElement } from "./canvas-elements.types";
-export const TYPE_ICON: Record<CanvasElement["type"], string> = {
+export const TYPE_ICON: Record<CanvasElement["type"] | "video", string> = {
image: "solar:gallery-bold",
text: "solar:text-bold",
svg: "mdi:svg",
+ video: "solar:videocamera-record-bold",
};
export interface LayersPanelProps {
@@ -22,6 +23,11 @@ export interface LayersPanelProps {
onUngroup?: (ids: string[]) => void;
onSetGroupId?: (id: string, groupId: string | undefined) => void;
toolbar?: React.ReactNode;
+ // Video/image layer support
+ videoLayerVisible?: boolean;
+ onVideoLayerSelect?: () => void;
+ isVideoLayerSelected?: boolean;
+ mediaType?: "video" | "image";
}
export interface CtxMenuState { x: number; y: number; id: string }