Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Encoding quality (#474)

Brendan Allan8517d8aa2577
diff --git a/crates/media/src/encoders/h264.rs b/crates/media/src/encoders/h264.rs
index bfed00478..bc2c2c4d1 100644
--- a/crates/media/src/encoders/h264.rs
+++ b/crates/media/src/encoders/h264.rs
@@ -79,8 +79,15 @@ impl H264Encoder {
encoder.set_format(format);
encoder.set_time_base(config.frame_rate.invert());
encoder.set_frame_rate(Some(config.frame_rate));
- encoder.set_bit_rate(12_000_000);
- encoder.set_max_bit_rate(12_000_000);
+
+ let bitrate = get_bitrate(
+ config.width,
+ config.height,
+ config.frame_rate.0 as f32 / config.frame_rate.1 as f32,
+ );
+
+ encoder.set_bit_rate(bitrate);
+ encoder.set_max_bit_rate(bitrate);
let video_encoder = encoder.open_with(options)?;
@@ -163,6 +170,7 @@ fn get_codec_and_options(config: &VideoInfo) -> Result<(Codec, Dictionary), Medi
"libx264"
}
};
+
if let Some(codec) = encoder::find_by_name(encoder_name) {
let mut options = Dictionary::new();
@@ -188,3 +196,24 @@ fn get_codec_and_options(config: &VideoInfo) -> Result<(Codec, Dictionary), Medi
Err(MediaError::MissingCodec("H264 video"))
}
+
+fn get_bitrate(width: u32, height: u32, frame_rate: f32) -> usize {
+ // higher frame rates don't really need double the bitrate lets be real
+ let frame_rate_multiplier = (frame_rate - 30.0).max(0.0) * 0.4 + 30.0;
+ let pixels_per_second = (width * height) as f32 * frame_rate_multiplier;
+
+ let base_bpp = 0.15;
+
+ // Apply resolution efficiency scaling (larger resolutions can use relatively lower bitrates)
+ let resolution_factor = if width * height > 1920 * 1080 {
+ 0.7
+ } else if width * height > 1280 * 720 {
+ 0.8
+ } else if width * height > 640 * 480 {
+ 0.9
+ } else {
+ 1.0
+ };
+
+ (pixels_per_second * base_bpp * resolution_factor) as usize
+}
diff --git a/crates/media/src/sources/screen_capture.rs b/crates/media/src/sources/screen_capture.rs
index fa6f15af1..8b6fa8207 100644
--- a/crates/media/src/sources/screen_capture.rs
+++ b/crates/media/src/sources/screen_capture.rs
@@ -134,6 +134,7 @@ pub struct ScreenCaptureSource<TCaptureFormat: ScreenCaptureFormat> {
show_camera: bool,
force_show_cursor: bool,
bounds: Bounds,
+ // logical display size
display_size: (f32, f32),
video_tx: Sender<(TCaptureFormat::VideoFormat, f64)>,
audio_tx: Option<Sender<(ffmpeg::frame::Audio, f64)>>,
@@ -268,12 +269,16 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
this.options = Arc::new(options);
- this.video_info = VideoInfo::from_raw(
- RawVideoFormat::Bgra,
- bounds.width as u32,
- bounds.height as u32,
- fps,
- );
+ #[cfg(target_os = "macos")]
+ let video_size = {
+ let [x, y] = scap::capturer::get_output_frame_size(&this.options);
+ (x, y)
+ };
+ #[cfg(windows)]
+ let video_size = (bounds.width as u32, bounds.height as u32);
+
+ this.video_info =
+ VideoInfo::from_raw(RawVideoFormat::Bgra, video_size.0, video_size.1, fps);
Ok(this)
}