The mp4 audio copy follows the lane that holds the sound
Browse files at this commit000621aceb7b232a0a836746efc3e958ec02b5c0Parent: 08af6bc- fix
- the mp4 audio copy follows the lane that holds the sound
2 changed files +85 -6
- crates/engine/src/export.rs +10 -6
- crates/engine/tests/lanes.rs +75 -0
crates/engine/src/export.rsmodified+10 -6
| @@ -218,8 +218,15 @@ fn run( | |||
| 218 | // is no AAC encoder here to encode it with. Refused by name rather than | 218 | // is no AAC encoder here to encode it with. Refused by name rather than |
| 219 | // silently exporting the first lane -- a file missing half its sound is the | 219 | // silently exporting the first lane -- a file missing half its sound is the |
| 220 | // one failure a user would not notice. | 220 | // one failure a user would not notice. |
| 221 | + // | ||
| 222 | + // *Which* lane is the same question `audio_segments_from` answers for | ||
| 223 | + // playback, and it is asked here rather than assumed: the lane that holds | ||
| 224 | + // the sound need not be `A1` (a project may leave `A1` empty and place | ||
| 225 | + // everything on `A2`), and copying `A1`'s list in that case would write a | ||
| 226 | + // file with no audio track at all -- silently, which is the failure this | ||
| 227 | + // whole comment is about. | ||
| 221 | let lanes = project.audio_segments_from(0, meta.frame_rate); | 228 | let lanes = project.audio_segments_from(0, meta.frame_rate); |
| 222 | - if lanes.len() > 1 { | 229 | + let [segments] = &lanes[..] else { |
| 223 | return Err(format!( | 230 | return Err(format!( |
| 224 | "this timeline has {} audio lanes and an mp4 export copies one: \ | 231 | "this timeline has {} audio lanes and an mp4 export copies one: \ |
| 225 | there is no AAC encoder here to mix them with. Export WAV or FLAC, \ | 232 | there is no AAC encoder here to mix them with. Export WAV or FLAC, \ |
| @@ -227,11 +234,8 @@ fn run( | |||
| 227 | lanes.len() | 234 | lanes.len() |
| 228 | ) | 235 | ) |
| 229 | .into()); | 236 | .into()); |
| 230 | - } | 237 | + }; |
| 231 | - let audio = AudioSession::copy_multi_streams( | 238 | + let audio = AudioSession::copy_multi_streams(&project.audio_sources(), segments)?; |
| 232 | - &project.audio_sources(), | ||
| 233 | - &project.segments_from(0, meta.frame_rate), | ||
| 234 | - )?; | ||
| 235 | let audio_params = audio.as_ref().map(|(track, _)| AudioParams { | 239 | let audio_params = audio.as_ref().map(|(track, _)| AudioParams { |
| 236 | freq_index: track.freq_index, | 240 | freq_index: track.freq_index, |
| 237 | chan_conf: track.chan_conf, | 241 | chan_conf: track.chan_conf, |
crates/engine/tests/lanes.rsmodified+75 -0
| @@ -198,6 +198,81 @@ fn export_writes_the_same_composite() { | |||
| 198 | std::fs::remove_file(&out).unwrap(); | 198 | std::fs::remove_file(&out).unwrap(); |
| 199 | } | 199 | } |
| 200 | 200 | ||
| 201 | +/// Every sample of `path`'s first audio stream, interleaved -- what the export | ||
| 202 | +/// wrote, read back through the engine's own decoder. | ||
| 203 | +fn decoded_audio(path: &Path) -> Vec<f32> { | ||
| 204 | + let sources = [(path.to_path_buf(), 0)]; | ||
| 205 | + let (_, chunks) = | ||
| 206 | + engine::AudioSession::open_multi_streams(&sources, &[(Some(0), 0.0, f64::INFINITY)]) | ||
| 207 | + .expect("open the audio") | ||
| 208 | + .expect("there is an audio track"); | ||
| 209 | + chunks.into_iter().flat_map(|c| c.samples).collect() | ||
| 210 | +} | ||
| 211 | + | ||
| 212 | +fn rms(samples: &[f32]) -> f64 { | ||
| 213 | + assert!(!samples.is_empty(), "no samples to measure"); | ||
| 214 | + (samples | ||
| 215 | + .iter() | ||
| 216 | + .map(|s| f64::from(*s) * f64::from(*s)) | ||
| 217 | + .sum::<f64>() | ||
| 218 | + / samples.len() as f64) | ||
| 219 | + .sqrt() | ||
| 220 | +} | ||
| 221 | + | ||
| 222 | +/// The sound need not be on `A1`: a project that leaves `A1` empty and places | ||
| 223 | +/// everything on `A2` is still *one* audio lane, so the mp4 export copies it | ||
| 224 | +/// rather than refusing -- and rather than writing a file with no audio track | ||
| 225 | +/// at all, which is what pinning the copy to `A1` did (verifier's repro: the | ||
| 226 | +/// export succeeded, ffprobe showed video only, and nobody would notice until | ||
| 227 | +/// they played it). | ||
| 228 | +#[test] | ||
| 229 | +fn the_mp4_copy_follows_the_lane_that_holds_the_sound() { | ||
| 230 | + pin_software(); | ||
| 231 | + let source = asset("test_av.mp4"); | ||
| 232 | + let clip = Clip { | ||
| 233 | + start: 0, | ||
| 234 | + in_frame: 0, | ||
| 235 | + out_frame: TOTAL, | ||
| 236 | + source: 0, | ||
| 237 | + link: None, | ||
| 238 | + }; | ||
| 239 | + let project = Project::from_parts( | ||
| 240 | + vec![Source::new(&source, 0)], | ||
| 241 | + vec![ | ||
| 242 | + (LaneKind::Video, vec![clip]), | ||
| 243 | + (LaneKind::Audio, Vec::new()), | ||
| 244 | + (LaneKind::Audio, vec![clip]), | ||
| 245 | + ], | ||
| 246 | + ) | ||
| 247 | + .expect("valid parts"); | ||
| 248 | + assert_eq!( | ||
| 249 | + project.audio_segments_from(0, FPS).len(), | ||
| 250 | + 1, | ||
| 251 | + "one lane holds sound, so this is not a mix" | ||
| 252 | + ); | ||
| 253 | + | ||
| 254 | + let out = out_path("a2_only", "mp4"); | ||
| 255 | + let (meta, _) = engine::demux::Demuxer::open(&source).unwrap(); | ||
| 256 | + let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); | ||
| 257 | + wait(&handle, Duration::from_secs(300)).expect("export the A2-only timeline"); | ||
| 258 | + | ||
| 259 | + let written = decoded_audio(&out); | ||
| 260 | + let played = decoded_audio(&source); | ||
| 261 | + println!( | ||
| 262 | + "A2-only export: {} samples rms {:.4}, source {} samples rms {:.4}", | ||
| 263 | + written.len(), | ||
| 264 | + rms(&written), | ||
| 265 | + played.len(), | ||
| 266 | + rms(&played) | ||
| 267 | + ); | ||
| 268 | + assert!(rms(&written) > 0.001, "the export has to carry the sound"); | ||
| 269 | + assert!( | ||
| 270 | + (rms(&written) / rms(&played) - 1.0).abs() < 0.05, | ||
| 271 | + "the exported audio is not the lane's content" | ||
| 272 | + ); | ||
| 273 | + std::fs::remove_file(&out).unwrap(); | ||
| 274 | +} | ||
| 275 | + | ||
| 201 | /// `A1` and `A2` playing the *same* source range at the same place: over that | 276 | /// `A1` and `A2` playing the *same* source range at the same place: over that |
| 202 | /// stretch the mix is that audio twice over, so it measures at exactly double | 277 | /// stretch the mix is that audio twice over, so it measures at exactly double |
| 203 | /// the level the one-lane export of the same timeline has there -- and outside | 278 | /// the level the one-lane export of the same timeline has there -- and outside |