Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(gif): accumulate fractional frame delays so GIF playback matches its fps
Etienne Lescot423301779fd8
diff --git a/crates/compositor/src/gif_export.rs b/crates/compositor/src/gif_export.rsindex 7ec366a57..e3fc12f4d 100644--- a/crates/compositor/src/gif_export.rs+++ b/crates/compositor/src/gif_export.rs@@ -218,17 +218,12 @@ fn export_gif_inner(// the header is empty.let mut writer = BufWriter::new(file);- // GIF frame delay in centiseconds (= 1/100 s). `fps` →- // `100 / fps` cs per frame, rounded to the nearest unit the- // GIF spec supports. u16 caps at 65535 — 10.9 minutes per- // frame, plenty.- //- // ponytail: integer centiseconds can't express every fps exactly- // (12 → 8 cs → 12.5 fps). `video_duration_s` below is computed- // from the delays actually written, so the reported duration never- // disagrees with the file. Fractional accumulation if a viewer ever- // cares about the ~4% drift.- let delay_cs: u16 = (100_u32 / fps).max(1) as u16;+ // GIF frame delays are integer centiseconds, which can't express most+ // fps exactly (15 fps = 6.67 cs). Each frame's delay comes from+ // `frame_delay_cs`, which accumulates the fractional remainder+ // (15 fps → 7,7,6,…) so playback length tracks frames / fps. The sum of+ // delays actually written is kept for `video_duration_s`.+ let mut total_delay_cs: u64 = 0;// Pre-allocate the per-frame index buffer. Reused across// frames so we don't hit the allocator in the hot loop.@@ -310,6 +305,8 @@ fn export_gif_inner(// Per-frame palette (GIF local palette, written by `write_frame`).control.check()?;+ let delay_cs = frame_delay_cs(frame_index as u64, fps);+ total_delay_cs += delay_cs as u64;gw.write_frame(&indices, &palette_rgb, delay_cs, fps)?;progress(frame_index + 1);Ok(())@@ -330,9 +327,8 @@ fn export_gif_inner(let wall_s = t0.elapsed().as_secs_f64();let fps_actual = if wall_s > 0.0 { frames as f64 / wall_s } else { 0.0 };- // From the delays actually written, not from the requested fps — see the- // `delay_cs` note above.- let video_duration_s = frames as f64 * (delay_cs as f64 / 100.0);+ // From the delays actually written, not from the requested fps.+ let video_duration_s = total_delay_cs as f64 / 100.0;let file_bytes = std::fs::metadata(out_path).map(|m| m.len()).unwrap_or(0);Ok(GifStats { frames, wall_s, fps: fps_actual, video_duration_s, file_bytes })@@ -979,11 +975,38 @@ fn map_to_indices_dithered(// the Floyd-Steinberg dither. The full `export_gif` pipeline// (Player + GPU + readback) is exercised by the bench, not here.+/// Delay in centiseconds for frame `index` at `fps`: the difference between+/// the rounded end and start times of the frame, so the fractional remainder+/// carries over and the running total stays within half a centisecond of+/// `(index + 1) * 100 / fps`. Never 0 (browsers treat 0 as "as fast as+/// possible" / 10 cs).+fn frame_delay_cs(index: u64, fps: u32) -> u16 {+ let fps = fps.max(1) as u64;+ let rounded_cs = |n: u64| (n * 200 + fps) / (2 * fps);+ (rounded_cs(index + 1) - rounded_cs(index)).clamp(1, u16::MAX as u64) as u16+}+#[cfg(test)]mod tests {use super::*;use std::io::Cursor;+ #[test]+ fn frame_delays_sum_to_programme_duration() {+ for fps in [15u32, 30, 12, 20, 25, 24, 60] {+ for n in [1u64, 2, 3, 7, 100, 508, 10_000] {+ let delays: Vec<u16> = (0..n).map(|i| frame_delay_cs(i, fps)).collect();+ assert!(delays.iter().all(|&d| d >= 1), "fps {fps}: zero delay");+ let sum: u64 = delays.iter().map(|&d| d as u64).sum();+ let expected = (n as f64 * 100.0 / fps as f64).round() as i64;+ assert!((sum as i64 - expected).abs() <= 1, "fps {fps} n {n}: {sum} vs {expected}");+ }+ }+ // 15 fps alternates 7/7/6 rather than a flat (fast) 6.+ let d: Vec<u16> = (0..3).map(|i| frame_delay_cs(i, 15)).collect();+ assert_eq!(d.iter().sum::<u16>(), 20);+ }+/// The most basic round-trip: write a 2×2 frame and check the/// file is well-formed GIF89a. No decode — we just walk the/// output bytes and confirm the structural shape.