Skip to content

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

engine: file.exportInterchange refuses an unknown format

punkhop285da5c66bf1
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index e5cde54e5..547e6f276 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -737,9 +737,15 @@ fn build() -> Vec<CommandSpec> {
}
Ok(out)
}),
- cmd!("file.exportInterchange", "Export Interchange", [], None, r#"{"format":"edl|xml|fcpxml|otio","path":str,"sequence":id?}"#, has_seq, |s, p| {
- crate::interchange::export(s, p)
- }),
+ cmd!(
+ "file.exportInterchange",
+ "Export Interchange",
+ [],
+ None,
+ r#"{"format":"edl|xml|fcpxml|otio|aaf|omf"=xml,"path":str,"sequence":id?}"#,
+ has_seq,
+ crate::interchange::export
+ ),
cmd!("file.exportEdl", "EDL…", ["File", "Export"], None, r#"{"path":str}"#, has_seq, |s, p| {
let mut q = p.clone();
q["format"] = json!("edl");
diff --git a/crates/engine/src/interchange.rs b/crates/engine/src/interchange.rs
index b961eea27..583d80295 100644
--- a/crates/engine/src/interchange.rs
+++ b/crates/engine/src/interchange.rs
@@ -108,15 +108,17 @@ pub fn import(s: &mut Session, path: &str, bytes: &[u8], format: Format) -> Resu
}))
}
-/// `file.exportInterchange {format: "edl"|"xml"|"fcpxml"|"otio", path, sequence?}`
+/// `file.exportInterchange {format: "edl"|"xml"|"fcpxml"|"otio"|"aaf"|"omf" = "xml", path, sequence?}`
pub fn export(s: &mut Session, p: &Value) -> Result<Value> {
- let format = match p.get("format").and_then(Value::as_str).unwrap_or("xml") {
- "edl" => Format::Edl,
- "fcpxml" => Format::Fcpxml,
- "otio" => Format::Otio,
- "aaf" => Format::Aaf,
- "omf" => Format::Omf,
- _ => Format::Fcp7Xml,
+ // FCP7 XML only when no format is asked for: a format we don't know is an error, not XML.
+ // The names are the documented ones (each format's extension, in either case) and nothing
+ // else, so the error can list exactly what is accepted.
+ let format = match p.get("format") {
+ None | Some(Value::Null) => Format::Fcp7Xml,
+ Some(v) => v.as_str().and_then(|name| Format::ALL.into_iter().find(|f| f.extension().eq_ignore_ascii_case(name))).ok_or_else(|| {
+ let names: Vec<&str> = Format::ALL.iter().map(|f| f.extension()).collect();
+ crate::commands::bad("file.exportInterchange", format!("unknown format {v}: use one of {}", names.join(", ")))
+ })?,
};
let path = p.get("path").and_then(Value::as_str).ok_or_else(|| EngineError::Other("need `path`".into()))?.to_string();
let seq = p.get("sequence").and_then(Value::as_u64).map(ItemId).or(s.state.active_sequence).ok_or(EngineError::NoSequence)?;
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 742a7f6a6..2889977aa 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -335,6 +335,39 @@ fn interchange_roundtrip_through_files() {
let _ = std::fs::remove_dir_all(&dir);
}
+/// `file.exportInterchange {format: "<unknown>"}` used to write FCP7 XML without a word.
+#[test]
+fn export_interchange_refuses_an_unknown_format() {
+ let d = std::env::temp_dir().join(format!("fc-ix-format-{}", std::process::id()));
+ std::fs::create_dir_all(&d).unwrap();
+ let path = |n: &str| d.join(n).to_string_lossy().to_string();
+ let mut s = demo();
+ // unknown names, values that are not names, and spellings that are not the documented ones
+ for bad in [json!("premiere"), json!("mp4"), json!(""), json!(7), json!(["xml"]), json!(".xml"), json!("fcpxmld"), json!("omfi"), json!("xml ")] {
+ let e = s.execute("file.exportInterchange", json!({"format": bad, "path": path("bad.out")})).unwrap_err();
+ assert!(matches!(&e, EngineError::BadParams { cmd, .. } if cmd == "file.exportInterchange"), "{bad}: {e:?}");
+ let e = e.to_string();
+ assert!(e.contains("unknown format") && e.ends_with("use one of edl, xml, fcpxml, otio, aaf, omf"), "the error lists exactly the accepted names: {e}");
+ assert!(!d.join("bad.out").exists(), "nothing is written for {bad}");
+ }
+ // the documented formats still export (in either case), and no format still means FCP7 XML
+ let text = |n: &str| String::from_utf8_lossy(&std::fs::read(d.join(n)).unwrap()).into_owned();
+ s.execute("file.exportInterchange", json!({"path": path("default.xml")})).unwrap();
+ s.execute("file.exportInterchange", json!({"format": "xml", "path": path("a.xml")})).unwrap();
+ assert!(text("default.xml").contains("<xmeml") && text("a.xml").contains("<xmeml"));
+ s.execute("file.exportInterchange", json!({"format": "fcpxml", "path": path("a.fcpxml")})).unwrap();
+ assert!(text("a.fcpxml").contains("<fcpxml"));
+ s.execute("file.exportInterchange", json!({"format": "otio", "path": path("a.otio")})).unwrap();
+ assert!(text("a.otio").contains("OTIO_SCHEMA"));
+ s.execute("file.exportInterchange", json!({"format": "EDL", "path": path("a.edl")})).unwrap();
+ assert!(text("a.edl").contains("TITLE:"));
+ for (format, magic) in [("aaf", "aaf"), ("omf", "omf")] {
+ let r = s.execute("file.exportInterchange", json!({"format": format, "path": path(&format!("a.{magic}"))})).unwrap();
+ assert!(r["bytes"].as_u64().unwrap() > 0, "{format}");
+ }
+ let _ = std::fs::remove_dir_all(&d);
+}
+
#[test]
fn move_items_to_bin_and_back_with_undo() {
let mut s = demo();
diff --git a/docs/architecture.md b/docs/architecture.md
index bcb71fb13..4082762e3 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -624,7 +624,9 @@ Video encoders implement `export::VideoEncoder`. Codec crates plug in with `regi
Timelines can be exchanged as EDL, FCP7 XML, FCPXML, OTIO, AAF or OMF. `file.import` detects these
formats and merges the result into the project as one undoable step (AAF / OMF embedded audio is
written next to the document first), and `file.exportInterchange`, `file.exportEdl`,
-`file.exportFcpxml`, `file.exportOtio`, `file.exportAaf` and `file.exportOmf` write them. For AAF and
+`file.exportFcpxml`, `file.exportOtio`, `file.exportAaf` and `file.exportOmf` write them.
+`file.exportInterchange {format}` takes `edl`, `xml` (the default), `fcpxml`, `otio`, `aaf` or `omf`;
+any other value is a parameter error and nothing is written. For AAF and
OMF the engine (`engine::aaf_omf`) first prepares the audio the document references: it lists the
used ranges (`interchange::essence::audio_needs`), decodes or renders them (clip effects through the
export audio pipeline), embeds them or writes WAV / AIFF files, and optionally renders a video