Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Merge pull request #186 from dcarnelutti/platform/videotoolbox-hevc-encode

Brandon Thomasb5f197567bcd
diff --cc crates/export/src/settings_tests.rs
index 78dd22be3,2c39c4b11..2fb9988ca
--- a/crates/export/src/settings_tests.rs
+++ b/crates/export/src/settings_tests.rs
@@@ -411,19 -411,54 +411,71 @@@ fn aac_bitrate_is_capped_for_low_sample
}
}
+#[test]
+fn a_time_left_reads_like_a_clock() {
+ use std::time::Duration;
+ let f = |ms: u64| format_eta(Duration::from_millis(ms));
+ assert_eq!(f(0), "0 s");
+ assert_eq!(f(1), "1 s", "rounded up: never say 0 s while there is time left");
+ assert_eq!(f(45_000), "45 s");
+ assert_eq!(f(59_001), "1:00");
+ assert_eq!(f(60_000), "1:00");
+ assert_eq!(f(125_000), "2:05");
+ assert_eq!(f(3_599_000), "59:59");
+ assert_eq!(f(3_600_000), "1:00:00");
+ assert_eq!(f(3_725_000), "1:02:05");
+ assert_eq!(f(100 * 3600 * 1000), "100:00:00");
+ assert!(!format_eta(Duration::MAX).is_empty(), "the largest duration cannot overflow");
+}
++
+ #[test]
+ fn h265_is_a_format_that_needs_a_registered_encoder() {
+ use std::sync::atomic::{AtomicBool, Ordering};
+ // names, ids, files and serialized settings
+ for name in ["hevc", "H.265", "h265", "HVC1", "x265"] {
+ assert_eq!(Format::from_name(name), Some(Format::Hevc), "{name}");
+ }
+ assert_eq!(Format::from_name(Format::Hevc.id()), Some(Format::Hevc));
+ assert_eq!((Format::Hevc.extension(), Format::Hevc.label()), ("mp4", "H.265 (HEVC)"));
+ assert!(Format::ALL.contains(&Format::Hevc) && Format::ALL.len() == 14);
+ let s: ExportSettings = serde_json::from_value(serde_json::json!({"format": "hevc"})).unwrap();
+ assert_eq!(s.format, Format::Hevc);
+ assert_eq!(serde_json::to_value(&s).unwrap()["format"], "hevc");
+
+ // it is H.264's family: MP4 with AAC, or QuickTime; the same size estimate
+ let h = ExportSettings { format: Format::Hevc, ..Default::default() };
+ assert_eq!((h.audio_codec(), h.extension()), (AudioCodec::Aac, "mp4"));
+ assert_eq!(ExportSettings { multiplexer: Multiplexer::Mov, ..h.clone() }.extension(), "mov");
+ // 10 s of 20 Mbps video + 320 kbps AAC ≈ 25.4 MB, like H.264
+ let b = h.estimate_bytes(1920, 1080, FrameRate::FPS_25, 48_000, Tick(10 * TICKS_PER_SECOND));
+ assert!((b as f64 - 25.4e6).abs() < 0.2e6, "{b}");
+ let summary = h.summary(1920, 1080, FrameRate::FPS_25, 48_000, Tick(10 * TICKS_PER_SECOND));
+ assert_eq!(summary.format, "H.265 (HEVC) (MP4)");
+ assert!(summary.video.contains("HEVC Main") && summary.video.contains("Target 20.00 Mbps"), "{}", summary.video);
+
+ // a hardware encoder has one pass
+ let e = ExportSettings { bitrate_mode: BitrateMode::Vbr2Pass, ..h.clone() }.validate().unwrap_err();
+ assert!(e.to_string().contains("two-pass"), "{e}");
+ assert!(ExportSettings { bitrate_mode: BitrateMode::Cbr, ..h.clone() }.validate().is_ok());
+
+ // formats with a built-in encoder are always available; this one only when a registered probe says so
+ assert!(Format::ALL.iter().filter(|f| f.has_builtin_encoder()).all(|f| available(*f)));
+ assert!(!Format::Hevc.has_builtin_encoder());
+ static HERE: AtomicBool = AtomicBool::new(false);
+ fn probe() -> bool {
+ HERE.load(Ordering::SeqCst)
+ }
+ assert!(!available(Format::Hevc), "nothing registered an encoder for it");
+ // and an export says so, honestly
+ let (p, seq, m) = matte([0.0, 0.0, 1.0, 1.0], 160, 90, None);
+ let sc = Scratch::new("h265-missing");
+ let err = export(&p, seq, &ExportSettings { path: sc.path("x.mp4"), include_audio: false, ..h.clone() }, &m, &Progress::default()).unwrap_err().to_string();
+ assert!(err.contains("H.265") && err.contains("encoder not available"), "{err}");
+ register_format_probe(Format::Hevc, probe);
+ register_format_probe(Format::Hevc, probe); // the same probe twice is harmless
+ assert!(!available(Format::Hevc));
+ HERE.store(true, Ordering::SeqCst);
+ assert!(available(Format::Hevc));
+ HERE.store(false, Ordering::SeqCst);
+ assert!(!available(Format::Hevc));
+ }
diff --cc crates/ui-egui/tests/export_ui.rs
index 1eb3a18a8,72d349470..04b47c563
--- a/crates/ui-egui/tests/export_ui.rs
+++ b/crates/ui-egui/tests/export_ui.rs
@@@ -179,46 -179,38 +179,78 @@@ fn export_mode_settings_and_summary()
d.snapshot("export-mode");
}
+#[test]
+fn the_status_bar_and_the_queue_show_the_time_left() {
+ use std::sync::atomic::Ordering;
+ use std::time::Duration;
+
+ use filmcraft_engine::Job;
+ use filmcraft_engine::export_tools::QueueStatus;
+ let mut d = Driver::new("eta");
+ d.ok("ui.set", json!({"mode": "export"}));
+ d.frames(3);
+ d.exec(
+ "export.queue.add",
+ json!({"preset": "Waveform Audio 48 kHz 16-bit", "path": d.path("eta.wav"), "range": "custom", "startSeconds": 0.0, "endSeconds": 0.25}),
+ );
+ // a job at 25 %, doing 50 units a second: 750 left, 15 s (readings from the near future, because
+ // the app reads the real clock, which is then behind them)
+ let job = Job { id: 900, label: "Exporting".into(), progress: Default::default(), result: Default::default() };
+ job.progress.total.store(1000, Ordering::Relaxed);
+ let t0 = web_time::Instant::now() + Duration::from_secs(60);
+ for i in 0..=50u64 {
+ job.progress.done.store(i * 5, Ordering::Relaxed);
+ job.progress.eta_at(t0 + Duration::from_millis(i * 100));
+ }
+ d.app().session.jobs.push(job);
+ {
+ let item = &mut d.app().session.export_queue.items[0];
+ item.status = QueueStatus::Encoding;
+ item.job = Some(900);
+ }
+ d.frames(4);
+ // the status bar says it next to the percentage, and agents read it from the same label
+ let label = d.ok("ui.elements", json!({"prefix": "status.job.progress"}))[0]["label"].as_str().unwrap().to_string();
+ assert_eq!(label, "25% · 15 s left");
+ // the queue item has it too
+ let item = d.queue().into_iter().next().unwrap();
+ assert!((item["etaSeconds"].as_f64().unwrap() - 15.0).abs() < 0.5, "{item}");
+ assert_eq!(item["status"], "encoding");
+ d.snapshot("export-eta");
+}
+
+ #[test]
+ fn h265_shows_the_h264_family_controls_without_the_h264_only_ones() {
+ use filmcraft_engine::export::{ExportSettings, Format};
+ let mut d = Driver::new("h265");
+ d.ok("ui.set", json!({"mode": "export"}));
+ d.frames(4);
+ // H.264, the default: profile, level and the hardware checkbox
+ for id in ["export.video.profile", "export.video.level", "export.video.hardwareEncoding", "export.video.bitrateMode"] {
+ assert!(d.has(id), "H.264: {id}");
+ }
+ d.app().ui.export.settings = ExportSettings { format: Format::Hevc, ..Default::default() };
+ d.frames(4);
+ for id in [
+ "export.video.bitrateMode",
+ "export.video.target",
+ "export.video.max",
+ "export.video.keyframeOn",
+ "export.section.multiplexer",
+ "export.section.audio",
+ "export.audio.sampleRate",
+ ] {
+ assert!(d.has(id), "H.265: {id}");
+ }
+ // Main is its only profile, the level is the encoder's choice, and hardware is the only encoder
+ for id in ["export.video.profile", "export.video.level", "export.video.hardwareEncoding"] {
+ assert!(!d.has(id), "H.265 has no {id}");
+ }
+ let sum = d.ok("ui.elements", json!({"prefix": "export.summary"}))[0]["label"].as_str().unwrap().to_string();
+ assert!(sum.contains("HEVC Main") && sum.contains("Target 20.00 Mbps"), "{sum}");
+ d.snapshot("export-h265");
+ }
+
#[test]
fn preset_manager_search_favourite_save_apply() {
let mut d = Driver::new("manager");