Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: improve keyframe distribution and frame iteration

Richie McIlroy4789aed6d767
diff --git a/crates/video-decode/src/avassetreader.rs b/crates/video-decode/src/avassetreader.rs
index fce3f6fd7..1a021151e 100644
--- a/crates/video-decode/src/avassetreader.rs
+++ b/crates/video-decode/src/avassetreader.rs
@@ -141,13 +141,25 @@ impl KeyframeIndex {
return self.keyframes.iter().map(|(_, time)| *time).collect();
}
- let step = total_keyframes / num_positions;
- self.keyframes
- .iter()
- .step_by(step.max(1))
- .take(num_positions)
- .map(|(_, time)| *time)
- .collect()
+ let mut positions: Vec<f64> = Vec::with_capacity(num_positions);
+
+ positions.push(self.keyframes[0].1);
+
+ if num_positions > 2 {
+ let inner_count = num_positions - 2;
+ let step = (total_keyframes - 1) as f64 / (inner_count + 1) as f64;
+ for i in 1..=inner_count {
+ let idx = (step * i as f64).round() as usize;
+ let idx = idx.min(total_keyframes - 2);
+ positions.push(self.keyframes[idx].1);
+ }
+ }
+
+ if num_positions > 1 {
+ positions.push(self.keyframes[total_keyframes - 1].1);
+ }
+
+ positions
}
pub fn fps(&self) -> f64 {
diff --git a/crates/video-decode/src/ffmpeg.rs b/crates/video-decode/src/ffmpeg.rs
index f5bb10349..200eaf6fd 100644
--- a/crates/video-decode/src/ffmpeg.rs
+++ b/crates/video-decode/src/ffmpeg.rs
@@ -366,16 +366,23 @@ impl<'a> Iterator for FramesIter<'a> {
match self.decoder.receive_frame(&mut frame) {
Ok(()) => {
return match &self.hw_device {
- Some(hw_device) => Some(Ok(hw_device.get_hwframe(&frame).unwrap_or(frame))),
+ Some(hw_device) => {
+ let hw_result = hw_device.get_hwframe(&frame);
+ Some(Ok(hw_result.unwrap_or(frame)))
+ }
None => Some(Ok(frame)),
};
}
- Err(ffmpeg::Error::Eof) => return None,
+ Err(ffmpeg::Error::Eof) => {
+ return None;
+ }
Err(ffmpeg::Error::Other { errno }) if errno == EAGAIN => {}
Err(e) => return Some(Err(e)),
}
- let (stream, packet) = self.packets.next()?;
+ let Some((stream, packet)) = self.packets.next() else {
+ return None;
+ };
if stream.index() != self.stream_index {
continue;