From 000621aceb7b232a0a836746efc3e958ec02b5c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 10 Aug 2026 10:52:47 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(engine):=20the=20mp4=20audio?= =?UTF-8?q?=20copy=20follows=20the=20lane=20that=20holds=20the=20sound?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The refusal gate counted non-empty audio lanes but the copy still read A1's play list, so a project that leaves A1 empty and places everything on A2 passed the gate as "one lane", played its sound, and exported an mp4 with no audio track at all -- silently, which is the one failure nobody notices until they play the file back. The copy now takes the single list `audio_segments_from` already resolved for playback, so "which lane holds the sound" is asked once and answered the same way everywhere. The gate keeps its wording: that list is never empty, so the refusal is still exactly the >1-lane case. --- crates/engine/src/export.rs | 16 +++++--- crates/engine/tests/lanes.rs | 75 ++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index 4a0db4b..c1f7136 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -218,8 +218,15 @@ fn run( // is no AAC encoder here to encode it with. Refused by name rather than // silently exporting the first lane -- a file missing half its sound is the // one failure a user would not notice. + // + // *Which* lane is the same question `audio_segments_from` answers for + // playback, and it is asked here rather than assumed: the lane that holds + // the sound need not be `A1` (a project may leave `A1` empty and place + // everything on `A2`), and copying `A1`'s list in that case would write a + // file with no audio track at all -- silently, which is the failure this + // whole comment is about. let lanes = project.audio_segments_from(0, meta.frame_rate); - if lanes.len() > 1 { + let [segments] = &lanes[..] else { return Err(format!( "this timeline has {} audio lanes and an mp4 export copies one: \ there is no AAC encoder here to mix them with. Export WAV or FLAC, \ @@ -227,11 +234,8 @@ fn run( lanes.len() ) .into()); - } - let audio = AudioSession::copy_multi_streams( - &project.audio_sources(), - &project.segments_from(0, meta.frame_rate), - )?; + }; + let audio = AudioSession::copy_multi_streams(&project.audio_sources(), segments)?; let audio_params = audio.as_ref().map(|(track, _)| AudioParams { freq_index: track.freq_index, chan_conf: track.chan_conf, diff --git a/crates/engine/tests/lanes.rs b/crates/engine/tests/lanes.rs index 6a1e285..a80bbe9 100644 --- a/crates/engine/tests/lanes.rs +++ b/crates/engine/tests/lanes.rs @@ -198,6 +198,81 @@ fn export_writes_the_same_composite() { std::fs::remove_file(&out).unwrap(); } +/// Every sample of `path`'s first audio stream, interleaved -- what the export +/// wrote, read back through the engine's own decoder. +fn decoded_audio(path: &Path) -> Vec { + let sources = [(path.to_path_buf(), 0)]; + let (_, chunks) = + engine::AudioSession::open_multi_streams(&sources, &[(Some(0), 0.0, f64::INFINITY)]) + .expect("open the audio") + .expect("there is an audio track"); + chunks.into_iter().flat_map(|c| c.samples).collect() +} + +fn rms(samples: &[f32]) -> f64 { + assert!(!samples.is_empty(), "no samples to measure"); + (samples + .iter() + .map(|s| f64::from(*s) * f64::from(*s)) + .sum::() + / samples.len() as f64) + .sqrt() +} + +/// The sound need not be on `A1`: a project that leaves `A1` empty and places +/// everything on `A2` is still *one* audio lane, so the mp4 export copies it +/// rather than refusing -- and rather than writing a file with no audio track +/// at all, which is what pinning the copy to `A1` did (verifier's repro: the +/// export succeeded, ffprobe showed video only, and nobody would notice until +/// they played it). +#[test] +fn the_mp4_copy_follows_the_lane_that_holds_the_sound() { + pin_software(); + let source = asset("test_av.mp4"); + let clip = Clip { + start: 0, + in_frame: 0, + out_frame: TOTAL, + source: 0, + link: None, + }; + let project = Project::from_parts( + vec![Source::new(&source, 0)], + vec![ + (LaneKind::Video, vec![clip]), + (LaneKind::Audio, Vec::new()), + (LaneKind::Audio, vec![clip]), + ], + ) + .expect("valid parts"); + assert_eq!( + project.audio_segments_from(0, FPS).len(), + 1, + "one lane holds sound, so this is not a mix" + ); + + let out = out_path("a2_only", "mp4"); + let (meta, _) = engine::demux::Demuxer::open(&source).unwrap(); + let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); + wait(&handle, Duration::from_secs(300)).expect("export the A2-only timeline"); + + let written = decoded_audio(&out); + let played = decoded_audio(&source); + println!( + "A2-only export: {} samples rms {:.4}, source {} samples rms {:.4}", + written.len(), + rms(&written), + played.len(), + rms(&played) + ); + assert!(rms(&written) > 0.001, "the export has to carry the sound"); + assert!( + (rms(&written) / rms(&played) - 1.0).abs() < 0.05, + "the exported audio is not the lane's content" + ); + std::fs::remove_file(&out).unwrap(); +} + /// `A1` and `A2` playing the *same* source range at the same place: over that /// stretch the mix is that audio twice over, so it measures at exactly double /// the level the one-lane export of the same timeline has there -- and outside