clipasm 0.2.1

A strict, typed, stack-based programmatic video compiler
Documentation
use std::fs;
use std::path::Path;

use crate::diagnostic::Result;
use crate::model::{AudioSpec, NodeId, VideoDomain, VideoSpec};
use crate::preflight::RenderPolicy;
use crate::source::SourceSpan;

use super::super::artifact::verify_video_artifact;
use super::context::run_command;
use super::recipe::FfmpegRecipe;

#[allow(clippy::too_many_arguments)]
pub(super) fn stage_export(
    result: NodeId,
    artifact: &Path,
    staged: &Path,
    spec: &VideoSpec,
    audio: &AudioSpec,
    domain: &VideoDomain,
    has_audio: bool,
    render_policy: RenderPolicy,
    ffmpeg: &Path,
    ffprobe: &Path,
) -> Result<()> {
    let result = export_video(
        result,
        artifact,
        staged,
        spec,
        audio,
        has_audio,
        render_policy,
        ffmpeg,
    )
    .and_then(|()| {
        verify_video_artifact(
            ffprobe,
            staged,
            domain,
            audio,
            has_audio,
            false,
            render_policy.export_pixel_format(),
        )
    });
    if let Err(error) = result {
        let _ = fs::remove_file(staged);
        return Err(error);
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn export_video(
    result: NodeId,
    artifact: &Path,
    output: &Path,
    spec: &VideoSpec,
    audio: &AudioSpec,
    has_audio: bool,
    render_policy: RenderPolicy,
    ffmpeg: &Path,
) -> Result<()> {
    let recipe = export_recipe(result, spec, audio, has_audio, render_policy);
    let command = recipe.materialize(ffmpeg, output, &SourceSpan::file_start(output), |node| {
        (node == result).then_some(artifact)
    })?;
    run_command(command, "E_FFMPEG", &SourceSpan::file_start(output))
}

pub(crate) fn export_recipe(
    result: NodeId,
    spec: &VideoSpec,
    audio: &AudioSpec,
    has_audio: bool,
    render_policy: RenderPolicy,
) -> FfmpegRecipe {
    let mut recipe = FfmpegRecipe::new();
    recipe
        .args(["-i"])
        .artifact(result)
        .args([
            "-map",
            "0:v:0",
            "-c:v",
            render_policy.export_video_encoder(),
            "-pix_fmt",
        ])
        .arg(render_policy.export_pixel_format())
        .arg("-r")
        .arg(format!(
            "{}/{}",
            spec.fps().numerator(),
            spec.fps().denominator()
        ));
    if has_audio {
        recipe.args([
            "-map",
            "0:a:0",
            "-c:a",
            render_policy.export_audio_encoder(),
            "-ar",
            &audio.sample_rate().to_string(),
            "-ac",
            &audio.channels().to_string(),
        ]);
    } else {
        recipe.arg("-an");
    }
    recipe.args([
        "-movflags",
        render_policy.export_movflags(),
        "-f",
        render_policy.export_container(),
    ]);
    recipe
}

#[cfg(test)]
mod tests {
    use std::ffi::OsString;
    use std::process::Command;

    use super::super::super::publication::PublicationTransaction;
    use super::*;
    use crate::model::{FrameCount, FrameRate, VideoDomain, VideoSpec};

    fn export_arguments(has_audio: bool) -> Vec<OsString> {
        let spec =
            VideoSpec::new(64, 64, FrameRate::new(10, 1).expect("frame rate")).expect("video spec");
        let audio = AudioSpec::default();
        let recipe = export_recipe(
            NodeId::new(4),
            &spec,
            &audio,
            has_audio,
            RenderPolicy::CURRENT,
        );
        let artifact = Path::new("cache/result.mkv");
        let command = recipe
            .materialize(
                Path::new("ffmpeg"),
                Path::new("final.mp4"),
                &SourceSpan::file_start("final.mp4"),
                |node| (node == NodeId::new(4)).then_some(artifact),
            )
            .expect("export command");
        command
            .get_args()
            .map(std::ffi::OsStr::to_os_string)
            .collect()
    }

    #[test]
    fn video_only_export_recipe_preserves_the_native_arguments() {
        assert_eq!(
            export_arguments(false),
            [
                "-y",
                "-v",
                "error",
                "-i",
                "cache/result.mkv",
                "-map",
                "0:v:0",
                "-c:v",
                "libx264",
                "-pix_fmt",
                "yuv420p",
                "-r",
                "10/1",
                "-an",
                "-movflags",
                "+faststart",
                "-f",
                "mp4",
                "final.mp4",
            ]
            .map(OsString::from)
        );
    }

    #[test]
    fn audiovisual_export_recipe_preserves_the_native_arguments() {
        assert_eq!(
            export_arguments(true),
            [
                "-y",
                "-v",
                "error",
                "-i",
                "cache/result.mkv",
                "-map",
                "0:v:0",
                "-c:v",
                "libx264",
                "-pix_fmt",
                "yuv420p",
                "-r",
                "10/1",
                "-map",
                "0:a:0",
                "-c:a",
                "aac",
                "-ar",
                "48000",
                "-ac",
                "2",
                "-movflags",
                "+faststart",
                "-f",
                "mp4",
                "final.mp4",
            ]
            .map(OsString::from)
        );
    }

    #[test]
    fn failed_final_export_preserves_existing_pair() {
        if Command::new("ffmpeg").arg("-version").output().is_err() {
            return;
        }
        let directory = tempfile::tempdir().expect("temporary directory");
        let invalid_artifact = directory.path().join("invalid.mkv");
        let output = directory.path().join("final.mp4");
        let manifest = directory.path().join("final.mp4.manifest.json");
        fs::write(&invalid_artifact, b"not video").expect("invalid artifact");
        fs::write(&output, b"existing valid output").expect("existing output");
        fs::write(&manifest, b"existing manifest").expect("existing manifest");
        let spec =
            VideoSpec::new(64, 64, FrameRate::new(10, 1).expect("frame rate")).expect("video spec");
        let domain = VideoDomain::new(FrameCount(10), spec);
        let publication =
            PublicationTransaction::new(&output, &manifest).expect("publication transaction");
        stage_export(
            NodeId::new(0),
            &invalid_artifact,
            publication.staged_output(),
            &spec,
            &AudioSpec::default(),
            &domain,
            false,
            RenderPolicy::CURRENT,
            Path::new("ffmpeg"),
            Path::new("ffprobe"),
        )
        .expect_err("export failure");
        assert_eq!(
            fs::read(&output).expect("preserved output"),
            b"existing valid output"
        );
        assert_eq!(
            fs::read(&manifest).expect("preserved manifest"),
            b"existing manifest"
        );
    }
}