diff --git a/apps/desktop-gpui/locales/de.toml b/apps/desktop-gpui/locales/de.toml index c96d36f22a2..7ec8d9f5529 100644 --- a/apps/desktop-gpui/locales/de.toml +++ b/apps/desktop-gpui/locales/de.toml @@ -2633,6 +2633,7 @@ model_downloaded = "Modell {model} heruntergeladen" model_label = "Modell" model_updated = "Empfohlenes Modell aktualisiert" model_updated_description = "Wir empfehlen die neue Version. Sie ist genauer und erkennt auch kurze Aufnahmen, die der alten entgangen sind." +new_caption = "Neuer Untertitel" position = "Position" refining_word_timings = "Wort-Timings werden verfeinert…" regenerate = "Untertitel neu erstellen" @@ -2908,6 +2909,7 @@ video = "Video" zoom = "Zoom" [editor.transcript] +add_caption = "Untertitel am Abspielkopf hinzufügen" cleanup = "Aufräumen" cleanup_tooltip = "Füllwörter entfernen und Pausen kürzen" delete_matches = "Alle löschen" @@ -2967,6 +2969,7 @@ audio_segment_changed = "Dieses Audiosegment hat sich geändert. Wählen Sie es crop = "Zuschneiden" crop_tooltip = "Video zuschneiden" no_room_for_3d_shot = "Kein Platz für eine weitere 3D-Einstellung" +no_room_for_caption = "Hier ist kein Platz für einen weiteren Untertitel" redo = "Wiederholen" save_failed = "{path} konnte nicht gespeichert werden. Lassen Sie den Editor geöffnet und versuchen Sie es erneut: {error}" undo = "Rückgängig" diff --git a/apps/desktop-gpui/locales/en.toml b/apps/desktop-gpui/locales/en.toml index 29f268aa5c6..09db6d59673 100644 --- a/apps/desktop-gpui/locales/en.toml +++ b/apps/desktop-gpui/locales/en.toml @@ -2635,6 +2635,7 @@ model_downloaded = "{model} model downloaded" model_label = "Model" model_updated = "Recommended model updated" model_updated_description = "We recommend the new version. It is more accurate and catches short recordings the old one missed." +new_caption = "New caption" position = "Position" refining_word_timings = "Refining word timings…" regenerate = "Regenerate Captions" @@ -2910,6 +2911,7 @@ video = { text = "Video", context = "Timeline gutter label of the recording's vi zoom = "Zoom" [editor.transcript] +add_caption = "Add caption at playhead" cleanup = "Clean up" cleanup_tooltip = "Remove filler words and shorten pauses" delete_matches = "Delete all" @@ -2969,6 +2971,7 @@ audio_segment_changed = "This audio segment changed. Select it and choose a repl crop = "Crop" crop_tooltip = "Crop Video" no_room_for_3d_shot = "No room for another 3D shot" +no_room_for_caption = "No room for another caption here" redo = "Redo" save_failed = { text = "Could not save {path}. Keep the editor open and try again: {error}", context = "{path} is the recording's folder; {error} is a technical message that stays in English." } undo = "Undo" diff --git a/apps/desktop-gpui/locales/es.toml b/apps/desktop-gpui/locales/es.toml index 27fbf306d17..3bc7f030c36 100644 --- a/apps/desktop-gpui/locales/es.toml +++ b/apps/desktop-gpui/locales/es.toml @@ -2633,6 +2633,7 @@ model_downloaded = "Modelo {model} descargado" model_label = "Modelo" model_updated = "Modelo recomendado actualizado" model_updated_description = "Te recomendamos la nueva versión. Es más precisa y transcribe grabaciones cortas que la anterior pasaba por alto." +new_caption = "Subtítulo nuevo" position = "Posición" refining_word_timings = "Ajustando los tiempos de las palabras…" regenerate = "Regenerar subtítulos" @@ -2908,6 +2909,7 @@ video = "Vídeo" zoom = "Zoom" [editor.transcript] +add_caption = "Añadir subtítulo en el cabezal" cleanup = "Limpiar" cleanup_tooltip = "Quitar muletillas y acortar pausas" delete_matches = "Eliminar todo" @@ -2967,6 +2969,7 @@ audio_segment_changed = "Este segmento de audio cambió. Selecciónalo y vuelve crop = "Recortar" crop_tooltip = "Recortar vídeo" no_room_for_3d_shot = "No hay espacio para otra toma 3D" +no_room_for_caption = "No hay espacio para otro subtítulo aquí" redo = "Rehacer" save_failed = "No se pudo guardar {path}. Mantén el editor abierto e inténtalo de nuevo: {error}" undo = "Deshacer" diff --git a/apps/desktop-gpui/locales/fr.toml b/apps/desktop-gpui/locales/fr.toml index 87b46c59460..7d32287f919 100644 --- a/apps/desktop-gpui/locales/fr.toml +++ b/apps/desktop-gpui/locales/fr.toml @@ -2633,6 +2633,7 @@ model_downloaded = "Modèle {model} téléchargé" model_label = "Modèle" model_updated = "Modèle recommandé mis à jour" model_updated_description = "Nous recommandons la nouvelle version. Elle est plus précise et transcrit les enregistrements courts que l'ancienne ne captait pas." +new_caption = "Nouveau sous-titre" position = "Position" refining_word_timings = "Affinage du minutage des mots…" regenerate = "Régénérer les sous-titres" @@ -2908,6 +2909,7 @@ video = "Vidéo" zoom = "Zoom" [editor.transcript] +add_caption = "Ajouter un sous-titre à la tête de lecture" cleanup = "Nettoyer" cleanup_tooltip = "Supprimer les mots de remplissage et raccourcir les pauses" delete_matches = "Tout supprimer" @@ -2967,6 +2969,7 @@ audio_segment_changed = "Ce segment audio a changé. Sélectionnez-le et choisis crop = "Recadrer" crop_tooltip = "Recadrer la vidéo" no_room_for_3d_shot = "Plus de place pour un autre plan 3D" +no_room_for_caption = "Pas de place pour un autre sous-titre ici" redo = "Rétablir" save_failed = "Impossible d'enregistrer {path}. Gardez l'éditeur ouvert et réessayez : {error}" undo = "Annuler" diff --git a/apps/desktop-gpui/locales/hi.toml b/apps/desktop-gpui/locales/hi.toml index 319ed6675bb..c01b160cd3c 100644 --- a/apps/desktop-gpui/locales/hi.toml +++ b/apps/desktop-gpui/locales/hi.toml @@ -2633,6 +2633,7 @@ model_downloaded = "{model} मॉडल डाउनलोड हो गया" model_label = "मॉडल" model_updated = "सुझाया गया मॉडल अपडेट हुआ" model_updated_description = "हम नया वर्ज़न इस्तेमाल करने की सलाह देते हैं। यह ज़्यादा सटीक है और उन छोटी रिकॉर्डिंग को भी पकड़ता है जो पुराने वर्ज़न से छूट जाती थीं।" +new_caption = "नया कैप्शन" position = "पोज़िशन" refining_word_timings = "शब्दों का समय सुधारा जा रहा है…" regenerate = "कैप्शन दोबारा बनाएँ" @@ -2908,6 +2909,7 @@ video = "वीडियो" zoom = "ज़ूम" [editor.transcript] +add_caption = "प्लेहेड पर कैप्शन जोड़ें" cleanup = "साफ़ करें" cleanup_tooltip = "फ़िलर शब्द हटाएँ और ठहराव छोटे करें" delete_matches = "सभी हटाएँ" @@ -2967,6 +2969,7 @@ audio_segment_changed = "यह ऑडियो सेगमेंट बदल crop = "क्रॉप" crop_tooltip = "वीडियो क्रॉप करें" no_room_for_3d_shot = "एक और 3D शॉट के लिए जगह नहीं है" +no_room_for_caption = "यहाँ एक और कैप्शन के लिए जगह नहीं है" redo = "फिर करें" save_failed = "{path} सेव नहीं हो सका। एडिटर खुला रखें और फिर से कोशिश करें: {error}" undo = "पहले जैसा करें" diff --git a/apps/desktop-gpui/locales/ja.toml b/apps/desktop-gpui/locales/ja.toml index d007debfd05..5ae6a6237cd 100644 --- a/apps/desktop-gpui/locales/ja.toml +++ b/apps/desktop-gpui/locales/ja.toml @@ -2633,6 +2633,7 @@ model_downloaded = "{model}モデルをダウンロード済み" model_label = "モデル" model_updated = "推奨モデルを更新しました" model_updated_description = "新しいバージョンをおすすめします。より正確になり、以前のバージョンでは聞き取れなかった短い録画も文字起こしできます。" +new_caption = "新しい字幕" position = "位置" refining_word_timings = "単語のタイミングを調整中…" regenerate = "字幕を再生成" @@ -2908,6 +2909,7 @@ video = "動画" zoom = "ズーム" [editor.transcript] +add_caption = "再生ヘッドに字幕を追加" cleanup = "クリーンアップ" cleanup_tooltip = "フィラーを削除し、間を短くします" delete_matches = "すべて削除" @@ -2967,6 +2969,7 @@ audio_segment_changed = "この音声セグメントは変更されました。 crop = "切り抜き" crop_tooltip = "動画を切り抜く" no_room_for_3d_shot = "別の3Dショットを入れる余地がありません" +no_room_for_caption = "ここには字幕を追加する余地がありません" redo = "やり直す" save_failed = "{path}を保存できませんでした。エディタを開いたまま、もう一度お試しください:{error}" undo = "取り消す" diff --git a/apps/desktop-gpui/locales/zh-CN.toml b/apps/desktop-gpui/locales/zh-CN.toml index 922d4ee4a5d..19c4fba9dfc 100644 --- a/apps/desktop-gpui/locales/zh-CN.toml +++ b/apps/desktop-gpui/locales/zh-CN.toml @@ -2633,6 +2633,7 @@ model_downloaded = "已下载“{model}”模型" model_label = "模型" model_updated = "推荐模型已更新" model_updated_description = "推荐使用新版本。它更准确,还能转写旧版本漏掉的短录制内容。" +new_caption = "新字幕" position = "位置" refining_word_timings = "正在优化单词时间…" regenerate = "重新生成字幕" @@ -2908,6 +2909,7 @@ video = "视频" zoom = "缩放" [editor.transcript] +add_caption = "在播放头处添加字幕" cleanup = "清理" cleanup_tooltip = "移除填充词并缩短停顿" delete_matches = "全部删除" @@ -2967,6 +2969,7 @@ audio_segment_changed = "此音频分段已更改。请选中它并重新选择 crop = "裁剪" crop_tooltip = "裁剪视频" no_room_for_3d_shot = "没有空间再添加 3D 镜头" +no_room_for_caption = "这里没有空间再添加字幕" redo = "重做" save_failed = "无法保存 {path}。请保持编辑器打开并重试:{error}" undo = "撤销" diff --git a/apps/desktop-gpui/src/editor_edits.rs b/apps/desktop-gpui/src/editor_edits.rs index 079eab3403a..b0cf7173c73 100644 --- a/apps/desktop-gpui/src/editor_edits.rs +++ b/apps/desktop-gpui/src/editor_edits.rs @@ -2109,6 +2109,83 @@ pub fn insert_scene_segment(timeline: &mut TimelineConfiguration, segment: Scene index } +pub const NEW_CAPTION_LENGTH: f64 = 2.0; + +pub const MIN_ADDED_CAPTION: f64 = 0.25; + +/// Adds a caption with no word timings over `output`, in edited-timeline +/// seconds, to the source-time captions, and switches captions on. Returns +/// its index in `captions.segments`. +/// +/// It is written in recording time like every other caption, so it stays on +/// its moment as clips are trimmed, cut or moved. It ends where its clip does: +/// past that point the recording jumps, and the caption would land on footage +/// it was never placed over. `None` when the clip leaves less than +/// [`MIN_ADDED_CAPTION`] of room, as inside a fullscreen text hold, or when +/// legacy captions cannot be moved to recording time yet. +pub fn add_caption_segment( + project: &mut ProjectConfiguration, + output: std::ops::Range, + id: String, + text: &str, + recording_durations: &[f64], +) -> Option { + let timeline = project.timeline.as_ref()?; + let holds = timeline.hold_windows(); + if holds + .iter() + .any(|(start, end)| output.start >= *start && output.start < *end) + { + return None; + } + let effective = |time: f64| time - held_time_before(&holds, time); + let (start, end) = (effective(output.start), effective(output.end)); + let mappings = crate::transcript::model::build_mappings(timeline, recording_durations); + let mapping = mappings + [..mappings.partition_point(|mapping| mapping.effective_start <= start + 1e-9)] + .last()?; + let start = start.max(mapping.effective_start); + let end = end.min(mapping.effective_end()); + if !start.is_finite() || !end.is_finite() || end - start < MIN_ADDED_CAPTION { + return None; + } + let source = |time: f64| { + (mapping.source_start + (time - mapping.effective_start) * mapping.timescale) as f32 + }; + let (source_start, source_end) = (source(start), source(end)); + if project + .captions + .as_ref() + .is_some_and(|captions| !captions.source_timed) + { + cap_project::synchronize_captions(project, recording_durations); + } + let captions = project + .captions + .get_or_insert_with(|| cap_project::CaptionsData { + source_timed: true, + ..Default::default() + }); + if !captions.source_timed { + return None; + } + captions.settings.enabled = true; + let index = captions + .segments + .partition_point(|segment| segment.start <= source_start); + captions.segments.insert( + index, + cap_project::CaptionSegment { + id, + start: source_start, + end: source_end, + text: text.to_string(), + words: Vec::new(), + }, + ); + Some(index) +} + pub fn find_placement( segments: &[T], time: f64, @@ -5788,3 +5865,142 @@ mod style_image_replacement_tests { ); } } + +#[cfg(test)] +mod add_caption_tests { + use super::*; + + fn project(timeline: serde_json::Value) -> ProjectConfiguration { + serde_json::from_value(serde_json::json!({ "timeline": timeline })).unwrap() + } + + fn add(project: &mut ProjectConfiguration, output: std::ops::Range) -> Option { + add_caption_segment(project, output, "caption-test".into(), "Hello", &[10.0]) + } + + fn source(project: &ProjectConfiguration) -> Vec<(String, f32, f32)> { + project + .captions + .as_ref() + .unwrap() + .segments + .iter() + .map(|segment| (segment.text.clone(), segment.start, segment.end)) + .collect() + } + + fn track(project: &mut ProjectConfiguration) -> Vec<(String, f64, f64)> { + cap_project::synchronize_captions(project, &[10.0]); + project + .timeline + .as_ref() + .unwrap() + .caption_segments + .iter() + .map(|segment| (segment.text.clone(), segment.start, segment.end)) + .collect() + } + + #[test] + fn a_caption_with_no_transcript_turns_captions_on_in_recording_time() { + let mut project = project(serde_json::json!({ + "segments": [{ "start": 0.0, "end": 10.0, "timescale": 1.0 }], + "zoomSegments": [] + })); + assert_eq!(add(&mut project, 1.0..3.0), Some(0)); + let captions = project.captions.as_ref().unwrap(); + assert!(captions.settings.enabled && captions.source_timed); + assert!(captions.segments[0].words.is_empty()); + assert_eq!(captions.segments[0].id, "caption-test"); + assert_eq!(track(&mut project), [("Hello".to_string(), 1.0, 3.0)]); + } + + #[test] + fn a_caption_follows_clip_speed_and_stops_at_its_clip_edge() { + let mut project = project(serde_json::json!({ + "segments": [ + { "start": 0.0, "end": 4.0, "timescale": 2.0 }, + { "start": 6.0, "end": 10.0, "timescale": 1.0 } + ], + "zoomSegments": [] + })); + assert_eq!(add(&mut project, 0.5..1.5), Some(0)); + assert_eq!(add(&mut project, 1.5..3.5), Some(1)); + assert_eq!(add(&mut project, 4.0..6.0), Some(2)); + assert_eq!( + source(&project), + [ + ("Hello".to_string(), 1.0, 3.0), + ("Hello".to_string(), 3.0, 4.0), + ("Hello".to_string(), 8.0, 10.0), + ] + ); + assert_eq!( + track(&mut project), + [ + ("Hello".to_string(), 0.5, 1.5), + ("Hello".to_string(), 1.5, 2.0), + ("Hello".to_string(), 4.0, 6.0), + ] + ); + assert_eq!(add(&mut project, 1.9..3.0), None); + } + + #[test] + fn a_caption_under_a_fullscreen_hold_is_refused() { + let mut project = project(serde_json::json!({ + "segments": [{ "start": 0.0, "end": 10.0, "timescale": 1.0 }], + "zoomSegments": [], + "textSegments": [{ "start": 2.0, "end": 4.0, "layout": "fullscreen" }] + })); + assert_eq!(add(&mut project, 2.5..3.5), None); + assert_eq!(add(&mut project, 3.0..5.0), None); + assert_eq!(add(&mut project, 2.0..4.5), None); + assert!(project.captions.is_none()); + assert_eq!(add(&mut project, 4.0..6.0), Some(0)); + assert_eq!(source(&project), [("Hello".to_string(), 2.0, 4.0)]); + assert_eq!(track(&mut project), [("Hello".to_string(), 4.0, 6.0)]); + } + + #[test] + fn a_caption_inside_a_later_cut_leaves_with_it_and_comes_back_on_restore() { + let mut project = project(serde_json::json!({ + "segments": [{ "start": 0.0, "end": 10.0, "timescale": 1.0 }], + "zoomSegments": [] + })); + assert_eq!(add(&mut project, 4.0..5.0), Some(0)); + let uncut = project.clone(); + let timeline = project.timeline.as_mut().unwrap(); + crate::transcript::cuts::apply_source_cuts(timeline, &[10.0], &[3.5..5.5]); + assert!(track(&mut project).is_empty()); + assert_eq!(source(&project), [("Hello".to_string(), 4.0, 5.0)]); + let mut restored = uncut; + assert_eq!(track(&mut restored), [("Hello".to_string(), 4.0, 5.0)]); + } + + #[test] + fn legacy_output_time_captions_move_to_recording_time_first() { + let mut project: ProjectConfiguration = serde_json::from_value(serde_json::json!({ + "timeline": { + "segments": [ + { "start": 0.0, "end": 4.0, "timescale": 1.0 }, + { "start": 6.0, "end": 10.0, "timescale": 1.0 } + ], + "zoomSegments": [] + }, + "captions": { + "segments": [{ "id": "segment-0", "start": 5.0, "end": 6.0, "text": "old" }], + "settings": {} + } + })) + .unwrap(); + assert_eq!(add(&mut project, 1.0..2.0), Some(0)); + assert_eq!( + source(&project), + [ + ("Hello".to_string(), 1.0, 2.0), + ("old".to_string(), 7.0, 8.0), + ] + ); + } +} diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index d19371448c6..3f135afb6c2 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -4891,6 +4891,13 @@ impl EditorWindow { } else if kind == TrackKind::Audio { cx.stop_propagation(); self.open_audio_picker(lane, cx); + } else if kind == TrackKind::Caption { + cx.stop_propagation(); + let length = (1.0_f64).max(secs_per_pixel * 80.); + if self.add_caption_at(press_time, length, true, window, cx) { + self.seek_to_time(press_time, cx); + self.view.preview_time = None; + } } else if matches!( kind, TrackKind::Text | TrackKind::Shape | TrackKind::Mask | TrackKind::Scene @@ -5933,7 +5940,7 @@ pub(crate) struct EditorNotice { /// How long a notice stays up. const EDITOR_NOTICE_DURATION: Duration = Duration::from_millis(2500); -fn fresh_caption_source_id(project: &ProjectConfiguration) -> String { +fn fresh_caption_source_id(project: &ProjectConfiguration, prefix: &str) -> String { use std::hash::{BuildHasher, Hasher}; loop { @@ -5944,7 +5951,7 @@ fn fresh_caption_source_id(project: &ProjectConfiguration) -> String { let random = std::collections::hash_map::RandomState::new() .build_hasher() .finish(); - let id = format!("cap-split-{millis}-{random:x}"); + let id = format!("{prefix}-{millis}-{random:x}"); if project .captions .as_ref() @@ -5995,7 +6002,12 @@ fn split_caption_segment( if split_source <= f64::from(source.start) || split_source >= f64::from(source.end) { return false; } - let tail_id = fresh_caption_source_id(project); + let tail_prefix = if crate::transcription::is_manual_caption(&source) { + crate::transcription::MANUAL_CAPTION_PREFIX.trim_end_matches('-') + } else { + "cap-split" + }; + let tail_id = fresh_caption_source_id(project, tail_prefix); let Some(timeline) = project.timeline.as_mut() else { return false; }; @@ -6515,6 +6527,66 @@ impl EditorWindow { self.note_edit("add-track", Some(kind)); } + pub(crate) fn add_caption_at( + &mut self, + time: f64, + length: f64, + centered: bool, + window: &mut Window, + cx: &mut Context, + ) -> bool { + let durations = self.clip_display_durations.clone(); + if !edits::ensure_timeline(&mut self.project, &durations) { + return false; + } + let total = self.total_duration(); + let length = length.min(total); + let desired = if centered { time } else { time + length / 2.0 }; + let placement = self.project.timeline.as_ref().and_then(|timeline| { + edits::find_placement(&timeline.caption_segments, desired, length, total) + }); + let id = fresh_caption_source_id( + &self.project, + crate::transcription::MANUAL_CAPTION_PREFIX.trim_end_matches('-'), + ); + let showed_track = self.tracks.caption; + self.tracks.caption = true; + let mut added = false; + if let Some((start, end)) = placement { + self.edit_timeline_project( + |project| { + added = edits::add_caption_segment( + project, + start..end, + id.clone(), + tr::editor::tabs::captions::new_caption(), + &durations, + ) + .is_some(); + added + }, + window, + cx, + ); + } + if !added { + self.tracks.caption = showed_track; + self.show_notice(tr::editor::window::no_room_for_caption(), window, cx); + return false; + } + let index = self.project.timeline.as_ref().and_then(|timeline| { + timeline + .caption_segments + .iter() + .position(|segment| cap_project::source_caption_id(&segment.id) == id) + }); + if let Some(index) = index { + self.set_selection(Some(Selection::single(TrackKind::Caption, index)), cx); + } + self.note_edit("add-caption", Some(TrackKind::Caption)); + true + } + /// `finish(e)`: resume the history, and -- if the drag never promoted -- /// select instead, which also moves the playhead /// (`props.handleUpdatePlayhead(e)`). @@ -14916,6 +14988,38 @@ mod tests { assert_eq!(project.timeline.as_ref().unwrap().caption_segments.len(), 1); } + #[test] + fn both_halves_of_a_split_hand_added_caption_survive_a_new_transcript() { + let mut project: ProjectConfiguration = serde_json::from_value(serde_json::json!({ + "timeline": { + "segments": [{ "start": 0.0, "end": 10.0, "timescale": 1.0 }], + "zoomSegments": [] + }, + "captions": { + "segments": [{ "id": "caption-1-a", "start": 4.0, "end": 6.0, "text": "Chapter two" }], + "settings": { "enabled": true }, + "sourceTimed": true + } + })) + .unwrap(); + cap_project::synchronize_captions(&mut project, &[10.0]); + + assert!(split_caption_segment(&mut project, 0, 1.0, &[10.0])); + let ids: Vec = project + .captions + .as_ref() + .unwrap() + .segments + .iter() + .map(|segment| segment.id.clone()) + .collect(); + assert_eq!(ids.len(), 2); + assert!(ids.iter().all(|id| id.starts_with("caption-")), "{ids:?}"); + + crate::transcription::apply_caption_result(&mut project, Vec::new(), "en", &[10.0], 10.0); + assert_eq!(project.captions.as_ref().unwrap().segments.len(), 2); + } + #[test] fn caption_split_delete_survives_a_repeated_trimmed_edl() { let mut project = ProjectConfiguration { @@ -16709,3 +16813,117 @@ mod tests { assert!(!bytes.contains(&99)); } } + +#[cfg(all(test, target_os = "linux"))] +mod add_caption_ui_tests { + use gpui::{AppContext, Context, Entity, IntoElement, Render, Window, div}; + + use crate::editor_window::EditorWindow; + + struct View { + editor: Entity, + _project: tempfile::TempDir, + } + + impl Render for View { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + div() + } + } + + fn fixture( + cx: &mut gpui::TestAppContext, + captions: serde_json::Value, + ) -> (Entity, &mut gpui::VisualTestContext) { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().to_path_buf(); + let (view, cx) = cx.add_window_view(move |window, cx| { + let editor = cx.new(|cx| EditorWindow::new(path, window, cx)); + editor.update(cx, |editor, _| { + editor.project = serde_json::from_value(serde_json::json!({ + "timeline": { + "segments": [{ "start": 0.0, "end": 10.0, "timescale": 1.0 }], + "zoomSegments": [] + }, + "captions": captions + })) + .unwrap(); + editor.clip_display_durations = vec![10.0]; + editor.total = 10.0; + cap_project::synchronize_captions(&mut editor.project, &[10.0]); + }); + View { + editor, + _project: directory, + } + }); + (view.read_with(cx, |view, _| view.editor.clone()), cx) + } + + fn texts(editor: &mut EditorWindow) -> Vec<(String, f64, f64)> { + cap_project::synchronize_captions(&mut editor.project, &[10.0]); + editor + .project + .timeline + .as_ref() + .unwrap() + .caption_segments + .iter() + .map(|segment| (segment.text.clone(), segment.start, segment.end)) + .collect() + } + + #[gpui::test] + fn the_playhead_caption_takes_the_next_free_gap(cx: &mut gpui::TestAppContext) { + let (editor, cx) = fixture( + cx, + serde_json::json!({ + "segments": [{ "id": "segment-0", "start": 2.0, "end": 4.0, "text": "spoken", + "words": [{ "text": "spoken", "start": 2.0, "end": 4.0 }] }], + "settings": { "enabled": false }, + "sourceTimed": true + }), + ); + editor.update_in(cx, |editor, window, cx| { + assert!(editor.add_caption_at( + 2.5, + crate::editor_edits::NEW_CAPTION_LENGTH, + false, + window, + cx + )); + assert!(editor.tracks.caption); + assert!(editor.project.captions.as_ref().unwrap().settings.enabled); + assert_eq!( + texts(editor), + [ + ("spoken".to_string(), 2.0, 4.0), + ("New caption".to_string(), 4.0, 6.0) + ] + ); + assert!(editor.add_caption_at(9.5, 1.0, true, window, cx)); + assert_eq!(texts(editor)[2], ("New caption".to_string(), 9.0, 10.0)); + let doc = + crate::transcript::model::flatten_words(editor.project.captions.as_ref().unwrap()); + assert_eq!(doc.len(), 1); + }); + } + + #[gpui::test] + fn a_full_caption_track_refuses_with_a_notice(cx: &mut gpui::TestAppContext) { + let (editor, cx) = fixture( + cx, + serde_json::json!({ + "segments": [{ "id": "caption-full", "start": 0.0, "end": 10.0, "text": "all" }], + "settings": { "enabled": true }, + "sourceTimed": true + }), + ); + editor.update_in(cx, |editor, window, cx| { + let before = serde_json::to_value(&editor.project).unwrap(); + assert!(!editor.add_caption_at(5.0, 2.0, false, window, cx)); + assert_eq!(serde_json::to_value(&editor.project).unwrap(), before); + assert!(editor.notice.is_some()); + }); + } +} 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 05e50b7b5a8..ede5ba53d46 100644 --- a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs @@ -572,6 +572,7 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { "Smallest download", ), (tr::editor::tabs::captions::model_label(), "Model"), + (tr::editor::tabs::captions::new_caption(), "New caption"), ( tr::editor::tabs::captions::model_updated(), "Recommended model updated", @@ -1068,6 +1069,10 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { "Open recording bundle", ), (tr::editor::window::header::presets(), "Presets"), + ( + tr::editor::transcript::add_caption(), + "Add caption at playhead", + ), (tr::editor::transcript::cleanup(), "Clean up"), ( tr::editor::transcript::cleanup_tooltip(), @@ -1179,6 +1184,10 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { tr::editor::window::no_room_for_3d_shot(), "No room for another 3D shot", ), + ( + tr::editor::window::no_room_for_caption(), + "No room for another caption here", + ), ( tr::editor::window::open::damaged(), "This recording's video tracks could not be opened. The bundle looks damaged.", diff --git a/apps/desktop-gpui/src/transcript/view.rs b/apps/desktop-gpui/src/transcript/view.rs index 0d2009a2317..9cc9eac0e25 100644 --- a/apps/desktop-gpui/src/transcript/view.rs +++ b/apps/desktop-gpui/src/transcript/view.rs @@ -1578,6 +1578,22 @@ impl EditorWindow { .flex_row() .items_center() .gap(px(2.)) + .child( + ui::EditorButton::plain(&theme, "transcript-add-caption") + .left_icon("icons/plus.svg") + .tooltip(&theme, tr::editor::transcript::add_caption()) + .disabled(!self.project_ready()) + .on_click(cx.listener(|this, _, window, cx| { + let playhead = this.playhead_time(); + this.add_caption_at( + playhead, + crate::editor_edits::NEW_CAPTION_LENGTH, + false, + window, + cx, + ); + })), + ) .child( ui::EditorButton::plain(&theme, "transcript-search") .left_icon("icons/search.svg") diff --git a/apps/desktop-gpui/src/transcription.rs b/apps/desktop-gpui/src/transcription.rs index 7c88c907293..063c07c49ba 100644 --- a/apps/desktop-gpui/src/transcription.rs +++ b/apps/desktop-gpui/src/transcription.rs @@ -2577,14 +2577,24 @@ pub fn write_caption_edit_to_source( source.end = end as f32; } source.text = track_text; - source.words = sync_caption_words_with_text( - &source.text, - &source.words, - f64::from(source.start), - f64::from(source.end), - ); + if !is_manual_caption(source) { + source.words = sync_caption_words_with_text( + &source.text, + &source.words, + f64::from(source.start), + f64::from(source.end), + ); + } +} + +/// A caption typed in by hand rather than transcribed: no word timings, and +/// the `caption-` id both this app and the 0.6.0 editor give one. +pub fn is_manual_caption(segment: &CaptionSegment) -> bool { + segment.words.is_empty() && segment.id.starts_with(MANUAL_CAPTION_PREFIX) } +pub const MANUAL_CAPTION_PREFIX: &str = "caption-"; + /// `applyCaptionResultToProject` (`captions.ts:469-527`): set the captions /// block (enabled, preserving existing style settings), mark it source-timed, /// and re-derive the rendered track through the current edit list. @@ -2639,6 +2649,22 @@ pub fn apply_caption_result( .as_mut() .expect("timeline was just created"); + let mut segments = segments; + if let Some(previous) = project + .captions + .as_ref() + .filter(|captions| captions.source_timed) + { + segments.extend( + previous + .segments + .iter() + .filter(|segment| is_manual_caption(segment)) + .cloned(), + ); + segments.sort_by(|a, b| a.start.total_cmp(&b.start)); + } + timeline.caption_segments = derive_caption_track_segments(&segments, timeline, recording_durations); @@ -3294,6 +3320,130 @@ mod tests { // -- applyCaptionResultToProject ------------------------------------------ + #[test] + fn a_new_transcript_keeps_captions_typed_in_by_hand() { + let mut project = ProjectConfiguration { + timeline: Some(timeline(serde_json::json!({ + "segments": [{ "timescale": 1.0, "start": 0.0, "end": 10.0 }], + "zoomSegments": [] + }))), + captions: Some(CaptionsData { + segments: vec![ + caption("segment-0", 1.0, 2.0, &[("old", 1.0, 2.0)]), + caption("caption-1-a", 5.0, 7.0, &[]), + caption("segment-legacy", 8.0, 9.0, &[]), + ], + source_timed: true, + ..Default::default() + }), + ..Default::default() + }; + project.captions.as_mut().unwrap().segments[1].text = "Chapter two".into(); + project.captions.as_mut().unwrap().segments[2].text = "legacy".into(); + + apply_caption_result( + &mut project, + vec![ + caption("segment-0", 0.5, 1.5, &[("hello", 0.5, 1.5)]), + caption("segment-1", 6.0, 6.5, &[("world", 6.0, 6.5)]), + ], + "en", + &[10.0], + 10.0, + ); + + let ids: Vec<&str> = project + .captions + .as_ref() + .unwrap() + .segments + .iter() + .map(|segment| segment.id.as_str()) + .collect(); + assert_eq!(ids, ["segment-0", "caption-1-a", "segment-1"]); + let track: Vec<&str> = project + .timeline + .as_ref() + .unwrap() + .caption_segments + .iter() + .map(|segment| segment.text.as_str()) + .collect(); + assert_eq!(track, ["hello", "Chapter two", "world"]); + } + + #[test] + fn editing_a_caption_typed_in_by_hand_keeps_it_out_of_the_transcript() { + let mut project = ProjectConfiguration { + timeline: Some(timeline(serde_json::json!({ + "segments": [{ "timescale": 1.0, "start": 0.0, "end": 10.0 }], + "zoomSegments": [] + }))), + captions: Some(CaptionsData { + segments: vec![caption("caption-1-a", 5.0, 7.0, &[])], + source_timed: true, + ..Default::default() + }), + ..Default::default() + }; + let durations = [10.0]; + cap_project::synchronize_captions(&mut project, &durations); + let track = &mut project.timeline.as_mut().unwrap().caption_segments[0]; + track.text = "Step one: open settings".into(); + track.end = 8.0; + + write_caption_edit_to_source(&mut project, 0, &durations); + + let source = &project.captions.as_ref().unwrap().segments[0]; + assert_eq!(source.text, "Step one: open settings"); + assert_eq!((source.start, source.end), (5.0, 8.0)); + assert!(source.words.is_empty()); + assert!( + crate::transcript::model::flatten_words(project.captions.as_ref().unwrap()).is_empty() + ); + } + + #[test] + fn retyping_a_cleared_transcribed_caption_brings_its_words_back() { + let mut project = ProjectConfiguration { + timeline: Some(timeline(serde_json::json!({ + "segments": [{ "timescale": 1.0, "start": 0.0, "end": 10.0 }], + "zoomSegments": [] + }))), + captions: Some(CaptionsData { + segments: vec![caption( + "segment-0", + 1.0, + 3.0, + &[("hello", 1.0, 2.0), ("world", 2.0, 3.0)], + )], + source_timed: true, + ..Default::default() + }), + ..Default::default() + }; + let durations = [10.0]; + cap_project::synchronize_captions(&mut project, &durations); + + project.timeline.as_mut().unwrap().caption_segments[0].text = String::new(); + write_caption_edit_to_source(&mut project, 0, &durations); + assert!( + project.captions.as_ref().unwrap().segments[0] + .words + .is_empty() + ); + + project.timeline.as_mut().unwrap().caption_segments[0].text = "fresh take".into(); + write_caption_edit_to_source(&mut project, 0, &durations); + + let words: Vec = + crate::transcript::model::flatten_words(project.captions.as_ref().unwrap()) + .into_iter() + .map(|word| word.text) + .collect(); + assert_eq!(words, ["fresh", "take"]); + } + #[test] fn apply_caption_result_enables_captions_and_derives_the_track() { let mut project: ProjectConfiguration = serde_json::from_value(serde_json::json!({