Skip to content

Commit b7160f6

Browse files
fix: exclude hidden audio from render mix (#2870)
* fix: exclude hidden audio from render mix * fix(engine): honor hidden media ancestry in audio mix
1 parent 4ad1cf4 commit b7160f6

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎packages/engine/src/services/audioMixer.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,33 @@ describe("parseAudioElements — relative data-start resolution", () => {
844844
expect(track!.start).toBe(4);
845845
});
846846
});
847+
848+
describe("parseAudioElements — hidden tracks", () => {
849+
it("excludes directly hidden audio and audible video from the render mix", () => {
850+
const html =
851+
`<div data-composition-id="main" data-start="0" data-duration="3">` +
852+
`<audio id="master" src="master.wav" data-start="0" data-duration="3"></audio>` +
853+
`<audio id="hidden" src="hidden.wav" data-start="0" data-duration="3" data-hidden></audio>` +
854+
`<video id="hidden-video" src="hidden.mp4" data-has-audio="true" data-hidden></video>` +
855+
`</div>`;
856+
857+
expect(parseAudioElements(html).map((track) => track.id)).toEqual(["master"]);
858+
});
859+
860+
it("excludes audio and audible video beneath a hidden ancestor", () => {
861+
const html =
862+
`<div data-composition-id="main" data-start="0" data-duration="3">` +
863+
`<audio id="master" src="master.wav" data-start="0" data-duration="3"></audio>` +
864+
`<section data-hidden>` +
865+
`<audio id="nested-audio" src="nested.wav"></audio>` +
866+
`<video id="nested-video" src="nested.mp4" data-has-audio="true"></video>` +
867+
`</section>` +
868+
`<video id="visible-video" src="visible.mp4" data-has-audio="true"></video>` +
869+
`</div>`;
870+
871+
expect(parseAudioElements(html).map((track) => track.id)).toEqual([
872+
"master",
873+
"visible-video-audio",
874+
]);
875+
});
876+
});

‎packages/engine/src/services/audioMixer.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ function ffmpegFailure(
304304
export function parseAudioElements(html: string): AudioElement[] {
305305
const elements: AudioElement[] = [];
306306
const { document } = parseHTML(unwrapTemplate(html));
307+
interface AudioMediaElement extends RefResolverEl {
308+
hasAttribute(name: string): boolean;
309+
parentElement: AudioMediaElement | null;
310+
}
307311

308312
// Shared resolver state so a relative `data-start` ("start when clip X ends")
309313
// resolves against every clip in the composition — exactly as
@@ -321,6 +325,12 @@ export function parseAudioElements(html: string): AudioElement[] {
321325
const end = raw ? parseFloat(raw) : 0;
322326
return Number.isFinite(end) ? end : 0;
323327
};
328+
const isHidden = (el: AudioMediaElement): boolean => {
329+
for (let current: AudioMediaElement | null = el; current; current = current.parentElement) {
330+
if (current.hasAttribute("data-hidden")) return true;
331+
}
332+
return false;
333+
};
324334

325335
// <audio> and <video data-has-audio> tracks differ only in the emitted id
326336
// and `type`; everything else (timing, layer, volume) is read identically.
@@ -342,13 +352,13 @@ export function parseAudioElements(html: string): AudioElement[] {
342352

343353
for (const el of document.querySelectorAll("audio[id][src]")) {
344354
const id = el.getAttribute("id");
345-
if (!id || !el.getAttribute("src")) continue;
355+
if (!id || !el.getAttribute("src") || isHidden(el)) continue;
346356
elements.push(build(el, id, "audio"));
347357
}
348358

349359
for (const el of document.querySelectorAll('video[id][src][data-has-audio="true"]')) {
350360
const id = el.getAttribute("id");
351-
if (!id || !el.getAttribute("src")) continue;
361+
if (!id || !el.getAttribute("src") || isHidden(el)) continue;
352362
elements.push(build(el, `${id}-audio`, "video"));
353363
}
354364

0 commit comments

Comments
 (0)