Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Refactor FFmpeg decoder forcing for export and preview

Richie McIlroy17763a7f0ae4
diff --git a/apps/desktop/src-tauri/src/export.rs b/apps/desktop/src-tauri/src/export.rs
index b2dde7d1d..e461e0371 100644
--- a/apps/desktop/src-tauri/src/export.rs
+++ b/apps/desktop/src-tauri/src/export.rs
@@ -10,7 +10,7 @@ use image::codecs::jpeg::JpegEncoder;
use serde::{Deserialize, Serialize};
use specta::Type;
use std::{
- path::PathBuf,
+ path::{Path, PathBuf},
sync::{Arc, atomic::Ordering},
};
use tracing::{info, instrument};
@@ -32,11 +32,13 @@ impl ExportSettings {
}
async fn do_export(
- project_path: &PathBuf,
+ project_path: &Path,
settings: &ExportSettings,
progress: &tauri::ipc::Channel<FramesRendered>,
+ force_ffmpeg: bool,
) -> Result<PathBuf, String> {
- let exporter_base = ExporterBase::builder(project_path.clone())
+ let exporter_base = ExporterBase::builder(project_path.to_path_buf())
+ .with_force_ffmpeg_decoder(force_ffmpeg)
.build()
.await
.map_err(|e| e.to_string())?;
@@ -80,7 +82,6 @@ async fn do_export(
fn is_frame_decode_error(error: &str) -> bool {
error.contains("Failed to decode video frames")
- || error.contains("FrameDecodeFailed")
|| error.contains("Too many consecutive frame failures")
}
@@ -96,13 +97,11 @@ pub async fn export_video(
ExportSettings::Mp4(s) => s.force_ffmpeg_decoder,
ExportSettings::Gif(_) => false,
};
- cap_rendering::set_force_ffmpeg_decoder(force_ffmpeg);
- let result = do_export(&project_path, &settings, &progress).await;
+ let result = do_export(&project_path, &settings, &progress, force_ffmpeg).await;
match result {
Ok(path) => {
- cap_rendering::set_force_ffmpeg_decoder(false);
info!("Exported to {} completed", path.display());
Ok(path)
}
@@ -112,11 +111,7 @@ pub async fn export_video(
e
);
- cap_rendering::set_force_ffmpeg_decoder(true);
-
- let retry_result = do_export(&project_path, &settings, &progress).await;
-
- cap_rendering::set_force_ffmpeg_decoder(false);
+ let retry_result = do_export(&project_path, &settings, &progress, true).await;
match retry_result {
Ok(path) => {
@@ -133,7 +128,6 @@ pub async fn export_video(
}
}
Err(e) => {
- cap_rendering::set_force_ffmpeg_decoder(false);
sentry::capture_message(&e, sentry::Level::Error);
Err(e)
}
@@ -279,7 +273,7 @@ pub async fn generate_export_preview(
.map_err(|e| format!("Failed to create render constants: {e}"))?,
);
- let segments = create_segments(&recording_meta, studio_meta)
+ let segments = create_segments(&recording_meta, studio_meta, false)
.await
.map_err(|e| format!("Failed to create segments: {e}"))?;
diff --git a/apps/desktop/src/routes/editor/ExportPage.tsx b/apps/desktop/src/routes/editor/ExportPage.tsx
index 5033de03e..fd8e011fa 100644
--- a/apps/desktop/src/routes/editor/ExportPage.tsx
+++ b/apps/desktop/src/routes/editor/ExportPage.tsx
@@ -1086,12 +1086,13 @@ export function ExportPage() {
</p>
</Show>
- <Show
- when={ostype() === "macos" && settings.format === "Mp4"}
- >
+ <Show when={ostype() === "macos"}>
<div class="mt-4 pt-3 border-t border-gray-4">
<button
type="button"
+ role="switch"
+ aria-checked={forceFfmpegDecoder()}
+ aria-label="Force FFmpeg decoder"
class="flex items-center gap-2 text-xs text-gray-11 hover:text-gray-12 transition-colors w-full"
onClick={() =>
setForceFfmpegDecoder(!forceFfmpegDecoder())
diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs
index fde6d9d87..f118c0786 100644
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@ -248,7 +248,7 @@ impl EditorInstance {
meta.as_ref(),
)?);
- let segments = create_segments(&recording_meta, meta.as_ref()).await?;
+ let segments = create_segments(&recording_meta, meta.as_ref(), false).await?;
let render_constants = Arc::new(
RenderVideoConstants::new(
@@ -581,6 +581,7 @@ pub struct SegmentMedia {
pub async fn create_segments(
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
+ force_ffmpeg: bool,
) -> Result<Vec<SegmentMedia>, String> {
match &meta {
cap_project::StudioRecordingMeta::SingleSegment { segment: s } => {
@@ -622,6 +623,7 @@ pub async fn create_segments(
camera: s.camera.as_ref().map(|c| recording_meta.path(&c.path)),
},
0,
+ force_ffmpeg,
)
.await
.map_err(|e| format!("SingleSegment / {e}"))?;
@@ -667,6 +669,7 @@ pub async fn create_segments(
camera: s.camera.as_ref().map(|c| recording_meta.path(&c.path)),
},
i,
+ force_ffmpeg,
)
.await
.map_err(|e| format!("MultipleSegments {i} / {e}"))?;
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs
index 4b187f7c9..60b9d675f 100644
--- a/crates/export/src/lib.rs
+++ b/crates/export/src/lib.rs
@@ -52,6 +52,7 @@ pub struct ExporterBuilder {
project_path: PathBuf,
config: Option<ProjectConfiguration>,
output_path: Option<PathBuf>,
+ force_ffmpeg_decoder: bool,
}
impl ExporterBuilder {
@@ -60,6 +61,11 @@ impl ExporterBuilder {
self
}
+ pub fn with_force_ffmpeg_decoder(mut self, force: bool) -> Self {
+ self.force_ffmpeg_decoder = force;
+ self
+ }
+
pub async fn build(self) -> Result<ExporterBase, ExporterBuildError> {
type Error = ExporterBuildError;
@@ -90,9 +96,10 @@ impl ExporterBuilder {
.map_err(Error::RendererSetup)?,
);
- let segments = cap_editor::create_segments(&recording_meta, studio_meta)
- .await
- .map_err(Error::MediaLoad)?;
+ let segments =
+ cap_editor::create_segments(&recording_meta, studio_meta, self.force_ffmpeg_decoder)
+ .await
+ .map_err(Error::MediaLoad)?;
let output_path = self
.output_path
@@ -144,6 +151,7 @@ impl ExporterBase {
project_path,
config: None,
output_path: None,
+ force_ffmpeg_decoder: false,
}
}
}
diff --git a/crates/rendering/src/decoder/mod.rs b/crates/rendering/src/decoder/mod.rs
index f4cc76895..5b39876a3 100644
--- a/crates/rendering/src/decoder/mod.rs
+++ b/crates/rendering/src/decoder/mod.rs
@@ -2,29 +2,12 @@ use ::ffmpeg::Rational;
use std::{
fmt,
path::PathBuf,
- sync::{
- Arc,
- atomic::{AtomicBool, Ordering},
- mpsc,
- },
+ sync::{Arc, mpsc},
time::Duration,
};
use tokio::sync::oneshot;
use tracing::info;
-static FORCE_FFMPEG_DECODER: AtomicBool = AtomicBool::new(false);
-
-pub fn set_force_ffmpeg_decoder(force: bool) {
- FORCE_FFMPEG_DECODER.store(force, Ordering::SeqCst);
- if force {
- tracing::info!("FFmpeg decoder forced via experimental setting");
- }
-}
-
-pub fn is_ffmpeg_decoder_forced() -> bool {
- FORCE_FFMPEG_DECODER.load(Ordering::SeqCst)
-}
-
#[cfg(target_os = "macos")]
mod avassetreader;
mod ffmpeg;
@@ -569,13 +552,14 @@ pub async fn spawn_decoder(
path: PathBuf,
fps: u32,
offset: f64,
+ force_ffmpeg: bool,
) -> Result<AsyncVideoDecoderHandle, String> {
let path_display = path.display().to_string();
let timeout_duration = Duration::from_secs(30);
#[cfg(target_os = "macos")]
{
- if is_ffmpeg_decoder_forced() {
+ if force_ffmpeg {
info!(
"Video '{}' using FFmpeg decoder (forced via experimental setting)",
name
@@ -668,6 +652,7 @@ pub async fn spawn_decoder(
#[cfg(target_os = "windows")]
{
+ let _ = force_ffmpeg;
let (ready_tx, ready_rx) = oneshot::channel::<Result<DecoderInitResult, String>>();
let (tx, rx) = mpsc::channel();
@@ -704,6 +689,7 @@ pub async fn spawn_decoder(
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
{
+ let _ = force_ffmpeg;
let (ready_tx, ready_rx) = oneshot::channel::<Result<DecoderInitResult, String>>();
let (tx, rx) = mpsc::channel();
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index b9c144422..c110af62d 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -7,7 +7,6 @@ use composite_frame::CompositeVideoFrameUniforms;
use core::f64;
use cursor_interpolation::{InterpolatedCursorPosition, interpolate_cursor};
use decoder::{AsyncVideoDecoderHandle, spawn_decoder};
-pub use decoder::{is_ffmpeg_decoder_forced, set_force_ffmpeg_decoder};
use frame_pipeline::{RenderSession, finish_encoder};
use futures::FutureExt;
use futures::future::OptionFuture;
@@ -121,6 +120,7 @@ impl RecordingSegmentDecoders {
meta: &StudioRecordingMeta,
segment: SegmentVideoPaths,
segment_i: usize,
+ force_ffmpeg: bool,
) -> Result<Self, String> {
let latest_start_time = match &meta {
StudioRecordingMeta::SingleSegment { .. } => None,
@@ -149,6 +149,7 @@ impl RecordingSegmentDecoders {
.unwrap_or(0.0)
}
},
+ force_ffmpeg,
)
.await
.map_err(|e| format!("Screen:{e}"))?;
@@ -175,6 +176,7 @@ impl RecordingSegmentDecoders {
.unwrap_or(0.0)
}
},
+ force_ffmpeg,
)
.then(|r| async { r.map_err(|e| format!("Camera:{e}")) })
}))
diff --git a/crates/rendering/src/main.rs b/crates/rendering/src/main.rs
index afdde2020..3fa4d028f 100644
--- a/crates/rendering/src/main.rs
+++ b/crates/rendering/src/main.rs
@@ -101,6 +101,7 @@ async fn main() -> Result<()> {
.map(|c| recording_meta.path(&c.path)),
},
0,
+ false,
)
.await
.map_err(|e| anyhow::anyhow!("Failed to create decoders for single segment: {}", e))?;
@@ -121,6 +122,7 @@ async fn main() -> Result<()> {
camera: s.camera.as_ref().map(|c| recording_meta.path(&c.path)),
},
i,
+ false,
)
.await
.map_err(|e| {