Files
filmcraft/crates/engine/src/interchange.rs
T
Brandon ThomasandClaude Opus 5.5 72425d476f M11.4: file.exportAaf / file.exportOmf / file.importAaf engine commands
Premiere's AAF and OMF export options: embedded or separate WAV/AIFF audio,
sample rate and bit depth, trim with handles, rendered clip effects, breakout
to mono, AAF video mixdown and linked media; AAF/OMF import writes embedded
audio next to the document and links it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 04:43:37 -04:00

132 lines
6.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Interchange documents (CMX 3600 EDL, FCP7 XML, FCPXML, OTIO, AAF, OMF) ↔ the session's project.
use serde_json::{Value, json};
use filmcraft_interchange::{ExportOptions, Format, ImportOptions};
use filmcraft_project::{ItemId, ItemKind, MediaRef};
use crate::{EngineError, Result, Session};
/// The interchange format of a file, if it is one (by content, with the extension as a hint).
pub fn detect(path: &str, bytes: &[u8]) -> Option<Format> {
let ext = std::path::Path::new(path).extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase);
if !matches!(ext.as_deref(), Some("edl" | "xml" | "fcpxml" | "otio" | "aaf" | "omf" | "omfi")) {
return None;
}
filmcraft_interchange::detect(bytes, ext.as_deref())
}
fn report_json(r: &filmcraft_interchange::Report) -> Vec<String> {
r.entries.iter().map(|e| if e.count > 1 { format!("{} (×{})", e.message, e.count) } else { e.message.clone() }).collect()
}
/// Import a document: merge its bins, media and sequences into the project (one undo step), then
/// link each media file that exists on disk. Returns the new sequences.
pub fn import(s: &mut Session, path: &str, bytes: &[u8], format: Format) -> Result<Value> {
let p = std::path::Path::new(path);
let opts = ImportOptions {
base_dir: p.parent().map(|d| d.to_string_lossy().to_string()),
name: p.file_stem().map(|n| n.to_string_lossy().to_string()),
..Default::default()
};
let (fragment, report) = match format {
// AAF / OMF may embed audio: write it next to the document (the media items point there)
Format::Aaf | Format::Omf => {
let r = if format == Format::Aaf { filmcraft_interchange::aaf::import(bytes, &opts) } else { filmcraft_interchange::omf::import(bytes, &opts) };
let (fragment, extracted, report) = r.map_err(|e| EngineError::Other(format!("{path}: {e}")))?;
for m in extracted {
if let Some(dir) = std::path::Path::new(&m.path).parent().filter(|_| !s.services.export_in_memory()) {
let _ = std::fs::create_dir_all(dir);
}
s.services.write_file(&m.path, &m.wav).map_err(|e| EngineError::Other(format!("{}: {e}", m.path)))?;
}
(fragment, report)
}
_ => filmcraft_interchange::import_with(bytes, format, &opts).map_err(|e| EngineError::Other(format!("{path}: {e}")))?,
};
let before: std::collections::HashSet<ItemId> = s.project.items.keys().copied().collect();
let name = opts.name.clone().unwrap_or_else(|| format.name().to_string());
let seqs = s.edit(&format!("Import {name}"), |proj, _| Ok(filmcraft_interchange::merge_into(proj, fragment, None)))?;
// Link media: probe each new file-backed item that exists.
let new_media: Vec<(ItemId, String)> = s
.project
.items
.values()
.filter(|i| !before.contains(&i.id))
.filter_map(|i| match &i.kind {
ItemKind::Media(m) => match &m.media {
MediaRef::File { path } => Some((i.id, path.clone())),
_ => None,
},
_ => None,
})
.collect();
let mut linked = 0;
let mut offline = Vec::new();
for (id, mpath) in new_media {
let opened = s.services.read_file(&mpath).ok().and_then(|b| {
let fname = std::path::Path::new(&mpath).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
s.media.open_bytes(&fname, b.into()).ok()
});
match opened {
Some(src) => {
let info = src.info().clone();
let identity = crate::relink::identity_of(&*s.services, &mpath).ok();
let rebase = (format == Format::Edl).then_some(info.start_timecode).flatten();
s.edit("Link Media", |proj, _| {
if let Some(item) = proj.items.get_mut(&id)
&& let ItemKind::Media(m) = &mut item.kind
{
let rate = info.video.as_ref().map(|v| v.frame_rate);
m.info = info.clone();
m.offline = false;
m.identity = identity;
if let (Some(tc), Some(rate)) = (rebase, rate) {
filmcraft_interchange::rebase_source_timecode(proj, id, tc, rate);
}
}
Ok(())
})?;
s.media.insert_file(id, &mpath, src);
linked += 1;
}
None => offline.push(mpath),
}
}
if let Some(&first) = seqs.first() {
s.state.active_sequence = Some(first);
if !s.state.open_sequences.contains(&first) {
s.state.open_sequences.push(first);
}
}
Ok(json!({
"format": format.name(),
"sequences": seqs.iter().map(|i| i.0).collect::<Vec<_>>(),
"linkedMedia": linked,
"offlineMedia": offline,
"report": report_json(&report),
}))
}
/// `file.exportInterchange {format: "edl"|"xml"|"fcpxml"|"otio", 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,
};
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)?;
let opts = ExportOptions {
relative_to: std::path::Path::new(&path).parent().map(|d| d.to_string_lossy().to_string()),
name: s.project.item(seq).map(|i| i.name.clone()),
..Default::default()
};
let (bytes, report) = filmcraft_interchange::export(&s.project, seq, format, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
s.services.write_file(&path, &bytes).map_err(|e| EngineError::Other(e.to_string()))?;
Ok(json!({"path": path, "bytes": bytes.len(), "report": report_json(&report)}))
}