Skip to content

Commit c8da1fb

Browse files
committed
fix(cli): info --media leaves out a clip that starts past its media's end
1 parent db86286 commit c8da1fb

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

‎packages/cli/src/utils/projectMedia.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ describe("heardClips", () => {
5454
]);
5555
});
5656

57+
it("leaves out a clip that starts past its media's end, and reads each file's length once", async () => {
58+
const probed: string[] = [];
59+
const counting = { seconds: async (path: string) => (probed.push(path), 6) };
60+
const clips = await heardClips(
61+
project(`<video id="late" src="../assets/talk.mp4" data-has-audio="true" data-start="0" data-media-start="7"></video>
62+
<video id="talk" src="../assets/talk.mp4" data-has-audio="true" data-start="8" data-duration="3"></video>`),
63+
counting,
64+
);
65+
expect(clips.map(({ id, start, end }) => ({ id, start, end }))).toEqual([
66+
{ id: "talk-audio", start: 18, end: 21 },
67+
]);
68+
expect(probed).toHaveLength(1);
69+
});
70+
5771
it("hears a video only when it says data-has-audio and is not muted, as the render does", async () => {
5872
const clips = await heardClips(
5973
project(`<video id="broll" src="../assets/broll.mp4" data-start="0" data-duration="3"></video>

‎packages/cli/src/utils/projectMedia.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,34 @@ const PROBE: HeardProbe = {
4646
};
4747

4848
/** Every clip the render mixes, read as the render reads them (scenes inlined, on the film's clock), each ended where
49-
* its media runs out if that comes first; a ramped rate or a loop keeps its end. */
49+
* its media runs out if that comes first, and left out if it starts past that; a ramped rate or a loop keeps its end. */
5050
export async function heardClips(
5151
projectDir: string,
5252
probe: HeardProbe = PROBE,
5353
): Promise<HeardClip[]> {
54-
const { bundleToSingleHtml } = await import("@hyperframes/core/compiler");
55-
const { audios } = (await loadProducer()).collectRenderMedia(
56-
await bundleToSingleHtml(projectDir),
57-
);
58-
return Promise.all(audios.map((clip) => endedClip(clip, projectDir, probe)));
54+
const { audios } = await collectProjectMedia(projectDir);
55+
const lengths = new Map<string, Promise<number | null>>();
56+
const length = (src: string): Promise<number | null> => {
57+
const path = mediaPath(src, projectDir);
58+
const known = lengths.get(path) ?? probe.seconds(path).catch(() => null);
59+
lengths.set(path, known);
60+
return known;
61+
};
62+
const clips = await Promise.all(audios.map((clip) => endedClip(clip, length)));
63+
return clips.filter((clip) => clip !== null);
5964
}
6065

6166
async function endedClip(
6267
clip: AudioElement,
63-
projectDir: string,
64-
probe: HeardProbe,
65-
): Promise<HeardClip> {
68+
length: (src: string) => Promise<number | null>,
69+
): Promise<HeardClip | null> {
6670
const { id, src, start, mediaStart, playbackRate, volume, type } = clip;
6771
const heard = { id, src, start, end: clip.end, mediaStart, playbackRate, volume, type };
6872
const rate = playbackRate === undefined ? 1 : playbackRate;
6973
if (clip.loop || typeof rate !== "number" || !(rate > 0)) return heard;
70-
const seconds = await probe.seconds(mediaPath(src, projectDir)).catch(() => null);
71-
if (seconds === null || seconds <= mediaStart) return heard;
74+
const seconds = await length(src);
75+
if (seconds === null) return heard;
76+
if (seconds <= mediaStart) return null;
7277
const runsOut = start + (seconds - mediaStart) / rate;
7378
return { ...heard, end: clip.end > 0 ? Math.min(clip.end, runsOut) : runsOut };
7479
}

0 commit comments

Comments
 (0)