-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(editor): split clips without moving their source in-point #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
uma-co82
wants to merge
2
commits into
webadderallorg:main
Choose a base branch
from
uma-co82:fix/clip-split-source-in-point
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { planClipSplit } from "./clipSplit"; | ||
| import { | ||
| type ClipRegion, | ||
| clipsToTrims, | ||
| getClipSourceEndMs, | ||
| getClipSourceStartMs, | ||
| mapTimelineTimeToSourceTime, | ||
| } from "./types"; | ||
|
|
||
| function createIdFactory() { | ||
| let next = 1; | ||
| return () => `clip-${next++}`; | ||
| } | ||
|
|
||
| function splitAndDeleteMiddle(clip: ClipRegion, firstSplitMs: number, secondOffsetMs: number) { | ||
| const createId = createIdFactory(); | ||
| const first = planClipSplit({ clipRegions: [clip], splitMs: firstSplitMs, createId }); | ||
| if (!first) throw new Error("first split failed"); | ||
| const second = planClipSplit({ | ||
| clipRegions: [first.right], | ||
| splitMs: first.right.startMs + secondOffsetMs, | ||
| createId, | ||
| }); | ||
| if (!second) throw new Error("second split failed"); | ||
| return { kept: [first.left, second.right], deleted: second.left }; | ||
| } | ||
|
|
||
| describe("planClipSplit", () => { | ||
| it("returns null when no clip contains the split position", () => { | ||
| const clips: ClipRegion[] = [{ id: "clip-1", startMs: 0, endMs: 1000, speed: 1 }]; | ||
| expect( | ||
| planClipSplit({ clipRegions: clips, splitMs: 2000, createId: createIdFactory() }), | ||
| ).toBeNull(); | ||
| expect( | ||
| planClipSplit({ clipRegions: clips, splitMs: 0, createId: createIdFactory() }), | ||
| ).toBeNull(); | ||
| expect( | ||
| planClipSplit({ clipRegions: clips, splitMs: 1000, createId: createIdFactory() }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("splits a 1x clip at the playhead", () => { | ||
| const clip: ClipRegion = { id: "clip-1", startMs: 0, endMs: 120_000, speed: 1 }; | ||
| const plan = planClipSplit({ | ||
| clipRegions: [clip], | ||
| splitMs: 30_000, | ||
| createId: createIdFactory(), | ||
| }); | ||
|
|
||
| expect(plan?.left).toMatchObject({ startMs: 0, endMs: 30_000 }); | ||
| expect(plan?.right).toMatchObject({ startMs: 30_000, endMs: 120_000 }); | ||
| }); | ||
|
|
||
| it("anchors the right half to the source time the split maps to at non-1x speed", () => { | ||
| const clip: ClipRegion = { id: "clip-1", startMs: 0, endMs: 60_000, speed: 2 }; | ||
| const plan = planClipSplit({ | ||
| clipRegions: [clip], | ||
| splitMs: 10_000, | ||
| createId: createIdFactory(), | ||
| }); | ||
| if (!plan) throw new Error("split failed"); | ||
|
|
||
| // 10s of playback at 2x consumes 20s of source. | ||
| expect(getClipSourceEndMs(plan.left)).toBe(20_000); | ||
| expect(getClipSourceStartMs(plan.right)).toBe(20_000); | ||
| // The halves still cover exactly the source the original clip covered. | ||
| expect(getClipSourceEndMs(plan.right)).toBe(getClipSourceEndMs(clip)); | ||
| }); | ||
|
|
||
| it("leaves both halves where the clip already sat on the timeline", () => { | ||
| const clip: ClipRegion = { id: "clip-1", startMs: 0, endMs: 60_000, speed: 2 }; | ||
| const plan = planClipSplit({ | ||
| clipRegions: [clip], | ||
| splitMs: 10_000, | ||
| createId: createIdFactory(), | ||
| }); | ||
|
|
||
| // No visual gap: the halves abut at the playhead and still end where the clip did. | ||
| expect(plan?.left).toMatchObject({ startMs: 0, endMs: 10_000 }); | ||
| expect(plan?.right).toMatchObject({ startMs: 10_000, endMs: 60_000 }); | ||
| }); | ||
|
|
||
| it("keeps the timeline-to-source mapping continuous across the split", () => { | ||
| const clip: ClipRegion = { id: "clip-1", startMs: 0, endMs: 60_000, speed: 2 }; | ||
| const plan = planClipSplit({ | ||
| clipRegions: [clip], | ||
| splitMs: 10_000, | ||
| createId: createIdFactory(), | ||
| }); | ||
| if (!plan) throw new Error("split failed"); | ||
|
|
||
| const halves = [plan.left, plan.right]; | ||
| for (const timelineMs of [0, 5_000, 10_000, 30_000, 60_000]) { | ||
| expect(mapTimelineTimeToSourceTime(timelineMs, halves)).toBe( | ||
| mapTimelineTimeToSourceTime(timelineMs, [clip]), | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| it("keeps split halves contiguous in source time so no gap is trimmed", () => { | ||
| const clip: ClipRegion = { id: "clip-1", startMs: 0, endMs: 48_000, speed: 2.5 }; | ||
| const plan = planClipSplit({ | ||
| clipRegions: [clip], | ||
| splitMs: 17_333, | ||
| createId: createIdFactory(), | ||
| }); | ||
| if (!plan) throw new Error("split failed"); | ||
|
|
||
| expect(clipsToTrims([plan.left, plan.right], getClipSourceEndMs(clip))).toEqual([]); | ||
| }); | ||
|
|
||
| it("removes the source range the user cut out when the clip is sped up", () => { | ||
| const sourceDurationMs = 120_000; | ||
| const clip: ClipRegion = { id: "clip-1", startMs: 0, endMs: 60_000, speed: 2 }; | ||
|
|
||
| // Cut the 10s-20s playback window out of a 2x clip => source 20s-40s. | ||
| const { kept, deleted } = splitAndDeleteMiddle(clip, 10_000, 10_000); | ||
|
|
||
| expect([getClipSourceStartMs(deleted), getClipSourceEndMs(deleted)]).toEqual([ | ||
| 20_000, 40_000, | ||
| ]); | ||
| expect(clipsToTrims(kept, sourceDurationMs)).toEqual([ | ||
| { id: "trim-gap-1", startMs: 20_000, endMs: 40_000 }, | ||
| ]); | ||
| // Nothing else is lost: the tail of the recording is still covered. | ||
| expect(getClipSourceEndMs(kept[1])).toBe(sourceDurationMs); | ||
| }); | ||
|
|
||
| it("removes the source range the user cut out at 1x", () => { | ||
| const sourceDurationMs = 120_000; | ||
| const clip: ClipRegion = { id: "clip-1", startMs: 0, endMs: sourceDurationMs, speed: 1 }; | ||
|
|
||
| const { kept } = splitAndDeleteMiddle(clip, 10_000, 10_000); | ||
|
|
||
| expect(clipsToTrims(kept, sourceDurationMs)).toEqual([ | ||
| { id: "trim-gap-1", startMs: 10_000, endMs: 20_000 }, | ||
| ]); | ||
| expect(getClipSourceEndMs(kept[1])).toBe(sourceDurationMs); | ||
| }); | ||
|
|
||
| it("carries clip settings into both halves", () => { | ||
| const clip: ClipRegion = { | ||
| id: "clip-1", | ||
| startMs: 0, | ||
| endMs: 60_000, | ||
| speed: 2, | ||
| muted: true, | ||
| showSourceAudio: true, | ||
| }; | ||
| const plan = planClipSplit({ | ||
| clipRegions: [clip], | ||
| splitMs: 10_000, | ||
| createId: createIdFactory(), | ||
| }); | ||
|
|
||
| for (const half of [plan?.left, plan?.right]) { | ||
| expect(half).toMatchObject({ speed: 2, muted: true, showSourceAudio: true }); | ||
| } | ||
| expect(plan?.left.id).not.toBe(plan?.right.id); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { type ClipRegion, getClipSourceEndMs } from "./types"; | ||
|
|
||
| export interface ClipSplitPlan { | ||
| targetId: string; | ||
| left: ClipRegion; | ||
| right: ClipRegion; | ||
| } | ||
|
|
||
| /** | ||
| * Split the clip under the playhead into two clips. | ||
| * | ||
| * `splitMs` is a timeline position, so the halves stay put on the timeline and | ||
| * only their source in-points differ. At non-1x speed the split consumes more | ||
| * (or less) source than timeline, so the right half reads from | ||
| * `getClipSourceEndMs(left)` — otherwise it re-reads footage the left half | ||
| * already covers and the tail of the recording falls outside every clip. | ||
| */ | ||
| export function planClipSplit(params: { | ||
| clipRegions: ClipRegion[]; | ||
| splitMs: number; | ||
| createId: () => string; | ||
| }): ClipSplitPlan | null { | ||
| const { clipRegions, splitMs, createId } = params; | ||
| if (!Number.isFinite(splitMs)) { | ||
| return null; | ||
| } | ||
|
|
||
| const splitAtMs = Math.round(splitMs); | ||
| const target = clipRegions.find((clip) => splitAtMs > clip.startMs && splitAtMs < clip.endMs); | ||
| if (!target) { | ||
| return null; | ||
| } | ||
|
|
||
| const left: ClipRegion = { | ||
| ...target, | ||
| id: createId(), | ||
| endMs: splitAtMs, | ||
| }; | ||
| const right: ClipRegion = { | ||
| ...target, | ||
| id: createId(), | ||
| startMs: splitAtMs, | ||
| endMs: target.endMs, | ||
| sourceStartMs: getClipSourceEndMs(left), | ||
| }; | ||
|
|
||
| return { targetId: target.id, left, right }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.