Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: video frame seek mismatch — disable libx265 B-frames + detect PTS offset

Louis Beaumont2fab25bc7bcd
diff --git a/apps/screenpipe-app-tauri/components/rewind/current-frame-timeline.tsx b/apps/screenpipe-app-tauri/components/rewind/current-frame-timeline.tsx
index c2b047152..cdb5d7ab9 100644
--- a/apps/screenpipe-app-tauri/components/rewind/current-frame-timeline.tsx
+++ b/apps/screenpipe-app-tauri/components/rewind/current-frame-timeline.tsx
@@ -61,6 +61,9 @@ const failedChunks = new Set<string>();
// Cache calibrated fps per video file path so we only compute once
const calibratedFpsCache = new Map<string, number>();
+// Cache PTS offset per video file (old libx265 videos start at 2/fps instead of 0)
+const ptsOffsetCache = new Map<string, number>();
+
export const CurrentFrameTimeline: FC<CurrentFrameTimelineProps> = ({
@@ -258,8 +261,24 @@ export const CurrentFrameTimeline: FC<CurrentFrameTimelineProps> = ({
throw new Error(`cannot determine fps for ${path}`);
}
- // Seek to frame with bounds check
- let targetTime = idx / effectiveFps;
+ // Seek to frame: offset_index / fps gives the ideal time assuming pts starts at 0.
+ // Old videos encoded with libx265 B-frames have pts starting at 2/fps (e.g. 4s at 0.5fps).
+ // Probe once per video by seeking to 0 and reading where the browser actually lands.
+ let ptsOffset = ptsOffsetCache.get(path) ?? 0;
+ if (!ptsOffsetCache.has(path)) {
+ try {
+ video.currentTime = 0;
+ await new Promise<void>((r) => {
+ const done = () => { video.removeEventListener("seeked", done); r(); };
+ video.addEventListener("seeked", done);
+ });
+ ptsOffset = video.currentTime; // browser snaps to first frame PTS
+ ptsOffsetCache.set(path, ptsOffset);
+ } catch {
+ ptsOffsetCache.set(path, 0);
+ }
+ }
+ let targetTime = (idx / effectiveFps) + ptsOffset;
// Clamp to video duration (safety net)
if (targetTime > video.duration) {
console.warn(`seek target ${targetTime.toFixed(1)}s > duration ${video.duration.toFixed(1)}s, clamping`);
diff --git a/crates/screenpipe-server/src/video.rs b/crates/screenpipe-server/src/video.rs
index 8936cc45a..ffb695b6d 100644
--- a/crates/screenpipe-server/src/video.rs
+++ b/crates/screenpipe-server/src/video.rs
@@ -452,6 +452,11 @@ pub async fn start_ffmpeg_process(
args.extend_from_slice(&[
"-vcodec", "libx265", "-tag:v", "hvc1", "-preset", preset, "-crf", crf,
+ // Disable B-frames: libx265 default B-frame buffering shifts PTS by 2 frames
+ // (e.g. first frame at 4s instead of 0s at 0.5fps). This causes the frontend
+ // to seek to the wrong frame. B-frames provide no benefit for screenshot
+ // captures anyway since every frame is visually independent.
+ "-x265-params", "bframes=0",
]);
// Use fragmented MP4 to allow reading frames while file is still being written
@@ -460,10 +465,6 @@ pub async fn start_ffmpeg_process(
// - Streaming playback before recording finishes
args.extend_from_slice(&["-movflags", "frag_keyframe+empty_moov+default_base_moof"]);
- // Fix pts offset: ffmpeg image2pipe with -r buffers 2 frames, starting pts at 2/fps
- // instead of 0. This causes seek mismatches in the frontend (offset N shows frame N-2).
- args.extend_from_slice(&["-output_ts_offset", "0"]);
-
args.extend_from_slice(&["-pix_fmt", "yuv420p", output_file]);
command