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.

raea media share FILE -o OUT.mp4: a copy to send, held to a bitrate (2600 kbps unless --kbps), and raea ffmpeg reads the skill's -maxrate line as it (#219)

shinyobjectzc0d94f8f0cb1
diff --git a/apps/media/src/encode.rs b/apps/media/src/encode.rs
index 3ce5f1f3..ce15ce36 100644
--- a/apps/media/src/encode.rs
+++ b/apps/media/src/encode.rs
@@ -85,4 +85,14 @@ pub fn encode(srcs: &[(PathBuf, Option<f64>, Option<f64>)], height: Option<u32>,
Ok(json!({"wrote": out, "frames": frames, "seconds": round3(frames as f64 / fps), "width": w, "height": h, "fps": round3(fps), "kbps": kbps, "bytes": stats.bytes, "sound": any_sound}))
}
+/// `media share`: a copy any phone, chat and upload form takes (H.264 and AAC in an MP4, its index first), held to
+/// `cap` kbit/s (2600: a 1080p minute in about 20 MB) and never given more than a full-size copy would get (#219).
+pub fn share(file: &Path, height: Option<u32>, out: &Path, cap: u32) -> R<Value> {
+ std::fs::metadata(file).map_err(|e| format!("{}: {e}", file.display()))?; // the platform's decoder panics on no path
+ let i = backend()?.open_video(file).map_err(|e| format!("{}: {e}", file.display()))?.info().clone();
+ let k = height.map_or(1.0, |h| (h as f64 / i.height as f64).min(1.0));
+ let full = (i.width as f64 * k * i.height as f64 * k * standard_fps(i.fps) * 0.1 / 1000.0).max(300.0) as u32;
+ encode(&[(file.to_path_buf(), None, None)], height, out, Some(cap.min(full)))
+}
+
// ---- ffmpeg and ffprobe, read
diff --git a/apps/media/src/ff.rs b/apps/media/src/ff.rs
index 4e5cf69f..2f42c3f5 100644
--- a/apps/media/src/ff.rs
+++ b/apps/media/src/ff.rs
@@ -38,6 +38,8 @@ pub(crate) fn ffmpeg(args: &[String]) -> R<()> {
let mut pending = FfInput::default();
let (mut ss, mut t, mut to) = (None, None, None);
let (mut vf, mut af): (Option<String>, Option<String>) = (None, None);
+ // -maxrate: the rate the copy is held to, the one quality choice read (the skill's share copy, #219)
+ let mut cap = None::<u32>;
let (mut ar, mut ac, mut fmt, mut one_frame, mut outp) = (None, None, None, false, None::<String>);
let mut i = 0;
let next = |i: &mut usize| -> R<String> {
@@ -76,11 +78,19 @@ pub(crate) fn ffmpeg(args: &[String]) -> R<()> {
"-ar" => ar = Some(next(&mut i)?.parse::<u32>().map_err(|_| "-ar takes a rate")?),
"-ac" => ac = Some(next(&mut i)?.parse::<u32>().map_err(|_| "-ac takes a channel count")?),
"-frames:v" | "-vframes" => one_frame = next(&mut i)? == "1",
+ "-maxrate" => {
+ let v = next(&mut i)?.to_lowercase();
+ let (n, k) = match v.strip_suffix('k').map(|n| (n, 1.0)).or(v.strip_suffix('m').map(|n| (n, 1000.0))) {
+ Some(nk) => nk,
+ None => (v.as_str(), 0.001),
+ };
+ cap = Some((n.parse::<f64>().map_err(|_| "-maxrate takes a rate (2600k)")? * k).round().max(1.0) as u32);
+ }
"-filter_complex" | "-lavfi" => {
return Err(format!("-filter_complex isn't read here: overlays, wipes and layouts are a composition, rendered with raea render. Or {VERBS_HINT}"));
}
// codec, quality and container choices: the platform encoder decides (H.264 + AAC in MP4)
- "-c" | "-c:v" | "-c:a" | "-codec" | "-vcodec" | "-acodec" | "-crf" | "-preset" | "-b:v" | "-b:a" | "-q:v" | "-qscale:v" | "-pix_fmt" | "-movflags" | "-v" | "-loglevel" | "-r" | "-safe" | "-map" | "-tune" | "-profile:v" | "-g" | "-threads" => {
+ "-c" | "-c:v" | "-c:a" | "-codec" | "-vcodec" | "-acodec" | "-crf" | "-preset" | "-b:v" | "-b:a" | "-q:v" | "-qscale:v" | "-pix_fmt" | "-movflags" | "-v" | "-loglevel" | "-r" | "-safe" | "-map" | "-tune" | "-profile:v" | "-g" | "-threads" | "-bufsize" => {
next(&mut i)?;
}
"-y" | "-n" | "-hide_banner" | "-nostats" | "-nostdin" | "-an" | "-vn" | "-sn" | "-shortest" | "-accurate_seek" => {}
@@ -194,7 +204,10 @@ pub(crate) fn ffmpeg(args: &[String]) -> R<()> {
} else {
vec![(path.clone(), from, until)]
};
- encode(&srcs, height, Path::new(&out), None)?;
+ match (cap, srcs.as_slice()) {
+ (Some(cap), [(one, None, None)]) => share(one, height, Path::new(&out), cap)?,
+ (cap, _) => encode(&srcs, height, Path::new(&out), cap)?,
+ };
Ok(())
}
diff --git a/apps/media/src/lib.rs b/apps/media/src/lib.rs
index c2aad340..33e20562 100644
--- a/apps/media/src/lib.rs
+++ b/apps/media/src/lib.rs
@@ -11,6 +11,7 @@
//! raea media cuts FILE [--threshold 0.08] shot changes (ffmpeg's scene score)
//! raea media freezes FILE [--noise 0.001] [--min 2] held pictures (ffmpeg's freezedetect)
//! raea media proxy FILE [--height 720] -o OUT.mp4 a smaller copy
+//! raea media share FILE [--kbps 2600] [--height H] -o OUT.mp4 a copy to send: any player takes it, held to a bitrate
//! raea media lod MODEL.glb [--level 1] [--texture 1024] -o OUT.glb a model at a lower level of detail (triangles halved a level)
//! raea media trim FILE --from S --to S -o OUT.mp4 a stretch, frame-exact (re-encoded)
//! raea media concat A B ... -o OUT.mp4 one after another (same aspect)
@@ -118,11 +119,11 @@ fn num(fl: &BTreeMap<String, String>, k: &str) -> R<Option<f64>> {
}
/// The verbs `raea media` answers here.
-pub const VERBS: &[&str] = &["probe", "still", "sheet", "audio", "loudness", "motion", "cuts", "freezes", "proxy", "lod", "trim", "concat", "encode", "help"];
+pub const VERBS: &[&str] = &["probe", "still", "sheet", "audio", "loudness", "motion", "cuts", "freezes", "proxy", "share", "lod", "trim", "concat", "encode", "help"];
pub fn cli(args: &[String]) -> i32 {
let Some(verb) = args.first() else {
- println!("usage: raea media <probe|still|sheet|audio|loudness|motion|cuts|proxy|lod|trim|concat|encode> FILE ... (raea media help)");
+ println!("usage: raea media <probe|still|sheet|audio|loudness|motion|cuts|proxy|share|lod|trim|concat|encode> FILE ... (raea media help)");
return 2;
};
let (pos, fl) = parse(&args[1..]);
@@ -198,6 +199,10 @@ fn run(verb: &str, pos: &[String], fl: &BTreeMap<String, String>) -> R<Option<Va
let h = fl.get("height").and_then(|h| h.parse().ok()).unwrap_or(720);
encode(&[(file()?, None, None)], Some(h), &out()?, fl.get("kbps").and_then(|k| k.parse().ok())).map(Some)
}
+ "share" => {
+ let cap = fl.get("kbps").and_then(|k| k.parse().ok()).unwrap_or(2600);
+ share(&file()?, fl.get("height").and_then(|h| h.parse().ok()), &out()?, cap).map(Some)
+ }
"trim" => {
let (from, to) = range()?;
if from.is_none() && to.is_none() {
@@ -221,6 +226,6 @@ fn run(verb: &str, pos: &[String], fl: &BTreeMap<String, String>) -> R<Option<Va
println!("{}", include_str!("lib.rs").lines().take_while(|l| l.starts_with("//!")).map(|l| l.trim_start_matches("//!").strip_prefix(' ').unwrap_or("")).collect::<Vec<_>>().join("\n"));
Ok(None)
}
- v => Err(format!("no media verb {v:?}: probe, still, sheet, audio, loudness, motion, cuts, freezes, proxy, trim, concat, encode")),
+ v => Err(format!("no media verb {v:?}: probe, still, sheet, audio, loudness, motion, cuts, freezes, proxy, share, trim, concat, encode")),
}
}
diff --git a/docs/manual/reference/cli/media.md b/docs/manual/reference/cli/media.md
index f67b9ce3..fa4810ff 100644
--- a/docs/manual/reference/cli/media.md
+++ b/docs/manual/reference/cli/media.md
@@ -39,6 +39,7 @@ What agents used ffmpeg and ffprobe for, on raea-media (the platform's decoders
raea media cuts FILE [--threshold 0.08] shot changes (ffmpeg's scene score)
raea media freezes FILE [--noise 0.001] [--min 2] held pictures (ffmpeg's freezedetect)
raea media proxy FILE [--height 720] -o OUT.mp4 a smaller copy
+ raea media share FILE [--kbps 2600] [--height H] -o OUT.mp4 a copy to send: any player takes it, held to a bitrate
raea media lod MODEL.glb [--level 1] [--texture 1024] -o OUT.glb a model at a lower level of detail (triangles halved a level)
raea media trim FILE --from S --to S -o OUT.mp4 a stretch, frame-exact (re-encoded)
raea media concat A B ... -o OUT.mp4 one after another (same aspect)