Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Clip cuts pick the H.264 encoder this machine's ffmpeg has (raea_io_ffmpeg::h264)

shinyobjectze8e350046652
diff --git a/Cargo.lock b/Cargo.lock
index 5d21833f..19eeabcd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13954,6 +13954,7 @@ name = "raea-app-notes"
version = "0.1.0"
dependencies = [
"raea-core-facts",
+ "raea-io-ffmpeg",
"raea-io-home",
"raea-io-trace",
"serde",
@@ -15867,6 +15868,7 @@ dependencies = [
"dunce",
"fastrand",
"image",
+ "raea-io-ffmpeg",
"raea-sys-columns",
"raea-sys-fingerprint",
"serde",
diff --git a/apps/notes/Cargo.toml b/apps/notes/Cargo.toml
index cee121f8..0e73b5cd 100644
--- a/apps/notes/Cargo.toml
+++ b/apps/notes/Cargo.toml
@@ -10,6 +10,8 @@ description = "A person's notes on a project as files the work is held to (docs/
# Note and Ask, the project found by its .raea/project, and the world a note is an entity in
raea-core-facts = { path = "../../core/facts" }
raea-io-home = { path = "../../io/home" }
+# the H.264 encoder this machine's ffmpeg has, for a clip a person plays (raea_io_ffmpeg::h264)
+raea-io-ffmpeg = { path = "../../io/ffmpeg" }
# when a run of an ask's check was (UTC), as raea check stamps its record
raea-io-trace = { path = "../../io/trace" }
# what each ask has been through, kept as JSON (state.rs)
diff --git a/apps/notes/src/check.rs b/apps/notes/src/check.rs
index 1f22b2f1..f00f1a05 100644
--- a/apps/notes/src/check.rs
+++ b/apps/notes/src/check.rs
@@ -131,7 +131,8 @@ fn cut(out: &Path, [a, b]: [i64; 2], fps: f64, to: &Path) -> R<()> {
}
let (from, len) = (format!("{:.4}", a as f64 / fps), format!("{:.4}", (b - a) as f64 / fps));
let args = ["-hide_banner", "-loglevel", "error", "-y", "-ss", &from, "-i"];
- let o = std::process::Command::new("ffmpeg").args(args).arg(out).args(["-t", &len, "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac"]).arg(to).output().map_err(|e| format!("ffmpeg: {e}"))?;
+ let h264 = raea_io_ffmpeg::h264().ok_or("ffmpeg here has no H.264 encoder (libx264, Windows' h264_mf or VideoToolbox's) to cut the clip with")?;
+ let o = std::process::Command::new("ffmpeg").args(args).arg(out).args(["-t", &len]).args(h264).args(["-c:a", "aac"]).arg(to).output().map_err(|e| format!("ffmpeg: {e}"))?;
if !o.status.success() {
return Err(format!("ffmpeg: {}", String::from_utf8_lossy(&o.stderr).trim()));
}
diff --git a/io/ffmpeg/src/lib.rs b/io/ffmpeg/src/lib.rs
index a6dd3bab..1837ee6c 100644
--- a/io/ffmpeg/src/lib.rs
+++ b/io/ffmpeg/src/lib.rs
@@ -46,6 +46,18 @@ fn says(option: &str) -> String {
String::from_utf8_lossy(&out).into_owned()
}
+/// The H.264 encoder this machine's ffmpeg has, as its arguments, for a clip a person plays (in a browser or on a
+/// phone): libx264 where ffmpeg was built with it (Homebrew's, Linux distributions'), else the system's own (Windows'
+/// Media Foundation, h264_mf, in the LGPL build raea ships there; macOS's VideoToolbox) at 8 Mb/s; None without one.
+pub fn h264() -> Option<Vec<&'static str>> {
+ static ENCODERS: std::sync::OnceLock<String> = std::sync::OnceLock::new();
+ let have = ENCODERS.get_or_init(|| says("-encoders"));
+ let has = |c: &str| have.split_whitespace().any(|w| w == c);
+ let enc = ["libx264", "h264_mf", "h264_videotoolbox"].into_iter().find(|c| has(c))?;
+ let rate: &[&'static str] = if enc == "libx264" { &[] } else { &["-b:v", "8M"] };
+ Some([&["-c:v", enc][..], rate, &["-pix_fmt", "yuv420p"]].concat())
+}
+
/// The facts, the presentation times, whether the colour matrix is untagged, and the decoder a VP8 or VP9 stream with
/// alpha needs (libvpx: ffmpeg's own VP9 decoder drops the alpha WebM keeps beside the picture, #113).
fn probe(path: &Path) -> R<(Info, Vec<f64>, bool, Option<&'static str>)> {
diff --git a/systems/fingerprint-use/Cargo.toml b/systems/fingerprint-use/Cargo.toml
index 26abefb4..64ab33cd 100644
--- a/systems/fingerprint-use/Cargo.toml
+++ b/systems/fingerprint-use/Cargo.toml
@@ -10,6 +10,8 @@ description = "Using a fingerprint (docs/fingerprint.feature FP3, FP6e, FP8b, FP
dunce = "1"
# the fingerprint: its taxonomy, folder, evidence, heads and words
raea-sys-fingerprint = { path = "../fingerprint" }
+# the H.264 encoder this machine's ffmpeg has, for a clip a person plays (raea_io_ffmpeg::h264)
+raea-io-ffmpeg = { path = "../../io/ffmpeg" }
# a piece's columns, and the frames and sound the sheet draws
raea-sys-columns = { path = "../columns" }
serde = { version = "1", features = ["derive"] }
diff --git a/systems/fingerprint-use/src/postmortem.rs b/systems/fingerprint-use/src/postmortem.rs
index 1a6ffcc9..76b19e59 100644
--- a/systems/fingerprint-use/src/postmortem.rs
+++ b/systems/fingerprint-use/src/postmortem.rs
@@ -70,10 +70,12 @@ pub fn picks(scored: &[Scored], trained: &Trained, fp: &Fingerprint, l: &Ledger,
/// The moment cut to a clip to play beside its question (ffmpeg), else None and the whole piece plays
pub fn clip(x: &Ask, out: &Path) -> Option<PathBuf> {
std::fs::create_dir_all(out.parent()?).ok()?;
+ let h264 = raea_io_ffmpeg::h264()?;
let ok = std::process::Command::new("ffmpeg")
.args(["-y", "-loglevel", "error", "-ss", &format!("{:.2}", (x.a - 0.5).max(0.0)), "-t", &format!("{:.2}", x.b - x.a + 1.0), "-i"])
.arg(&x.file)
- .args(["-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac"])
+ .args(h264)
+ .args(["-c:a", "aac"])
.arg(out)
.status()
.is_ok_and(|s| s.success());