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.

check: a freeze that runs to the end of the file is found

James Dimachkiea819751fb5dc
diff --git a/apps/check/src/lib.rs b/apps/check/src/lib.rs
index 020eb6f0..fa62d0fc 100644
--- a/apps/check/src/lib.rs
+++ b/apps/check/src/lib.rs
@@ -176,22 +176,39 @@ struct Probe {
w: f64,
h: f64,
duration: f64,
+ /// the picture's own length: sound can run on past it, and the container's duration counts that
+ picture: f64,
audio: bool,
}
+/// Where the picture's last packet ends, for a container that doesn't state the video stream's own length (Matroska)
+fn last_packet_end(video: &str) -> Option<f64> {
+ let out = Command::new("ffprobe").args(["-v", "error", "-select_streams", "v:0", "-show_entries", "packet=pts_time,duration_time", "-of", "csv=p=0", video]).output().ok()?;
+ String::from_utf8_lossy(&out.stdout)
+ .lines()
+ .filter_map(|l| {
+ let mut parts = l.split(',');
+ let t: f64 = parts.next()?.trim().parse().ok()?;
+ Some(t + parts.next().and_then(|d| d.trim().parse::<f64>().ok()).unwrap_or(0.0))
+ })
+ .max_by(f64::total_cmp)
+}
+
impl Probe {
fn of(video: &str) -> R<Probe> {
let out = Command::new("ffprobe")
- .args(["-v", "error", "-show_entries", "stream=codec_type,width,height,r_frame_rate:format=duration", "-of", "json", video])
+ .args(["-v", "error", "-show_entries", "stream=codec_type,width,height,r_frame_rate,duration:format=duration", "-of", "json", video])
.output()
.map_err(|e| format!("ffprobe: {e}"))?;
let v: Value = serde_json::from_slice(&out.stdout).map_err(|e| format!("ffprobe: {e}"))?;
let streams = v["streams"].as_array().cloned().unwrap_or_default();
let vid = streams.iter().find(|s| s["codec_type"] == "video").ok_or("no video stream")?;
+ let duration = v["format"]["duration"].as_str().and_then(|d| d.parse().ok()).unwrap_or(0.0);
Ok(Probe {
w: vid["width"].as_f64().unwrap_or(1920.0),
h: vid["height"].as_f64().unwrap_or(1080.0),
- duration: v["format"]["duration"].as_str().and_then(|d| d.parse().ok()).unwrap_or(0.0),
+ duration,
+ picture: vid["duration"].as_str().and_then(|d| d.parse().ok()).or_else(|| last_packet_end(video)).unwrap_or(duration),
audio: streams.iter().any(|s| s["codec_type"] == "audio"),
})
}
diff --git a/apps/check/src/sound.rs b/apps/check/src/sound.rs
index 8c7e61f2..d47d8592 100644
--- a/apps/check/src/sound.rs
+++ b/apps/check/src/sound.rs
@@ -231,27 +231,38 @@ pub(crate) fn loudness(video: &str, taste: Option<&Taste>, issues: &mut Vec<Issu
}
/// three seconds or more where the picture doesn't change (the floor: nothing stands still that long without a reason)
-pub(crate) fn freezes(video: &str, probe: &Probe, taste: Option<&Taste>, issues: &mut Vec<Issue>, passed: &mut Vec<String>) -> R<()> {
- let most = taste.map_or(3.0, |t| t.value);
- let err = ffmpeg_stderr(&["-hide_banner", "-nostats", "-i", video, "-an", "-vf", &format!("freezedetect=n=-60dB:d={most}"), "-f", "null", "-"])?;
+/// The stretches freezedetect logged, each (start, seconds). FFmpeg logs a freeze's duration when the picture moves
+/// again, so a freeze still running when the file ends has only its start, and lasts to the end of the file.
+pub(crate) fn frozen_stretches(log: &str, duration: f64) -> Vec<(f64, f64)> {
+ let mut stretches = Vec::new();
let mut start = None;
- let mut found = 0;
- for l in err.lines() {
+ for l in log.lines() {
if let Some(x) = l.split("freeze_start: ").nth(1) {
start = x.trim().parse::<f64>().ok();
}
if let (Some(x), Some(s)) = (l.split("freeze_duration: ").nth(1), start) {
- let d: f64 = x.trim().parse().unwrap_or(0.0);
- found += 1;
- let says = format!("nothing changes for {d:.1} s; give it a reason to hold, or cut it shorter");
- issues.push(match taste {
- Some(t) => Issue { rule: "taste", p: 1, at: Some(s), says: format!("{says} (\"{}\")", t.line) },
- None => Issue { rule: "frozen", p: if d >= 6.0 || d >= probe.duration * 0.5 { 1 } else { 2 }, at: Some(s), says },
- });
+ stretches.push((s, x.trim().parse().unwrap_or(0.0)));
start = None;
}
}
- if found == 0 {
+ if let Some(s) = start {
+ stretches.push((s, (duration - s).max(0.0)));
+ }
+ stretches
+}
+
+pub(crate) fn freezes(video: &str, probe: &Probe, taste: Option<&Taste>, issues: &mut Vec<Issue>, passed: &mut Vec<String>) -> R<()> {
+ let most = taste.map_or(3.0, |t| t.value);
+ let err = ffmpeg_stderr(&["-hide_banner", "-nostats", "-i", video, "-an", "-vf", &format!("freezedetect=n=-60dB:d={most}"), "-f", "null", "-"])?;
+ let stretches = frozen_stretches(&err, probe.picture);
+ for &(s, d) in &stretches {
+ let says = format!("nothing changes for {d:.1} s; give it a reason to hold, or cut it shorter");
+ issues.push(match taste {
+ Some(t) => Issue { rule: "taste", p: 1, at: Some(s), says: format!("{says} (\"{}\")", t.line) },
+ None => Issue { rule: "frozen", p: if d >= 6.0 || d >= probe.duration * 0.5 { 1 } else { 2 }, at: Some(s), says },
+ });
+ }
+ if stretches.is_empty() {
passed.push(format!("frozen: nothing holds still for {most} s"));
}
Ok(())
diff --git a/apps/check/src/tests.rs b/apps/check/src/tests.rs
index b9b0970b..42a98887 100644
--- a/apps/check/src/tests.rs
+++ b/apps/check/src/tests.rs
@@ -147,6 +147,23 @@ fn a_voice_that_ends_a_sentence_isnt_a_dead_stop() {
assert!(found.iter().any(|(at, says)| (at - 1.6).abs() < 0.02 && says.starts_with("the highs")), "{found:?}");
}
+#[test]
+fn a_freeze_still_running_when_the_file_ends_is_found() {
+ // what FFmpeg 9.0.2's freezedetect logged on a film held from 3.4 s to its end, after a hold that ended at 3 s: the
+ // last freeze has a start and nothing after it, since it logs the duration when the picture moves again
+ let log = "[Parsed_freezedetect_0 @ 0000] lavfi.freezedetect.freeze_start: 0
+ [Parsed_freezedetect_0 @ 0000] lavfi.freezedetect.freeze_duration: 3
+ [Parsed_freezedetect_0 @ 0000] lavfi.freezedetect.freeze_end: 3
+ [Parsed_freezedetect_0 @ 0000] lavfi.freezedetect.freeze_start: 3.4
+";
+ assert_eq!(frozen_stretches(log, 10.0), vec![(0.0, 3.0), (3.4, 6.6)]);
+ // the whole file frozen: one start, at zero
+ assert_eq!(frozen_stretches("lavfi.freezedetect.freeze_start: 0
+", 8.0), vec![(0.0, 8.0)]);
+ assert!(frozen_stretches("frame= 300 fps=0.0
+", 10.0).is_empty());
+}
+
#[test]
fn a_bed_close_under_the_voice_and_a_loud_stretch_without_it_are_found() {
// 8 s of voice (-20 dBFS) in phrases with gaps, over a bed at -26 (6 dB under) and then at -40 (20 dB under)