Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(video-decode): cache video params and scan keyframes with one open

Richie McIlroyd1e2d9f018a8
diff --git a/crates/video-decode/src/avassetreader.rs b/crates/video-decode/src/avassetreader.rs
index a8db3b606..2158f892a 100644
--- a/crates/video-decode/src/avassetreader.rs
+++ b/crates/video-decode/src/avassetreader.rs
@@ -16,45 +16,67 @@ pub struct KeyframeIndex {
keyframes: Vec<(u32, f64)>,
fps: f64,
duration_secs: f64,
+ pixel_format: Option<cv::PixelFormat>,
+ width: u32,
+ height: u32,
}
impl KeyframeIndex {
pub fn build(path: &Path) -> Result<Self, String> {
let build_start = std::time::Instant::now();
- let input = avformat::input(path)
+ let mut input = avformat::input(path)
.map_err(|e| format!("Failed to open video for keyframe scan: {e}"))?;
- let video_stream = input
- .streams()
- .best(ffmpeg::media::Type::Video)
- .ok_or("No video stream found")?;
-
- let stream_index = video_stream.index();
- let time_base = video_stream.time_base();
- let fps = {
- let rate = video_stream.avg_frame_rate();
- if rate.denominator() == 0 {
- 30.0
- } else {
- rate.numerator() as f64 / rate.denominator() as f64
- }
- };
+ let (stream_index, time_base, fps, duration_secs, pixel_format, width, height) = {
+ let video_stream = input
+ .streams()
+ .best(ffmpeg::media::Type::Video)
+ .ok_or("No video stream found")?;
+
+ let stream_index = video_stream.index();
+ let time_base = video_stream.time_base();
+ let fps = {
+ let rate = video_stream.avg_frame_rate();
+ if rate.denominator() == 0 {
+ 30.0
+ } else {
+ rate.numerator() as f64 / rate.denominator() as f64
+ }
+ };
- let duration_secs = {
- let duration = video_stream.duration();
- if duration > 0 {
- duration as f64 * time_base.numerator() as f64 / time_base.denominator() as f64
- } else {
- 0.0
- }
+ let duration_secs = {
+ let duration = video_stream.duration();
+ if duration > 0 {
+ duration as f64 * time_base.numerator() as f64 / time_base.denominator() as f64
+ } else {
+ 0.0
+ }
+ };
+
+ let decoder = avcodec::Context::from_parameters(video_stream.parameters())
+ .map_err(|e| format!("decoder context / {e}"))?
+ .decoder()
+ .video()
+ .map_err(|e| format!("video decoder / {e}"))?;
+
+ let pixel_format = pixel_to_pixel_format(decoder.format()).ok();
+ let width = decoder.width();
+ let height = decoder.height();
+
+ (
+ stream_index,
+ time_base,
+ fps,
+ duration_secs,
+ pixel_format,
+ width,
+ height,
+ )
};
let mut keyframes = Vec::new();
- let mut input =
- avformat::input(path).map_err(|e| format!("Failed to reopen video for scan: {e}"))?;
-
for (stream, packet) in input.packets() {
if stream.index() != stream_index {
continue;
@@ -83,6 +105,9 @@ impl KeyframeIndex {
keyframes,
fps,
duration_secs,
+ pixel_format,
+ width,
+ height,
})
}
@@ -177,6 +202,10 @@ impl KeyframeIndex {
pub fn keyframes(&self) -> &[(u32, f64)] {
&self.keyframes
}
+
+ pub fn cached_video_info(&self) -> Option<(cv::PixelFormat, u32, u32)> {
+ self.pixel_format.map(|pf| (pf, self.width, self.height))
+ }
}
fn compute_seek_time(keyframe_index: Option<&Arc<KeyframeIndex>>, requested_time: f32) -> f32 {
@@ -236,7 +265,12 @@ impl AVAssetReaderDecoder {
start_time: f32,
keyframe_index: Option<Arc<KeyframeIndex>>,
) -> Result<Self, String> {
- let (pixel_format, width, height) = {
+ let (pixel_format, width, height) = if let Some(info) = keyframe_index
+ .as_ref()
+ .and_then(|ki| ki.cached_video_info())
+ {
+ info
+ } else {
let input = ffmpeg::format::input(&path)
.map_err(|e| format!("Failed to open video input '{}': {e}", path.display()))?;