Skip to content

Open source CapCut alternative.

TypeScript

fix(site): micro gaps close on write and the in-tab mix ramps track-0 fades

David Le1e29670c5875
diff --git a/site/src/cut/lib/audioMix.test.ts b/site/src/cut/lib/audioMix.test.ts
index e2cb9d1d..c68a9636 100644
--- a/site/src/cut/lib/audioMix.test.ts
+++ b/site/src/cut/lib/audioMix.test.ts
@@ -58,6 +58,19 @@ describe("foldClips", () => {
expect(geo[1].at).toBe(1);
});
+ test("a clip's own fade animation ramps its sound, the tail in the room the head left", () => {
+ const geo = foldClips([clip({ out: 2, fadeIn: 0.5, fadeOut: 1.8 }), clip({ fadeIn: 0.3 })]);
+ expect(geo[0].fadeIn).toBeCloseTo(0.5, 5);
+ expect(geo[0].fadeOut).toBeCloseTo(1.5, 5);
+ expect(geo[1].fadeIn).toBeCloseTo(0.3, 5);
+ expect(geo[1].fadeOut).toBe(0);
+ });
+
+ test("a transition's tail fade takes over a shorter own fade at the same edge", () => {
+ const geo = foldClips([clip({ fadeOut: 0.2, transition: 1 }), clip()]);
+ expect(geo[0].fadeOut).toBeCloseTo(1, 5);
+ });
+
test("counts a speed change against the footprint, not the source span", () => {
expect(foldClips([clip({ out: 4, speed: 2 })])[0].dur).toBeCloseTo(2, 5);
});
diff --git a/site/src/cut/lib/audioMix.ts b/site/src/cut/lib/audioMix.ts
index 9dd9ec67..960a899f 100644
--- a/site/src/cut/lib/audioMix.ts
+++ b/site/src/cut/lib/audioMix.ts
@@ -44,6 +44,11 @@ export interface MixClip {
volume?: number;
/** The clip's own treatment, run on its sound before the fades. */
sound?: ClipSound;
+ /** Head and tail ramps of the clip's own fade animation, timeline seconds:
+ * the sound goes down to silence with the picture, the gain the preview
+ * plays. An edge a transition owns carries none. */
+ fadeIn?: number;
+ fadeOut?: number;
/** Cross-dissolve overlap into the next clip, timeline seconds. */
transition?: number;
/** Half the cross dissolve into the next clip, timeline seconds: the two
@@ -146,10 +151,16 @@ export function foldClips(clips: MixClip[]) {
geo.forEach((g, j) => {
g.at = acc;
acc += g.dur;
+ // The clip's own fade animation ramps its sound; the tail's room is what
+ // the head left, the way the engine's stanza splits them.
+ const own = Math.min(Math.max(0, g.clip.fadeIn ?? 0), g.dur);
+ if (own > 0.01) g.fadeIn = own;
+ const ownOut = Math.min(Math.max(0, g.clip.fadeOut ?? 0), Math.max(0, g.dur - g.fadeIn));
+ if (ownOut > 0.01) g.fadeOut = ownOut;
const next = geo[j + 1];
if (!next) return;
const fade = Math.min(g.clip.transition ?? 0, g.dur * 0.9);
- if (fade > 0.01) g.fadeOut = fade;
+ if (fade > 0.01) g.fadeOut = Math.max(g.fadeOut, fade);
const cross = Math.min(g.clip.soundCross ?? 0, g.dur * 0.9, next.dur * 0.9);
if (cross > 0.01) {
g.crossOut = cross;
diff --git a/site/src/cut/lib/exportRender.ts b/site/src/cut/lib/exportRender.ts
index 6e09750a..90716a44 100644
--- a/site/src/cut/lib/exportRender.ts
+++ b/site/src/cut/lib/exportRender.ts
@@ -44,7 +44,7 @@ import { hasSubjectOverlays, SubjectMaskCompositor } from "./behindPass";
import { createRasterCanvas, type RasterSurface } from "./raster";
import { renderElementPng } from "./textRender";
import { assetIsSilent, behindSubjectOverlay, clipCovers, frameOf, frontSubjectOverlay, isEffectOverlay, isTextOverlay, laneOf, overlayAnimStyle, projectBackground, projectFadeSeconds, rectOf, removalActive } from "./types";
-import type { ClipSpan, EffectOverlay, MediaAsset, Overlay, StickerOverlay } from "./types";
+import type { ClipAnim, ClipSpan, EffectOverlay, MediaAsset, Overlay, StickerOverlay } from "./types";
import type { ExportDoc, ExportSettings } from "./exportClient";
import { videoBitrateFor } from "./exportDelivery";
@@ -615,6 +615,23 @@ function overlayRamps(
return ramps;
}
+/**
+ * The audio ramps a track-0 clip's own entrance and exit carry — the gain
+ * the preview's frame plan applies: a fade takes the sound down with the
+ * picture, a zoom, a pop and a slide leave it, and an edge a transition owns
+ * stands down. The tail gets the room the head left.
+ */
+function trackZeroFades(spans: ClipSpan[], i: number): { fadeIn: number; fadeOut: number } {
+ const sp = spans[i];
+ const ramps = (anim: ClipAnim | undefined, room: number) =>
+ anim && !["zoom", "pop", "slideleft", "slideright", "slideup", "slidedown"].includes(anim.style)
+ ? Math.min(anim.seconds, Math.max(0, room))
+ : 0;
+ const fadeIn = (spans[i - 1]?.transitionOut ?? 0) > 0 ? 0 : ramps(sp.clip.animIn, sp.len);
+ const fadeOut = sp.transitionOut > 0 ? 0 : ramps(sp.clip.animOut, sp.len - fadeIn);
+ return { fadeIn, fadeOut };
+}
+
/**
* The mix spec for a doc: track-0 clip audio in sequence, plus everything
* placed at an absolute time — the soundtrack and every upper-track clip's own
@@ -677,7 +694,7 @@ export function mixSpecFor(doc: ExportDoc, resolve: (asset: MediaAsset) => strin
const clips: MixClip[] =
spans.length === 0
? [spacer(duration)]
- : spanSequence(spans).flatMap(({ gapBefore, span: sp }) => [
+ : spanSequence(spans).flatMap(({ gapBefore, span: sp }, i) => [
...(gapBefore > 0 ? [spacer(gapBefore)] : []),
{
file: resolve(sp.asset),
@@ -689,6 +706,7 @@ export function mixSpecFor(doc: ExportDoc, resolve: (asset: MediaAsset) => strin
reverse: sp.clip.reverse,
volume: sp.clip.volume,
sound: sp.clip.sound,
+ ...trackZeroFades(spans, i),
transition: sp.transitionOut,
soundCross: sp.soundOut,
soundBack: sp.soundBack,
diff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.ts
index a786a955..8d09a3e5 100644
--- a/site/src/cut/lib/store.test.ts
+++ b/site/src/cut/lib/store.test.ts
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, test } from "bun:test";
import { groupRemap } from "@donkeycut/effects-kit";
-import { adoptTransitionFields, assetIdsInUse, clipLen, cutTranscribeSpec, deriveTransitionFields, docOverlays, getClipSpans, liftClipLooks, moveOverlayGroup, overlayLaneOrder, normalizeElementLanes, parkedTransitions, placeInRun, projectDuration, rippleInsert, separateOverlaps, serializeDoc, startTrimRipple, useEditor } from "./store";
+import { adoptTransitionFields, assetIdsInUse, clipLen, closeMicroGaps, cutTranscribeSpec, deriveTransitionFields, docOverlays, getClipSpans, liftClipLooks, moveOverlayGroup, overlayLaneOrder, normalizeElementLanes, parkedTransitions, placeInRun, projectDuration, rippleInsert, separateOverlaps, serializeDoc, startTrimRipple, useEditor } from "./store";
import { playheadAt, setPlayhead, setSkim } from "./playhead";
import { emptySubtitles, uploadedFontId } from "./types";
import type { AudioClip, MediaAsset, SubtitleCue, TextOverlay, VideoClip } from "./types";
@@ -187,6 +187,48 @@ describe("the document projection", () => {
});
});
+describe("micro gaps", () => {
+ beforeEach(() => {
+ useEditor.setState({ clips: [], audioClips: [], overlays: [], transitions: [], loaded: true });
+ });
+
+ test("a clip dropped a hair short of its neighbour lands on it, on every write", () => {
+ useEditor.setState({
+ clips: [
+ vclip({ id: "a", start: 0, out: 4 }),
+ vclip({ id: "b", start: 4.03, out: 3 }),
+ vclip({ id: "c", start: 7.06, out: 2 }),
+ vclip({ id: "p", track: 1, start: 0.02, out: 1 }),
+ ],
+ });
+ const by = (id: string) => useEditor.getState().clips.find((c) => c.id === id)!;
+ expect(by("b").start).toBe(4);
+ expect(by("c").start).toBe(7);
+ expect(by("p").start).toBe(0);
+ });
+
+ test("a shrink that pulls the run left carries the clips that abutted it", () => {
+ useEditor.setState({
+ clips: [
+ vclip({ id: "a", start: 0, out: 4 }),
+ vclip({ id: "b", start: 4, out: 3 }),
+ vclip({ id: "c", start: 7, out: 2 }),
+ vclip({ id: "p", track: 1, start: 1, out: 1 }),
+ ],
+ });
+ useEditor.getState().setClipTrim("a", 0, 3.97);
+ const by = (id: string) => useEditor.getState().clips.find((c) => c.id === id)!;
+ expect(by("a").out).toBeCloseTo(3.97, 5);
+ expect(by("b").start).toBeCloseTo(3.97, 5);
+ expect(by("c").start).toBeCloseTo(6.97, 5);
+ });
+
+ test("a real gap stays, and so does a clip already on the edge", () => {
+ const clips = [vclip({ id: "a", start: 0, out: 4 }), vclip({ id: "b", start: 4, out: 3 }), vclip({ id: "c", start: 7.2, out: 2 })];
+ expect(closeMicroGaps(clips)).toBe(clips);
+ });
+});
+
describe("video placement", () => {
test("adding onto an occupied upper track inserts at the pointer and ripples the resident", () => {
const a = asset(2);
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 6b231fb3..43fc1e32 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -128,6 +128,49 @@ export const overlayLayers = (clips: VideoClip[]) => clips.filter((c) => c.track
* dragging away the last track-0 clip re-grounds the rows above it, so the
* spine — the sequence that carries transitions, fades, and ripple — always
* exists while any clip does. */
+/**
+ * Two clips on one track closer than this abut. The sound fold has always
+ * closed a gap this small, and a picture drawn at a fixed frame rate lands a
+ * frame of bare background in one, so the document never keeps it.
+ */
+export const MICRO_GAP_S = 0.05;
+
+/**
+ * Every clip within `MICRO_GAP_S` of the end of the clip before it on its
+ * track — or of the head of the timeline — moved onto that edge. A drop that
+ * lands a hair short of a neighbour, and a speed change that leaves the run
+ * behind it a hair adrift, both close here, so no renderer meets a gap too
+ * small to mean anything. The same array comes back when nothing moves.
+ */
+export function closeMicroGaps(clips: VideoClip[]): VideoClip[] {
+ const byTrack = new Map<number, VideoClip[]>();
+ for (const c of clips) {
+ const run = byTrack.get(c.track);
+ if (run) run.push(c);
+ else byTrack.set(c.track, [c]);
+ }
+ let out: VideoClip[] | null = null;
+ for (const run of byTrack.values()) {
+ // A gap is judged where the run ended before anything moved, so a row of
+ // clips each dropped a hair short closes onto its own neighbour as the
+ // run pulls left. A clip that abutted its neighbour follows it too: a
+ // shrink that pulled the run left would otherwise open a gap behind it.
+ let end = 0;
+ let judged = 0;
+ for (const c of [...run].sort((a, b) => a.start - b.start)) {
+ const gap = c.start - judged;
+ const start = gap > -1e-6 && gap <= MICRO_GAP_S && Math.abs(end - c.start) > 1e-6 ? end : c.start;
+ if (start !== c.start) {
+ out ??= [...clips];
+ out[clips.indexOf(c)] = { ...c, start };
+ }
+ judged = Math.max(judged, c.start + clipLen(c));
+ end = Math.max(end, start + clipLen(c));
+ }
+ }
+ return out ?? clips;
+}
+
export function groundTracks(clips: VideoClip[]): VideoClip[] {
const lift = clips.length ? Math.min(...clips.map((c) => c.track)) : 0;
return lift > 0 ? clips.map((c) => ({ ...c, track: c.track - lift })) : clips;
@@ -1281,7 +1324,7 @@ function normalizeWrite(prev: EditorState, incoming: Partial<EditorState>): Part
next = { ...next };
for (const k of DOC_KEYS) delete (next as Record<string, unknown>)[k];
}
- if (next.clips) next = { ...next, clips: groundTracks(next.clips) };
+ if (next.clips) next = { ...next, clips: closeMicroGaps(groundTracks(next.clips)) };
if (next.clips || next.transitions) {
const clips = next.clips ?? prev.clips;
const derived = deriveTransitionFields(clips, next.transitions ?? prev.transitions);
@@ -5757,7 +5800,7 @@ export function laneGapAt(
): { start: number; len: number } | null {
let prevEnd = 0;
for (const s of laneSpans(doc, lane)) {
- if (s.start - prevEnd > 0.05 && t >= prevEnd && t < s.start) {
+ if (s.start - prevEnd > MICRO_GAP_S && t >= prevEnd && t < s.start) {
return { start: prevEnd, len: s.start - prevEnd };
}
prevEnd = Math.max(prevEnd, s.end);
@@ -6060,7 +6103,7 @@ export function spanSequence(spans: ClipSpan[]): { gapBefore: number; span: Clip
let cursor = 0;
for (const sp of spans) {
const gap = sp.start - cursor;
- out.push({ gapBefore: gap > 0.05 ? gap : 0, span: sp });
+ out.push({ gapBefore: gap > MICRO_GAP_S ? gap : 0, span: sp });
cursor = sp.start + sp.len;
}
return out;