Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(macos): size the video bitrate from the actual capture, not the 4K ceiling

EtienneLescota4d92a795cfa
diff --git a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/CaptureSizing.swift b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/CaptureSizing.swift
index 9741d9559..6782e9d03 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/CaptureSizing.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/CaptureSizing.swift
@@ -72,3 +72,17 @@ private func evenFloor(_ value: Int) -> Int {
let clamped = max(2, value)
return clamped - (clamped % 2)
}
+
+/// The video bitrate for the size the stream actually got, the rule the Linux helper uses
+/// (`default_bitrate` in `pipewire-capture/src/capture.rs`): 0.1 bit per pixel per frame,
+/// about 12 Mbit/s at 1920×1080/60, kept between 2 and 60 Mbit/s.
+///
+/// The renderer used to send `computeBitrate(TARGET_WIDTH, TARGET_HEIGHT)`, the 4K ceiling with
+/// its high-frame-rate boost: 76.5 Mbit/s for every take, a 1080p one included, 4.25× what
+/// Windows spends on the same capture (getopenscreen/openscreen#924). It cannot know the real
+/// size, which `captureOutputSize` settles here, so it now sends none.
+public func defaultVideoBitrate(width: Int, height: Int, fps: Int) -> Int {
+ let bitsPerPixel = 0.1
+ let pixelsPerSecond = Double(max(1, width)) * Double(max(1, height)) * Double(max(1, fps))
+ return min(max(Int(pixelsPerSecond * bitsPerPixel), 2_000_000), 60_000_000)
+}
diff --git a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
index 4e0723f55..054b29904 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
@@ -755,7 +755,9 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
AVVideoWidthKey: outputWidth,
AVVideoHeightKey: outputHeight,
AVVideoCompressionPropertiesKey: [
- AVVideoAverageBitRateKey: request.video.bitrate ?? 18_000_000,
+ // From the size this stream really got. The renderer sends none (#924).
+ AVVideoAverageBitRateKey: request.video.bitrate
+ ?? defaultVideoBitrate(width: outputWidth, height: outputHeight, fps: request.video.fps),
AVVideoExpectedSourceFrameRateKey: request.video.fps,
// Without this the encoder defaults to B-frames, and a reordered
// stream needs a composition offset per sample. AVAssetWriter emits
diff --git a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/CaptureSizingTests.swift b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/CaptureSizingTests.swift
index 53029a31a..bce814993 100644
--- a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/CaptureSizingTests.swift
+++ b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/CaptureSizingTests.swift
@@ -134,4 +134,20 @@ final class CaptureSizingTests: XCTestCase {
XCTAssertEqual(result.height, 1080, "scale \(scale)")
}
}
+
+ // MARK: - Bitrate from the real size (#924)
+
+ func testBitrateFollowsTheCapturedSizeNotTheCeiling() {
+ // 1080p60 used to be asked for 76.5 Mbit/s, the 4K ceiling's figure.
+ XCTAssertEqual(defaultVideoBitrate(width: 1920, height: 1080, fps: 60), 12_441_600)
+ XCTAssertEqual(defaultVideoBitrate(width: 2560, height: 1440, fps: 60), 22_118_400)
+ XCTAssertEqual(defaultVideoBitrate(width: 3840, height: 2160, fps: 60), 49_766_400)
+ }
+
+ func testBitrateStaysWithinItsFloorAndCeiling() {
+ // A small window still gets enough bits to stay sharp; 4K at 120 fps does not ask for
+ // more than any disk wants.
+ XCTAssertEqual(defaultVideoBitrate(width: 320, height: 240, fps: 30), 2_000_000)
+ XCTAssertEqual(defaultVideoBitrate(width: 3840, height: 2160, fps: 120), 60_000_000)
+ }
}
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index c827dd985..65d4ac003 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -1398,7 +1398,9 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
fps: TARGET_FRAME_RATE,
width: TARGET_WIDTH,
height: TARGET_HEIGHT,
- bitrate: computeBitrate(TARGET_WIDTH, TARGET_HEIGHT),
+ // No bitrate, as on Linux: TARGET_WIDTH/HEIGHT are the 4K ceiling,
+ // not the capture size, and the ceiling's 76.5 Mbit/s went out for a
+ // 1080p take too (#924). The helper derives it from the size it got.
hideSystemCursor: cursorCaptureMode === "editable-overlay",
},
audio: {