Skip to content

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

FCP7: trackindex 1 means channel 0 when the file is split across mono tracks (#463)

Brandon Thomas24f8279327cd
diff --git a/crates/interchange/src/fcp7.rs b/crates/interchange/src/fcp7.rs
index f1af5aaf6..d5de669d2 100644
--- a/crates/interchange/src/fcp7.rs
+++ b/crates/interchange/src/fcp7.rs
@@ -110,6 +110,24 @@ struct Imp<'a, 'i, 'r> {
in_progress: HashSet<String>,
links: Vec<PendingLink>,
xml_clip_ids: HashMap<(ItemId, String), ClipId>,
+ /// `(sequence key, file key)` pairs where some audio clip item of that file uses `sourcetrack/trackindex` > 1,
+ /// i.e. the file's channels are split onto mono tracks in that sequence (#463).
+ split_files: HashSet<(String, String)>,
+}
+
+/// The `(sequence key, file key)` an audio clip item belongs to, if it references a file.
+fn clip_seq_file(c: Node) -> Option<(String, String)> {
+ let seq = c.ancestors().skip(1).find(|a| a.has_tag_name("sequence"))?;
+ Some((node_key(seq), node_key(child(c, "file")?)))
+}
+
+/// The 1-based `sourcetrack/trackindex` of an audio clip item.
+fn audio_track_index(c: Node) -> Option<i64> {
+ let st = child(c, "sourcetrack")?;
+ if child_text(st, "mediatype").is_some_and(|m| m != "audio") {
+ return None;
+ }
+ child_i64(st, "trackindex")
}
fn node_key(n: Node) -> String {
@@ -136,9 +154,15 @@ pub(crate) fn import(text_in: &str, opts: &ImportOptions, report: &mut Report) -
in_progress: HashSet::new(),
links: Vec::new(),
xml_clip_ids: HashMap::new(),
+ split_files: HashSet::new(),
};
for n in root.descendants().filter(|n| n.is_element()) {
match n.tag_name().name() {
+ "clipitem" if audio_track_index(n).is_some_and(|i| i > 1) => {
+ if let Some(key) = clip_seq_file(n) {
+ imp.split_files.insert(key);
+ }
+ }
"file" if elements(n).next().is_some() => {
imp.files.entry(node_key(n)).or_insert(n);
}
@@ -483,11 +507,12 @@ impl<'a, 'i> Imp<'a, 'i, '_> {
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.
+ // Index 1 is also what a stereo clip on a stereo track carries, so it means channel 0 only when another clip
+ // item of the same file in this sequence uses a higher index (the file is split); otherwise 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 Some(idx) = audio_track_index(c)
+ && (idx > 1 || (idx == 1 && clip_seq_file(c).is_some_and(|k| self.split_files.contains(&k))))
&& let Ok(ch) = u16::try_from(idx - 1)
{
ti.source_channels = vec![ch];
diff --git a/crates/interchange/tests/fcp7.rs b/crates/interchange/tests/fcp7.rs
index 8d36da38e..6b2a9414b 100644
--- a/crates/interchange/tests/fcp7.rs
+++ b/crates/interchange/tests/fcp7.rs
@@ -546,6 +546,7 @@ fn split_mono_tracks_keep_their_source_channel() {
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(0), vec![vec![0u16]], "{xml}");
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>"));
@@ -567,3 +568,35 @@ fn audio_sourcetrack_trackindex_2_imports_as_source_channel_1() {
let seq = imp.project.sequence(only_seq(&imp)).unwrap();
assert_eq!(seq.tracks(TrackKind::Audio)[0].items[0].source_channels, vec![1u16]);
}
+
+/// #463: Premiere writes a stereo file split onto two mono tracks as clip items with `trackindex` 1 and 2;
+/// a file used only with `trackindex` 1 is a stereo clip and keeps the default (all channels) mapping.
+#[test]
+fn audio_trackindex_1_is_channel_0_only_when_the_file_is_split() {
+ 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>1</trackindex></sourcetrack>
+ </clipitem>
+ <clipitem id="clipitem-3"><name>other</name>{RATE}<start>48</start><end>96</end><in>0</in><out>48</out>
+ <file id="file-2"><name>other.wav</name><pathurl>file://localhost/media/other.wav</pathurl>{RATE}<duration>48</duration><media><audio><channelcount>2</channelcount></audio></media></file>
+ <sourcetrack><mediatype>audio</mediatype><trackindex>1</trackindex></sourcetrack>
+ </clipitem></track>
+ <track><clipitem id="clipitem-2"><name>take</name>{RATE}<start>0</start><end>48</end><in>0</in><out>48</out>
+ <file id="file-1"/>
+ <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();
+ let a1 = &seq.tracks(TrackKind::Audio)[0].items;
+ assert_eq!(a1[0].source_channels, vec![0u16]);
+ assert!(a1[1].source_channels.is_empty(), "{:?}", a1[1].source_channels);
+ assert_eq!(seq.tracks(TrackKind::Audio)[1].items[0].source_channels, vec![1u16]);
+}