Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

scale in display.rs instead of encoder

Brendan Allan27878a542b1c
diff --git a/apps/desktop/src-tauri/src/display.rs b/apps/desktop/src-tauri/src/display.rs
index 9c31dec5c..ca0afcf5d 100644
--- a/apps/desktop/src-tauri/src/display.rs
+++ b/apps/desktop/src-tauri/src/display.rs
@@ -146,8 +146,6 @@ pub async fn start_capturing(
Capturer::new(dbg!(options))
};
- let [width, height] = capturer.get_output_frame_size();
-
let controller = CaptureController::new(output_path);
let (tx, rx) = oneshot::channel();
@@ -158,31 +156,45 @@ pub async fn start_capturing(
let capture_format = avformat::Pixel::BGRA;
let output_format = H264Encoder::output_format();
- let mut encoder = H264Encoder::new(&controller.output_path, width, height, FPS as f64);
-
- let mut scaler =
- software::converter((width, height), capture_format, output_format).unwrap();
+ let capture_size = capturer.get_output_frame_size();
+ let frame_size = scale_dimensions(capture_size[0], capture_size[1], 1920);
+
+ let mut encoder = H264Encoder::new(
+ &controller.output_path,
+ frame_size.0,
+ frame_size.1,
+ FPS as f64,
+ );
+
+ // 1080p scaling via scap causes artifacts on external monitors so ffmpeg it is
+ let mut scaler = software::scaling::Context::get(
+ capture_format,
+ capture_size[0],
+ capture_size[1],
+ output_format,
+ frame_size.0,
+ frame_size.1,
+ software::scaling::Flags::FAST_BILINEAR,
+ )
+ .unwrap();
capturer.start_capture();
let mut start_time_tx = Some(tx);
- let mut start = Instant::now();
-
loop {
if controller.is_stopped() {
break;
}
let frame = capturer.get_next_frame();
- let timestamp = SystemTime::now();
if controller.is_paused() {
continue;
}
if let Some(tx) = start_time_tx.take() {
- tx.send(Instant::now()).ok();
+ tx.send(()).ok();
}
if !*start_writing_rx.borrow() {
@@ -191,7 +203,7 @@ pub async fn start_capturing(
match frame {
Ok(Frame::BGRA(frame)) => {
- let rgb_frame = bgra_frame(&frame.data, width, height);
+ let rgb_frame = bgra_frame(&frame.data, frame_size.0, frame_size.1);
let mut yuv_frame = Video::empty();
scaler.run(&rgb_frame, &mut yuv_frame).unwrap();
@@ -225,3 +237,16 @@ pub fn get_window_bounds(window_number: u32) -> Option<Bounds> {
y: window.bounds.y as f64,
})
}
+
+fn scale_dimensions(width: u32, height: u32, max_width: u32) -> (u32, u32) {
+ if width <= max_width {
+ return (width, height);
+ }
+
+ let aspect_ratio = width as f32 / height as f32;
+ let new_width = max_width;
+ let new_height = (new_width as f32 / aspect_ratio).round() as u32;
+
+ // Ensure dimensions are divisible by 2
+ (new_width & !1, new_height & !1)
+}
diff --git a/apps/desktop/src-tauri/src/encoder.rs b/apps/desktop/src-tauri/src/encoder.rs
index 55af7debd..7c2b4152a 100644
--- a/apps/desktop/src-tauri/src/encoder.rs
+++ b/apps/desktop/src-tauri/src/encoder.rs
@@ -42,11 +42,9 @@ impl H264Encoder {
.video()
.unwrap();
- let (new_width, new_height) = Self::calculate_dimensions(width, height, 1080);
-
stream.set_parameters(&encoder);
- encoder.set_width(new_width);
- encoder.set_height(new_height);
+ encoder.set_width(width);
+ encoder.set_height(height);
encoder.set_format(Self::output_format());
encoder.set_frame_rate(Some(fps));
encoder.set_time_base(1.0 / fps);
@@ -78,43 +76,10 @@ impl H264Encoder {
}
}
- fn calculate_dimensions(width: u32, height: u32, max_width: u32) -> (u32, u32) {
- if width <= max_width {
- return (width, height);
- }
-
- let aspect_ratio = width as f32 / height as f32;
- let new_width = max_width;
- let new_height = (new_width as f32 / aspect_ratio).round() as u32;
+ pub fn encode_frame(&mut self, mut frame: ffmpeg::util::frame::Video) {
+ frame.set_pts(Some(self.frame_number as i64));
- // Ensure dimensions are divisible by 2
- (new_width & !1, new_height & !1)
- }
-
- pub fn encode_frame(&mut self, frame: ffmpeg::util::frame::Video) {
- // Scale the frame if necessary
- let mut scaled_frame =
- if frame.width() != self.context.width() || frame.height() != self.context.height() {
- let mut scaled = ffmpeg::frame::Video::empty();
- let mut scaler = ffmpeg::software::scaling::Context::get(
- frame.format(),
- frame.width(),
- frame.height(),
- self.context.format(),
- self.context.width(),
- self.context.height(),
- ffmpeg::software::scaling::Flags::BILINEAR,
- )
- .unwrap();
- scaler.run(&frame, &mut scaled).unwrap();
- scaled
- } else {
- frame
- };
-
- scaled_frame.set_pts(Some(self.frame_number as i64));
-
- self.context.send_frame(&scaled_frame).unwrap();
+ self.context.send_frame(&frame).unwrap();
self.frame_number += 1;
self.receive_and_process_packets();