GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
//! OMF Interchange 2.0 export (and import of the same structures) for audio post.
//!
//! Written from the public *OMF Interchange Specification Version 2.0* (Avid Technology, 1995/97)
//! and Apple's *Bento Specification* revision 1.0d5 for the container ([`bento`]).
//!
//! The file holds a composition mob (`CMOB`) with a timecode slot and one sound slot per audio
//! track (one per channel when broken out to mono): sequences (`SEQU`) of fillers (`FILL`), source
//! clips (`SCLP`) and transitions (`TRAN` with a `SimpleMonoAudioDissolve` effect); clip volume is a
//! `MonoAudioGain` effect (`EFFE`) whose slot −1 is the clip and slot 1 the amplitude
//! (`CVAL` constant or `VVAL` keyframes). Every media item has a master mob (`MMOB`), a file source
//! mob (`SMOB` with a `WAVD` / `AIFD` descriptor: embedded essence in a `WAVE` media data object,
//! or a locator to a separate file) and a tape source mob carrying its timecode. Times are in
//! samples (sound slots use the sample rate as edit rate) so edits are sample-exact.
//!
//! A nested sequence is not written as a composition: its sound, mixed by the caller for the clip
//! ([`crate::essence::NestNeeds::Render`]), is media like any other, in a clip named after the
//! sequence. Premiere Pro's OMF export does the same.

pub(crate) use craft_aaf::omf::bento;
pub(crate) mod shared_model;

use filmcraft_project::{ItemId, Project};
use serde::{Deserialize, Serialize};

use crate::comp::{self, ExtractedMedia};
use crate::essence::MediaOptions;
use crate::{ImportOptions, Imported, Report, Result};

/// OMF export options (Premiere's OMF export dialog: the caller renders / consolidates the audio
/// and passes it in [`MediaOptions::essence`]).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct OmfOptions {
    /// Composition name (default: the sequence name).
    pub name: Option<String>,
    pub media: MediaOptions,
}

/// Export the audio of `sequence` as an OMF 2.0 file.
pub fn export(project: &Project, sequence: ItemId, opts: &OmfOptions) -> Result<(Vec<u8>, Report)> {
    let mut report = Report::default();
    crate::warn_marker_metadata_loss(project, sequence, &mut report);
    let name = opts.name.clone().or_else(|| project.item(sequence).map(|i| i.name.clone())).unwrap_or_else(|| "Sequence".into());
    let mut media = opts.media.clone();
    media.audio_only = true;
    media.mixdown_video = None;
    let doc = comp::from_project(project, sequence, &name, &media, comp::Nests::Rendered, &mut report)?;
    if !doc.compositions[0].markers.is_empty() {
        report.info("sequence markers are not written to OMF");
    }
    if project.sequence(sequence).is_some_and(|q| q.video_tracks.iter().any(|t| !t.items.is_empty())) {
        report.info("video tracks are not written to OMF (audio only)");
    }
    let identity =
        craft_aaf::Identity { company: "FilmCraft", product: "FilmCraft", version: env!("CARGO_PKG_VERSION"), platform: "FilmCraft", product_id: [0; 16] };
    let mut shared_report = craft_aaf::Report::default();
    let bytes = craft_aaf::omf::write(&shared_model::to_shared(doc), &identity, &mut shared_report).map_err(shared_model::error)?;
    shared_model::merge_report(&mut report, shared_report);
    Ok((bytes, report))
}

/// Import an OMF 2.0 file (as written by [`export`] and by applications using the same
/// structures). Embedded audio is returned as WAV files.
pub fn import(bytes: &[u8], opts: &ImportOptions) -> Result<(Imported, Vec<ExtractedMedia>, Report)> {
    let mut report = Report::default();
    let mut shared_report = craft_aaf::Report::default();
    let doc = craft_aaf::omf::read(bytes, &mut shared_report).map_err(shared_model::error)?;
    shared_model::merge_report(&mut report, shared_report);
    let doc = shared_model::from_shared(doc);
    let (imported, extracted) = comp::to_project(doc, opts, &mut report)?;
    let mut imported = imported;
    crate::bound_start_timecodes(&mut imported, &mut report);
    Ok((imported, extracted, report))
}

/// Whether `bytes` end with a Bento container label (OMF files do).
pub fn sniff(bytes: &[u8]) -> bool {
    bento::sniff(bytes)
}

#[cfg(test)]
mod tests;