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.

A VP9 WebM keeps its alpha after ffmpeg -c copy: the muxer writes the tag as ALPHA_MODE, and only alpha_mode was read (#224)

shinyobjectz477b14a0aec4
diff --git a/io/ffmpeg/src/lib.rs b/io/ffmpeg/src/lib.rs
index 1837ee6c..cc7a342d 100644
--- a/io/ffmpeg/src/lib.rs
+++ b/io/ffmpeg/src/lib.rs
@@ -108,7 +108,10 @@ fn probe(path: &Path) -> R<(Info, Vec<f64>, bool, Option<&'static str>)> {
.and_then(|d| d.parse().ok())
.unwrap_or(0.0);
let untagged = !matches!(s["color_space"].as_str(), Some(c) if c != "unknown");
- let alpha = match (s["codec_name"].as_str(), s["tags"]["alpha_mode"].as_str()) {
+ // the tag in either case: ffmpeg's muxer writes ALPHA_MODE when it copies a stream that has alpha, and a file
+ // remuxed with -c copy drew opaque (#224)
+ let tagged = s["tags"].as_object().and_then(|t| t.iter().find(|(k, _)| k.eq_ignore_ascii_case("alpha_mode"))).and_then(|(_, v)| v.as_str());
+ let alpha = match (s["codec_name"].as_str(), tagged) {
(Some("vp8"), Some("1")) => Some("libvpx"),
(Some("vp9"), Some("1")) => Some("libvpx-vp9"),
_ => None,
diff --git a/io/ffmpeg/src/tests.rs b/io/ffmpeg/src/tests.rs
index f06d1056..9df8b8c6 100644
--- a/io/ffmpeg/src/tests.rs
+++ b/io/ffmpeg/src/tests.rs
@@ -18,11 +18,17 @@ fn vp9_webm_keeps_its_alpha() {
let dir = std::env::temp_dir().join(format!("raea-ffmpeg-alpha-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let Some(webm) = alpha_webm(&dir) else { return eprintln!("skipped: no ffmpeg with libvpx-vp9 here") };
- let mut d = Ffmpeg.open_video(&webm).expect("the ffmpeg backend opens a VP9 WebM");
- for t in [0.0, 0.2] {
- let f = d.frame_at(t).expect("a frame");
- let at = |x: usize, y: usize| f.rgba[(y * f.width as usize + x) * 4 + 3];
- assert_eq!((at(2, 2), at(32, 32)), (0, 255), "at {t} s: outside the square is transparent, inside opaque");
+ // as made, and copied by ffmpeg's muxer, which writes the tag as ALPHA_MODE (#224)
+ let copy = dir.join("copy.webm");
+ let copied = Command::new("ffmpeg").args(["-v", "error", "-y", "-i"]).arg(&webm).args(["-c", "copy"]).arg(&copy).status().is_ok_and(|s| s.success());
+ assert!(copied, "ffmpeg copies the WebM");
+ for file in [&webm, &copy] {
+ let mut d = Ffmpeg.open_video(file).expect("the ffmpeg backend opens a VP9 WebM");
+ for t in [0.0, 0.2] {
+ let f = d.frame_at(t).expect("a frame");
+ let at = |x: usize, y: usize| f.rgba[(y * f.width as usize + x) * 4 + 3];
+ assert_eq!((at(2, 2), at(32, 32)), (0, 255), "{} at {t} s: outside the square is transparent, inside opaque", file.display());
+ }
}
let _ = std::fs::remove_dir_all(&dir);
}