Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

fix code quality, and also isue with audio not being the right spot

Alan Trebugeais1dac159c2b24
diff --git a/src/components/video-editor/timeline/Item.tsx b/src/components/video-editor/timeline/Item.tsx
index 33703a82..e633e52e 100644
--- a/src/components/video-editor/timeline/Item.tsx
+++ b/src/components/video-editor/timeline/Item.tsx
@@ -19,6 +19,7 @@ interface ItemProps {
id: string;
span: Span;
rowId: string;
+ disabled?: boolean;
children: React.ReactNode;
isSelected?: boolean;
onSelect?: () => void;
@@ -55,6 +56,7 @@ export default function Item({
id,
span,
rowId,
+ disabled = false,
isSelected = false,
onSelect,
onSelectId,
@@ -69,6 +71,7 @@ export default function Item({
const { setNodeRef, attributes, listeners, itemStyle, itemContentStyle } = useItem({
id,
span,
+ disabled,
data: { rowId },
});
diff --git a/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx b/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx
index c7d65898..d0dae8c2 100644
--- a/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx
+++ b/src/components/video-editor/timeline/components/viewport/TimelineCanvas.tsx
@@ -20,7 +20,7 @@ import {
import glassStyles from "../../ItemGlass.module.css";
import Item from "../../Item";
import Row from "../../Row";
-import { CLIP_ROW_ID, ZOOM_ROW_ID } from "../../core/constants";
+import { CLIP_ROW_ID, SOURCE_AUDIO_ROW_ID, ZOOM_ROW_ID } from "../../core/constants";
import type { AudioPeaksData, TimelineRenderItem } from "../../core/timelineTypes";
import {
getAnnotationTrackIndex,
@@ -38,7 +38,6 @@ import { useTimelineAudioPeaks } from "../../hooks/useTimelineAudioPeaks";
const HINT_CLIP = "Press C to split clip";
const HINT_ANNOTATION = "Press A to add annotation";
const HINT_AUDIO = "Click music icon to add audio";
-const SOURCE_AUDIO_ROW_ID = "row-source-audio";
interface TimelineCanvasProps {
items: TimelineRenderItem[];
@@ -251,6 +250,10 @@ function AudioItemWithWaveform({
onSelectAudio,
}: AudioItemWithWaveformProps) {
const peaks = useTimelineAudioPeaks(item.audioPath ?? null);
+ const normalizedWaveformSpan = useMemo(() => {
+ const duration = Math.max(0, waveformSpan.end - waveformSpan.start);
+ return { start: 0, end: duration };
+ }, [waveformSpan.end, waveformSpan.start]);
return (
<Item
id={item.id}
@@ -260,7 +263,7 @@ function AudioItemWithWaveform({
onSelectId={onSelectAudio}
variant="audio"
waveformPeaks={peaks}
- waveformSegmentSpan={waveformSpan}
+ waveformSegmentSpan={normalizedWaveformSpan}
>
{item.label}
</Item>
@@ -370,6 +373,7 @@ const TimelineCanvasRows = memo(function TimelineCanvasRows({
id={`source-audio-${item.id}`}
rowId={SOURCE_AUDIO_ROW_ID}
span={liveSpanPreviewById?.[item.id] ?? item.span}
+ disabled
isSelected={selectAllBlocksActive || item.id === selectedClipId}
onSelect={() => onSelectClip?.(item.id)}
variant="audio"
diff --git a/src/components/video-editor/timeline/components/waveform/AudioWaveform.tsx b/src/components/video-editor/timeline/components/waveform/AudioWaveform.tsx
index eff26d09..4b8bf510 100644
--- a/src/components/video-editor/timeline/components/waveform/AudioWaveform.tsx
+++ b/src/components/video-editor/timeline/components/waveform/AudioWaveform.tsx
@@ -23,6 +23,7 @@ function AudioWaveformComponent({
const canvasRef = useRef<HTMLCanvasElement>(null);
const { range } = useTimelineContext();
const [resizeKey, setResizeKey] = useState(0);
+ const lastDrawAtRef = useRef(0);
// Bump resizeKey when the canvas element changes size.
const observerRef = useRef<ResizeObserver | null>(null);
@@ -42,57 +43,66 @@ function AudioWaveformComponent({
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
-
- const ctx = canvas.getContext("2d");
- if (!ctx) return;
-
- const rect = canvas.getBoundingClientRect();
- const dpr = window.devicePixelRatio || 1;
- const width = Math.round(rect.width * dpr);
- const height = Math.round(rect.height * dpr);
-
- if (width === 0 || height === 0) return;
-
- canvas.width = width;
- canvas.height = height;
-
- ctx.clearRect(0, 0, width, height);
-
- const { peaks: peakData, durationMs } = peaks;
- if (durationMs <= 0 || peakData.length === 0) return;
-
- const rawVisibleStartMs = segmentStartMs ?? range.start;
- const rawVisibleEndMs = segmentEndMs ?? range.end;
- const msPerBin = durationMs / peakData.length;
- const visibleStartMs =
- msPerBin > 0 ? Math.round(rawVisibleStartMs / msPerBin) * msPerBin : rawVisibleStartMs;
- const visibleEndMs =
- msPerBin > 0 ? Math.round(rawVisibleEndMs / msPerBin) * msPerBin : rawVisibleEndMs;
- const visibleDurationMs = visibleEndMs - visibleStartMs;
- if (visibleDurationMs <= 0) return;
-
- const midY = height / 2;
-
- ctx.beginPath();
- for (let px = 0; px < width; px++) {
- const t = visibleStartMs + (px / width) * visibleDurationMs;
- const exactIndex = Math.max(
- 0,
- Math.min(peakData.length - 1, (t / durationMs) * (peakData.length - 1)),
- );
- const leftIndex = Math.floor(exactIndex);
- const rightIndex = Math.min(peakData.length - 1, leftIndex + 1);
- const mix = exactIndex - leftIndex;
- const amplitude = peakData[leftIndex] * (1 - mix) + peakData[rightIndex] * mix;
- const barHeight = amplitude * midY * 0.85;
-
- ctx.moveTo(px, midY - barHeight);
- ctx.lineTo(px, midY + barHeight);
- }
-
- ctx.strokeStyle = "rgba(255, 255, 255, 0.55)";
- ctx.lineWidth = dpr;
- ctx.stroke();
+ let rafId = 0;
+
+ const draw = () => {
+ const now = performance.now();
+ if (now - lastDrawAtRef.current < 33) return;
+ lastDrawAtRef.current = now;
+
+ const ctx = canvas.getContext("2d");
+ if (!ctx) return;
+
+ const rect = canvas.getBoundingClientRect();
+ const dpr = window.devicePixelRatio || 1;
+ const width = Math.round(rect.width * dpr);
+ const height = Math.round(rect.height * dpr);
+
+ if (width === 0 || height === 0) return;
+
+ canvas.width = width;
+ canvas.height = height;
+
+ ctx.clearRect(0, 0, width, height);
+
+ const { peaks: peakData, durationMs } = peaks;
+ if (durationMs <= 0 || peakData.length === 0) return;
+
+ const rawVisibleStartMs = segmentStartMs ?? range.start;
+ const rawVisibleEndMs = segmentEndMs ?? range.end;
+ const msPerBin = durationMs / peakData.length;
+ const visibleStartMs =
+ msPerBin > 0 ? Math.round(rawVisibleStartMs / msPerBin) * msPerBin : rawVisibleStartMs;
+ const visibleEndMs =
+ msPerBin > 0 ? Math.round(rawVisibleEndMs / msPerBin) * msPerBin : rawVisibleEndMs;
+ const visibleDurationMs = visibleEndMs - visibleStartMs;
+ if (visibleDurationMs <= 0) return;
+
+ const midY = height / 2;
+
+ ctx.beginPath();
+ for (let px = 0; px < width; px++) {
+ const t = visibleStartMs + (px / width) * visibleDurationMs;
+ const exactIndex = Math.max(
+ 0,
+ Math.min(peakData.length - 1, (t / durationMs) * (peakData.length - 1)),
+ );
+ const leftIndex = Math.floor(exactIndex);
+ const rightIndex = Math.min(peakData.length - 1, leftIndex + 1);
+ const mix = exactIndex - leftIndex;
+ const amplitude = peakData[leftIndex] * (1 - mix) + peakData[rightIndex] * mix;
+ const barHeight = amplitude * midY * 0.85;
+
+ ctx.moveTo(px, midY - barHeight);
+ ctx.lineTo(px, midY + barHeight);
+ }
+
+ ctx.strokeStyle = "rgba(255, 255, 255, 0.55)";
+ ctx.lineWidth = dpr;
+ ctx.stroke();
+ };
+ rafId = requestAnimationFrame(draw);
+ return () => cancelAnimationFrame(rafId);
}, [peaks, range.start, range.end, resizeKey, segmentStartMs, segmentEndMs]);
return (
diff --git a/src/components/video-editor/timeline/components/waveform/WaveformGenerator.ts b/src/components/video-editor/timeline/components/waveform/WaveformGenerator.ts
index 07d00419..128a65ad 100644
--- a/src/components/video-editor/timeline/components/waveform/WaveformGenerator.ts
+++ b/src/components/video-editor/timeline/components/waveform/WaveformGenerator.ts
@@ -1,7 +1,6 @@
import WorkerConstructor from "./waveform.worker?worker";
import type { AudioPeaksData } from "../../core/timelineTypes";
-
-const DEFAULT_PEAK_COUNT = 2048;
+import { WAVEFORM_DEFAULT_PEAK_COUNT } from "../../core/constants";
export class WaveformGenerator {
private audioContext: AudioContext;
@@ -50,7 +49,7 @@ export class WaveformGenerator {
});
}
- public async generate(url: string, peakCount = DEFAULT_PEAK_COUNT): Promise<AudioPeaksData> {
+ public async generate(url: string, peakCount = WAVEFORM_DEFAULT_PEAK_COUNT): Promise<AudioPeaksData> {
const cacheKey = `${url}::${peakCount}`;
const cached = this.peaksCache.get(cacheKey);
if (cached) return cached;
diff --git a/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx b/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx
index 917b171a..db0925c1 100644
--- a/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx
+++ b/src/components/video-editor/timeline/components/wrapper/TimelineWrapper.tsx
@@ -134,9 +134,8 @@ export default function TimelineWrapper({
(event: DragStartEvent) => {
const span = event.active.data.current.getSpanFromDragEvent?.(event);
if (span) showTooltip(span);
- onLiveSpanPreviewChange?.(event.active.id as string, span ?? null);
},
- [onLiveSpanPreviewChange, showTooltip],
+ [showTooltip],
);
const onDragMove = useCallback(
@@ -147,7 +146,10 @@ export default function TimelineWrapper({
? (event.activatorEvent as PointerEvent).clientX + (event.delta?.x ?? 0)
: undefined;
if (span) showTooltip(span, screenX);
- onLiveSpanPreviewChange?.(event.active.id as string, span ?? null);
+ const moved = Math.abs(event.delta?.x ?? 0) > 0.01;
+ if (moved) {
+ onLiveSpanPreviewChange?.(event.active.id as string, span ?? null);
+ }
},
[onLiveSpanPreviewChange, showTooltip],
);
diff --git a/src/components/video-editor/timeline/core/constants.ts b/src/components/video-editor/timeline/core/constants.ts
index 1b71e996..88acba23 100644
--- a/src/components/video-editor/timeline/core/constants.ts
+++ b/src/components/video-editor/timeline/core/constants.ts
@@ -2,8 +2,10 @@ export const ZOOM_ROW_ID = "row-zoom";
export const CLIP_ROW_ID = "row-clip";
export const ANNOTATION_ROW_ID = "row-annotation";
export const AUDIO_ROW_ID = "row-audio";
+export const SOURCE_AUDIO_ROW_ID = "row-source-audio";
export const ANNOTATION_ROW_PREFIX = `${ANNOTATION_ROW_ID}-`;
export const AUDIO_ROW_PREFIX = `${AUDIO_ROW_ID}-`;
export const FALLBACK_RANGE_MS = 1000;
export const TARGET_MARKER_COUNT = 12;
+export const WAVEFORM_DEFAULT_PEAK_COUNT = 2048;
diff --git a/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts b/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts
index 4ca2a8f4..499bab2b 100644
--- a/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts
+++ b/src/components/video-editor/timeline/hooks/useTimelineAudioPeaks.ts
@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react";
import { resolveMediaResourceUrl } from "@/lib/exporter/localMediaSource";
import { fromFileUrl } from "../../projectPersistence";
import { waveformGenerator } from "../components/waveform/WaveformGenerator";
+import { WAVEFORM_DEFAULT_PEAK_COUNT } from "../core/constants";
import type { AudioPeaksData } from "../core/timelineTypes";
function buildSidecarAudioCandidates(sourcePath: string): string[] {
@@ -36,6 +37,7 @@ function extractLocalPathFromMediaServerUrl(input: string): string | null {
interface TimelineAudioPeaksOptions {
enableSourceSidecarFallback?: boolean;
+ peakCount?: number;
}
export function useTimelineAudioPeaks(
@@ -45,6 +47,7 @@ export function useTimelineAudioPeaks(
const [data, setData] = useState<AudioPeaksData | null>(null);
const sourceRef = useRef(mediaResource);
const enableSourceSidecarFallback = options.enableSourceSidecarFallback ?? false;
+ const peakCount = options.peakCount ?? WAVEFORM_DEFAULT_PEAK_COUNT;
useEffect(() => {
sourceRef.current = mediaResource;
@@ -54,14 +57,9 @@ export function useTimelineAudioPeaks(
let cancelled = false;
const run = async () => {
- const localPathFromServer = extractLocalPathFromMediaServerUrl(mediaResource);
- const localSourcePath =
- localPathFromServer ||
- (/^file:\/\//i.test(mediaResource) ? fromFileUrl(mediaResource) : mediaResource);
-
const tryGenerate = async (resource: string): Promise<AudioPeaksData> => {
const resolvedUrl = await resolveMediaResourceUrl(resource);
- return waveformGenerator.generate(resolvedUrl);
+ return waveformGenerator.generate(resolvedUrl, peakCount);
};
try {
@@ -72,7 +70,13 @@ export function useTimelineAudioPeaks(
// fallthrough
}
- if (!enableSourceSidecarFallback || !localSourcePath) return;
+ if (!enableSourceSidecarFallback) return;
+
+ const localPathFromServer = extractLocalPathFromMediaServerUrl(mediaResource);
+ const localSourcePath =
+ localPathFromServer ||
+ (/^file:\/\//i.test(mediaResource) ? fromFileUrl(mediaResource) : mediaResource);
+ if (!localSourcePath) return;
for (const candidate of buildSidecarAudioCandidates(localSourcePath)) {
try {
@@ -90,7 +94,7 @@ export function useTimelineAudioPeaks(
return () => {
cancelled = true;
};
- }, [mediaResource, enableSourceSidecarFallback]);
+ }, [mediaResource, enableSourceSidecarFallback, peakCount]);
return data;
}