Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

FCP7 XML: keep the source channel of split mono audio tracks (#463)

MarekSpodBiedryc25cc5b18b03
diff --git a/crates/interchange/src/fcp7.rs b/crates/interchange/src/fcp7.rs
index 1b94f7122..f1af5aaf6 100644
--- a/crates/interchange/src/fcp7.rs
+++ b/crates/interchange/src/fcp7.rs
@@ -482,6 +482,16 @@ impl<'a, 'i> Imp<'a, 'i, '_> {
if let Some(l) = path_text(c, &["labels", "label2"]).and_then(label_from) {
ti.label = l;
}
+ // `sourcetrack/trackindex` (1-based) picks the source channel of a mono track split off a multichannel file (#463).
+ // Index 1 is also what a stereo clip on a stereo track carries, so it keeps the default (empty) channel mapping.
+ if kind == TrackKind::Audio
+ && let Some(st) = child(c, "sourcetrack")
+ && let Some(idx) = child_i64(st, "trackindex")
+ && idx > 1
+ && let Ok(ch) = u16::try_from(idx - 1)
+ {
+ ti.source_channels = vec![ch];
+ }
if let Some(mode) = child_text(c, "compositemode") {
match BLEND_XML.iter().find(|(n, _)| n.eq_ignore_ascii_case(mode)) {
Some((_, v)) if *v != 0 => set_param(&mut ti, "opacity", "blend", Param::new(ParamValue::Choice(*v))),
@@ -962,7 +972,7 @@ impl Exp<'_, '_> {
if kind == TrackKind::Audio {
self.w.open("sourcetrack", &[]);
self.w.text("mediatype", "audio");
- self.w.text("trackindex", 1);
+ self.w.text("trackindex", c.source_channels.first().map_or(1, |ch| u32::from(*ch).saturating_add(1)));
self.w.close();
}
if let Some(blend) = param(c, "opacity", "blend").and_then(|p| p.value.as_f64()).map(|v| v as u32).filter(|v| *v != 0) {
diff --git a/crates/interchange/tests/fcp7.rs b/crates/interchange/tests/fcp7.rs
index 4d3677efc..8d36da38e 100644
--- a/crates/interchange/tests/fcp7.rs
+++ b/crates/interchange/tests/fcp7.rs
@@ -527,3 +527,43 @@ fn sub_frame_clips_keep_equal_source_and_record_spans() {
assert_eq!(out_f - in_f, end_f - start_f, "start {start}, in {src}, duration {dur} (tenths of a second): {spans:?}");
}
}
+
+/// #463: a stereo file split onto two mono tracks (A1 = channel 1, A2 = channel 2) lost the right channel,
+/// because every audio clip item was written with `sourcetrack/trackindex` 1 and the importer ignored it.
+#[test]
+fn split_mono_tracks_keep_their_source_channel() {
+ let r = FrameRate::FPS_23_976;
+ let mut p = Project::new("P");
+ let a = media(&mut p, "/m/a.wav", false, true, r);
+ let s = sequence(&mut p, "t", r, false);
+ let left = clip(&mut p, s, TrackKind::Audio, 0, a, 0, 24, 0);
+ let right = clip(&mut p, s, TrackKind::Audio, 1, a, 0, 24, 0);
+ for (id, ch) in [(left, 0u16), (right, 1u16)] {
+ let (_, it) = p.sequence_mut(s).unwrap().find_item_mut(id).unwrap();
+ it.source_channels = vec![ch];
+ }
+ let (imp, xml, _) = roundtrip(&p, s, Format::Fcp7Xml, &ExportOptions::default());
+ assert!(xml.contains("<trackindex>2</trackindex>"), "{xml}");
+ let seq = imp.project.sequence(only_seq(&imp)).unwrap();
+ let channels = |t: usize| seq.tracks(TrackKind::Audio)[t].items.iter().map(|i| i.source_channels.clone()).collect::<Vec<_>>();
+ assert_eq!(channels(1), vec![vec![1u16]], "{xml}");
+ let (bytes, _) = export(&imp.project, only_seq(&imp), Format::Fcp7Xml, &ExportOptions::default()).expect("export");
+ assert!(String::from_utf8(bytes).unwrap().contains("<trackindex>2</trackindex>"));
+}
+
+#[test]
+fn audio_sourcetrack_trackindex_2_imports_as_source_channel_1() {
+ let doc = format!(
+ r#"<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE xmeml>
+<xmeml version="4"><sequence id="sequence-1"><name>S</name><duration>48</duration>{RATE}
+ <media><audio><track><clipitem id="clipitem-1"><name>take</name>{RATE}<start>0</start><end>48</end><in>0</in><out>48</out>
+ <file id="file-1"><name>take.wav</name><pathurl>file://localhost/media/take.wav</pathurl>{RATE}<duration>48</duration><media><audio><channelcount>2</channelcount></audio></media></file>
+ <sourcetrack><mediatype>audio</mediatype><trackindex>2</trackindex></sourcetrack>
+ </clipitem></track></audio></media>
+</sequence></xmeml>"#
+ );
+ let (imp, _) = import(doc.as_bytes(), Format::Fcp7Xml, None).unwrap();
+ let seq = imp.project.sequence(only_seq(&imp)).unwrap();
+ assert_eq!(seq.tracks(TrackKind::Audio)[0].items[0].source_channels, vec![1u16]);
+}