Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: flush timed video fragments for Studio recordings

Richie McIlroyd345a9b1b440
diff --git a/crates/enc-ffmpeg/src/mux/segmented_stream.rs b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
index 3b6091ceb..d252a921c 100644
--- a/crates/enc-ffmpeg/src/mux/segmented_stream.rs
+++ b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
@@ -197,6 +197,15 @@ impl SegmentedVideoEncoder {
base_path: PathBuf,
video_config: VideoInfo,
config: SegmentedVideoEncoderConfig,
+ ) -> Result<Self, InitError> {
+ Self::init_with_timed_fragments(base_path, video_config, config, false)
+ }
+
+ pub fn init_with_timed_fragments(
+ base_path: PathBuf,
+ video_config: VideoInfo,
+ config: SegmentedVideoEncoderConfig,
+ timed_fragments: bool,
) -> Result<Self, InitError> {
std::fs::create_dir_all(&base_path)?;
@@ -231,6 +240,11 @@ impl SegmentedVideoEncoder {
set_opt("use_template", "1");
set_opt("single_file", "0");
set_opt("hls_playlist", "1");
+ if timed_fragments {
+ set_opt("frag_type", "duration");
+ set_opt("frag_duration", "2");
+ set_opt("streaming", "1");
+ }
}
let mut builder = H264EncoderBuilder::new(video_config)
diff --git a/crates/recording/src/capture_pipeline.rs b/crates/recording/src/capture_pipeline.rs
index c55cf72e2..d935e0e30 100644
--- a/crates/recording/src/capture_pipeline.rs
+++ b/crates/recording/src/capture_pipeline.rs
@@ -177,6 +177,7 @@ impl MakeCapturePipeline for screen_capture::CMSampleBufferCapture {
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.build::<MacOSFragmentedM4SMuxer>(MacOSFragmentedM4SMuxerConfig {
+ timed_fragments: true,
preset,
bpp,
output_size,
@@ -218,6 +219,7 @@ impl MakeCapturePipeline for screen_capture::CMSampleBufferCapture {
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.build::<MacOSFragmentedM4SMuxer>(MacOSFragmentedM4SMuxerConfig {
+ timed_fragments: false,
bpp: H264EncoderBuilder::INSTANT_MODE_BPP,
output_size: Some(output_size),
segment_tx,
@@ -310,6 +312,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.build::<WindowsFragmentedM4SMuxer>(WindowsFragmentedM4SMuxerConfig {
+ timed_fragments: true,
segment_duration: std::time::Duration::from_secs(2),
preset,
bpp,
@@ -358,6 +361,7 @@ impl MakeCapturePipeline for screen_capture::Direct3DCapture {
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.build::<WindowsFragmentedM4SMuxer>(WindowsFragmentedM4SMuxerConfig {
+ timed_fragments: false,
segment_duration: std::time::Duration::from_secs(2),
preset: H264Preset::Ultrafast,
bpp: H264EncoderBuilder::INSTANT_MODE_BPP,
@@ -394,6 +398,7 @@ impl MakeCapturePipeline for screen_capture::X11Capture {
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.build::<crate::ffmpeg::SegmentedVideoMuxer>(crate::ffmpeg::SegmentedVideoMuxerConfig {
+ timed_fragments: true,
segment_duration: std::time::Duration::from_secs(2),
preset: if ultra {
H264Preset::Medium
@@ -420,6 +425,7 @@ impl MakeCapturePipeline for screen_capture::X11Capture {
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.build::<crate::ffmpeg::SegmentedVideoMuxer>(crate::ffmpeg::SegmentedVideoMuxerConfig {
+ timed_fragments: false,
segment_duration: std::time::Duration::from_secs(2),
preset: H264Preset::Ultrafast,
output_size: Some(output_size),
diff --git a/crates/recording/src/instant_recording.rs b/crates/recording/src/instant_recording.rs
index 759025f1f..18776eaaa 100644
--- a/crates/recording/src/instant_recording.rs
+++ b/crates/recording/src/instant_recording.rs
@@ -764,6 +764,7 @@ async fn create_pipeline(
.with_timestamps(start_time)
.with_start_gate(start_gate.clone())
.build::<crate::ffmpeg::SegmentedVideoMuxer>(crate::ffmpeg::SegmentedVideoMuxerConfig {
+ timed_fragments: false,
segment_duration: std::time::Duration::from_secs(2),
preset: cap_enc_ffmpeg::h264::H264Preset::Ultrafast,
output_size: Some(output_resolution),
diff --git a/crates/recording/src/output_pipeline/ffmpeg.rs b/crates/recording/src/output_pipeline/ffmpeg.rs
index 1c2f6cbd0..013528c27 100644
--- a/crates/recording/src/output_pipeline/ffmpeg.rs
+++ b/crates/recording/src/output_pipeline/ffmpeg.rs
@@ -444,6 +444,7 @@ struct SegmentedEncoderState {
}
pub struct SegmentedVideoMuxer {
+ timed_fragments: bool,
base_path: PathBuf,
video_config: VideoInfo,
segment_duration: Duration,
@@ -458,6 +459,7 @@ pub struct SegmentedVideoMuxer {
}
pub struct SegmentedVideoMuxerConfig {
+ pub timed_fragments: bool,
pub segment_duration: Duration,
pub preset: H264Preset,
pub output_size: Option<(u32, u32)>,
@@ -468,6 +470,7 @@ pub struct SegmentedVideoMuxerConfig {
impl Default for SegmentedVideoMuxerConfig {
fn default() -> Self {
Self {
+ timed_fragments: false,
segment_duration: Duration::from_secs(3),
preset: H264Preset::Ultrafast,
output_size: None,
@@ -502,6 +505,7 @@ impl Muxer for SegmentedVideoMuxer {
.unwrap_or_else(|| SharedPauseState::new(pause_flag));
Ok(Self {
+ timed_fragments: config.timed_fragments,
base_path: output_path,
video_config,
segment_duration: config.segment_duration,
@@ -665,8 +669,12 @@ impl SegmentedVideoMuxer {
};
let slow_threshold_ms = frame_timing_log_threshold_ms(&self.video_config);
- let mut encoder =
- SegmentedVideoEncoder::init(self.base_path.clone(), self.video_config, encoder_config)?;
+ let mut encoder = SegmentedVideoEncoder::init_with_timed_fragments(
+ self.base_path.clone(),
+ self.video_config,
+ encoder_config,
+ self.timed_fragments,
+ )?;
if let Some(tx) = &self.segment_tx {
encoder.set_segment_callback(tx.clone());
}
diff --git a/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs b/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
index 6c490b64e..d2c6f7cec 100644
--- a/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
+++ b/crates/recording/src/output_pipeline/macos_fragmented_m4s.rs
@@ -154,6 +154,7 @@ struct EncoderState {
}
pub struct MacOSFragmentedM4SMuxer {
+ timed_fragments: bool,
base_path: PathBuf,
video_config: VideoInfo,
segment_duration: Duration,
@@ -170,6 +171,7 @@ pub struct MacOSFragmentedM4SMuxer {
}
pub struct MacOSFragmentedM4SMuxerConfig {
+ pub timed_fragments: bool,
pub segment_duration: Duration,
pub preset: H264Preset,
pub bpp: f32,
@@ -182,6 +184,7 @@ pub struct MacOSFragmentedM4SMuxerConfig {
impl Default for MacOSFragmentedM4SMuxerConfig {
fn default() -> Self {
Self {
+ timed_fragments: false,
segment_duration: Duration::from_secs(2),
preset: H264Preset::Ultrafast,
bpp: H264EncoderBuilder::QUALITY_BPP,
@@ -218,6 +221,7 @@ impl Muxer for MacOSFragmentedM4SMuxer {
.unwrap_or_else(|| SharedPauseState::new(pause_flag));
Ok(Self {
+ timed_fragments: config.timed_fragments,
base_path: output_path,
video_config,
segment_duration: config.segment_duration,
@@ -295,8 +299,12 @@ impl MacOSFragmentedM4SMuxer {
prefer_videotoolbox_hw_input: true,
};
- let mut encoder =
- SegmentedVideoEncoder::init(self.base_path.clone(), self.video_config, encoder_config)?;
+ let mut encoder = SegmentedVideoEncoder::init_with_timed_fragments(
+ self.base_path.clone(),
+ self.video_config,
+ encoder_config,
+ self.timed_fragments,
+ )?;
if let Some(callback) = &self.disk_space_callback {
encoder.set_disk_space_callback(callback.clone());
}
@@ -682,8 +690,12 @@ impl MacOSFragmentedM4SCameraMuxer {
prefer_videotoolbox_hw_input: false,
};
- let mut encoder =
- SegmentedVideoEncoder::init(self.base_path.clone(), self.video_config, encoder_config)?;
+ let mut encoder = SegmentedVideoEncoder::init_with_timed_fragments(
+ self.base_path.clone(),
+ self.video_config,
+ encoder_config,
+ true,
+ )?;
if let Some(callback) = &self.disk_space_callback {
encoder.set_disk_space_callback(callback.clone());
}
diff --git a/crates/recording/src/output_pipeline/win_fragmented_m4s.rs b/crates/recording/src/output_pipeline/win_fragmented_m4s.rs
index 208ab9f4e..303c82ec6 100644
--- a/crates/recording/src/output_pipeline/win_fragmented_m4s.rs
+++ b/crates/recording/src/output_pipeline/win_fragmented_m4s.rs
@@ -199,6 +199,7 @@ struct EncoderState {
}
pub struct WindowsFragmentedM4SMuxer {
+ timed_fragments: bool,
base_path: PathBuf,
video_config: VideoInfo,
segment_duration: Duration,
@@ -216,6 +217,7 @@ pub struct WindowsFragmentedM4SMuxer {
}
pub struct WindowsFragmentedM4SMuxerConfig {
+ pub timed_fragments: bool,
pub segment_duration: Duration,
pub preset: H264Preset,
pub bpp: f32,
@@ -229,6 +231,7 @@ pub struct WindowsFragmentedM4SMuxerConfig {
impl Default for WindowsFragmentedM4SMuxerConfig {
fn default() -> Self {
Self {
+ timed_fragments: false,
segment_duration: Duration::from_secs(2),
preset: H264Preset::Ultrafast,
bpp: H264EncoderBuilder::QUALITY_BPP,
@@ -265,6 +268,7 @@ impl Muxer for WindowsFragmentedM4SMuxer {
.unwrap_or_else(|| SharedPauseState::new(pause_flag));
let mut muxer = Self {
+ timed_fragments: config.timed_fragments,
base_path: output_path,
video_config,
segment_duration: config.segment_duration,
@@ -337,8 +341,12 @@ impl WindowsFragmentedM4SMuxer {
prefer_videotoolbox_hw_input: false,
};
- let mut encoder =
- SegmentedVideoEncoder::init(self.base_path.clone(), self.video_config, encoder_config)?;
+ let mut encoder = SegmentedVideoEncoder::init_with_timed_fragments(
+ self.base_path.clone(),
+ self.video_config,
+ encoder_config,
+ self.timed_fragments,
+ )?;
if let Some(callback) = &self.disk_space_callback {
encoder.set_disk_space_callback(callback.clone());
}
@@ -805,8 +813,12 @@ impl WindowsFragmentedM4SCameraMuxer {
prefer_videotoolbox_hw_input: false,
};
- let mut encoder =
- SegmentedVideoEncoder::init(self.base_path.clone(), self.video_config, encoder_config)?;
+ let mut encoder = SegmentedVideoEncoder::init_with_timed_fragments(
+ self.base_path.clone(),
+ self.video_config,
+ encoder_config,
+ true,
+ )?;
if let Some(callback) = &self.disk_space_callback {
encoder.set_disk_space_callback(callback.clone());
}
diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs
index 762ae4857..0ba38ba56 100644
--- a/crates/recording/src/studio_recording.rs
+++ b/crates/recording/src/studio_recording.rs
@@ -3319,6 +3319,7 @@ async fn create_segment_pipeline(
builder
.build::<crate::ffmpeg::SegmentedVideoMuxer>(
crate::ffmpeg::SegmentedVideoMuxerConfig {
+ timed_fragments: true,
segment_duration: Duration::from_secs(2),
shared_pause_state: shared_pause_state.clone(),
..Default::default()
@@ -3522,6 +3523,7 @@ async fn create_segment_pipeline(
.with_start_gate(start_gate.clone())
.build::<crate::ffmpeg::SegmentedVideoMuxer>(
crate::ffmpeg::SegmentedVideoMuxerConfig {
+ timed_fragments: true,
segment_duration: Duration::from_secs(2),
shared_pause_state: shared_pause_state.clone(),
..Default::default()