Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions ai/oak-ai-tools/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,46 @@ pub trait EngineExt: EngineBackend {
"this backend has no `sequence.set_format` support",
))
}

/// `analysis.detect_silence` — ffmpeg silencedetect over `src`.
///
/// The default body runs the real `ffmpeg` process (the analysis tools
/// are file-level reads, not engine state), so mock and SDK backends
/// both get them; a machine without ffmpeg answers the structured
/// `GAP_ANALYSIS_FFMPEG` gap instead of a fabricated success.
fn analysis_detect_silence(
&self,
parsed: &crate::tools::analysis::DetectSilence,
) -> Result<Value, ToolError> {
crate::tools::analysis_exec::detect_silence(parsed)
}

/// `analysis.detect_scenes` — ffmpeg scdet over `src`.
fn analysis_detect_scenes(
&self,
parsed: &crate::tools::analysis::DetectScenes,
) -> Result<Value, ToolError> {
crate::tools::analysis_exec::detect_scenes(parsed)
}

/// `analysis.detect_black` — ffmpeg blackdetect over `src`.
fn analysis_detect_black(
&self,
parsed: &crate::tools::analysis::DetectBlack,
) -> Result<Value, ToolError> {
crate::tools::analysis_exec::detect_black(parsed)
}

/// `analysis.loudness` — ffmpeg ebur128 over `src`.
fn analysis_loudness(&self, parsed: &crate::tools::analysis::Loudness) -> Result<Value, ToolError> {
crate::tools::analysis_exec::loudness(parsed)
}

/// `analysis.export_thumbnails` — ffmpeg frame dump into `outdir`.
fn analysis_export_thumbnails(
&self,
parsed: &crate::tools::analysis::ExportThumbnails,
) -> Result<Value, ToolError> {
crate::tools::analysis_exec::export_thumbnails(parsed)
}
}
43 changes: 42 additions & 1 deletion ai/oak-ai-tools/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ pub struct V2ToolDef {

/// The v2 registry: parse, then forward to the extension method.
pub fn v2_registry() -> &'static [V2ToolDef] {
static REGISTRY: [V2ToolDef; 2] = [
static REGISTRY: [V2ToolDef; 7] = [
V2ToolDef {
name: "sequence.get_settings",
transactional: true,
Expand All @@ -358,6 +358,47 @@ pub fn v2_registry() -> &'static [V2ToolDef] {
backend.sequence_set_format(args.clone())
},
},
V2ToolDef {
name: "analysis.detect_silence",
// Pure read: no project state, no undo row → not transactional.
transactional: false,
call: |backend, args| {
let parsed = tools::analysis::parse_detect_silence(args)?;
backend.analysis_detect_silence(&parsed)
},
},
V2ToolDef {
name: "analysis.detect_scenes",
transactional: false,
call: |backend, args| {
let parsed = tools::analysis::parse_detect_scenes(args)?;
backend.analysis_detect_scenes(&parsed)
},
},
V2ToolDef {
name: "analysis.detect_black",
transactional: false,
call: |backend, args| {
let parsed = tools::analysis::parse_detect_black(args)?;
backend.analysis_detect_black(&parsed)
},
},
V2ToolDef {
name: "analysis.loudness",
transactional: false,
call: |backend, args| {
let parsed = tools::analysis::parse_loudness(args)?;
backend.analysis_loudness(&parsed)
},
},
V2ToolDef {
name: "analysis.export_thumbnails",
transactional: false,
call: |backend, args| {
let parsed = tools::analysis::parse_export_thumbnails(args)?;
backend.analysis_export_thumbnails(&parsed)
},
},
];

&REGISTRY
Expand Down
199 changes: 199 additions & 0 deletions ai/oak-ai-tools/src/ffmpeg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
//! Minimal `ffmpeg` process wrapper: one invocation per call, every
//! argument passed individually through [`std::process::Command::arg`]
//! (never through a shell — the tool face runs on paths a model
//! supplied, so a string command line would be a command-injection hole).
//!
//! The five analysis filters are parsed from the raw `stderr`/`stdout`
//! ffmpeg prints; this module only *runs* and *classifies*, the parsers
//! live in [`crate::tools::analysis`].

use std::path::Path;
use std::process::{Command, Stdio};

use serde_json::json;

use crate::error::ToolError;

/// The gap id reported when `ffmpeg` is absent from `PATH`.
pub const GAP_FFMPEG_MISSING: &str = "GAP_ANALYSIS_FFMPEG";

/// True when an `ffmpeg -version` probe succeeds on this machine.
pub fn ffmpeg_available() -> bool {
Command::new("ffmpeg")
.arg("-version")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|status| status.success())
.unwrap_or(false)
}

/// The error every analysis tool returns when ffmpeg cannot be spawned:
/// a *structured* capability gap, never a fabricated success.
pub fn missing_ffmpeg() -> ToolError {
ToolError::unsupported(
GAP_FFMPEG_MISSING,
"ffmpeg was not found on PATH; install ffmpeg or expose it to the process \
to use the analysis.* tools",
)
}

/// Input-file gate: exists, is a regular file, is readable.
pub fn check_input(src: &str) -> Result<(), ToolError> {
let path = Path::new(src);
if !path.exists() {
return Err(ToolError::not_found(format!("input file not found: {src}")));
}
if !path.is_file() {
return Err(ToolError::invalid_params(format!(
"input is not a regular file: {src}"
)));
}
if !path.metadata().map(|m| m.len() > 0).unwrap_or(false) {
return Err(ToolError::invalid_params(format!(
"input file is empty: {src}"
)));
}
Ok(())
}

/// Captured result of one ffmpeg run: exit status, stdout, stderr.
pub struct Capture {
/// Process exit code (`None` when killed by a signal).
pub code: Option<i32>,
/// Everything ffmpeg wrote to stdout.
pub out: String,
/// Everything ffmpeg wrote to stderr (the log channel the filters log to).
pub err: String,
}

/// Runs `ffmpeg` with `args` (each passed as one argv entry, no shell) and
/// captures both streams.
///
/// A spawn failure is either the missing-binary gap (spawn error on
/// `ffmpeg`) or an IO error; a non-zero exit is *not* turned into an error
/// here — callers classify the code together with the captured text
/// (ffmpeg's exit codes are wide: 254 = no such input, 251 = output
/// open failed, 234 = no stream for the filtergraph).
pub fn run(args: &[&str]) -> Result<Capture, ToolError> {
let mut cmd = Command::new("ffmpeg");
// Never `-c`: argv is built entry by entry, paths with spaces or
// shell metacharacters stay inert data.
for arg in args {
cmd.arg(arg);
}
let output = cmd
.stdin(Stdio::null())
.output()
.map_err(|err| {
if err.kind() == std::io::ErrorKind::NotFound {
missing_ffmpeg()
} else {
ToolError::new(
crate::error::CODE_IO,
format!("failed to spawn ffmpeg: {err}"),
)
}
})?;
Ok(Capture {
code: output.status.code(),
out: String::from_utf8_lossy(&output.stdout).into_owned(),
err: String::from_utf8_lossy(&output.stderr).into_owned(),
})
}

/// Runs ffmpeg and requires a zero exit; on failure the message carries the
/// code plus the last stderr lines so the model can act on it.
pub fn run_checked(args: &[&str], tool: &str) -> Result<Capture, ToolError> {
let cap = run(args)?;
if cap.code == Some(0) {
return Ok(cap);
}
Err(ToolError::new(
crate::error::CODE_BACKEND,
format!(
"ffmpeg failed with exit code {:?} for '{tool}': {}",
cap.code,
tail(&cap.err, 6)
),
))
.map_err(|e| e.with_tool(tool))
}

/// Last `n` non-empty lines of `text`, joined — the part of ffmpeg's log
/// that names the actual failure.
pub fn tail(text: &str, n: usize) -> String {
let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
let start = lines.len().saturating_sub(n);
lines[start..].join(" | ")
}

/// Structured "ffmpeg ran but produced nothing for this filter" — raised
/// when a filter that must emit markers printed none *and* the run cannot
/// be explained by "this input has no such stream" (checked by the caller
/// against the input's streams).
pub fn no_markers(tool: &str, detail: &str) -> ToolError {
ToolError::new(
crate::error::CODE_BACKEND,
format!("'{tool}' found no markers: {detail}"),
)
.with_tool(tool)
}

/// Input stream probe via `ffprobe` — used to give an honest error when a
/// filter found nothing because the stream type is absent (a silent clip
/// really has no silence; a video with no audio track has no loudness).
pub fn probe_has_audio(src: &str) -> Result<bool, ToolError> {
probe_stream(src, "a")
}

/// Same probe for a video stream.
pub fn probe_has_video(src: &str) -> Result<bool, ToolError> {
probe_stream(src, "v")
}

fn probe_stream(src: &str, kind: &str) -> Result<bool, ToolError> {
let output = Command::new("ffprobe")
.args([
"-v",
"error",
"-select_streams",
kind,
"-show_entries",
"stream=index",
"-of",
"csv=p=0",
src,
])
.stdin(Stdio::null())
.output()
.map_err(|err| {
if err.kind() == std::io::ErrorKind::NotFound {
missing_ffmpeg()
} else {
ToolError::new(
crate::error::CODE_IO,
format!("failed to spawn ffprobe: {err}"),
)
}
})?;
let code = output.status.code();
if code != Some(0) {
// ffprobe exits 1 on a missing file; the input gate already ran,
// so this is an unreadable/corrupt input.
return Err(ToolError::new(
crate::error::CODE_IO,
format!(
"ffprobe failed for '{src}': {}",
tail(&String::from_utf8_lossy(&output.stderr), 3)
),
));
}
Ok(!String::from_utf8_lossy(&output.stdout).trim().is_empty())
}

/// Small helper so failure payloads can name the tool uniformly.
pub fn error_json(err: &ToolError) -> serde_json::Value {
json!({ "code": err.code, "message": err.message })
}
7 changes: 6 additions & 1 deletion ai/oak-ai-tools/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,14 @@ pub const TOOL_CLASSES: [(&str, OperationClass); 22] = [
/// The v2 face's grading table (T9.2). Kept separate from [`TOOL_CLASSES`]
/// so the frozen 22-tool table stays byte-identical; [`class_of_v2`]
/// resolves a v2 name, and the dispatcher tries it only for v2 tools.
pub const V2_TOOL_CLASSES: [(&str, OperationClass); 2] = [
pub const V2_TOOL_CLASSES: [(&str, OperationClass); 7] = [
("sequence.get_settings", OperationClass::Read),
("sequence.set_format", OperationClass::Edit),
("analysis.detect_silence", OperationClass::Read),
("analysis.detect_scenes", OperationClass::Read),
("analysis.detect_black", OperationClass::Read),
("analysis.loudness", OperationClass::Read),
("analysis.export_thumbnails", OperationClass::Read),
];

/// The grading of a v2 `tool`, or `None` when the v2 table has no entry.
Expand Down
1 change: 1 addition & 0 deletions ai/oak-ai-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
pub mod backend;
pub mod dispatch;
pub mod error;
pub mod ffmpeg;
pub mod hooks;
pub mod mock;
pub mod render;
Expand Down
Loading
Loading