mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-06 08:46:06 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca3b2400a4 |
No files matched your search
+33
-21
@@ -50,14 +50,40 @@ fn says(option: &str) -> String {
|
|||||||
/// phone): libx264 where ffmpeg was built with it (Homebrew's, Linux distributions'), else the system's own (Windows'
|
/// 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.
|
/// 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>> {
|
pub fn h264() -> Option<Vec<&'static str>> {
|
||||||
static ENCODERS: std::sync::OnceLock<String> = std::sync::OnceLock::new();
|
let enc = listed()?;
|
||||||
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"] };
|
let rate: &[&'static str] = if enc == "libx264" { &[] } else { &["-b:v", "8M"] };
|
||||||
Some([&["-c:v", enc][..], rate, &["-pix_fmt", "yuv420p"]].concat())
|
Some([&["-c:v", enc][..], rate, &["-pix_fmt", "yuv420p"]].concat())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The first of libx264, Media Foundation's and VideoToolbox's H.264 encoders this machine's ffmpeg lists
|
||||||
|
fn listed() -> Option<&'static str> {
|
||||||
|
["libx264", "h264_mf", "h264_videotoolbox"].into_iter().find(|c| lists("-encoders", c))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `codec` is among what this machine's ffmpeg prints for `option` (-encoders, -decoders), each asked once
|
||||||
|
fn lists(option: &'static str, codec: &str) -> bool {
|
||||||
|
static PRINTED: std::sync::OnceLock<std::sync::Mutex<std::collections::HashMap<&'static str, String>>> = std::sync::OnceLock::new();
|
||||||
|
let mut printed = PRINTED.get_or_init(Default::default).lock().unwrap_or_else(|e| e.into_inner());
|
||||||
|
printed.entry(option).or_insert_with(|| says(option)).split_whitespace().any(|w| w == codec)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A render's encoder and its rate, as ffmpeg's arguments: NVENC and libx264 at a constant quality when one is asked
|
||||||
|
/// (0.85 -> 13, 0.95 -> 11, 0.6 -> 18), else the average bitrate. The system's own encoders (h264_mf, h264_videotoolbox)
|
||||||
|
/// have no CRF, so they take the bitrate either way: the LGPL ffmpeg raea ships on Windows has no libx264, and
|
||||||
|
/// RAEA_MEDIA=ffmpeg failed there on `-c:v libx264` (#279)
|
||||||
|
fn video_args(enc: &str, o: &EncodeOpts) -> Vec<String> {
|
||||||
|
let rate = || vec!["-b:v".to_string(), format!("{}k", o.kbps), "-maxrate".to_string(), format!("{}k", o.kbps * 3 / 2), "-bufsize".to_string(), format!("{}k", o.kbps * 2)];
|
||||||
|
let q = |q: f32| format!("{:.0}", 30.0 - 20.0 * q.clamp(0.0, 1.0));
|
||||||
|
let (nvenc, x264) = (NVENC.map(String::from).to_vec(), ["-c:v", "libx264", "-preset", "medium"].map(String::from).to_vec());
|
||||||
|
match (enc, o.quality) {
|
||||||
|
("h264_nvenc", Some(v)) => [nvenc, vec!["-rc".into(), "constqp".into(), "-qp".into(), q(v)]].concat(),
|
||||||
|
("h264_nvenc", None) => [nvenc, rate()].concat(),
|
||||||
|
("libx264", Some(v)) => [x264, vec!["-crf".into(), q(v)]].concat(),
|
||||||
|
("libx264", None) => [x264, rate()].concat(),
|
||||||
|
_ => [vec!["-c:v".into(), enc.to_string()], rate()].concat(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// RAEA_NV=1 (the render farm sets it on a GPU's runs of a piece with 3D in it, docs/cloud-render.feature CR34): NVIDIA's
|
/// RAEA_NV=1 (the render farm sets it on a GPU's runs of a piece with 3D in it, docs/cloud-render.feature CR34): NVIDIA's
|
||||||
/// encoder (`enc`, NVENC) or decoder (NVDEC, through CUDA) in place of libx264 and ffmpeg's own, each only once a
|
/// encoder (`enc`, NVENC) or decoder (NVDEC, through CUDA) in place of libx264 and ffmpeg's own, each only once a
|
||||||
/// one-frame try of it worked on this machine (a GPU without the driver's video libraries keeps libx264, said once).
|
/// one-frame try of it worked on this machine (a GPU without the driver's video libraries keeps libx264, said once).
|
||||||
@@ -154,9 +180,7 @@ fn probe(path: &Path) -> R<(Info, Vec<f64>, bool, Option<&'static str>)> {
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
// an ffmpeg built without libvpx decodes the picture alone, as before
|
// an ffmpeg built without libvpx decodes the picture alone, as before
|
||||||
static DECODERS: std::sync::OnceLock<String> = std::sync::OnceLock::new();
|
let alpha = alpha.filter(|c| lists("-decoders", c));
|
||||||
let have = DECODERS.get_or_init(|| says("-decoders"));
|
|
||||||
let alpha = alpha.filter(|c| have.split_whitespace().any(|w| w == *c));
|
|
||||||
Ok((
|
Ok((
|
||||||
Info {
|
Info {
|
||||||
codec: s["codec_name"].as_str().unwrap_or("?").into(),
|
codec: s["codec_name"].as_str().unwrap_or("?").into(),
|
||||||
@@ -400,6 +424,7 @@ impl Backend for Ffmpeg {
|
|||||||
}
|
}
|
||||||
fn encoder(&self, out: &Path, o: &EncodeOpts) -> R<Box<dyn VideoEncoder>> {
|
fn encoder(&self, out: &Path, o: &EncodeOpts) -> R<Box<dyn VideoEncoder>> {
|
||||||
let video = out.with_extension("video.mp4");
|
let video = out.with_extension("video.mp4");
|
||||||
|
let enc = if nv(true) { "h264_nvenc" } else { listed().ok_or("this machine's ffmpeg has no H.264 encoder (libx264, h264_mf or h264_videotoolbox)")? };
|
||||||
let child = cli("ffmpeg", false)?
|
let child = cli("ffmpeg", false)?
|
||||||
.args([
|
.args([
|
||||||
"-v",
|
"-v",
|
||||||
@@ -416,20 +441,7 @@ impl Backend for Ffmpeg {
|
|||||||
"-i",
|
"-i",
|
||||||
"-",
|
"-",
|
||||||
])
|
])
|
||||||
.args(if nv(true) { &NVENC[..] } else { &["-c:v", "libx264", "-preset", "medium"][..] })
|
.args(video_args(enc, o))
|
||||||
// constant quality as CRF (0.85 -> 13, 0.95 -> 11, 0.6 -> 18), else the average bitrate
|
|
||||||
.args(match o.quality {
|
|
||||||
Some(q) if nv(true) => vec!["-rc".to_string(), "constqp".to_string(), "-qp".to_string(), format!("{:.0}", 30.0 - 20.0 * q.clamp(0.0, 1.0))],
|
|
||||||
Some(q) => vec!["-crf".to_string(), format!("{:.0}", 30.0 - 20.0 * q.clamp(0.0, 1.0))],
|
|
||||||
None => vec![
|
|
||||||
"-b:v".to_string(),
|
|
||||||
format!("{}k", o.kbps),
|
|
||||||
"-maxrate".to_string(),
|
|
||||||
format!("{}k", o.kbps * 3 / 2),
|
|
||||||
"-bufsize".to_string(),
|
|
||||||
format!("{}k", o.kbps * 2),
|
|
||||||
],
|
|
||||||
})
|
|
||||||
.args([
|
.args([
|
||||||
"-pix_fmt",
|
"-pix_fmt",
|
||||||
"yuv420p",
|
"yuv420p",
|
||||||
|
|||||||
@@ -47,3 +47,21 @@ fn nv_falls_back_where_nvidia_isnt() {
|
|||||||
assert!(!nv(true) && !nv(false), "off without RAEA_NV=1");
|
assert!(!nv(true) && !nv(false), "off without RAEA_NV=1");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// libx264's arguments are what they were, and the system's encoder (macOS's here, Windows' Media Foundation the same
|
||||||
|
/// way) takes a bitrate even when a quality is asked, and encodes with them (#279)
|
||||||
|
#[test]
|
||||||
|
fn a_system_encoder_takes_the_bitrate() {
|
||||||
|
let o = |quality| EncodeOpts { width: 256, height: 256, fps: 30.0, kbps: 900, quality, nv12: false, realtime: false };
|
||||||
|
assert_eq!(video_args("libx264", &o(Some(0.85))), ["-c:v", "libx264", "-preset", "medium", "-crf", "13"]);
|
||||||
|
assert_eq!(video_args("libx264", &o(None)), ["-c:v", "libx264", "-preset", "medium", "-b:v", "900k", "-maxrate", "1350k", "-bufsize", "1800k"]);
|
||||||
|
let args = video_args("h264_videotoolbox", &o(Some(0.85)));
|
||||||
|
assert_eq!(args, ["-c:v", "h264_videotoolbox", "-b:v", "900k", "-maxrate", "1350k", "-bufsize", "1800k"]);
|
||||||
|
if !lists("-encoders", "h264_videotoolbox") {
|
||||||
|
return eprintln!("skipped the encode: no h264_videotoolbox in this ffmpeg");
|
||||||
|
}
|
||||||
|
let out = std::env::temp_dir().join(format!("raea-ffmpeg-system-{}.mp4", std::process::id()));
|
||||||
|
let ok = Command::new("ffmpeg").args(["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=red:s=256x256:d=0.3"]).args(&args).args(["-pix_fmt", "yuv420p"]).arg(&out).status().is_ok_and(|s| s.success());
|
||||||
|
assert!(ok && std::fs::metadata(&out).is_ok_and(|m| m.len() > 500), "ffmpeg encodes with the system encoder's arguments");
|
||||||
|
let _ = std::fs::remove_file(&out);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user