Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(rendering): read recording metadata from container headers
x1xhlol24785404c575
diff --git a/crates/rendering/src/project_recordings.rs b/crates/rendering/src/project_recordings.rsindex 7d7718d5e..e0fbdd9d2 100644--- a/crates/rendering/src/project_recordings.rs+++ b/crates/rendering/src/project_recordings.rs@@ -1,12 +1,71 @@use std::{cell::RefCell,+ ffi::CString,path::{Path, PathBuf},+ ptr,};use cap_project::{AudioMeta, StudioRecordingMeta, VideoMeta};+use ffmpeg::Rescale;use serde::Serialize;use specta::Type;+fn open_headers(path: &Path) -> Option<ffmpeg::format::context::Input> {+ let path = CString::new(path.to_str()?).ok()?;+ let mut context = ptr::null_mut();+ let result = unsafe {+ ffmpeg::ffi::avformat_open_input(&mut context, path.as_ptr(), ptr::null(), ptr::null_mut())+ };++ if result < 0 {+ return None;+ }++ Some(unsafe { ffmpeg::format::context::Input::wrap(context) })+}++fn header_duration_secs(input: &ffmpeg::format::context::Input) -> Option<f64> {+ let mut min_start = None;+ let mut max_end = None;+ let mut max_duration = 0_i64;++ for stream in input.streams() {+ let time_base = stream.time_base();+ let start_time = stream.start_time();+ let duration = stream.duration();++ if start_time != ffmpeg::ffi::AV_NOPTS_VALUE {+ let start = start_time.rescale(time_base, ffmpeg::rescale::TIME_BASE);+ min_start = Some(min_start.map_or(start, |min: i64| min.min(start)));++ if duration != ffmpeg::ffi::AV_NOPTS_VALUE {+ let duration = unsafe {+ ffmpeg::ffi::av_rescale_q_rnd(+ duration,+ time_base.into(),+ ffmpeg::ffi::AV_TIME_BASE_Q,+ ffmpeg::ffi::AVRounding::AV_ROUND_NEAR_INF,+ )+ };+ let end = start + duration;+ max_end = Some(max_end.map_or(end, |max: i64| max.max(end)));+ }+ }++ if duration != ffmpeg::ffi::AV_NOPTS_VALUE {+ let duration = duration.rescale(time_base, ffmpeg::rescale::TIME_BASE);+ max_duration = max_duration.max(duration);+ }+ }++ let duration = match (min_start, max_end) {+ (Some(start), Some(end)) if end - start > max_duration => end - start,+ _ => max_duration,+ };++ (duration > 0).then_some(duration as f64 / 1_000_000.0)+}+#[derive(Debug, Clone, Copy, Serialize, Type)]pub struct Video {pub duration: f64,@@ -23,12 +82,43 @@ impl Video {let input = cap_video_decode::ffmpeg::open_fragmented_input(path)?;Self::from_input(input.input(), start_time)} else {+ if let Some(video) =+ open_headers(path).and_then(|input| Self::from_headers(&input, start_time))+ {+ return Ok(video);+ }+let input = ffmpeg::format::input(path).map_err(|error| format!("Failed to open video: {error}"))?;Self::from_input(&input, start_time)}}+ fn from_headers(input: &ffmpeg::format::context::Input, start_time: f64) -> Option<Self> {+ let stream = input.streams().best(ffmpeg::media::Type::Video)?;+ let parameters = stream.parameters();+ let parameters = unsafe { &*parameters.as_ptr() };++ if parameters.width <= 0 || parameters.height <= 0 {+ return None;+ }++ let rate = stream.avg_frame_rate();+ if rate.numerator() <= 0 || rate.denominator() <= 0 {+ return None;+ }++ let fps = rate.numerator() as f64 / rate.denominator() as f64;++ Some(Video {+ duration: header_duration_secs(input)?,+ width: parameters.width as u32,+ height: parameters.height as u32,+ fps: fps.round() as u32,+ start_time,+ })+ }+pub fn from_input(input: &ffmpeg::format::context::Input,start_time: f64,@@ -111,11 +201,37 @@ pub struct Audio {impl Audio {pub fn new(path: impl AsRef<Path>, start_time: f64) -> Result<Self, String> {- let input = ffmpeg::format::input(path.as_ref())- .map_err(|e| format!("Failed to open audio: {e}"))?;+ let path = path.as_ref();+ if let Some(audio) =+ open_headers(path).and_then(|input| Self::from_headers(&input, start_time))+ {+ return Ok(audio);+ }++ let input =+ ffmpeg::format::input(path).map_err(|e| format!("Failed to open audio: {e}"))?;Self::from_input(&input, start_time)}+ fn from_headers(input: &ffmpeg::format::context::Input, start_time: f64) -> Option<Self> {+ let stream = input.streams().best(ffmpeg::media::Type::Audio)?;+ let parameters = stream.parameters();+ let parameters = unsafe { &*parameters.as_ptr() };+ let sample_rate = u32::try_from(parameters.sample_rate)+ .ok()+ .filter(|sample_rate| *sample_rate > 0)?;+ let channels = u16::try_from(parameters.ch_layout.nb_channels)+ .ok()+ .filter(|channels| *channels > 0)?;++ Some(Audio {+ duration: header_duration_secs(input)?,+ sample_rate,+ channels,+ start_time,+ })+ }+pub fn from_input(input: &ffmpeg::format::context::Input,start_time: f64,@@ -284,9 +400,129 @@ impl SegmentRecordings {#[cfg(test)]mod ordinary_media_access_tests {- use super::ProjectRecordingsMeta;+ use super::{Audio, ProjectRecordingsMeta, Video, open_headers};use cap_project::StudioRecordingMeta;- use std::path::PathBuf;+ use std::path::{Path, PathBuf};++ fn write_video_packets(+ encoder: &mut ffmpeg::codec::encoder::video::Encoder,+ output: &mut ffmpeg::format::context::Output,+ stream_index: usize,+ output_time_base: ffmpeg::Rational,+ ) {+ let mut packet = ffmpeg::Packet::empty();+ while encoder.receive_packet(&mut packet).is_ok() {+ packet.set_stream(stream_index);+ packet.rescale_ts(encoder.time_base(), output_time_base);+ packet.write_interleaved(output).unwrap();+ }+ }++ fn write_audio_packets(+ encoder: &mut ffmpeg::codec::encoder::audio::Encoder,+ output: &mut ffmpeg::format::context::Output,+ stream_index: usize,+ output_time_base: ffmpeg::Rational,+ ) {+ let mut packet = ffmpeg::Packet::empty();+ while encoder.receive_packet(&mut packet).is_ok() {+ packet.set_stream(stream_index);+ packet.rescale_ts(encoder.time_base(), output_time_base);+ packet.write_interleaved(output).unwrap();+ }+ }++ fn write_test_video(path: &Path, codec_id: ffmpeg::codec::Id) {+ ffmpeg::init().unwrap();+ let codec = ffmpeg::encoder::find(codec_id).unwrap_or_else(|| {+ panic!("bundled FFmpeg has no encoder for video codec {codec_id:?}")+ });+ let mut output = ffmpeg::format::output_as(path, "mp4").unwrap();+ let global_header = output+ .format()+ .flags()+ .contains(ffmpeg::format::Flags::GLOBAL_HEADER);+ let (stream_index, mut encoder) = {+ let mut stream = output.add_stream(codec).unwrap();+ let stream_index = stream.index();+ stream.set_time_base((1, 30));+ stream.set_rate((30, 1));+ stream.set_avg_frame_rate((30, 1));+ let mut encoder = ffmpeg::codec::context::Context::new_with_codec(codec)+ .encoder()+ .video()+ .unwrap();+ encoder.set_width(64);+ encoder.set_height(48);+ encoder.set_format(ffmpeg::format::Pixel::YUV420P);+ encoder.set_frame_rate(Some((30, 1)));+ encoder.set_time_base((1, 30));+ if global_header {+ encoder.set_flags(ffmpeg::codec::Flags::GLOBAL_HEADER);+ }+ stream.set_parameters(&encoder);+ let encoder = encoder.open_as(codec).unwrap();+ stream.set_parameters(&encoder);+ (stream_index, encoder)+ };+ output.write_header().unwrap();+ let output_time_base = output.stream(stream_index).unwrap().time_base();++ let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::YUV420P, 64, 48);+ frame.data_mut(0).fill(16);+ frame.data_mut(1).fill(128);+ frame.data_mut(2).fill(128);+ for pts in 0..45 {+ frame.set_pts(Some(pts));+ encoder.send_frame(&frame).unwrap();+ write_video_packets(&mut encoder, &mut output, stream_index, output_time_base);+ }++ encoder.send_eof().unwrap();+ write_video_packets(&mut encoder, &mut output, stream_index, output_time_base);+ output.write_trailer().unwrap();+ }++ fn write_test_audio(path: &Path) {+ ffmpeg::init().unwrap();+ let codec = ffmpeg::encoder::find(ffmpeg::codec::Id::PCM_S16LE).unwrap();+ let mut output = ffmpeg::format::output_as(path, "wav").unwrap();++ let mut encoder = ffmpeg::codec::context::Context::new_with_codec(codec)+ .encoder()+ .audio()+ .unwrap();+ encoder.set_rate(48_000);+ encoder.set_channel_layout(ffmpeg::ChannelLayout::STEREO);+ encoder.set_format(ffmpeg::format::Sample::I16(+ ffmpeg::format::sample::Type::Packed,+ ));+ encoder.set_time_base((1, 48_000));+ let mut encoder = encoder.open_as(codec).unwrap();+ let stream_index = {+ let mut stream = output.add_stream(codec).unwrap();+ let stream_index = stream.index();+ stream.set_time_base((1, 48_000));+ stream.set_parameters(&encoder);+ stream_index+ };+ output.write_header().unwrap();+ let output_time_base = output.stream(stream_index).unwrap().time_base();++ let mut frame = ffmpeg::frame::Audio::new(+ ffmpeg::format::Sample::I16(ffmpeg::format::sample::Type::Packed),+ 72_000,+ ffmpeg::ChannelLayout::STEREO,+ );+ frame.set_rate(48_000);+ frame.set_pts(Some(0));+ frame.data_mut(0).fill(0);+ encoder.send_frame(&frame).unwrap();+ write_audio_packets(&mut encoder, &mut output, stream_index, output_time_base);+ encoder.send_eof().unwrap();+ write_audio_packets(&mut encoder, &mut output, stream_index, output_time_base);+ output.write_trailer().unwrap();+ }#[test]fn failed_project_refuses_before_opening_invalid_media() {@@ -300,4 +536,68 @@ mod ordinary_media_access_tests {};assert!(error.contains("requested source failed"));}++ #[test]+ fn h264_video_headers_match_full_probe() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("video.mp4");+ write_test_video(&path, ffmpeg::codec::Id::H264);++ let full_input = ffmpeg::format::input(&path).unwrap();+ let expected = Video::from_input(&full_input, 0.25).unwrap();+ let header_input = open_headers(&path).expect("header open should succeed");+ let actual =+ Video::from_headers(&header_input, 0.25).expect("video headers should be sufficient");++ assert_eq!(actual.width, expected.width);+ assert_eq!(actual.height, expected.height);+ assert_eq!(actual.fps, expected.fps);+ assert_eq!(actual.duration, expected.duration);+ assert_eq!(actual.start_time, expected.start_time);+ }++ #[test]+ fn mpeg4_video_falls_back_to_full_probe() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("video.mp4");+ write_test_video(&path, ffmpeg::codec::Id::MPEG4);++ let full_input = ffmpeg::format::input(&path).unwrap();+ let expected = Video::from_input(&full_input, 0.25).unwrap();+ let header_input = open_headers(&path).expect("header open should succeed");+ assert!(Video::from_headers(&header_input, 0.25).is_none());++ let actual = Video::new(&path, 0.25).unwrap();+ assert_eq!(actual.width, expected.width);+ assert_eq!(actual.height, expected.height);+ assert_eq!(actual.fps, expected.fps);+ assert_eq!(actual.duration, expected.duration);+ assert_eq!(actual.start_time, expected.start_time);+ }++ #[test]+ fn audio_headers_match_full_probe() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("audio.wav");+ write_test_audio(&path);++ let full_input = ffmpeg::format::input(&path).unwrap();+ let expected = Audio::from_input(&full_input, 0.75).unwrap();+ let header_input = open_headers(&path).expect("header open should succeed");+ let actual =+ Audio::from_headers(&header_input, 0.75).expect("audio headers should be sufficient");++ assert_eq!(actual.sample_rate, expected.sample_rate);+ assert_eq!(actual.channels, expected.channels);+ assert_eq!(actual.duration, expected.duration);+ assert_eq!(actual.start_time, expected.start_time);+ }++ #[test]+ fn nonexistent_video_still_returns_an_error() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("missing.mp4");++ assert!(Video::new(path, 0.0).is_err());+ }}