Skip to content

Fix macOS recording duration when final frames stop updating#685

Open
kax168 wants to merge 1 commit into
webadderallorg:mainfrom
kax168:codex/fix-mac-static-tail-duration
Open

Fix macOS recording duration when final frames stop updating#685
kax168 wants to merge 1 commit into
webadderallorg:mainfrom
kax168:codex/fix-mac-static-tail-duration

Conversation

@kax168

@kax168 kax168 commented Jun 16, 2026

Copy link
Copy Markdown

Summary

  • Pad the final macOS ScreenCaptureKit video sample to the actual adjusted capture end time before finishing the asset writer.
  • Preserve pause accounting when computing the final capture duration.
  • Prevent recordings with little or no screen-frame activity near the end from finalizing at the timestamp of the last emitted video frame.

Background

On macOS, ScreenCaptureKit may stop delivering new screen samples when the captured display/window is visually static. Recordly hides the native cursor and records cursor telemetry separately, so a session can continue collecting cursor data while the video track remains stuck at the last screen sample timestamp. The old finalization path appended only one extra frame, so the resulting MP4 duration could be much shorter than the real recording session.

Validation

  • npm run build:native-helpers

No user recording paths, filenames, or private recording content are included in this PR.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved video capture timing accuracy when pauses occur during recording.
    • Enhanced handling of final video frames to ensure proper padding at the end of captures.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

finishCapture() in ScreenCaptureKitRecorder.swift is refactored to use a pause-aware end-time pipeline. Two new helper methods are added: currentAdjustedCaptureTime (subtracts accumulated/ongoing pause duration from elapsed time) and appendFinalVideoFrameIfNeeded (retimes and appends a padding frame when a gap exists between video end and capture end time).

Changes

Pause-aware video end padding in ScreenCaptureKitRecorder

Layer / File(s) Summary
New pause-aware helper methods
electron/native/ScreenCaptureKitRecorder.swift
Adds currentAdjustedCaptureTime to compute capture elapsed time minus accumulated/ongoing pause duration, and appendFinalVideoFrameIfNeeded to retime and append one final video sample when the video stream trails the adjusted capture end time.
finishCapture() end-time pipeline
electron/native/ScreenCaptureKitRecorder.swift
Replaces the single resolvedCaptureEndTime(videoEndTime:) call with a three-step flow: compute videoEndTime, compute pause-adjusted captureEndTime, call appendFinalVideoFrameIfNeeded, then resolve final end time from the padded video end. Includes a minor trailing-line adjustment before waitUntilFinished().

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 When the recording stops, don't rush away,
A final frame must pad the fray.
Subtract the pauses, one by one,
Retime the tail before we're done.
The clock adjusted, the stream aligned—
No gappy video left behind! 🎬

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: fixing macOS recording duration when final frames stop updating, which directly corresponds to the padding logic and pause accounting changes in the changeset.
Description check ✅ Passed The description covers the purpose, motivation, validation steps, and includes appropriate sections. However, it lacks structured template compliance: missing explicit Type of Change checkbox selection, Related Issue(s) link, and checklist items that the template requires.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@electron/native/ScreenCaptureKitRecorder.swift`:
- Around line 485-509: The appendFinalVideoFrameIfNeeded function appends a
padding frame whenever lastSampleBuffer exists, but should only append when the
video actually ends early relative to the capture. When captureEndTime is less
than or equal to videoEndTime, the targetPresentationTime becomes equal to
videoEndTime due to the max() calculation, resulting in an unnecessary extra
frame. Add a condition before creating and appending the final sample buffer to
check that captureEndTime is actually greater than videoEndTime, ensuring
padding is only added when needed to fill the gap between video and capture end
times.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: be701247-bcf3-4ba2-be03-a5454dd2ace7

📥 Commits

Reviewing files that changed from the base of the PR and between 8c7d23d and 0f052b1.

📒 Files selected for processing (1)
  • electron/native/ScreenCaptureKitRecorder.swift

Comment on lines +485 to +509
private func appendFinalVideoFrameIfNeeded(videoEndTime: CMTime, captureEndTime: CMTime) -> CMTime {
guard let originalBuffer = lastSampleBuffer, let videoInput else {
return videoEndTime
}

let duration = frameDuration(for: originalBuffer)
let minimumAdditionalTime = videoEndTime
let targetPresentationTime = max(
minimumAdditionalTime,
captureEndTime - duration
)
let timing = CMSampleTimingInfo(
duration: duration,
presentationTimeStamp: targetPresentationTime,
decodeTimeStamp: originalBuffer.decodeTimeStamp
)
if let finalSampleBuffer = try? CMSampleBuffer(copying: originalBuffer, withNewTiming: [timing]),
videoInput.append(finalSampleBuffer) {
lastVideoPresentationTime = targetPresentationTime
lastVideoDuration = duration
return targetPresentationTime + duration
}

return videoEndTime
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Only append the padding frame when the video actually ends early.

As written, this appends whenever lastSampleBuffer exists. When captureEndTime <= videoEndTime, targetPresentationTime becomes videoEndTime, so the returned end time is extended by one extra frame even though no padding was needed.

Proposed fix
 private func appendFinalVideoFrameIfNeeded(videoEndTime: CMTime, captureEndTime: CMTime) -> CMTime {
 	guard let originalBuffer = lastSampleBuffer, let videoInput else {
 		return videoEndTime
 	}
+
+	guard CMTimeCompare(captureEndTime, videoEndTime) > 0 else {
+		return videoEndTime
+	}
 
 	let duration = frameDuration(for: originalBuffer)
 	let minimumAdditionalTime = videoEndTime
 	let targetPresentationTime = max(
 		minimumAdditionalTime,
 		captureEndTime - duration
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private func appendFinalVideoFrameIfNeeded(videoEndTime: CMTime, captureEndTime: CMTime) -> CMTime {
guard let originalBuffer = lastSampleBuffer, let videoInput else {
return videoEndTime
}
let duration = frameDuration(for: originalBuffer)
let minimumAdditionalTime = videoEndTime
let targetPresentationTime = max(
minimumAdditionalTime,
captureEndTime - duration
)
let timing = CMSampleTimingInfo(
duration: duration,
presentationTimeStamp: targetPresentationTime,
decodeTimeStamp: originalBuffer.decodeTimeStamp
)
if let finalSampleBuffer = try? CMSampleBuffer(copying: originalBuffer, withNewTiming: [timing]),
videoInput.append(finalSampleBuffer) {
lastVideoPresentationTime = targetPresentationTime
lastVideoDuration = duration
return targetPresentationTime + duration
}
return videoEndTime
}
private func appendFinalVideoFrameIfNeeded(videoEndTime: CMTime, captureEndTime: CMTime) -> CMTime {
guard let originalBuffer = lastSampleBuffer, let videoInput else {
return videoEndTime
}
guard CMTimeCompare(captureEndTime, videoEndTime) > 0 else {
return videoEndTime
}
let duration = frameDuration(for: originalBuffer)
let minimumAdditionalTime = videoEndTime
let targetPresentationTime = max(
minimumAdditionalTime,
captureEndTime - duration
)
let timing = CMSampleTimingInfo(
duration: duration,
presentationTimeStamp: targetPresentationTime,
decodeTimeStamp: originalBuffer.decodeTimeStamp
)
if let finalSampleBuffer = try? CMSampleBuffer(copying: originalBuffer, withNewTiming: [timing]),
videoInput.append(finalSampleBuffer) {
lastVideoPresentationTime = targetPresentationTime
lastVideoDuration = duration
return targetPresentationTime + duration
}
return videoEndTime
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@electron/native/ScreenCaptureKitRecorder.swift` around lines 485 - 509, The
appendFinalVideoFrameIfNeeded function appends a padding frame whenever
lastSampleBuffer exists, but should only append when the video actually ends
early relative to the capture. When captureEndTime is less than or equal to
videoEndTime, the targetPresentationTime becomes equal to videoEndTime due to
the max() calculation, resulting in an unnecessary extra frame. Add a condition
before creating and appending the final sample buffer to check that
captureEndTime is actually greater than videoEndTime, ensuring padding is only
added when needed to fill the gap between video and capture end times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant