Video: Interpret Footage and New Video Layer from File reject an invalid frame rate (#5657)

Both commands stored the `fps` param unchecked: 0 or a negative rate was
kept, and 1e39 became an infinite rate the next .pcraft save refused. They
now use timeline_cmds::fps_param (finite, above 0, at most MAX_FPS), as
timeline.create and timeline.setProps do; a missing fps keeps the old
fallback. Regression test covers 0, -1, 1e39 and "x" for both commands.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-14 19:55:35 +02:00
1 parent f499d97eee
commit f31fb26fe8
3 files changed
+30 -4

No files matched your search

+1 -1
View File
@@ -59,7 +59,7 @@ fn duration_param(cmd: &str, p: &Value) -> Result<Option<usize>> {
}
/// The optional `fps` param, rejected unless it's a finite number in `(0, MAX_FPS]` (#1009).
fn fps_param(cmd: &str, p: &Value) -> Result<Option<f32>> {
pub(crate) fn fps_param(cmd: &str, p: &Value) -> Result<Option<f32>> {
let Some(v) = p.get("fps").filter(|v| !v.is_null()) else { return Ok(None) };
v.as_f64()
.filter(|f| f.is_finite() && *f > 0.0 && *f <= f64::from(MAX_FPS))
+3 -3
View File
@@ -297,7 +297,7 @@ fn new_from_file(s: &mut Session, p: &Value) -> Result<Value> {
let fmt = s.active().ok_or(EngineError::NoDocument)?.doc.pixel_format();
let Footage { frames, fps: footage_fps } = load_frames(&path, fmt)?;
// An explicit `fps` wins, then the rate the footage implies, then 30.
let fps = p.get("fps").and_then(Value::as_f64).map(|f| f as f32).or(footage_fps).unwrap_or(30.0);
let fps = crate::timeline_cmds::fps_param("layer.videoLayers.newVideoLayerFromFile", p)?.or(footage_fps).unwrap_or(30.0);
let n = frames.len();
let id = s.edit("New Video Layer from File", |doc, active| {
store(doc);
@@ -337,10 +337,10 @@ fn replace_footage(s: &mut Session, p: &Value) -> Result<Value> {
}
fn interpret_footage(s: &mut Session, p: &Value) -> Result<Value> {
let fps = p.get("fps").and_then(Value::as_f64);
let fps = crate::timeline_cmds::fps_param("layer.videoLayers.interpretFootage", p)?;
edit_video(s, "Interpret Footage", move |v, _| {
if let Some(f) = fps {
v.fps = f as f32;
v.fps = f;
}
Ok(())
})
+26
View File
@@ -330,6 +330,32 @@ fn a_still_gif_is_one_frame_at_the_default_rate() {
assert_eq!(s.active().unwrap().doc.layer(id).unwrap().video.as_ref().unwrap().fps, 10.0);
}
/// Interpret Footage and New Video Layer from File reject an fps that isn't a finite number in
/// `(0, MAX_FPS]` and keep the layer as it was (#5657). 1e39 used to become an infinite rate the
/// next `.pcraft` save refused.
#[test]
fn an_invalid_frame_rate_is_an_error_and_changes_nothing() {
let mut s = session();
let id = LayerId(s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap()["layer"].as_u64().unwrap());
let fps = |s: &Session| s.active().unwrap().doc.layer(id).unwrap().video.as_ref().unwrap().fps;
assert_eq!(fps(&s), 30.0);
for bad in [json!(0), json!(-1), json!(1e39), json!("x")] {
assert!(s.execute("layer.videoLayers.interpretFootage", json!({"fps": bad})).is_err(), "fps {bad}");
assert_eq!(fps(&s), 30.0, "fps {bad}");
}
s.execute("layer.videoLayers.interpretFootage", json!({"fps": 24})).unwrap();
assert_eq!(fps(&s), 24.0);
let dir = tmpdir("gifin-badfps");
let path = format!("{dir}/anim.gif");
write_gif(&path, 8, 8, &[([255, 0, 0], 10), ([0, 255, 0], 10)]);
let layers = s.active().unwrap().doc.layer_count();
for bad in [json!(0), json!(-1), json!(1e39), json!("x")] {
assert!(s.execute("layer.videoLayers.newVideoLayerFromFile", json!({"path": path, "fps": bad})).is_err(), "fps {bad}");
assert_eq!(s.active().unwrap().doc.layer_count(), layers, "fps {bad}");
}
}
#[test]
fn video_frames_to_layers_takes_every_gif_frame() {
let dir = tmpdir("gifin-ftl");