FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#228: Export Frame reports a frame still loading instead of saving it blank
vangel767f59211912f9
diff --git a/crates/engine/src/frame_export_tests.rs b/crates/engine/src/frame_export_tests.rsindex 12702ba6b..3e2a00038 100644--- a/crates/engine/src/frame_export_tests.rs+++ b/crates/engine/src/frame_export_tests.rs@@ -101,3 +101,54 @@ fn frame_export_snapshots_are_independent_of_later_monitor_selection() {Arc::make_mut(&mut s.project).sequence_mut(seq).unwrap().settings.frame_rate = FrameRate { num: 1, den: i64::MAX };assert!(keyboard::frame_export_target(&s, &json!({})).is_err());}++/// A source whose pictures are still on their way, as a WebCodecs decode or a Blob read on the web.+struct Loading(filmcraft_media::MediaInfo);++impl filmcraft_media::MediaSource for Loading {+ fn info(&self) -> &filmcraft_media::MediaInfo {+ &self.0+ }+ fn video_frame(&self, _: filmcraft_media::FrameRequest) -> filmcraft_media::Result<Arc<filmcraft_frame::VideoFrame>> {+ filmcraft_media::pending::mark();+ Err(filmcraft_media::MediaError::Decode("still decoding".into()))+ }+ fn audio(&self, _: i64, _: usize, _: u32) -> filmcraft_media::Result<filmcraft_frame::AudioBuffer> {+ filmcraft_media::pending::mark();+ Err(filmcraft_media::MediaError::Decode("still loading".into()))+ }+}++#[test]+fn a_frame_still_loading_is_reported_not_saved_blank() {+ let (mut s, item) = session();+ let generator = s.media.cached(item).unwrap();+ s.media.insert(item, Arc::new(Loading(generator.info().clone())));+ let dir = std::env::temp_dir().join(format!("fc-frame-loading-{}", std::process::id()));+ std::fs::create_dir_all(&dir).unwrap();+ for target in ["program", "source"] {+ let path = dir.join(format!("{target}.png"));+ let err = s.execute("file.exportFrame", json!({"target":target,"path":path.to_string_lossy()})).unwrap_err();+ assert!(err.to_string().contains("still loading"), "{target}: {err}");+ assert!(!path.exists(), "{target}: nothing is written");+ }+ // once the picture is there, the same export succeeds+ s.media.insert(item, generator);+ for target in ["program", "source"] {+ let path = dir.join(format!("{target}.png"));+ s.execute("file.exportFrame", json!({"target":target,"path":path.to_string_lossy()})).unwrap();+ assert!(path.exists(), "{target}");+ }+ drop(s);+ std::fs::remove_dir_all(dir).unwrap();+}++#[test]+fn an_earlier_deferred_read_does_not_fail_the_export() {+ let (mut s, _) = session();+ filmcraft_media::pending::mark();+ let path = std::env::temp_dir().join(format!("fc-frame-stale-pending-{}.png", std::process::id()));+ s.execute("file.exportFrame", json!({"target":"program","path":path.to_string_lossy()})).unwrap();+ assert!(!filmcraft_media::pending::is_set());+ std::fs::remove_file(path).unwrap();+}diff --git a/crates/engine/src/keyboard.rs b/crates/engine/src/keyboard.rsindex ad5f5d2aa..57ddf72fd 100644--- a/crates/engine/src/keyboard.rs+++ b/crates/engine/src/keyboard.rs@@ -635,13 +635,20 @@ fn export_frame(s: &mut Session, p: &Value) -> Result<Value> {}};let provider = s.media.full_res_provider(s.project.clone(), s.services.clone());+ // On the web a frame can still be decoding (WebCodecs) or its bytes loading (Blob reads): the+ // render then leaves that clip out, so saving it would pass a blank or partial picture off as+ // the requested frame (#228). Report it instead and write nothing; a later call succeeds.+ let _ = filmcraft_media::pending::take();let img = if target.source {filmcraft_render::render_item(&s.project, target.item, target.time, 1.0, &provider)- .ok_or_else(|| bad("file.exportFrame", "cannot decode the Source video frame"))?} else {let opts = filmcraft_render::RenderOptions { scale: 1.0, captions: true, ..Default::default() };- filmcraft_render::render_sequence(&s.project, target.item, target.time, opts, &provider)+ Some(filmcraft_render::render_sequence(&s.project, target.item, target.time, opts, &provider))};+ if filmcraft_media::pending::take() {+ return Err(EngineError::Other("the frame is still loading; export it again in a moment".into()));+ }+ let img = img.ok_or_else(|| bad("file.exportFrame", "cannot decode the Source video frame"))?;let (w, h) = (u32::try_from(img.w).map_err(|_| bad("file.exportFrame", "image width exceeds limits"))?,u32::try_from(img.h).map_err(|_| bad("file.exportFrame", "image height exceeds limits"))?,