diff --git a/apps/cli/src/export.rs b/apps/cli/src/export.rs index 7beefbe6e93..e989ca005d2 100644 --- a/apps/cli/src/export.rs +++ b/apps/cli/src/export.rs @@ -3,7 +3,7 @@ use std::{ path::{Path, PathBuf}, sync::{ Arc, Mutex, - atomic::{AtomicBool, AtomicU32, Ordering}, + atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}, }, }; @@ -87,6 +87,12 @@ pub struct Export { /// Emit a final JSON completion event to stdout ({"type":"Completed","path":"..."}). Implied by --json #[arg(long)] completion_json: bool, + /// Export the recorded tracks as separate files instead of one video: a comma-separated list of + /// screen, camera, microphone, system-audio, or `all`. They follow the edited timeline and are + /// written to the --output folder (default '/output/tracks') as Screen.mp4, Camera.mp4, + /// Microphone.wav and System audio.wav. Takes --fps (default 60); one project at a time + #[arg(long, value_name = "TRACKS")] + tracks: Option, } #[derive(Default)] @@ -453,6 +459,11 @@ impl Export { completion_json: bool, stdout: &Arc>, ) -> Result<(), String> { + if let Some(tracks) = self.tracks.clone() { + return self + .run_tracks(&tracks, progress_json, completion_json, stdout) + .await; + } let settings = self.resolve_settings()?; let (projects, output) = split_projects_and_output(self.project_paths, self.output)?; @@ -659,6 +670,150 @@ impl Export { } } +impl Export { + fn tracks_conflicts(&self) -> Option<&'static str> { + if self.settings_json.is_some() { + Some("--settings-json") + } else if self.format.is_some() { + Some("--format") + } else if self.resolution.is_some() { + Some("--resolution") + } else if self.quality.is_some() { + Some("--quality") + } else if self.optimize_filesize { + Some("--optimize-filesize") + } else { + None + } + } + + async fn run_tracks( + self, + tracks: &str, + progress_json: bool, + completion_json: bool, + stdout: &Arc>, + ) -> Result<(), String> { + if let Some(flag) = self.tracks_conflicts() { + return Err(format!( + "--tracks cannot be combined with {flag}; tracks keep their recorded size and only take --fps" + )); + } + let fps = self.fps.unwrap_or_else(|| default_fps(ExportFormat::Mp4)); + if fps == 0 { + return Err("--fps must be greater than zero".to_string()); + } + let (projects, output) = split_projects_and_output(self.project_paths, self.output)?; + let [project_path] = projects.as_slice() else { + return Err("--tracks exports one project at a time".to_string()); + }; + + ensure_remuxed(project_path.clone()).await?; + let meta = RecordingMeta::load_for_project(project_path) + .map_err(|e| format!("Failed to load recording meta: {e}"))?; + let Some(studio) = meta.studio_meta() else { + return Err(cap_export::tracks::TrackExportError::NotStudio.to_string()); + }; + let selection = parse_tracks(tracks, || { + let recordings = cap_rendering::ProjectRecordingsMeta::new(&meta.project_path, studio)?; + Ok(cap_export::tracks::available_tracks( + &recordings, + &meta.project_config(), + )) + })?; + let output = output.unwrap_or_else(|| project_path.join("output").join("tracks")); + + let total = Arc::new(AtomicU64::new(0)); + let done = Arc::new(AtomicU64::new(0)); + let progress_stdout = Arc::clone(stdout); + let progress_done = Arc::clone(&done); + let progress_total = Arc::clone(&total); + let files = cap_export::tracks::export_tracks( + project_path.clone(), + None, + cap_export::tracks::TrackExportSettings { + tracks: selection, + fps, + force_ffmpeg_decoder: self.force_ffmpeg_decoder, + }, + output.clone(), + Arc::new(AtomicBool::new(false)), + move |progress| { + progress_done.store(progress.done, Ordering::Relaxed); + progress_total.store(progress.total, Ordering::Relaxed); + if progress_json { + let _ = emit_export_message( + &progress_stdout, + &ExportProgressMessage::Progress { + rendered_count: progress.done.min(u64::from(u32::MAX)) as u32, + total_frames: progress.total.min(u64::from(u32::MAX)) as u32, + }, + ); + } + }, + ) + .await + .map_err(|error| error.to_string())?; + + if progress_json || completion_json { + emit_export_message(stdout, &ExportProgressMessage::Completed { path: &output })?; + } else { + println!("Exported {} tracks to {}", files.len(), output.display()); + for file in &files { + if let Some(name) = file.file_name() { + println!(" {}", name.to_string_lossy()); + } + } + } + info!( + "Exported {} tracks to '{}' ({}/{} steps)", + files.len(), + output.display(), + done.load(Ordering::Relaxed), + total.load(Ordering::Relaxed) + ); + Ok(()) + } +} + +/// `all`, or a comma-separated list of track names, in a stable order. +fn parse_tracks( + value: &str, + available: impl FnOnce() -> Result, String>, +) -> Result, String> { + use cap_export::tracks::TrackKind; + let names = value + .split(',') + .map(str::trim) + .filter(|name| !name.is_empty()) + .collect::>(); + if names.is_empty() { + return Err( + "--tracks needs at least one of screen, camera, microphone, system-audio, or all" + .to_string(), + ); + } + if names.iter().any(|name| name.eq_ignore_ascii_case("all")) { + if names.len() > 1 { + return Err("--tracks all cannot be combined with other track names".to_string()); + } + return available(); + } + let mut tracks = names + .iter() + .map(|name| { + TrackKind::from_slug(name).ok_or_else(|| { + format!( + "Unknown track '{name}'; use screen, camera, microphone, system-audio, or all" + ) + }) + }) + .collect::, _>>()?; + tracks.sort(); + tracks.dedup(); + Ok(tracks) +} + /// Remux a recording left as fragments (status `NeedsRemux`) into a progressive `display.mp4` before /// export, reusing the shared `RecoveryManager`. A graceful `cap record` stop already remuxes in /// `finalize`, so this only fires for recordings interrupted before that (e.g. a killed worker); @@ -1021,6 +1176,31 @@ fn emit_export_message( mod tests { use super::*; + #[test] + fn track_lists_parse_in_a_stable_order() { + use cap_export::tracks::TrackKind; + let none = || -> Result, String> { panic!("only `all` lists the project") }; + assert_eq!( + parse_tracks("system-audio, mic,screen,mic", none).unwrap(), + [ + TrackKind::Screen, + TrackKind::Microphone, + TrackKind::SystemAudio + ] + ); + assert_eq!( + parse_tracks("ALL", || Ok(vec![TrackKind::Screen, TrackKind::Camera])).unwrap(), + [TrackKind::Screen, TrackKind::Camera] + ); + assert!(parse_tracks("all,screen", none).is_err()); + assert!(parse_tracks(" , ", none).is_err()); + assert!( + parse_tracks("screen,cursor", none) + .unwrap_err() + .contains("Unknown track 'cursor'") + ); + } + fn playable_instant_project() -> tempfile::TempDir { ffmpeg::init().unwrap(); let project = tempfile::tempdir().unwrap(); diff --git a/apps/cli/src/guide.rs b/apps/cli/src/guide.rs index 1fe6537afd4..8d93cc7f80e 100644 --- a/apps/cli/src/guide.rs +++ b/apps/cli/src/guide.rs @@ -194,7 +194,7 @@ fn build() -> Guide { notes: Some( "EXCEPTION: export NDJSON uses PascalCase `type` tags and snake_case fields \ (rendered_count, total_frames) for desktop compatibility. --format selects the \ - CONTAINER (mp4/gif/mov), NOT output mode; use --json for machine-readable output. Pass several projects to export them in one run; each is written to '.' inside the output directory.", + CONTAINER (mp4/gif/mov), NOT output mode; use --json for machine-readable output. Pass several projects to export them in one run; each is written to '.' inside the output directory. --tracks screen,camera,microphone,system-audio (or all) writes the recorded tracks as separate files that follow the edit into the --output folder; Completed.path is that folder.", ), ..cmd( "export", diff --git a/apps/desktop-gpui/locales/de.toml b/apps/desktop-gpui/locales/de.toml index 2db5c8c2d7e..dcc006ba2e2 100644 --- a/apps/desktop-gpui/locales/de.toml +++ b/apps/desktop-gpui/locales/de.toml @@ -2532,6 +2532,7 @@ format = "Format" frame_rate = "Bildrate" quality = "Qualität" resolution = "Auflösung" +tracks = "Spuren" [editor.export.stats] calculating = "Wird berechnet…" @@ -2544,6 +2545,25 @@ refining_size = "Größe wird präzisiert…" refining_time = "Dauer wird präzisiert…" unavailable = "Nicht verfügbar" +[editor.export.tracks] +aligned = "Spuren folgen Ihren Schnitten und Ihrer Geschwindigkeit und passen in jedem Videoeditor zusammen. Video wird ohne Hintergrund und Effekte exportiert." +audio_detail = "WAV · 48 kHz" +camera = "Kamera" +choose_one = "Wählen Sie mindestens eine Spur zum Exportieren." +cta = "Spuren exportieren" +exported = "Spuren exportiert" +exporting = "Spuren werden exportiert" +file_count = { one = "{count} Datei", other = "{count} Dateien" } +file_only = "Spuren werden als einzelne Dateien in einem Ordner gespeichert." +files = "Dateien" +format = "Spuren" +microphone = "Mikrofon" +open_folder = "Ordner öffnen" +screen = "Bildschirm" +studio_only = "Separate Spuren werden nur im Studio-Modus aufgenommen." +system_audio = "Systemaudio" +video_detail = "MP4 · {width}×{height}" + [editor.export.turbo] title = "Turbo-Export" description = "Exportiert deutlich schneller mit allen CPU-Kernen und der GPU" diff --git a/apps/desktop-gpui/locales/en.toml b/apps/desktop-gpui/locales/en.toml index 3608151ff90..0491efc5052 100644 --- a/apps/desktop-gpui/locales/en.toml +++ b/apps/desktop-gpui/locales/en.toml @@ -2534,6 +2534,7 @@ format = "Format" frame_rate = "Frame rate" quality = "Quality" resolution = "Resolution" +tracks = "Tracks" [editor.export.stats] calculating = "Calculating…" @@ -2546,6 +2547,25 @@ refining_size = "Refining size…" refining_time = "Refining time…" unavailable = "Unavailable" +[editor.export.tracks] +aligned = "Tracks follow your cuts and speed, so they line up in any video editor. Video comes out clean, without background or effects." +audio_detail = "WAV · 48 kHz" +camera = "Camera" +choose_one = "Choose at least one track to export." +cta = "Export Tracks" +exported = "Tracks exported" +exporting = "Exporting tracks" +file_count = { one = "{count} file", other = "{count} files", context = "How many files a tracks export writes." } +file_only = "Tracks are saved as separate files in a folder." +files = "Files" +format = { text = "Tracks", context = "Export format: separate screen, camera and audio files." } +microphone = "Microphone" +open_folder = "Open Folder" +screen = "Screen" +studio_only = "Separate tracks are only recorded in Studio Mode." +system_audio = "System audio" +video_detail = { text = "MP4 · {width}×{height}", context = "Video track file type and size, for example MP4 · 1920×1080." } + [editor.export.turbo] title = "Turbo export" description = "Exports much faster using every CPU core and the GPU" diff --git a/apps/desktop-gpui/locales/es.toml b/apps/desktop-gpui/locales/es.toml index 32b5f6b19ed..c1befacab8c 100644 --- a/apps/desktop-gpui/locales/es.toml +++ b/apps/desktop-gpui/locales/es.toml @@ -2532,6 +2532,7 @@ format = "Formato" frame_rate = "Velocidad de fotogramas" quality = "Calidad" resolution = "Resolución" +tracks = "Pistas" [editor.export.stats] calculating = "Calculando…" @@ -2544,6 +2545,25 @@ refining_size = "Ajustando tamaño…" refining_time = "Ajustando tiempo…" unavailable = "No disponible" +[editor.export.tracks] +aligned = "Las pistas siguen tus cortes y tu velocidad, así que encajan en cualquier editor de vídeo. El vídeo sale limpio, sin fondo ni efectos." +audio_detail = "WAV · 48 kHz" +camera = "Cámara" +choose_one = "Elige al menos una pista para exportar." +cta = "Exportar pistas" +exported = "Pistas exportadas" +exporting = "Exportando pistas" +file_count = { one = "{count} archivo", other = "{count} archivos" } +file_only = "Las pistas se guardan como archivos separados en una carpeta." +files = "Archivos" +format = "Pistas" +microphone = "Micrófono" +open_folder = "Abrir carpeta" +screen = "Pantalla" +studio_only = "Las pistas separadas solo se graban en el modo Studio." +system_audio = "Audio del sistema" +video_detail = "MP4 · {width}×{height}" + [editor.export.turbo] title = "Exportación turbo" description = "Exporta mucho más rápido usando todos los núcleos de la CPU y la GPU" diff --git a/apps/desktop-gpui/locales/fr.toml b/apps/desktop-gpui/locales/fr.toml index df70f9574f3..8115221348e 100644 --- a/apps/desktop-gpui/locales/fr.toml +++ b/apps/desktop-gpui/locales/fr.toml @@ -2532,6 +2532,7 @@ format = "Format" frame_rate = "Fréquence d'images" quality = "Qualité" resolution = "Résolution" +tracks = "Pistes" [editor.export.stats] calculating = "Calcul…" @@ -2544,6 +2545,25 @@ refining_size = "Affinage de la taille…" refining_time = "Affinage de la durée…" unavailable = "Indisponible" +[editor.export.tracks] +aligned = "Les pistes suivent vos coupes et votre vitesse, et s'alignent dans n'importe quel logiciel de montage. La vidéo sort propre, sans arrière-plan ni effets." +audio_detail = "WAV · 48 kHz" +camera = "Caméra" +choose_one = "Choisissez au moins une piste à exporter." +cta = "Exporter les pistes" +exported = "Pistes exportées" +exporting = "Exportation des pistes" +file_count = { one = "{count} fichier", other = "{count} fichiers" } +file_only = "Les pistes sont enregistrées dans un dossier, en fichiers séparés." +files = "Fichiers" +format = "Pistes" +microphone = "Micro" +open_folder = "Ouvrir le dossier" +screen = "Écran" +studio_only = "Les pistes séparées ne sont enregistrées qu'en mode Studio." +system_audio = "Audio système" +video_detail = "MP4 · {width}×{height}" + [editor.export.turbo] title = "Exportation turbo" description = "Exporte beaucoup plus vite en utilisant tous les cœurs du processeur et le GPU" diff --git a/apps/desktop-gpui/locales/hi.toml b/apps/desktop-gpui/locales/hi.toml index ec91d733d1e..a7dfa8e5280 100644 --- a/apps/desktop-gpui/locales/hi.toml +++ b/apps/desktop-gpui/locales/hi.toml @@ -2532,6 +2532,7 @@ format = "फ़ॉर्मैट" frame_rate = "फ़्रेम रेट" quality = "क्वालिटी" resolution = "रेज़ोल्यूशन" +tracks = "ट्रैक" [editor.export.stats] calculating = "गणना हो रही है…" @@ -2544,6 +2545,25 @@ refining_size = "साइज़ सटीक किया जा रहा ह refining_time = "समय सटीक किया जा रहा है…" unavailable = "उपलब्ध नहीं" +[editor.export.tracks] +aligned = "ट्रैक आपके कट और स्पीड के हिसाब से चलते हैं, इसलिए किसी भी वीडियो एडिटर में एक साथ मेल खाते हैं। वीडियो बिना बैकग्राउंड या इफ़ेक्ट के साफ़ निकलता है।" +audio_detail = "WAV · 48 kHz" +camera = "कैमरा" +choose_one = "एक्सपोर्ट करने के लिए कम से कम एक ट्रैक चुनें।" +cta = "ट्रैक एक्सपोर्ट करें" +exported = "ट्रैक एक्सपोर्ट हो गए" +exporting = "ट्रैक एक्सपोर्ट हो रहे हैं" +file_count = { one = "{count} फ़ाइल", other = "{count} फ़ाइलें" } +file_only = "ट्रैक एक फ़ोल्डर में अलग-अलग फ़ाइलों के रूप में सेव होते हैं।" +files = "फ़ाइलें" +format = "ट्रैक" +microphone = "माइक्रोफ़ोन" +open_folder = "फ़ोल्डर खोलें" +screen = "स्क्रीन" +studio_only = "अलग-अलग ट्रैक सिर्फ़ Studio मोड में रिकॉर्ड होते हैं।" +system_audio = "सिस्टम ऑडियो" +video_detail = "MP4 · {width}×{height}" + [editor.export.turbo] title = "टर्बो एक्सपोर्ट" description = "हर CPU कोर और GPU का इस्तेमाल करके कहीं तेज़ी से एक्सपोर्ट करता है" diff --git a/apps/desktop-gpui/locales/ja.toml b/apps/desktop-gpui/locales/ja.toml index a9ff65381a1..7e370181020 100644 --- a/apps/desktop-gpui/locales/ja.toml +++ b/apps/desktop-gpui/locales/ja.toml @@ -2532,6 +2532,7 @@ format = "形式" frame_rate = "フレームレート" quality = "品質" resolution = "解像度" +tracks = "トラック" [editor.export.stats] calculating = "計算中…" @@ -2544,6 +2545,25 @@ refining_size = "サイズを精査中…" refining_time = "時間を精査中…" unavailable = "利用不可" +[editor.export.tracks] +aligned = "各トラックはカットや速度の変更に沿うため、どの動画編集ソフトでも揃います。映像は背景やエフェクトなしで書き出されます。" +audio_detail = "WAV · 48 kHz" +camera = "カメラ" +choose_one = "書き出すトラックを1つ以上選んでください。" +cta = "トラックを書き出す" +exported = "トラックを書き出しました" +exporting = "トラックを書き出し中" +file_count = { other = "{count}ファイル" } +file_only = "トラックはフォルダ内に個別のファイルとして保存されます。" +files = "ファイル" +format = "トラック" +microphone = "マイク" +open_folder = "フォルダを開く" +screen = "画面" +studio_only = "個別のトラックはStudioモードでのみ録画されます。" +system_audio = "システム音声" +video_detail = "MP4 · {width}×{height}" + [editor.export.turbo] title = "ターボ書き出し" description = "すべてのCPUコアとGPUを使って、はるかに高速に書き出します" diff --git a/apps/desktop-gpui/locales/zh-CN.toml b/apps/desktop-gpui/locales/zh-CN.toml index b9a7d23202d..8be271d175e 100644 --- a/apps/desktop-gpui/locales/zh-CN.toml +++ b/apps/desktop-gpui/locales/zh-CN.toml @@ -2532,6 +2532,7 @@ format = "格式" frame_rate = "帧率" quality = "画质" resolution = "分辨率" +tracks = "轨道" [editor.export.stats] calculating = "正在计算…" @@ -2544,6 +2545,25 @@ refining_size = "正在精确估算大小…" refining_time = "正在精确估算用时…" unavailable = "不可用" +[editor.export.tracks] +aligned = "各轨道会跟随你的剪辑和速度,在任何视频编辑软件中都能对齐。视频不含背景和效果。" +audio_detail = "WAV · 48 kHz" +camera = "摄像头" +choose_one = "请至少选择一个要导出的轨道。" +cta = "导出轨道" +exported = "轨道已导出" +exporting = "正在导出轨道" +file_count = { other = "{count} 个文件" } +file_only = "轨道会以单独文件的形式保存到一个文件夹中。" +files = "文件" +format = "轨道" +microphone = "麦克风" +open_folder = "打开文件夹" +screen = "屏幕" +studio_only = "只有 Studio 模式会录制单独的轨道。" +system_audio = "系统音频" +video_detail = "MP4 · {width}×{height}" + [editor.export.turbo] title = "极速导出" description = "使用所有 CPU 核心和 GPU,大幅加快导出" diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index b344201978e..920efe08ca1 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -6094,6 +6094,9 @@ async fn drive_auto_export(handle: WindowHandle, cx: &mut gpui::As if flags.contains(&"link") { export.destination = crate::editor_export::ExportDestination::Link; } + if flags.contains(&"tracks") { + export.use_tracks(None); + } } cx.notify(); }); @@ -6108,7 +6111,12 @@ async fn drive_auto_export(handle: WindowHandle, cx: &mut gpui::As .await; let _ = handle.update(cx, |view, window, cx| { view.open_export(window, cx); - if let Some(export) = view.export.as_mut() { + let tracks = std::env::var("CAP_GPUI_AUTO_EXPORT_TRACKS").ok(); + if let Some(export) = view.export.as_mut() + && let Some(tracks) = tracks.as_deref() + { + export.use_tracks(Some(tracks)); + } else if let Some(export) = view.export.as_mut() { export.destination = crate::editor_export::ExportDestination::File; export.format = if path.extension().is_some_and(|extension| extension == "gif") { crate::editor_export::ExportFormatKind::Gif diff --git a/apps/desktop-gpui/src/editor_export.rs b/apps/desktop-gpui/src/editor_export.rs index c5015044891..e626dd2b641 100644 --- a/apps/desktop-gpui/src/editor_export.rs +++ b/apps/desktop-gpui/src/editor_export.rs @@ -13,6 +13,7 @@ use cap_export::preview::{ ExportPreviewSettings, render_preview_with_config, render_preview_with_editor, }; use cap_export::settings::ExportSettings; +use cap_export::tracks::{self, TrackKind, TrackProgress}; use cap_export::{ExporterBase, make_cursor_only_project}; use cap_project::{BackgroundSource, RecordingMeta, XY}; use cap_utils::export_resources::{ @@ -102,13 +103,17 @@ impl ExportDestination { pub enum ExportFormatKind { Mp4, Gif, + Tracks, } impl ExportFormatKind { + const ALL: [Self; 3] = [Self::Mp4, Self::Gif, Self::Tracks]; + fn label(self) -> &'static str { match self { Self::Mp4 => "MP4", Self::Gif => "GIF", + Self::Tracks => tr::editor::export::tracks::format(), } } @@ -116,17 +121,127 @@ impl ExportFormatKind { match self { Self::Mp4 => "Mp4", Self::Gif => "Gif", + Self::Tracks => "Tracks", } } fn from_slug(slug: &str) -> Self { match slug { "Gif" => Self::Gif, + "Tracks" => Self::Tracks, _ => Self::Mp4, } } } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TrackOption { + pub kind: TrackKind, + pub size: Option<(u32, u32)>, +} + +fn track_options( + recordings: &cap_rendering::ProjectRecordingsMeta, + project: &cap_project::ProjectConfiguration, +) -> Vec { + let used = |index: usize| { + project.timeline.as_ref().is_none_or(|timeline| { + timeline + .segments + .iter() + .any(|segment| segment.recording_clip as usize == index) + }) + }; + tracks::available_tracks(recordings, project) + .into_iter() + .map(|kind| { + let size = recordings + .segments + .iter() + .enumerate() + .filter(|(index, _)| used(*index)) + .find_map(|(_, segment)| match kind { + TrackKind::Screen => Some((segment.display.width, segment.display.height)), + TrackKind::Camera => segment + .camera + .as_ref() + .map(|camera| (camera.width, camera.height)), + TrackKind::Microphone | TrackKind::SystemAudio => None, + }); + TrackOption { kind, size } + }) + .collect() +} + +fn track_label(kind: TrackKind) -> &'static str { + match kind { + TrackKind::Screen => tr::editor::export::tracks::screen(), + TrackKind::Camera => tr::editor::export::tracks::camera(), + TrackKind::Microphone => tr::editor::export::tracks::microphone(), + TrackKind::SystemAudio => tr::editor::export::tracks::system_audio(), + } +} + +fn track_icon(kind: TrackKind) -> &'static str { + match kind { + TrackKind::Screen => "icons/monitor.svg", + TrackKind::Camera => "icons/camera.svg", + TrackKind::Microphone => "icons/microphone.svg", + TrackKind::SystemAudio => "icons/audio-on.svg", + } +} + +fn track_toggle_id(kind: TrackKind) -> &'static str { + match kind { + TrackKind::Screen => "export-track-screen", + TrackKind::Camera => "export-track-camera", + TrackKind::Microphone => "export-track-microphone", + TrackKind::SystemAudio => "export-track-system-audio", + } +} + +fn track_detail(option: &TrackOption) -> String { + match option.size { + Some((width, height)) => tr::editor::export::tracks::video_detail(width, height), + None => tr::editor::export::tracks::audio_detail().into(), + } +} + +/// Every available track is on unless the user turned its kind off before. +/// If that leaves nothing, all of them are on again rather than none. +fn selected_tracks(options: &[TrackOption], excluded: &[TrackKind]) -> Vec { + let selected = options + .iter() + .map(|option| option.kind) + .filter(|kind| !excluded.contains(kind)) + .collect::>(); + if selected.is_empty() { + options.iter().map(|option| option.kind).collect() + } else { + selected + } +} + +fn estimated_tracks_bytes( + options: &[TrackOption], + selected: &[TrackKind], + fps: u32, + duration: f64, +) -> u64 { + let bpp = ExportCompression::Maximum.bits_per_pixel() as f64; + options + .iter() + .filter(|option| selected.contains(&option.kind)) + .map(|option| match option.size { + Some((width, height)) => { + width as f64 * height as f64 * fps as f64 * bpp / 8.0 * duration + } + None => 48_000.0 * 2.0 * 3.0 * duration, + }) + .sum::() + .max(0.0) as u64 +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum GifQualityPreset { Low, @@ -284,6 +399,9 @@ pub struct ExportUi { pub copy_link_pressed: bool, bpp_track: ui::SliderTrack, bpp_dragging: bool, + pub track_options: Vec, + pub tracks: Vec, + excluded_tracks: Vec, } impl Drop for ExportUi { @@ -327,6 +445,7 @@ impl ExportUi { organization_id: None, turbo: false, gif_quality: None, + excluded_tracks: None, }); Self::from_preferences(prefs) } @@ -375,6 +494,56 @@ impl ExportUi { copy_link_pressed: false, bpp_track: ui::SliderTrack::default(), bpp_dragging: false, + track_options: Vec::new(), + tracks: Vec::new(), + excluded_tracks: prefs + .excluded_tracks + .iter() + .flatten() + .filter_map(|slug| TrackKind::from_slug(slug)) + .collect(), + } + } + + fn set_track_options(&mut self, options: Vec) { + self.tracks = selected_tracks(&options, &self.excluded_tracks); + self.track_options = options; + if self.format == ExportFormatKind::Tracks && self.track_options.is_empty() { + self.format = ExportFormatKind::Mp4; + } + } + + /// Switches to separate tracks, for the `CAP_GPUI_AUTO_EXPORT*` hooks: + /// the comma-separated track slugs given, or the current selection. + pub(crate) fn use_tracks(&mut self, slugs: Option<&str>) { + if self.track_options.is_empty() { + return; + } + self.format = ExportFormatKind::Tracks; + self.destination = ExportDestination::File; + self.cursor_only = false; + if let Some(slugs) = slugs { + let mut tracks = slugs + .split(',') + .filter_map(TrackKind::from_slug) + .filter(|kind| self.track_options.iter().any(|option| option.kind == *kind)) + .collect::>(); + tracks.sort(); + tracks.dedup(); + self.tracks = tracks; + } + } + + fn toggle_track(&mut self, kind: TrackKind) { + if let Some(index) = self.tracks.iter().position(|track| *track == kind) { + self.tracks.remove(index); + if !self.excluded_tracks.contains(&kind) { + self.excluded_tracks.push(kind); + } + } else if self.track_options.iter().any(|option| option.kind == kind) { + self.tracks.push(kind); + self.tracks.sort(); + self.excluded_tracks.retain(|excluded| *excluded != kind); } } @@ -401,6 +570,12 @@ impl ExportUi { organization_id: self.organization_id.clone(), turbo: self.turbo, gif_quality: Some(self.gif_quality.slug().to_string()), + excluded_tracks: (!self.excluded_tracks.is_empty()).then(|| { + self.excluded_tracks + .iter() + .map(|kind| kind.slug().to_string()) + .collect() + }), }; store::update(|state| state.export = Some(prefs)); } @@ -514,6 +689,15 @@ fn format_duration(seconds: f64) -> String { } pub(crate) fn export_file_name(pretty_name: &str, extension: &str) -> String { + format!( + "{}.{extension}", + export_name_stem(pretty_name, extension.len() + 1) + ) +} + +/// The project's name made safe for a file or folder on every platform, +/// leaving `reserved` bytes of the 255-byte limit for a suffix. +fn export_name_stem(pretty_name: &str, reserved: usize) -> String { let mut name = sanitize_filename::sanitize_with_options( pretty_name, sanitize_filename::Options { @@ -521,13 +705,13 @@ pub(crate) fn export_file_name(pretty_name: &str, extension: &str) -> String { ..Default::default() }, ); - while name.len() > 255 - extension.len() - 1 { + while name.len() > 255 - reserved { let _ = name.pop(); } if name.trim().is_empty() { name = "Cap Recording".into(); } - format!("{name}.{extension}") + name } fn format_estimate_range(range: [f64; 2], time: bool) -> String { @@ -638,7 +822,12 @@ impl EditorWindow { if let Ok(meta) = RecordingMeta::load_for_project(&self.project_path) { self.sharing = meta.sharing; } - if has_transparent_background(&self.project) { + ui.set_track_options( + self.summary() + .map(|summary| track_options(&summary.recordings, &self.project)) + .unwrap_or_default(), + ); + if has_transparent_background(&self.project) && ui.format != ExportFormatKind::Tracks { ui.format = ExportFormatKind::Gif; if ui.resolution == ExportResolution::P4k { ui.resolution = ExportResolution::P1080; @@ -647,7 +836,9 @@ impl EditorWindow { ui.destination = ExportDestination::File; } } - if ui.cursor_only && ui.destination == ExportDestination::Link { + if (ui.cursor_only || ui.format == ExportFormatKind::Tracks) + && ui.destination == ExportDestination::Link + { ui.destination = ExportDestination::File; } self.normalize_export_options(&mut ui); @@ -740,8 +931,14 @@ impl EditorWindow { ui.estimate_cancel.store(true, Ordering::Release); ui.estimate_cancel = Arc::new(AtomicBool::new(false)); ui.estimate = None; - ui.estimate_loading = true; ui.preview_stats = None; + if ui.format == ExportFormatKind::Tracks { + ui.estimate_loading = false; + ui.preview_request = Arc::new(()); + ui.preview_task = None; + return; + } + ui.estimate_loading = true; let estimate_cancel = ui.estimate_cancel.clone(); let estimate_settings = ui.estimate_settings(); let estimate_turbo = ui.turbo; @@ -884,6 +1081,10 @@ impl EditorWindow { if ui.phase.is_busy() || ui.close_requested { return; } + if ui.format == ExportFormatKind::Tracks { + self.start_tracks_export(pretty_name, window, cx); + return; + } if ui.destination == ExportDestination::Link { let sign_in = SignIn::global(cx); if sign_in.read(cx).is_pending() { @@ -1124,6 +1325,197 @@ impl EditorWindow { })); } + fn start_tracks_export( + &mut self, + pretty_name: String, + window: &mut Window, + cx: &mut Context, + ) { + let duration = self.total_duration(); + let project_path = self.project_path.clone(); + let project = self.project.clone(); + let Some(ui) = self.export.as_mut() else { + return; + }; + if ui.tracks.is_empty() { + return; + } + let selection = ui.tracks.clone(); + let fps = ui.fps; + let force_ffmpeg_decoder = ui.force_ffmpeg; + let estimate = estimated_tracks_bytes(&ui.track_options, &selection, fps, duration); + let folder_name = export_name_stem(&pretty_name, 0); + ui.cancel = Arc::new(AtomicBool::new(false)); + let cancel = ui.cancel.clone(); + ui.phase = ExportPhase::ChoosingFile; + ui.error = None; + ui.rendered = 0; + ui.total_frames = 0; + ui.output_path = None; + cx.notify(); + + ui.export_task = Some(cx.spawn_in(window, async move |this, cx| { + let chosen: Result, String> = + match std::env::var_os("CAP_GPUI_AUTO_EXPORT") { + Some(path) => Ok(Some(PathBuf::from(path))), + None => { + #[cfg(target_os = "macos")] + { + platform::try_save_file_panel(&folder_name, &[]) + } + #[cfg(not(target_os = "macos"))] + { + Ok(platform::save_file_panel_async(&folder_name, &[], cx).await) + } + } + }; + let folder = match chosen { + Ok(Some(path)) => path, + Ok(None) => { + let _ = this.update_in(cx, |this, window, cx| { + if let Some(ui) = this.export.as_mut() + && Arc::ptr_eq(&ui.cancel, &cancel) + { + ui.phase = ExportPhase::Idle; + } + cx.notify(); + this.finish_requested_export_close(window, cx); + }); + return; + } + Err(error) => { + tracing::warn!( + error, + "Save dialog unavailable; keeping the tracks in the project output folder" + ); + project_path.join("output").join(&folder_name) + } + }; + + let resources = ExportResources::new(vec![DiskBudget { + path: folder.clone(), + description: "your export drive", + estimated_bytes: estimate, + }]); + if !confirm_export_resources(&this, cx, &resources, &cancel).await { + let _ = this.update_in(cx, |this, window, cx| { + this.finish_requested_export_close(window, cx) + }); + return; + } + let started = this.update(cx, |this, cx| { + let Some(ui) = this.export.as_mut() else { + return false; + }; + if !Arc::ptr_eq(&ui.cancel, &cancel) || cancel.load(Ordering::Relaxed) { + ui.phase = ExportPhase::Idle; + cx.notify(); + return false; + } + ui.phase = ExportPhase::Starting; + cx.notify(); + true + }); + if !started.unwrap_or(false) { + let _ = this.update_in(cx, |this, window, cx| { + this.finish_requested_export_close(window, cx) + }); + return; + } + + let (progress_tx, progress_rx) = flume::unbounded::(); + let export_cancel = cancel.clone(); + let export_folder = folder.clone(); + let started_at = std::time::Instant::now(); + let export = gpui_tokio::Tokio::spawn(cx, async move { + let stopped = export_cancel.clone(); + resources + .supervise( + async move { + tracks::export_tracks( + project_path, + Some(project), + tracks::TrackExportSettings { + tracks: selection, + fps, + force_ffmpeg_decoder, + }, + export_folder, + export_cancel, + move |progress| { + let _ = progress_tx.send(progress); + }, + ) + .await + .map_err(|error| error.to_string()) + }, + || stopped.store(true, Ordering::Release), + ) + .await + }); + + loop { + let latest = progress_rx.try_iter().last(); + if let Some(progress) = latest { + let _ = this.update(cx, |this, cx| { + if let Some(ui) = this.export.as_mut() { + ui.rendered = progress.done.min(u64::from(u32::MAX)) as u32; + ui.total_frames = progress.total.min(u64::from(u32::MAX)) as u32; + ui.phase = ExportPhase::Rendering; + } + cx.notify(); + }); + } + if progress_rx.is_disconnected() { + break; + } + cx.background_executor() + .timer(Duration::from_millis(50)) + .await; + } + + let result = export.await; + let _ = this.update(cx, |this, cx| { + let Some(ui) = this.export.as_mut() else { + return; + }; + match result { + Ok(Ok(files)) => { + tracing::info!( + folder = %folder.display(), + files = files.len(), + elapsed_ms = started_at.elapsed().as_millis() as u64, + "editor tracks export completed" + ); + crate::app_sounds::play_notification(); + ui.phase = ExportPhase::Done; + ui.output_path = Some(folder); + } + Ok(Err(error)) => { + let cancelled = !is_resource_stop(&error) + && (error == "Export cancelled" || cancel.load(Ordering::Relaxed)); + if cancelled { + ui.phase = ExportPhase::Idle; + } else { + tracing::error!(error, "editor tracks export failed"); + ui.phase = ExportPhase::Failed; + ui.error = Some(error); + } + } + Err(error) => { + tracing::error!(%error, "editor tracks export task ended without a result"); + ui.phase = ExportPhase::Failed; + ui.error = Some(tr::editor::export::task_failed().into()); + } + } + cx.notify(); + }); + let _ = this.update_in(cx, |this, window, cx| { + this.finish_requested_export_close(window, cx) + }); + })); + } + fn retry_clipboard_copy(&mut self, window: &mut Window, cx: &mut Context) { let Some(ui) = self.export.as_mut() else { return; @@ -1579,7 +1971,12 @@ impl EditorWindow { .flex_row() .flex_1() .min_h_0() - .child(self.render_export_preview_pane(ui, preview_box)) + .child(if ui.format == ExportFormatKind::Tracks { + self.render_tracks_stage(ui).into_any_element() + } else { + self.render_export_preview_pane(ui, preview_box) + .into_any_element() + }) .child(self.render_export_sidebar(ui, cx)) .when(ui.phase.shows_progress(), |this| { this.child(self.render_export_overlay(ui, cx)) @@ -1782,12 +2179,236 @@ impl EditorWindow { ) } + fn render_tracks_stage(&self, ui: &ExportUi) -> impl IntoElement { + let theme = self.theme; + let editor = theme.editor; + let duration = self.total_duration(); + let folder = export_name_stem( + self.summary() + .map(|summary| summary.pretty_name.as_str()) + .filter(|name| !name.trim().is_empty()) + .unwrap_or("Cap Recording"), + 0, + ); + let clips = self + .project + .timeline + .as_ref() + .map(|timeline| { + timeline + .segments + .iter() + .map(|segment| segment.duration()) + .filter(|duration| duration.is_finite() && *duration > 0.0) + .collect::>() + }) + .filter(|clips| !clips.is_empty()) + .unwrap_or_else(|| vec![1.0]); + let clips_total = clips.iter().sum::(); + let selected = ui.tracks.len(); + let size = estimated_tracks_bytes(&ui.track_options, &ui.tracks, ui.fps, duration) as f64 + / (1024.0 * 1024.0); + + let lane = |option: &TrackOption| { + let checked = ui.tracks.contains(&option.kind); + let color = gpui::rgb(if option.kind.is_video() { + crate::editor_timeline::track_color::CLIP + } else { + crate::editor_timeline::track_color::AUDIO + }); + let fill = Hsla::from(editor.seg_fill(color)); + let border = Hsla::from(editor.seg_border(color)); + div() + .flex() + .flex_row() + .items_center() + .gap(px(14.)) + .h(px(44.)) + .when(!checked, |lane| lane.opacity(0.35)) + .child( + div() + .w(px(176.)) + .flex_none() + .flex() + .flex_row() + .items_center() + .gap(px(10.)) + .child( + div() + .flex_none() + .size(px(28.)) + .rounded(px(7.)) + .bg(Hsla::from(editor.ctl)) + .flex() + .items_center() + .justify_center() + .child( + svg() + .path(track_icon(option.kind)) + .size(px(14.)) + .text_color(Hsla::from(editor.text_2)), + ), + ) + .child( + div() + .flex() + .flex_col() + .min_w_0() + .gap(px(1.)) + .child( + div() + .text_size(px(12.5)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(editor.text_1)) + .truncate() + .child(option.kind.file_name()), + ) + .child( + div() + .text_size(px(11.)) + .text_color(Hsla::from(editor.text_3)) + .truncate() + .child(track_detail(option)), + ), + ), + ) + .child( + div() + .flex_1() + .min_w_0() + .h(px(26.)) + .flex() + .flex_row() + .children(clips.iter().map(|clip| { + div() + .h_full() + .w(gpui::relative((clip / clips_total) as f32)) + .px(px(1.)) + .child( + div() + .size_full() + .rounded(px(6.)) + .bg(fill) + .border_1() + .border_color(border), + ) + })), + ) + }; + + div() + .flex() + .flex_col() + .flex_1() + .min_w_0() + .bg(Hsla::from(editor.stage)) + .pt(px(16.)) + .px(px(STAGE_PADDING_X)) + .pb(px(20.)) + .child( + div() + .h(px(22.)) + .flex() + .flex_row() + .items_center() + .text_size(px(12.)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(editor.text_2)) + .child(tr::editor::export::section::tracks()), + ) + .child( + div() + .flex() + .flex_1() + .min_h_0() + .items_center() + .justify_center() + .pt(px(14.)) + .pb(px(18.)) + .child( + div() + .w_full() + .max_w(px(640.)) + .flex() + .flex_col() + .gap(px(6.)) + .p(px(16.)) + .rounded(px(12.)) + .bg(Hsla::from(editor.card)) + .shadow(editor.card_shadow()) + .child( + div() + .flex() + .flex_row() + .items_center() + .gap(px(8.)) + .pb(px(8.)) + .mb(px(4.)) + .border_b_1() + .border_color(Hsla::from(editor.line)) + .child( + svg() + .path("icons/folder.svg") + .size(px(16.)) + .flex_none() + .text_color(Hsla::from(editor.text_2)), + ) + .child( + div() + .flex_1() + .min_w_0() + .text_size(px(13.)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(editor.text_1)) + .truncate() + .child(folder), + ), + ) + .children(ui.track_options.iter().map(lane)), + ), + ) + .child( + div().flex().flex_row().justify_center().child( + div() + .h(px(STATS_HEIGHT)) + .min_w(px(420.)) + .flex() + .flex_row() + .rounded(px(10.)) + .bg(Hsla::from(editor.card)) + .shadow(editor.card_shadow()) + .child(export_stat( + &theme, + tr::editor::export::stats::duration(), + Some(format_duration(duration)), + false, + )) + .child(export_stat( + &theme, + tr::editor::export::tracks::files(), + Some(tr::editor::export::tracks::file_count(selected)), + true, + )) + .child(export_stat( + &theme, + tr::editor::export::stats::estimated_size(), + Some(if selected == 0 { + tr::editor::export::stats::unavailable().into() + } else { + format_estimate_range([size, size], false) + }), + true, + )), + ), + ) + } + fn render_export_sidebar(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { let theme = self.theme; let editor = theme.editor; let transparent = has_transparent_background(&self.project); - let link_disabled = transparent || ui.cursor_only; - let format_locked = transparent || ui.cursor_only; + let tracks_mode = ui.format == ExportFormatKind::Tracks; + let link_disabled = transparent || ui.cursor_only || tracks_mode; let resolutions = self.export_resolutions(ui); let fps_options = self.export_fps_options(ui); @@ -1811,8 +2432,13 @@ impl EditorWindow { .p(px(16.)) .gap(px(20.)) .child(self.export_destination_field(ui, link_disabled, cx)) - .child(self.export_format_field(ui, format_locked, cx)) - .child(self.export_resolution_field(ui, &resolutions, cx)) + .child(self.export_format_field(ui, transparent, cx)) + .when(tracks_mode, |this| { + this.child(self.export_tracks_field(ui, cx)) + }) + .when(!tracks_mode, |this| { + this.child(self.export_resolution_field(ui, &resolutions, cx)) + }) .child(self.export_fps_field(ui, fps_options, cx)) .when( ui.format == ExportFormatKind::Mp4 && !ui.cursor_only, @@ -1822,8 +2448,10 @@ impl EditorWindow { ui.format == ExportFormatKind::Gif && !ui.cursor_only, |this| this.child(self.export_gif_quality_field(ui, cx)), ) - .child(div().h(px(1.)).flex_none().bg(Hsla::from(editor.line))) - .child(self.export_advanced(ui, cx)), + .when(!tracks_mode, |this| { + this.child(div().h(px(1.)).flex_none().bg(Hsla::from(editor.line))) + .child(self.export_advanced(ui, cx)) + }), ) .child( div() @@ -1842,6 +2470,11 @@ impl EditorWindow { let signed_in = !ui.auth_rejected && store::auth_snapshot().signed_in(); let sign_in_pending = SignIn::global(cx).read(cx).is_pending(); let (label, icon, primary) = match ui.destination { + ExportDestination::File if ui.format == ExportFormatKind::Tracks => ( + tr::editor::export::tracks::cta(), + Some("icons/folder.svg"), + true, + ), ExportDestination::File => ( tr::editor::export::cta::file(), Some("icons/folder.svg"), @@ -1870,7 +2503,8 @@ impl EditorWindow { true, ), }; - let busy = ui.phase.is_busy(); + let busy = + ui.phase.is_busy() || (ui.format == ExportFormatKind::Tracks && ui.tracks.is_empty()); let (bg, hover_bg, text) = if primary { ( Hsla::from(editor.accent), @@ -1951,7 +2585,11 @@ impl EditorWindow { dest.label() }; let mut option = ui::SegmentOption::new(label, ui.destination == *dest) - .disabled(*dest == ExportDestination::Link && link_disabled); + .disabled( + (*dest == ExportDestination::Link && link_disabled) + || (*dest == ExportDestination::Clipboard + && ui.format == ExportFormatKind::Tracks), + ); option.icon = Some(dest.icon().into()); option }) @@ -1964,6 +2602,9 @@ impl EditorWindow { return; }; if let Some(ui) = this.export.as_mut() { + if ui.format == ExportFormatKind::Tracks && dest != ExportDestination::File { + return; + } if dest == ExportDestination::Link && (has_transparent_background(&this.project) || ui.cursor_only) { @@ -1987,7 +2628,9 @@ impl EditorWindow { div() .text_size(px(11.)) .text_color(Hsla::from(editor.text_3)) - .child(if ui.cursor_only { + .child(if ui.format == ExportFormatKind::Tracks { + tr::editor::export::tracks::file_only() + } else if ui.cursor_only { tr::editor::export::link_unavailable_cursor() } else { tr::editor::export::link_unavailable_transparent() @@ -2116,13 +2759,20 @@ impl EditorWindow { fn export_format_field( &self, ui: &ExportUi, - locked: bool, + transparent: bool, cx: &mut Context, ) -> impl IntoElement { let theme = self.theme; - let options = [ExportFormatKind::Mp4, ExportFormatKind::Gif]; + let editor = theme.editor; + let options = ExportFormatKind::ALL; + let cursor_only = ui.cursor_only; + let no_tracks = ui.track_options.is_empty(); + let disabled = move |format: ExportFormatKind| match format { + ExportFormatKind::Mp4 => transparent || cursor_only, + ExportFormatKind::Gif => cursor_only, + ExportFormatKind::Tracks => no_tracks, + }; self.export_section(tr::editor::export::section::format(), "icons/video.svg") - .disabled(locked) .child( ui::SegmentedControl::editor( &theme, @@ -2131,21 +2781,25 @@ impl EditorWindow { .iter() .map(|format| { ui::SegmentOption::new(format.label(), ui.format == *format) - .disabled(locked) + .disabled(disabled(*format)) }) .collect(), ) .stretch() .item_height(px(SEGMENT_HEIGHT)) .on_select(cx.listener(move |this, index: &usize, window, cx| { - if locked { - return; - } let Some(format) = options.get(*index).copied() else { return; }; + if disabled(format) { + return; + } if let Some(ui) = this.export.as_mut() { ui.format = format; + if format == ExportFormatKind::Tracks { + ui.destination = ExportDestination::File; + ui.cursor_only = false; + } if format == ExportFormatKind::Gif { if ui.destination == ExportDestination::Link { ui.destination = ExportDestination::File; @@ -2163,6 +2817,104 @@ impl EditorWindow { cx.notify(); })), ) + .when(no_tracks, |field| { + field.child( + div() + .text_size(px(11.)) + .text_color(Hsla::from(editor.text_3)) + .child(tr::editor::export::tracks::studio_only()), + ) + }) + } + + fn export_tracks_field(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { + let editor = self.theme.editor; + self.export_section(tr::editor::export::section::tracks(), "icons/layers.svg") + .children(ui.track_options.iter().map(|option| { + self.export_track_row(option, ui.tracks.contains(&option.kind), cx) + .into_any_element() + })) + .child( + div() + .text_size(px(11.)) + .line_height(px(14.)) + .text_color(Hsla::from(editor.text_3)) + .child(if ui.tracks.is_empty() { + tr::editor::export::tracks::choose_one() + } else { + tr::editor::export::tracks::aligned() + }), + ) + } + + fn export_track_row( + &self, + option: &TrackOption, + checked: bool, + cx: &mut Context, + ) -> impl IntoElement { + let editor = self.theme.editor; + let kind = option.kind; + div() + .flex() + .flex_row() + .items_center() + .gap(px(10.)) + .min_h(px(36.)) + .child( + div() + .flex_none() + .size(px(28.)) + .rounded(px(7.)) + .bg(Hsla::from(editor.ctl)) + .flex() + .items_center() + .justify_center() + .child( + svg() + .path(track_icon(kind)) + .size(px(14.)) + .text_color(Hsla::from(if checked { + editor.text_1 + } else { + editor.text_3 + })), + ), + ) + .child( + div() + .flex() + .flex_col() + .flex_1() + .min_w_0() + .gap(px(1.)) + .child( + div() + .text_size(px(12.5)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(editor.text_1)) + .child(track_label(kind)), + ) + .child( + div() + .text_size(px(11.)) + .line_height(px(14.)) + .text_color(Hsla::from(editor.text_3)) + .truncate() + .child(track_detail(option)), + ), + ) + .child( + ui::Toggle::plain(&self.theme, track_toggle_id(kind), checked).on_click( + cx.listener(move |this, _, _, cx| { + if let Some(ui) = this.export.as_mut() { + ui.toggle_track(kind); + ui.persist(); + } + cx.notify(); + }), + ), + ) } fn export_resolution_field( @@ -2693,6 +3445,9 @@ impl EditorWindow { }; let heading = match ui.phase { ExportPhase::Starting => tr::editor::export::phase::starting(), + ExportPhase::Rendering if ui.format == ExportFormatKind::Tracks => { + tr::editor::export::tracks::exporting() + } ExportPhase::Rendering if ui.cursor_only => { tr::editor::export::phase::rendering_cursor() } @@ -2715,6 +3470,9 @@ impl EditorWindow { ExportPhase::Done if ui.destination == ExportDestination::Link => { tr::editor::export::phase::upload_complete() } + ExportPhase::Done if ui.format == ExportFormatKind::Tracks => { + tr::editor::export::tracks::exported() + } ExportPhase::Done => tr::editor::export::phase::complete(), ExportPhase::Failed if ui.clipboard_retry_path().is_some() => { tr::editor::export::phase::clipboard_failed() @@ -2780,7 +3538,9 @@ impl EditorWindow { ), ) .when( - ui.phase == ExportPhase::Rendering && ui.total_frames > 0, + ui.phase == ExportPhase::Rendering + && ui.total_frames > 0 + && ui.format != ExportFormatKind::Tracks, |this| { this.child( div() @@ -2855,14 +3615,16 @@ impl EditorWindow { .detach(); })), ) - .child( - div() - .max_w(px(320.)) - .text_size(px(12.)) - .text_color(Hsla::from(editor.text_2)) - .text_center() - .child(tr::editor::export::done::instant_hint()), - ) + .when(ui.format != ExportFormatKind::Tracks, |this| { + this.child( + div() + .max_w(px(320.)) + .text_size(px(12.)) + .text_color(Hsla::from(editor.text_2)) + .text_center() + .child(tr::editor::export::done::instant_hint()), + ) + }) }) .when( ui.phase == ExportPhase::Done || ui.phase == ExportPhase::Failed, @@ -2897,7 +3659,11 @@ impl EditorWindow { ui::ButtonVariant::Gray, ui::ButtonSize::Md, ) - .label(tr::editor::export::done::open_file()) + .label(if ui.format == ExportFormatKind::Tracks { + tr::editor::export::tracks::open_folder() + } else { + tr::editor::export::done::open_file() + }) .on_click(move |_, _, cx| { if let Some(path) = &path { library::open_with_system(path, cx); @@ -3170,7 +3936,9 @@ fn turbo_available() -> bool { } fn export_fps_options(format: ExportFormatKind, cursor_only: bool) -> &'static [u32] { - if format == ExportFormatKind::Gif && !cursor_only { + if format == ExportFormatKind::Tracks { + &[15, 30, 60] + } else if format == ExportFormatKind::Gif && !cursor_only { &[5, 10, 15, 20, 25, 30] } else { &[15, 30, 60] @@ -3202,6 +3970,9 @@ fn export_resolutions( } fn normalize_export_options(ui: &mut ExportUi, transparent: bool) { + if ui.format == ExportFormatKind::Tracks { + ui.destination = ExportDestination::File; + } if !export_fps_options(ui.format, ui.cursor_only).contains(&ui.fps) { ui.fps = if ui.format == ExportFormatKind::Gif { 15 @@ -3385,8 +4156,9 @@ mod tests { use super::{ ExportDestination, ExportFormatKind, ExportPhase, ExportResolution, ExportUi, - GifQualityPreset, cancel_matching_export, export_file_name, export_fps_options, - export_resolutions, normalize_export_options, + GifQualityPreset, TrackKind, TrackOption, cancel_matching_export, estimated_tracks_bytes, + export_file_name, export_fps_options, export_name_stem, export_resolutions, + normalize_export_options, selected_tracks, }; use crate::store::ExportPrefs; use std::path::PathBuf; @@ -3396,7 +4168,11 @@ mod tests { }; fn clipboard_export() -> ExportUi { - ExportUi::from_preferences(ExportPrefs { + ExportUi::from_preferences(prefs()) + } + + fn prefs() -> ExportPrefs { + ExportPrefs { format: "Mp4".into(), fps: 30, export_to: "clipboard".into(), @@ -3410,7 +4186,8 @@ mod tests { organization_id: None, turbo: false, gif_quality: None, - }) + excluded_tracks: None, + } } #[test] @@ -3677,6 +4454,106 @@ mod tests { assert!(!previous.load(Ordering::Relaxed)); } + fn options(kinds: &[TrackKind]) -> Vec { + kinds + .iter() + .map(|kind| TrackOption { + kind: *kind, + size: kind.is_video().then_some((1920, 1080)), + }) + .collect() + } + + #[test] + fn every_available_track_starts_on_unless_turned_off_before() { + let all = options(&TrackKind::ALL); + assert_eq!(selected_tracks(&all, &[]), TrackKind::ALL); + assert_eq!( + selected_tracks(&all, &[TrackKind::Camera, TrackKind::SystemAudio]), + [TrackKind::Screen, TrackKind::Microphone] + ); + let screen_only = options(&[TrackKind::Screen]); + assert_eq!( + selected_tracks(&screen_only, &[TrackKind::Screen]), + [TrackKind::Screen] + ); + assert!(selected_tracks(&[], &[]).is_empty()); + } + + #[test] + fn turning_tracks_off_is_remembered_and_turning_them_on_forgets_it() { + let mut ui = clipboard_export(); + ui.set_track_options(options(&[TrackKind::Screen, TrackKind::Microphone])); + ui.toggle_track(TrackKind::Microphone); + assert_eq!(ui.tracks, [TrackKind::Screen]); + ui.toggle_track(TrackKind::Camera); + assert_eq!(ui.tracks, [TrackKind::Screen]); + + let prefs = ExportPrefs { + excluded_tracks: Some(vec!["microphone".into(), "unknown".into()]), + ..prefs() + }; + let mut reopened = ExportUi::from_preferences(prefs); + reopened.set_track_options(options(&TrackKind::ALL)); + assert_eq!( + reopened.tracks, + [TrackKind::Screen, TrackKind::Camera, TrackKind::SystemAudio] + ); + reopened.toggle_track(TrackKind::Microphone); + assert_eq!(reopened.tracks, TrackKind::ALL); + assert!(reopened.excluded_tracks.is_empty()); + } + + #[test] + fn tracks_need_a_studio_recording_and_save_to_a_folder() { + let mut ui = ExportUi::from_preferences(ExportPrefs { + format: "Tracks".into(), + ..prefs() + }); + ui.set_track_options(Vec::new()); + assert_eq!(ui.format, ExportFormatKind::Mp4); + ui.use_tracks(None); + assert_eq!(ui.format, ExportFormatKind::Mp4); + + ui.set_track_options(options(&TrackKind::ALL)); + ui.cursor_only = true; + ui.use_tracks(Some("mic, screen,cursor")); + assert_eq!(ui.format, ExportFormatKind::Tracks); + assert!(!ui.cursor_only); + assert_eq!(ui.tracks, [TrackKind::Screen, TrackKind::Microphone]); + + ui.destination = ExportDestination::Clipboard; + ui.fps = 25; + normalize_export_options(&mut ui, true); + assert_eq!(ui.destination, ExportDestination::File); + assert_eq!(ui.fps, 30); + assert_eq!( + ExportFormatKind::from_slug(ExportFormatKind::Tracks.slug()), + ExportFormatKind::Tracks + ); + } + + #[test] + fn track_size_estimates_count_only_selected_tracks() { + let all = options(&TrackKind::ALL); + assert_eq!(estimated_tracks_bytes(&all, &[], 30, 60.0), 0); + let audio = estimated_tracks_bytes(&all, &[TrackKind::Microphone], 30, 60.0); + assert_eq!(audio, 48_000 * 2 * 3 * 60); + let screen = estimated_tracks_bytes(&all, &[TrackKind::Screen], 30, 60.0); + assert!(screen > audio); + assert_eq!( + estimated_tracks_bytes(&all, &[TrackKind::Screen, TrackKind::Microphone], 30, 60.0), + screen + audio + ); + } + + #[test] + fn tracks_folder_names_are_safe_and_whole() { + assert_eq!(export_name_stem("Sprint 1/2", 0), "Sprint 12"); + assert_eq!(export_name_stem(" ", 0), "Cap Recording"); + assert_eq!(export_name_stem(&"a".repeat(300), 0).len(), 255); + } + #[test] fn choosing_a_file_blocks_duplicate_exports_without_showing_progress() { assert!(ExportPhase::ChoosingFile.is_busy()); diff --git a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs index 3489b1fd005..e80d435ba55 100644 --- a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs @@ -60,6 +60,8 @@ const FORMATTED: &[&str] = &[ "editor.export.fps", "editor.export.frames", "editor.export.stats.output_value", + "editor.export.tracks.file_count", + "editor.export.tracks.video_detail", "editor.tabs.captions.download", "editor.tabs.captions.downloading", "editor.tabs.captions.generate_failed", @@ -1444,6 +1446,34 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { (tr::editor::tabs::audio::voice_eq::title(), "Voice EQ"), (tr::editor::timeline::segment::voice_eq(), "Voice EQ"), (tr::editor::window::clip_menu::voice_eq(), "Voice EQ"), + (tr::editor::export::section::tracks(), "Tracks"), + ( + tr::editor::export::tracks::aligned(), + "Tracks follow your cuts and speed, so they line up in any video editor. Video comes out clean, without background or effects.", + ), + (tr::editor::export::tracks::audio_detail(), "WAV · 48 kHz"), + (tr::editor::export::tracks::camera(), "Camera"), + ( + tr::editor::export::tracks::choose_one(), + "Choose at least one track to export.", + ), + (tr::editor::export::tracks::cta(), "Export Tracks"), + (tr::editor::export::tracks::exported(), "Tracks exported"), + (tr::editor::export::tracks::exporting(), "Exporting tracks"), + ( + tr::editor::export::tracks::file_only(), + "Tracks are saved as separate files in a folder.", + ), + (tr::editor::export::tracks::files(), "Files"), + (tr::editor::export::tracks::format(), "Tracks"), + (tr::editor::export::tracks::microphone(), "Microphone"), + (tr::editor::export::tracks::open_folder(), "Open Folder"), + (tr::editor::export::tracks::screen(), "Screen"), + ( + tr::editor::export::tracks::studio_only(), + "Separate tracks are only recorded in Studio Mode.", + ), + (tr::editor::export::tracks::system_audio(), "System audio"), (tr::editor::canvas::shape(), "Shape"), (tr::editor::timeline::track::shape(), "Shape"), ( @@ -1631,6 +1661,20 @@ fn editor_shell_messages_format_like_the_code_they_replaced() { format!("{}×{} · {} fps", 1920, 1080, fps) ); } + assert_eq!( + tr::editor::export::tracks::video_detail(2560_u32, 1440_u32), + "MP4 · 2560×1440" + ); + for count in [1_usize, 4] { + assert_eq!( + tr::editor::export::tracks::file_count(count), + if count == 1 { + "1 file".to_string() + } else { + format!("{count} files") + } + ); + } for total in [0_u32, 1, 2, 5] { let rendered = total / 2; let expected = if total == 1 { diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index 5415e2de15a..feee8f722c0 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -154,6 +154,8 @@ pub struct ExportPrefs { pub turbo: bool, #[serde(default)] pub gif_quality: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub excluded_tracks: Option>, } /// `so.cap.desktop`'s app-data dir -- where both stores live. diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index 7b5cd02bd80..0311fb8c3fd 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -4,6 +4,7 @@ pub mod mov; pub mod mp4; pub mod preview; pub mod settings; +pub mod tracks; use cap_editor::{ExportAudioPreparation, ExportAudioRenderer, SegmentMedia}; use cap_project::{ diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rs index 80ed0e2dd23..cffc3546fc7 100644 --- a/crates/export/src/mp4.rs +++ b/crates/export/src/mp4.rs @@ -1074,7 +1074,7 @@ fn nv12_from_rendered_frame(frame: Nv12RenderedFrame) -> ExportFrame { /// the stream stays gapless and strictly monotonic. The caller advances the /// cursor to `pts + samples`. Shared with the tests so they exercise this exact /// arithmetic rather than a re-implementation. -fn audio_frame_budget( +pub(crate) fn audio_frame_budget( frame_number: u64, sample_rate: u64, fps: u64, diff --git a/crates/export/src/tracks.rs b/crates/export/src/tracks.rs new file mode 100644 index 00000000000..af3be587a17 --- /dev/null +++ b/crates/export/src/tracks.rs @@ -0,0 +1,1467 @@ +//! Export a Studio project's recorded tracks as separate files that follow +//! the edited timeline, so they line up with the main export in an editor. +//! +//! Video tracks read the same decoders, at the same output frame times, as the +//! rendered export. Audio tracks run the editor's audio renderer with one +//! source loaded, sized by the same per-frame sample budget as the MP4 audio. + +use std::{ + fs::File, + io::{BufWriter, Seek, SeekFrom, Write}, + path::{Path, PathBuf}, + sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + mpsc, + }, + time::Duration, +}; + +use cap_editor::{AudioRenderer, SegmentMedia, audio_segment_from_decoded}; +use cap_enc_ffmpeg::{h264::H264Encoder, mp4::MP4File}; +use cap_media_info::VideoInfo; +use cap_project::{ProjectConfiguration, RecordingMeta}; +use cap_rendering::{DecodedFrame, PixelFormat, ProjectRecordingsMeta, cpu_yuv::YuvMatrix}; +use ffmpeg::{format::Pixel, frame, software::scaling}; + +use crate::mp4::{ExportCompression, audio_frame_budget}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum TrackKind { + Screen, + Camera, + Microphone, + SystemAudio, +} + +impl TrackKind { + pub const ALL: [Self; 4] = [ + Self::Screen, + Self::Camera, + Self::Microphone, + Self::SystemAudio, + ]; + + pub fn slug(self) -> &'static str { + match self { + Self::Screen => "screen", + Self::Camera => "camera", + Self::Microphone => "microphone", + Self::SystemAudio => "system-audio", + } + } + + pub fn from_slug(slug: &str) -> Option { + match slug.trim().to_ascii_lowercase().as_str() { + "screen" | "display" => Some(Self::Screen), + "camera" => Some(Self::Camera), + "microphone" | "mic" => Some(Self::Microphone), + "system-audio" | "system_audio" | "system" => Some(Self::SystemAudio), + _ => None, + } + } + + pub fn file_name(self) -> &'static str { + match self { + Self::Screen => "Screen.mp4", + Self::Camera => "Camera.mp4", + Self::Microphone => "Microphone.wav", + Self::SystemAudio => "System audio.wav", + } + } + + pub fn is_video(self) -> bool { + matches!(self, Self::Screen | Self::Camera) + } +} + +/// Tracks recorded in at least one clip the timeline plays. A track that only +/// exists in clips the edit removed would export as silence or black. +pub fn available_tracks( + recordings: &ProjectRecordingsMeta, + project: &ProjectConfiguration, +) -> Vec { + let used = |index: usize| { + project.timeline.as_ref().is_none_or(|timeline| { + timeline + .segments + .iter() + .any(|segment| segment.recording_clip as usize == index) + }) + }; + let clips = || { + recordings + .segments + .iter() + .enumerate() + .filter(|(index, _)| used(*index)) + .map(|(_, segment)| segment) + }; + TrackKind::ALL + .into_iter() + .filter(|kind| match kind { + TrackKind::Screen => clips().next().is_some(), + TrackKind::Camera => clips().any(|segment| segment.camera.is_some()), + TrackKind::Microphone => clips().any(|segment| segment.mic.is_some()), + TrackKind::SystemAudio => clips().any(|segment| segment.system_audio.is_some()), + }) + .collect() +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum TrackExportError { + Cancelled, + NotStudio, + NothingSelected, + Unavailable(TrackKind), + Failed(String), +} + +impl std::fmt::Display for TrackExportError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Cancelled => write!(f, "Export cancelled"), + Self::NotStudio => write!( + f, + "Separate tracks are only recorded in Studio Mode. Instant recordings are a single video." + ), + Self::NothingSelected => write!(f, "Choose at least one track to export"), + Self::Unavailable(kind) => write!( + f, + "This recording has no {} track", + match kind { + TrackKind::Screen => "screen", + TrackKind::Camera => "camera", + TrackKind::Microphone => "microphone", + TrackKind::SystemAudio => "system audio", + } + ), + Self::Failed(error) => write!(f, "{error}"), + } + } +} + +impl std::error::Error for TrackExportError {} + +impl From for TrackExportError { + fn from(error: String) -> Self { + Self::Failed(error) + } +} + +pub struct TrackExportSettings { + pub tracks: Vec, + pub fps: u32, + pub force_ffmpeg_decoder: bool, +} + +/// One export's worth of work: every video frame counts once (screen and +/// camera share a decode pass) and every audio track counts its frames again. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TrackProgress { + pub done: u64, + pub total: u64, +} + +pub fn progress_total(tracks: &[TrackKind], total_frames: u32) -> u64 { + let video_pass = u64::from(tracks.iter().any(|kind| kind.is_video())); + let audio = tracks.iter().filter(|kind| !kind.is_video()).count() as u64; + (video_pass + audio) * u64::from(total_frames) +} + +/// Writes the selected tracks into `output_dir` and returns their paths in +/// [`TrackKind::ALL`] order. Files are written under temporary names and +/// renamed only once every track has finished, so a cancelled or failed +/// export leaves nothing half written and earlier exports untouched. +pub async fn export_tracks( + project_path: PathBuf, + config: Option, + settings: TrackExportSettings, + output_dir: PathBuf, + cancel: Arc, + mut on_progress: impl FnMut(TrackProgress) + Send, +) -> Result, TrackExportError> { + let mut tracks = settings.tracks.clone(); + tracks.sort(); + tracks.dedup(); + if tracks.is_empty() { + return Err(TrackExportError::NothingSelected); + } + if settings.fps == 0 { + return Err(TrackExportError::Failed( + "The frame rate must be greater than zero".into(), + )); + } + let prepared = prepare( + &project_path, + config, + &tracks, + settings.force_ffmpeg_decoder, + ) + .await?; + if cancel.load(Ordering::Relaxed) { + return Err(TrackExportError::Cancelled); + } + + if output_dir.is_file() { + return Err(TrackExportError::Failed(format!( + "{} is a file, not a folder", + output_dir.display() + ))); + } + std::fs::create_dir_all(&output_dir) + .map_err(|error| format!("Could not create {}: {error}", output_dir.display()))?; + + let total_frames = total_frames(prepared.duration, settings.fps); + let total = progress_total(&tracks, total_frames); + let mut reporter = ProgressReporter { + done: 0, + total, + on_progress: &mut on_progress, + }; + reporter.report(); + + let token = export_token(); + let staged = tracks + .iter() + .map(|kind| (*kind, staging_path(&output_dir, *kind, &token))) + .collect::>(); + let mut cleanup = StagedFiles(staged.iter().map(|(_, path)| path.clone()).collect()); + + let video = staged + .iter() + .filter(|(kind, _)| kind.is_video()) + .cloned() + .collect::>(); + if !video.is_empty() { + export_video_tracks( + &prepared, + &video, + settings.fps, + total_frames, + &cancel, + &mut reporter, + ) + .await?; + } + + for (kind, path) in staged.iter().filter(|(kind, _)| !kind.is_video()) { + export_audio_track( + &prepared, + *kind, + path, + settings.fps, + total_frames, + &cancel, + &mut reporter, + ) + .await?; + } + + if cancel.load(Ordering::Relaxed) { + return Err(TrackExportError::Cancelled); + } + let files = staged + .iter() + .map(|(kind, path)| (path.clone(), output_dir.join(kind.file_name()))) + .collect::>(); + publish(&files, &token)?; + cleanup.0.clear(); + Ok(files + .into_iter() + .map(|(_, destination)| destination) + .collect()) +} + +struct ProgressReporter<'a, F: FnMut(TrackProgress)> { + done: u64, + total: u64, + on_progress: &'a mut F, +} + +impl ProgressReporter<'_, F> { + fn advance(&mut self) { + self.done = (self.done + 1).min(self.total); + self.report(); + } + + fn report(&mut self) { + (self.on_progress)(TrackProgress { + done: self.done, + total: self.total, + }); + } +} + +struct StagedFiles(Vec); + +impl Drop for StagedFiles { + fn drop(&mut self) { + for path in &self.0 { + let _ = std::fs::remove_file(path); + } + } +} + +/// Names this export's temporary files apart from any other export writing +/// to the same folder at the same time. +fn export_token() -> String { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_nanos()) + .unwrap_or_default(); + format!("{}-{nanos}", std::process::id()) +} + +fn staging_path(dir: &Path, kind: TrackKind, token: &str) -> PathBuf { + let extension = if kind.is_video() { "mp4" } else { "wav" }; + dir.join(format!(".{}.{token}.partial.{extension}", kind.slug())) +} + +/// Moves every staged file onto its destination, all or nothing. Files +/// already there are set aside first and put back if any move fails, so a +/// failed export never leaves a mix of old and new tracks. +fn publish(files: &[(PathBuf, PathBuf)], token: &str) -> Result<(), String> { + if let Some((_, folder)) = files.iter().find(|(_, destination)| destination.is_dir()) { + return Err(format!( + "{} is a folder, so the track cannot be saved there", + folder.display() + )); + } + let mut published: Vec<(&Path, Option)> = Vec::with_capacity(files.len()); + for (staged, destination) in files { + let name = destination + .file_name() + .map(|name| name.to_string_lossy().into_owned()) + .unwrap_or_default(); + let backup = if destination.exists() { + let backup = destination.with_file_name(format!(".{name}.{token}.previous")); + if let Err(error) = std::fs::rename(destination, &backup) { + roll_back(&published); + return Err(format!("Could not replace {name}: {error}")); + } + Some(backup) + } else { + None + }; + if let Err(error) = std::fs::rename(staged, destination) { + if let Some(backup) = &backup { + let _ = std::fs::rename(backup, destination); + } + roll_back(&published); + return Err(format!("Could not save {name}: {error}")); + } + published.push((destination, backup)); + } + for (_, backup) in published { + if let Some(backup) = backup { + let _ = std::fs::remove_file(backup); + } + } + Ok(()) +} + +fn roll_back(published: &[(&Path, Option)]) { + for (destination, backup) in published.iter().rev() { + let _ = std::fs::remove_file(destination); + if let Some(backup) = backup { + let _ = std::fs::rename(backup, destination); + } + } +} + +/// The rendered export's frame count for the same timeline and rate. +pub fn total_frames(duration: f64, fps: u32) -> u32 { + (fps as f64 * duration).ceil() as u32 +} + +struct Prepared { + project: ProjectConfiguration, + recordings: Arc, + segments: Vec, + duration: f64, +} + +async fn prepare( + project_path: &Path, + config: Option, + tracks: &[TrackKind], + force_ffmpeg_decoder: bool, +) -> Result { + let recording_meta = RecordingMeta::load_for_project(project_path) + .map_err(|error| format!("Could not read the recording: {error}"))?; + let studio_meta = recording_meta + .studio_meta() + .ok_or(TrackExportError::NotStudio)?; + let recordings = Arc::new( + ProjectRecordingsMeta::new(&recording_meta.project_path, studio_meta) + .map_err(TrackExportError::Failed)?, + ); + let mut project = match config { + Some(config) => config, + None => ProjectConfiguration::load(project_path) + .map_err(|error| format!("Could not read the project: {error}"))?, + }; + crate::synthesize_default_timeline(&mut project, &recordings); + crate::fill_clip_offsets(&mut project, &recording_meta.project_path, studio_meta); + let recording_durations = recordings + .segments + .iter() + .map(|segment| segment.display.duration) + .collect::>(); + cap_project::clamp_timeline_to_sources(&mut project, &recording_durations); + + let available = available_tracks(&recordings, &project); + if let Some(missing) = tracks.iter().find(|kind| !available.contains(kind)) { + return Err(TrackExportError::Unavailable(*missing)); + } + + let duration = cap_rendering::get_duration(&recordings, &recording_meta, studio_meta, &project); + if !duration.is_finite() || duration <= 0.0 { + return Err(TrackExportError::Failed( + "There is nothing to export: the timeline is empty".into(), + )); + } + if let Some(timeline) = &project.timeline + && timeline + .segments + .iter() + .any(|segment| segment.recording_clip as usize >= recordings.segments.len()) + { + return Err(TrackExportError::Failed( + "The timeline refers to a clip this recording does not have".into(), + )); + } + + // Media Foundation hands out GPU textures that the track encoder cannot + // read back; the FFmpeg decoders give CPU frames on every platform. + let force_ffmpeg_decoder = force_ffmpeg_decoder || !cfg!(target_os = "macos"); + cap_editor::release_playback_audio_caches(); + let needs_audio = tracks.iter().any(|kind| !kind.is_video()); + let segments = if needs_audio { + cap_editor::create_segments(&recording_meta, studio_meta, force_ffmpeg_decoder).await + } else { + cap_editor::create_segments_without_audio( + &recording_meta, + studio_meta, + force_ffmpeg_decoder, + ) + .await + } + .map_err(TrackExportError::Failed)?; + if crate::reads_recordings_forward(&project) { + for segment in &segments { + segment.decoders.prefer_sequential_access(); + } + } + + Ok(Prepared { + project, + recordings, + segments, + duration, + }) +} + +/// The clip and source time the rendered export reads for each output frame: +/// the incoming clip during a transition and the frozen frame under a hold. +pub fn frame_source(project: &ProjectConfiguration, frame: u32, fps: u32) -> Option<(u32, f64)> { + project + .get_segment_time(frame as f64 / fps as f64) + .map(|(time, segment)| (segment.recording_clip, time)) +} + +enum VideoJob { + Frame(DecodedFrame), + Repeat, + Black, +} + +struct VideoTrackWriter { + jobs: mpsc::SyncSender, + thread: std::thread::JoinHandle>, +} + +impl VideoTrackWriter { + fn spawn(path: PathBuf, width: u32, height: u32, fps: u32) -> Result { + let (jobs, rx) = mpsc::sync_channel::(4); + let thread = std::thread::Builder::new() + .name("track-export-video".into()) + .spawn(move || write_video_track(rx, path, width, height, fps)) + .map_err(|error| format!("Could not start the track encoder: {error}"))?; + Ok(Self { jobs, thread }) + } + + fn send(&self, job: VideoJob) -> Result<(), String> { + self.jobs + .send(job) + .map_err(|_| "The track encoder stopped early".to_string()) + } + + fn finish(self) -> Result<(), String> { + drop(self.jobs); + self.thread + .join() + .map_err(|_| "The track encoder crashed".to_string())? + } + + fn abandon(self) { + drop(self.jobs); + let _ = self.thread.join(); + } +} + +fn write_video_track( + jobs: mpsc::Receiver, + path: PathBuf, + width: u32, + height: u32, + fps: u32, +) -> Result<(), String> { + let input = VideoInfo::from_raw_ffmpeg(Pixel::NV12, width, height, fps); + let mut file = MP4File::init( + "track", + path, + false, + |output| { + H264Encoder::builder(input) + .with_bpp(ExportCompression::Maximum.bits_per_pixel()) + .with_export_priority() + .with_export_settings() + .build(output) + }, + |_| None, + ) + .map_err(|error| format!("Could not start the track encoder: {error}"))?; + + let mut converter = Nv12Converter::default(); + let mut current = black_nv12(width, height); + for (index, job) in jobs.into_iter().enumerate() { + match job { + VideoJob::Frame(decoded) => converter.fill(&decoded, &mut current)?, + VideoJob::Black => fill_black(&mut current), + VideoJob::Repeat => {} + } + let timestamp = Duration::from_secs_f64(index as f64 / fps as f64); + file.queue_video_frame(current.clone(), timestamp) + .map_err(|error| format!("Encoding a track frame failed: {error}"))?; + } + let result = file + .finish() + .map_err(|error| format!("Could not finish the track: {error}"))?; + result + .video_finish + .map_err(|error| format!("Could not finish the track: {error}")) +} + +fn black_nv12(width: u32, height: u32) -> frame::Video { + let mut frame = frame::Video::new(Pixel::NV12, width, height); + fill_black(&mut frame); + frame +} + +fn fill_black(frame: &mut frame::Video) { + frame.data_mut(0).fill(16); + frame.data_mut(1).fill(128); +} + +#[derive(Clone, Copy, PartialEq, Eq)] +struct ScalerKey { + format: Pixel, + width: u32, + height: u32, + target_width: u32, + target_height: u32, + bt709_source: bool, +} + +#[derive(Default)] +struct Nv12Converter { + scaler: Option<(ScalerKey, scaling::Context)>, +} + +const SWS_CS_ITU709: i32 = 1; +const SWS_CS_ITU601: i32 = 5; + +impl Nv12Converter { + /// Writes `decoded` into `target`, NV12 at the track's size, in BT.709 + /// because that is how the encoder tags every track. A BT.709 frame of + /// the same size is a plane copy; anything else (a BT.601 or untagged + /// source, which the renderer also reads as BT.601, or a camera swapped + /// mid-recording) goes through swscale with the source's matrix. + fn fill(&mut self, decoded: &DecodedFrame, target: &mut frame::Video) -> Result<(), String> { + let (width, height) = (decoded.width(), decoded.height()); + if width == 0 || height == 0 { + return Err("A decoded frame has no size".into()); + } + let bt709_source = decoded.matrix() == YuvMatrix::Bt709; + if decoded.format() == PixelFormat::Nv12 + && bt709_source + && width == target.width() + && height == target.height() + { + return copy_nv12(decoded, target); + } + + let source = source_frame(decoded)?; + let key = ScalerKey { + format: source.format(), + width, + height, + target_width: target.width(), + target_height: target.height(), + bt709_source, + }; + if self + .scaler + .as_ref() + .is_none_or(|(current, _)| *current != key) + { + let mut context = scaling::Context::get( + key.format, + width, + height, + Pixel::NV12, + key.target_width, + key.target_height, + scaling::Flags::BILINEAR, + ) + .map_err(|error| format!("Could not convert a track frame: {error}"))?; + let source_space = if bt709_source { + SWS_CS_ITU709 + } else { + SWS_CS_ITU601 + }; + // SAFETY: the context was just created and is exclusively owned; + // sws_getCoefficients returns pointers to libswscale's static tables. + unsafe { + ffmpeg::ffi::sws_setColorspaceDetails( + context.as_mut_ptr(), + ffmpeg::ffi::sws_getCoefficients(source_space), + 0, + ffmpeg::ffi::sws_getCoefficients(SWS_CS_ITU709), + 0, + 0, + 1 << 16, + 1 << 16, + ); + } + self.scaler = Some((key, context)); + } + let Some((_, context)) = self.scaler.as_mut() else { + return Err("Could not convert a track frame".into()); + }; + context + .run(&source, target) + .map_err(|error| format!("Could not convert a track frame: {error}")) + } +} + +fn unreadable() -> String { + "A decoded frame could not be read back from the decoder".into() +} + +fn copy_plane( + source: &[u8], + source_stride: usize, + row_bytes: usize, + rows: usize, + target: &mut [u8], + target_stride: usize, +) -> Result<(), String> { + for row in 0..rows { + let from = row * source_stride; + let to = row * target_stride; + let source_row = source.get(from..from + row_bytes).ok_or_else(unreadable)?; + target + .get_mut(to..to + row_bytes) + .ok_or_else(unreadable)? + .copy_from_slice(source_row); + } + Ok(()) +} + +fn copy_nv12(decoded: &DecodedFrame, target: &mut frame::Video) -> Result<(), String> { + let width = decoded.width() as usize; + let height = decoded.height() as usize; + let chroma_rows = height.div_ceil(2); + let chroma_bytes = width.div_ceil(2) * 2; + let y = decoded.y_plane().ok_or_else(unreadable)?; + let uv = decoded.uv_plane().ok_or_else(unreadable)?; + let y_stride = target.stride(0); + copy_plane( + y, + decoded.y_stride() as usize, + width, + height, + target.data_mut(0), + y_stride, + )?; + let uv_stride = target.stride(1); + copy_plane( + uv, + decoded.uv_stride() as usize, + chroma_bytes, + chroma_rows, + target.data_mut(1), + uv_stride, + ) +} + +fn source_frame(decoded: &DecodedFrame) -> Result { + let width = decoded.width(); + let height = decoded.height(); + let (w, h) = (width as usize, height as usize); + match decoded.format() { + PixelFormat::Nv12 => { + let mut frame = frame::Video::new(Pixel::NV12, width, height); + copy_nv12(decoded, &mut frame)?; + Ok(frame) + } + PixelFormat::Yuv420p => { + let mut frame = frame::Video::new(Pixel::YUV420P, width, height); + let planes = [ + (decoded.y_plane(), decoded.y_stride(), w, h), + ( + decoded.u_plane(), + decoded.uv_stride(), + w.div_ceil(2), + h.div_ceil(2), + ), + ( + decoded.v_plane(), + decoded.uv_stride(), + w.div_ceil(2), + h.div_ceil(2), + ), + ]; + for (index, (plane, stride, row_bytes, rows)) in planes.into_iter().enumerate() { + let stride_out = frame.stride(index); + copy_plane( + plane.ok_or_else(unreadable)?, + stride as usize, + row_bytes, + rows, + frame.data_mut(index), + stride_out, + )?; + } + Ok(frame) + } + PixelFormat::Rgba => { + let mut frame = frame::Video::new(Pixel::RGBA, width, height); + let stride_out = frame.stride(0); + copy_plane( + decoded.data(), + w * 4, + w * 4, + h, + frame.data_mut(0), + stride_out, + )?; + Ok(frame) + } + } +} + +async fn export_video_tracks( + prepared: &Prepared, + tracks: &[(TrackKind, PathBuf)], + fps: u32, + total_frames: u32, + cancel: &AtomicBool, + reporter: &mut ProgressReporter<'_, F>, +) -> Result<(), TrackExportError> { + let path_for = |kind| { + tracks + .iter() + .find(|(track, _)| *track == kind) + .map(|(_, path)| path.clone()) + }; + let used_clips = |kind: TrackKind| { + prepared + .recordings + .segments + .iter() + .enumerate() + .filter(move |(index, _)| { + prepared.project.timeline.as_ref().is_none_or(|timeline| { + timeline + .segments + .iter() + .any(|segment| segment.recording_clip as usize == *index) + }) + }) + .filter_map(move |(_, segment)| match kind { + TrackKind::Screen => Some((segment.display.width, segment.display.height)), + _ => segment + .camera + .as_ref() + .map(|camera| (camera.width, camera.height)), + }) + .next() + }; + + let mut screen = None; + let mut camera = None; + let mut setup = Ok(()); + for (kind, slot) in [ + (TrackKind::Screen, &mut screen), + (TrackKind::Camera, &mut camera), + ] { + let Some(path) = path_for(kind) else { + continue; + }; + let spawned = used_clips(kind) + .ok_or(TrackExportError::Unavailable(kind)) + .and_then(|(width, height)| { + VideoTrackWriter::spawn(path, width, height, fps).map_err(TrackExportError::from) + }); + match spawned { + Ok(writer) => *slot = Some(writer), + Err(error) => { + setup = Err(error); + break; + } + } + } + + let result = match setup { + Ok(()) => { + run_video_pass( + prepared, + fps, + total_frames, + cancel, + reporter, + screen.as_ref(), + camera.as_ref(), + ) + .await + } + Err(error) => Err(error), + }; + // Every started encoder is joined before returning, so none still holds + // its partial file open when the caller removes it. + let mut finished = Ok(()); + for writer in [screen, camera].into_iter().flatten() { + if result.is_ok() { + if let Err(error) = writer.finish() + && finished.is_ok() + { + finished = Err(TrackExportError::from(error)); + } + } else { + writer.abandon(); + } + } + result.and(finished) +} + +/// Consecutive output frames a track may go without a decoded frame, as the +/// rendered export allows: few before its first frame, more once running. +const MAX_INITIAL_MISSES: u32 = 8; +const MAX_CONSECUTIVE_MISSES: u32 = 200; + +#[derive(Default)] +struct MissTracker { + decoded: u64, + consecutive: u32, +} + +impl MissTracker { + fn record(&mut self, kind: TrackKind, decoded: bool, time: f64) -> Result<(), String> { + if decoded { + self.decoded += 1; + self.consecutive = 0; + return Ok(()); + } + self.consecutive += 1; + let limit = if self.decoded == 0 { + MAX_INITIAL_MISSES + } else { + MAX_CONSECUTIVE_MISSES + }; + if self.consecutive > limit { + return Err(format!( + "The {} recording stopped returning frames near {time:.1}s", + kind.slug() + )); + } + Ok(()) + } +} + +async fn run_video_pass( + prepared: &Prepared, + fps: u32, + total_frames: u32, + cancel: &AtomicBool, + reporter: &mut ProgressReporter<'_, F>, + screen: Option<&VideoTrackWriter>, + camera: Option<&VideoTrackWriter>, +) -> Result<(), TrackExportError> { + let mut decoded_any = false; + let mut screen_misses = MissTracker::default(); + let mut camera_misses = MissTracker::default(); + for frame_number in 0..total_frames { + if cancel.load(Ordering::Relaxed) { + return Err(TrackExportError::Cancelled); + } + let Some((clip, segment_time)) = frame_source(&prepared.project, frame_number, fps) else { + // Past the timeline's last instant (the frame count rounds up): + // hold the last frame so every track keeps the export's length. + for writer in [screen, camera].into_iter().flatten() { + writer.send(VideoJob::Repeat)?; + } + reporter.advance(); + continue; + }; + let segment = prepared + .segments + .get(clip as usize) + .ok_or_else(|| format!("Clip {clip} is missing from this recording"))?; + let offsets = prepared + .project + .clips + .iter() + .find(|config| config.index == clip) + .map(|config| config.offsets) + .unwrap_or_default(); + let wants_camera = camera.is_some() && segment.decoders.camera_video_dimensions().is_some(); + let frames = segment + .decoders + .decode_for_export( + segment_time, + wants_camera, + screen.is_some(), + offsets, + frame_number, + !decoded_any, + fps, + ) + .await + .map_err(|error| format!("Decoding frame {frame_number} failed: {error}"))?; + let (screen_frame, camera_frame) = match frames { + Some(frames) => (frames.screen_frame, frames.camera_frame), + None => (None, None), + }; + decoded_any |= screen_frame.is_some() || camera_frame.is_some(); + let time = frame_number as f64 / fps as f64; + if screen.is_some() { + screen_misses.record(TrackKind::Screen, screen_frame.is_some(), time)?; + } + if wants_camera { + camera_misses.record(TrackKind::Camera, camera_frame.is_some(), time)?; + } + if let Some(writer) = screen { + writer.send(screen_frame.map_or(VideoJob::Repeat, VideoJob::Frame))?; + } + if let Some(writer) = camera { + // A clip recorded without a camera is black on the camera track; + // only a frame the decoder failed to return repeats the last one. + writer.send(match camera_frame { + Some(frame) => VideoJob::Frame(frame), + None if wants_camera => VideoJob::Repeat, + None => VideoJob::Black, + })?; + } + reporter.advance(); + } + if screen.is_some() && screen_misses.decoded == 0 { + return Err(TrackExportError::Failed( + "The screen recording could not be decoded".into(), + )); + } + if camera.is_some() && camera_misses.decoded == 0 { + return Err(TrackExportError::Failed( + "The camera recording could not be decoded".into(), + )); + } + Ok(()) +} + +/// The editor's mix for one track: every clip edit (cuts, speed and its +/// audio mode, clip volume, enhancement and EQ, transitions, holds) stays, +/// while the project mute and the track level sliders do not, so a track +/// muted in the edit still exports its sound at the level it was recorded. +pub fn track_audio_project(project: &ProjectConfiguration) -> ProjectConfiguration { + let mut project = project.clone(); + project.audio.mute = false; + project.audio.mic_volume_db = 0.0; + project.audio.system_volume_db = 0.0; + if let Some(timeline) = project.timeline.as_mut() { + timeline.audio_segments.clear(); + } + project +} + +async fn export_audio_track( + prepared: &Prepared, + kind: TrackKind, + path: &Path, + fps: u32, + total_frames: u32, + cancel: &AtomicBool, + reporter: &mut ProgressReporter<'_, F>, +) -> Result<(), TrackExportError> { + let mut audio_segments = Vec::with_capacity(prepared.segments.len()); + for segment in &prepared.segments { + let loader = match kind { + TrackKind::Microphone => &segment.audio, + _ => &segment.system_audio, + }; + let audio = loader + .get() + .await + .map_err(|error| format!("Could not read the {} audio: {error}", kind.slug()))?; + let (mic, system) = match kind { + TrackKind::Microphone => (audio, None), + _ => (None, audio), + }; + audio_segments.push(audio_segment_from_decoded( + mic, + system, + segment.audio_timing_repair, + )); + } + + let project = track_audio_project(&prepared.project); + let mut renderer = AudioRenderer::new(audio_segments); + renderer.set_playhead(0.0, &project); + + run_blocking(|| { + let mut wav = WavWriter::create(path)?; + let sample_rate = u64::from(AudioRenderer::SAMPLE_RATE); + let fps = u64::from(fps); + let mut cursor = 0u64; + for frame_number in 0..u64::from(total_frames) { + if cancel.load(Ordering::Relaxed) { + return Err(TrackExportError::Cancelled); + } + if let Some((pts, samples)) = audio_frame_budget(frame_number, sample_rate, fps, cursor) + { + cursor = pts as u64 + samples as u64; + let written = match renderer.render_frame_raw(samples, &project) { + Some((count, data)) => { + let count = count.min(samples).min(data.len() / 2); + wav.write(&data[..count * 2])?; + count + } + None => 0, + }; + wav.write_silence(samples - written)?; + } + reporter.advance(); + } + wav.finish()?; + Ok(()) + }) +} + +/// Runs CPU-bound work without stalling the async workers that the video +/// decoders and the app share, when the runtime has others to hand off to. +fn run_blocking(work: impl FnOnce() -> T) -> T { + match tokio::runtime::Handle::try_current() { + Ok(handle) if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => { + tokio::task::block_in_place(work) + } + _ => work(), + } +} + +struct WavWriter { + file: BufWriter, + frames: u64, +} + +const WAV_CHANNELS: u16 = 2; +const WAV_BYTES_PER_SAMPLE: u16 = 3; +const WAV_HEADER_BYTES: u64 = 44; + +impl WavWriter { + fn create(path: &Path) -> Result { + let file = File::create(path) + .map_err(|error| format!("Could not create {}: {error}", path.display()))?; + let mut writer = Self { + file: BufWriter::with_capacity(1 << 20, file), + frames: 0, + }; + writer.header(0)?; + Ok(writer) + } + + fn header(&mut self, data_bytes: u32) -> Result<(), String> { + let header = wav_header(AudioRenderer::SAMPLE_RATE, data_bytes); + self.file.write_all(&header).map_err(io_error) + } + + fn write(&mut self, interleaved: &[f32]) -> Result<(), String> { + let mut bytes = Vec::with_capacity(interleaved.len() * 3); + for sample in interleaved { + bytes.extend_from_slice(&pcm24(*sample)); + } + self.file.write_all(&bytes).map_err(io_error)?; + self.frames += (interleaved.len() / usize::from(WAV_CHANNELS)) as u64; + Ok(()) + } + + fn write_silence(&mut self, frames: usize) -> Result<(), String> { + if frames == 0 { + return Ok(()); + } + let bytes = vec![0u8; frames * usize::from(WAV_CHANNELS * WAV_BYTES_PER_SAMPLE)]; + self.file.write_all(&bytes).map_err(io_error)?; + self.frames += frames as u64; + Ok(()) + } + + fn finish(mut self) -> Result<(), String> { + let data_bytes = self.frames * u64::from(WAV_CHANNELS * WAV_BYTES_PER_SAMPLE); + let data_bytes = u32::try_from(data_bytes) + .ok() + .filter(|bytes| u64::from(*bytes) + WAV_HEADER_BYTES <= u64::from(u32::MAX)) + .ok_or("This audio track is too long for a WAV file")?; + self.file.flush().map_err(io_error)?; + self.file.seek(SeekFrom::Start(0)).map_err(io_error)?; + self.header(data_bytes)?; + let file = self + .file + .into_inner() + .map_err(|error| io_error(error.into_error()))?; + file.sync_all().map_err(io_error) + } +} + +fn io_error(error: std::io::Error) -> String { + format!("Writing the audio track failed: {error}") +} + +fn wav_header(sample_rate: u32, data_bytes: u32) -> [u8; WAV_HEADER_BYTES as usize] { + let block_align = WAV_CHANNELS * WAV_BYTES_PER_SAMPLE; + let mut header = [0u8; WAV_HEADER_BYTES as usize]; + header[0..4].copy_from_slice(b"RIFF"); + header[4..8].copy_from_slice(&data_bytes.saturating_add(36).to_le_bytes()); + header[8..12].copy_from_slice(b"WAVE"); + header[12..16].copy_from_slice(b"fmt "); + header[16..20].copy_from_slice(&16u32.to_le_bytes()); + header[20..22].copy_from_slice(&1u16.to_le_bytes()); + header[22..24].copy_from_slice(&WAV_CHANNELS.to_le_bytes()); + header[24..28].copy_from_slice(&sample_rate.to_le_bytes()); + header[28..32].copy_from_slice(&(sample_rate * u32::from(block_align)).to_le_bytes()); + header[32..34].copy_from_slice(&block_align.to_le_bytes()); + header[34..36].copy_from_slice(&(WAV_BYTES_PER_SAMPLE * 8).to_le_bytes()); + header[36..40].copy_from_slice(b"data"); + header[40..44].copy_from_slice(&data_bytes.to_le_bytes()); + header +} + +fn pcm24(sample: f32) -> [u8; 3] { + let sample = if sample.is_finite() { sample } else { 0.0 }; + let value = (sample.clamp(-1.0, 1.0) * 8_388_607.0).round() as i32; + let bytes = value.to_le_bytes(); + [bytes[0], bytes[1], bytes[2]] +} + +#[cfg(test)] +mod tests { + use super::*; + + fn project(timeline: serde_json::Value) -> ProjectConfiguration { + serde_json::from_value(serde_json::json!({ "timeline": timeline })).unwrap() + } + + fn clip(index: u32, start: f64, end: f64, timescale: f64) -> serde_json::Value { + serde_json::json!({ + "recordingSegment": index, + "start": start, + "end": end, + "timescale": timescale, + }) + } + + fn timeline(segments: Vec) -> serde_json::Value { + serde_json::json!({ "segments": segments, "zoomSegments": [] }) + } + + #[test] + fn frames_follow_cuts_clip_order_and_speed() { + let project = project(timeline(vec![ + clip(1, 10.0, 12.0, 1.0), + clip(0, 4.0, 8.0, 2.0), + ])); + let fps = 10; + let duration = project.timeline.as_ref().unwrap().duration(); + assert_eq!(duration, 4.0); + assert_eq!(total_frames(duration, fps), 40); + + assert_eq!(frame_source(&project, 0, fps), Some((1, 10.0))); + let (clip, time) = frame_source(&project, 15, fps).unwrap(); + assert_eq!(clip, 1); + assert!((time - 11.5).abs() < 1e-9); + assert_eq!(frame_source(&project, 20, fps), Some((0, 4.0))); + let (clip, time) = frame_source(&project, 35, fps).unwrap(); + assert_eq!(clip, 0); + assert!((time - 7.0).abs() < 1e-9); + assert_eq!(frame_source(&project, 40, fps), None); + } + + #[test] + fn transitions_read_the_incoming_clip_and_shorten_the_tracks() { + let mut timeline = timeline(vec![clip(0, 0.0, 4.0, 1.0), clip(1, 0.0, 4.0, 1.0)]); + timeline["transitions"] = serde_json::json!([ + { "segmentIndex": 1, "type": "cross-fade", "duration": 1.0 } + ]); + let project = project(timeline); + let fps = 30; + assert_eq!( + total_frames(project.timeline.as_ref().unwrap().duration(), fps), + 210 + ); + assert_eq!(frame_source(&project, 89, fps).unwrap().0, 0); + let (clip, time) = frame_source(&project, 90, fps).unwrap(); + assert_eq!(clip, 1); + assert!(time.abs() < 1e-9); + let (clip, time) = frame_source(&project, 120, fps).unwrap(); + assert_eq!(clip, 1); + assert!((time - 1.0).abs() < 1e-9); + } + + #[test] + fn fullscreen_text_holds_freeze_the_source_and_lengthen_the_tracks() { + let mut timeline = timeline(vec![clip(0, 0.0, 4.0, 1.0)]); + timeline["textSegments"] = serde_json::json!([{ + "start": 2.0, + "end": 3.0, + "enabled": true, + "layout": "fullscreen", + "content": "Title" + }]); + let project = project(timeline); + let fps = 10; + assert_eq!( + total_frames(project.timeline.as_ref().unwrap().duration(), fps), + 50 + ); + let held = frame_source(&project, 20, fps).unwrap(); + assert_eq!(frame_source(&project, 25, fps).unwrap(), held); + assert_eq!(frame_source(&project, 29, fps).unwrap(), held); + let (_, after) = frame_source(&project, 30, fps).unwrap(); + assert!((after - 2.0).abs() < 1e-9); + } + + #[test] + fn audio_tracks_have_exactly_the_samples_of_the_video_frames() { + for fps in [15u64, 24, 30, 60] { + for total in [1u64, 29, 1_000, 5_401] { + let mut cursor = 0; + for frame in 0..total { + if let Some((pts, samples)) = audio_frame_budget(frame, 48_000, fps, cursor) { + assert_eq!(pts as u64, cursor); + cursor += samples as u64; + } + } + assert_eq!(cursor, total * 48_000 / fps, "{fps} fps, {total} frames"); + } + } + } + + #[test] + fn track_audio_keeps_clip_edits_but_ignores_mute_and_track_levels() { + let mut source = project(timeline(vec![clip(0, 0.0, 4.0, 1.0)])); + source.audio.mute = true; + source.audio.improve = true; + source.audio.mic_volume_db = -12.0; + source.audio.system_volume_db = -30.0; + if let Some(timeline) = source.timeline.as_mut() { + timeline.segments[0].volume = Some(0.5); + } + let audio = track_audio_project(&source); + assert!(!audio.audio.mute); + assert!(audio.audio.improve); + assert_eq!(audio.audio.mic_volume_db, 0.0); + assert_eq!(audio.audio.system_volume_db, 0.0); + assert_eq!(audio.timeline.unwrap().segments[0].volume, Some(0.5)); + } + + #[test] + fn slugs_round_trip_and_accept_short_names() { + for kind in TrackKind::ALL { + assert_eq!(TrackKind::from_slug(kind.slug()), Some(kind)); + } + assert_eq!(TrackKind::from_slug("mic"), Some(TrackKind::Microphone)); + assert_eq!( + TrackKind::from_slug(" System "), + Some(TrackKind::SystemAudio) + ); + assert_eq!(TrackKind::from_slug("cursor"), None); + let names = TrackKind::ALL.map(TrackKind::file_name); + assert_eq!( + names, + [ + "Screen.mp4", + "Camera.mp4", + "Microphone.wav", + "System audio.wav" + ] + ); + } + + #[test] + fn progress_counts_one_video_pass_and_each_audio_track() { + assert_eq!(progress_total(&[TrackKind::Screen], 100), 100); + assert_eq!( + progress_total(&[TrackKind::Screen, TrackKind::Camera], 100), + 100 + ); + assert_eq!(progress_total(&TrackKind::ALL, 100), 300); + assert_eq!(progress_total(&[TrackKind::Microphone], 100), 100); + } + + #[test] + fn wav_header_describes_48k_stereo_24_bit() { + let header = wav_header(48_000, 600); + assert_eq!(&header[0..4], b"RIFF"); + assert_eq!(u32::from_le_bytes(header[4..8].try_into().unwrap()), 636); + assert_eq!(u16::from_le_bytes(header[22..24].try_into().unwrap()), 2); + assert_eq!( + u32::from_le_bytes(header[24..28].try_into().unwrap()), + 48_000 + ); + assert_eq!( + u32::from_le_bytes(header[28..32].try_into().unwrap()), + 288_000 + ); + assert_eq!(u16::from_le_bytes(header[32..34].try_into().unwrap()), 6); + assert_eq!(u16::from_le_bytes(header[34..36].try_into().unwrap()), 24); + assert_eq!(u32::from_le_bytes(header[40..44].try_into().unwrap()), 600); + } + + #[test] + fn pcm24_clamps_and_survives_bad_samples() { + assert_eq!(pcm24(0.0), [0, 0, 0]); + assert_eq!(pcm24(1.0), [0xff, 0xff, 0x7f]); + assert_eq!(pcm24(2.0), [0xff, 0xff, 0x7f]); + assert_eq!(pcm24(-1.0), [0x01, 0x00, 0x80]); + assert_eq!(pcm24(f32::NAN), [0, 0, 0]); + } + + #[test] + fn wav_files_are_sized_on_finish() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("a.wav"); + let mut wav = WavWriter::create(&path).unwrap(); + wav.write(&[0.5, -0.5, 0.25, -0.25]).unwrap(); + wav.write_silence(3).unwrap(); + wav.finish().unwrap(); + let bytes = std::fs::read(&path).unwrap(); + assert_eq!(bytes.len(), 44 + 5 * 6); + assert_eq!(u32::from_le_bytes(bytes[40..44].try_into().unwrap()), 30); + assert_eq!(&bytes[44..47], &pcm24(0.5)); + assert!(bytes[44 + 12..].iter().all(|byte| *byte == 0)); + } + + #[test] + fn staged_files_are_removed_unless_kept() { + let dir = tempfile::tempdir().unwrap(); + let staged = staging_path(dir.path(), TrackKind::Camera, "a"); + std::fs::write(&staged, b"partial").unwrap(); + drop(StagedFiles(vec![staged.clone()])); + assert!(!staged.exists()); + assert_ne!(staged, staging_path(dir.path(), TrackKind::Camera, "b")); + assert_ne!(export_token(), ""); + } + + #[test] + fn publishing_replaces_every_track_or_none() { + let dir = tempfile::tempdir().unwrap(); + let path = |name: &str| dir.path().join(name); + std::fs::write(path("Screen.mp4"), b"old screen").unwrap(); + std::fs::write(path("Camera.mp4"), b"old camera").unwrap(); + std::fs::write(path("screen.new"), b"new screen").unwrap(); + let files = vec![ + (path("screen.new"), path("Screen.mp4")), + (path("camera.missing"), path("Camera.mp4")), + ]; + assert!(publish(&files, "t").is_err()); + assert_eq!(std::fs::read(path("Screen.mp4")).unwrap(), b"old screen"); + assert_eq!(std::fs::read(path("Camera.mp4")).unwrap(), b"old camera"); + + std::fs::write(path("screen.new"), b"new screen").unwrap(); + std::fs::write(path("camera.new"), b"new camera").unwrap(); + std::fs::write(path("mic.new"), b"new mic").unwrap(); + let files = vec![ + (path("screen.new"), path("Screen.mp4")), + (path("camera.new"), path("Camera.mp4")), + (path("mic.new"), path("Microphone.wav")), + ]; + std::fs::create_dir(path("Microphone.wav")).unwrap(); + assert!(publish(&files, "t").unwrap_err().contains("is a folder")); + assert_eq!(std::fs::read(path("Screen.mp4")).unwrap(), b"old screen"); + std::fs::remove_dir(path("Microphone.wav")).unwrap(); + + publish(&files, "t").unwrap(); + assert_eq!(std::fs::read(path("Screen.mp4")).unwrap(), b"new screen"); + assert_eq!(std::fs::read(path("Camera.mp4")).unwrap(), b"new camera"); + assert_eq!(std::fs::read(path("Microphone.wav")).unwrap(), b"new mic"); + let mut left = std::fs::read_dir(dir.path()) + .unwrap() + .map(|entry| entry.unwrap().file_name().into_string().unwrap()) + .collect::>(); + left.sort(); + assert_eq!(left, ["Camera.mp4", "Microphone.wav", "Screen.mp4"]); + } + + #[test] + fn missing_frames_fail_instead_of_exporting_a_blank_track() { + let mut misses = MissTracker::default(); + for _ in 0..MAX_INITIAL_MISSES { + misses.record(TrackKind::Screen, false, 0.0).unwrap(); + } + assert!(misses.record(TrackKind::Screen, false, 0.3).is_err()); + + let mut misses = MissTracker::default(); + misses.record(TrackKind::Camera, true, 0.0).unwrap(); + for _ in 0..MAX_CONSECUTIVE_MISSES { + misses.record(TrackKind::Camera, false, 1.0).unwrap(); + } + misses.record(TrackKind::Camera, true, 2.0).unwrap(); + for _ in 0..MAX_CONSECUTIVE_MISSES { + misses.record(TrackKind::Camera, false, 3.0).unwrap(); + } + let error = misses.record(TrackKind::Camera, false, 9.5).unwrap_err(); + assert!(error.contains("camera") && error.contains("9.5"), "{error}"); + } + + fn nv12_frame(y: u8, u: u8, v: u8, matrix: YuvMatrix) -> DecodedFrame { + let (width, height) = (16u32, 16u32); + let mut data = vec![y; (width * height) as usize]; + for _ in 0..(width * height / 4) { + data.extend_from_slice(&[u, v]); + } + DecodedFrame::new_nv12(data, width, height, width, width).with_matrix(matrix) + } + + #[test] + fn tracks_come_out_in_bt709_whatever_the_source_matrix() { + ffmpeg::init().unwrap(); + let mut converter = Nv12Converter::default(); + let mut target = black_nv12(16, 16); + + converter + .fill(&nv12_frame(81, 90, 240, YuvMatrix::Bt709), &mut target) + .unwrap(); + assert_eq!(target.data(0)[0], 81); + assert_eq!(&target.data(1)[..2], &[90, 240]); + + converter + .fill(&nv12_frame(81, 90, 240, YuvMatrix::Bt601), &mut target) + .unwrap(); + let close = |actual: u8, expected: u8| actual.abs_diff(expected) <= 3; + let (y, u, v) = (target.data(0)[0], target.data(1)[0], target.data(1)[1]); + assert!( + close(y, 63) && close(u, 102) && close(v, 240), + "{y} {u} {v}" + ); + } +} diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs index 8ba9ffeab83..7204ca19151 100644 --- a/crates/rendering/src/lib.rs +++ b/crates/rendering/src/lib.rs @@ -803,6 +803,32 @@ impl RecordingSegmentDecoders { .await } + /// The raw frames the rendered export would composite for `segment_time`, + /// with the same offline waits, retries and initial-frame recovery. + #[allow(clippy::too_many_arguments)] + pub async fn decode_for_export( + &self, + segment_time: f64, + needs_camera: bool, + needs_display: bool, + offsets: ClipOffsets, + frame_number: u32, + is_initial_frame: bool, + fps: u32, + ) -> Result, RenderingError> { + decode_segment_frames_with_retry( + self, + segment_time, + needs_camera, + needs_display, + offsets, + frame_number, + is_initial_frame, + fps, + ) + .await + } + async fn fetch_frames( &self, segment_time: f32,