framewatch 0.6.0

Event-driven, change-triggered window capture that emits timestamped screenshots + metadata for AI agents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Voice-narration transcript types, SubRip (`.srt`) formatting, and the
//! [`Transcriber`] that turns a recorded `audio.wav` into a [`Transcript`].
//!
//! These types are the timestamped-text half of a `record` package. Each
//! [`TranscriptSegment`] carries `start_ms`/`end_ms` measured from the start of
//! the recording — the same clock as the video — so a consumer can map a spoken
//! instruction to the exact moment it refers to in `recording.mp4`.
//!
//! With the `record` feature, the CLI can provision a pinned whisper.cpp runtime
//! and model into the user cache. Callers may also shell out to their own local
//! transcriber via `--transcribe-cmd` (see [`Transcriber`]). Everything here —
//! the types and SRT/JSON formatting/parsing — is pure and cross-platform,
//! exercised on every CI target.

use crate::error::TranscribeError;
use crate::util::tokenize;
use serde::{Deserialize, Serialize};
use std::path::Path;
#[cfg(feature = "record")]
use std::path::PathBuf;

/// One spoken span. `start_ms`/`end_ms` are milliseconds from the start of the
/// recording (the same clock as the video), so a consumer can seek the video to
/// the moment an instruction was spoken.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TranscriptSegment {
    /// Start of the span, in ms from recording start.
    pub start_ms: u64,
    /// End of the span, in ms from recording start.
    pub end_ms: u64,
    /// The spoken text.
    pub text: String,
}

/// A full voice-narration transcript, serialized to `transcript.json`.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Transcript {
    /// Detected/forced language code (e.g. `"en"`), if known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// Total duration covered, in ms (the maximum segment `end_ms`).
    pub duration_ms: u64,
    /// Segments, in chronological order.
    pub segments: Vec<TranscriptSegment>,
}

impl Transcript {
    /// Whether the transcript has no segments.
    pub fn is_empty(&self) -> bool {
        self.segments.is_empty()
    }

    /// Render the transcript as SubRip (`.srt`) subtitles.
    pub fn to_srt(&self) -> String {
        let mut out = String::new();
        for (i, seg) in self.segments.iter().enumerate() {
            out.push_str(&(i + 1).to_string());
            out.push('\n');
            out.push_str(&format_srt_timestamp(seg.start_ms));
            out.push_str(" --> ");
            out.push_str(&format_srt_timestamp(seg.end_ms));
            out.push('\n');
            out.push_str(&seg.text);
            out.push('\n');
            out.push('\n');
        }
        out
    }

    /// Parse SubRip (`.srt`) text into a transcript — the inverse of
    /// [`to_srt`](Transcript::to_srt). Multi-line cue text is joined with
    /// spaces; `language` is left `None`.
    pub fn from_srt(srt: &str) -> Result<Self, TranscribeError> {
        let normalized = srt.replace("\r\n", "\n").replace('\r', "\n");
        let mut segments = Vec::new();
        for block in normalized.split("\n\n") {
            let mut lines = block.lines().filter(|l| !l.trim().is_empty());
            // The first line is an optional numeric index; the timing line is the
            // one containing "-->".
            let first = match lines.next() {
                Some(l) => l,
                None => continue,
            };
            let timing = if first.contains("-->") {
                first
            } else {
                match lines.next() {
                    Some(l) => l,
                    None => continue,
                }
            };
            let (start_ms, end_ms) = parse_srt_timing(timing).ok_or_else(|| {
                TranscribeError::Parse(format!("bad SRT timing line: {timing:?}"))
            })?;
            let text = lines.collect::<Vec<_>>().join(" ").trim().to_string();
            if text.is_empty() {
                continue;
            }
            segments.push(TranscriptSegment {
                start_ms,
                end_ms,
                text,
            });
        }
        let duration_ms = segments.iter().map(|s| s.end_ms).max().unwrap_or(0);
        Ok(Self {
            language: None,
            duration_ms,
            segments,
        })
    }
}

/// Format milliseconds as an SRT timestamp `HH:MM:SS,mmm` (comma separator, per
/// the SubRip spec).
pub fn format_srt_timestamp(ms: u64) -> String {
    let h = ms / 3_600_000;
    let m = (ms % 3_600_000) / 60_000;
    let s = (ms % 60_000) / 1_000;
    let milli = ms % 1_000;
    format!("{h:02}:{m:02}:{s:02},{milli:03}")
}

fn parse_srt_timing(line: &str) -> Option<(u64, u64)> {
    let (a, b) = line.split_once("-->")?;
    Some((
        parse_srt_timestamp(a.trim())?,
        parse_srt_timestamp(b.trim())?,
    ))
}

fn parse_srt_timestamp(s: &str) -> Option<u64> {
    // HH:MM:SS,mmm — also tolerate '.' as the millisecond separator.
    let (hms, millis) = s.trim().split_once([',', '.'])?;
    let mut parts = hms.split(':');
    let h: u64 = parts.next()?.trim().parse().ok()?;
    let m: u64 = parts.next()?.trim().parse().ok()?;
    let sec: u64 = parts.next()?.trim().parse().ok()?;
    if parts.next().is_some() {
        return None;
    }
    let ms: u64 = millis.trim().parse().ok()?;
    Some(((h * 60 + m) * 60 + sec) * 1000 + ms)
}

/// How a recording's voice narration is turned into a [`Transcript`].
///
/// The CLI defaults to a managed, pinned whisper.cpp installation when built
/// with `record`. [`Command`](Transcriber::Command) remains available for any
/// compatible external transcriber.
#[derive(Debug, Clone)]
pub enum Transcriber {
    /// Use the managed whisper.cpp runtime and model provisioned by Framewatch.
    #[cfg(feature = "record")]
    ManagedWhisper {
        /// Absolute path to `whisper-cli` / `whisper-cli.exe`.
        executable: PathBuf,
        /// Absolute path to the GGML Whisper model.
        model: PathBuf,
    },
    /// Shell out to an external transcriber, run over `audio.wav`.
    ///
    /// The template is tokenized (quotes group args); `{audio}` is replaced with
    /// the WAV path and `{output}` with a framewatch-chosen output base path. If
    /// neither placeholder appears, the audio path is appended as the final arg
    /// and framewatch reads the command's **stdout**. The command must emit a
    /// framewatch [`Transcript`] JSON (`{ "segments": [...] }`) or SubRip (SRT).
    Command {
        /// The command template.
        template: String,
    },
    /// Produce an empty transcript (`--no-transcribe`, or no audio).
    Disabled,
}

impl Transcriber {
    /// A `(engine, model)` label pair recorded in the package manifest.
    pub fn engine_meta(&self) -> (&'static str, Option<String>) {
        match self {
            #[cfg(feature = "record")]
            Transcriber::ManagedWhisper { .. } => {
                ("whisper.cpp", Some(crate::whisper::WHISPER_MODEL.into()))
            }
            Transcriber::Command { template } => ("command", Some(template.clone())),
            Transcriber::Disabled => ("none", None),
        }
    }

    /// Transcribe `audio_wav`, using `work_dir` for any scratch output.
    pub fn transcribe(
        &self,
        audio_wav: &Path,
        work_dir: &Path,
    ) -> Result<Transcript, TranscribeError> {
        match self {
            #[cfg(feature = "record")]
            Transcriber::ManagedWhisper { executable, model } => {
                transcribe_managed_whisper(executable, model, audio_wav, work_dir)
            }
            Transcriber::Disabled => Ok(Transcript::default()),
            Transcriber::Command { template } => transcribe_command(template, audio_wav, work_dir),
        }
    }
}

#[cfg(feature = "record")]
fn transcribe_managed_whisper(
    executable: &Path,
    model: &Path,
    audio_wav: &Path,
    work_dir: &Path,
) -> Result<Transcript, TranscribeError> {
    let template = format!(
        "\"{}\" -m \"{}\" -f \"{{audio}}\" -l en -osrt -of \"{{output}}\" -np",
        executable.display(),
        model.display()
    );
    transcribe_command(&template, audio_wav, work_dir)
}

/// Substitute placeholders into a `--transcribe-cmd` template, run it, and parse
/// the resulting transcript (from the `{output}` file if written, else stdout).
fn transcribe_command(
    template: &str,
    audio_wav: &Path,
    work_dir: &Path,
) -> Result<Transcript, TranscribeError> {
    let audio = audio_wav.to_string_lossy().into_owned();
    // Output *base* path (no extension) so tools that append `.srt`/`.json`
    // (e.g. whisper-cli `-of`) land somewhere we can find.
    let output_base = work_dir.join("transcript_raw");
    let output_json = output_base.with_extension("json");
    let output_srt = output_base.with_extension("srt");
    let output_base_str = output_base.to_string_lossy().into_owned();

    // Catch malformed templates early: `tokenize` silently swallows a dangling
    // quote, which would otherwise fail later with a less actionable error.
    if template.chars().filter(|&c| c == '"').count() % 2 != 0 {
        return Err(TranscribeError::Parse(
            "unbalanced quotes in --transcribe-cmd".into(),
        ));
    }
    let tokens = tokenize(template);
    if tokens.is_empty() {
        return Err(TranscribeError::Parse("empty --transcribe-cmd".into()));
    }

    let mut used_audio = false;
    let mut used_output = false;
    let mut argv: Vec<String> = tokens
        .into_iter()
        .map(|t| {
            let mut s = t;
            if s.contains("{audio}") {
                s = s.replace("{audio}", &audio);
                used_audio = true;
            }
            if s.contains("{output}") {
                s = s.replace("{output}", &output_base_str);
                used_output = true;
            }
            s
        })
        .collect();
    if !used_audio {
        argv.push(audio);
    }

    // Clear any leftover {output} files first, so a transcriber that writes
    // nothing this run can't make us read a stale transcript from a reused dir.
    if used_output {
        for p in [&output_base, &output_json, &output_srt] {
            let _ = std::fs::remove_file(p);
        }
    }

    let program = argv.remove(0);
    let out = std::process::Command::new(&program).args(&argv).output()?;
    if !out.status.success() {
        let code = out.status.code().unwrap_or(-1);
        let stderr = String::from_utf8_lossy(&out.stderr).trim().to_string();
        return Err(TranscribeError::CommandFailed(code, stderr));
    }

    // Prefer a written {output} file (extension tells us the format); else stdout.
    let mut parsed = None;
    if used_output {
        for (cand, fmt) in [(&output_json, Blob::Json), (&output_srt, Blob::Srt)] {
            if let Ok(raw) = std::fs::read_to_string(cand) {
                if !raw.trim().is_empty() {
                    parsed = Some(parse_transcript_text(&raw, Some(fmt)));
                    break;
                }
            }
        }
        if parsed.is_none() {
            if let Ok(raw) = std::fs::read_to_string(&output_base) {
                if !raw.trim().is_empty() {
                    parsed = Some(parse_transcript_text(&raw, None));
                }
            }
        }
    }

    let result = parsed.unwrap_or_else(|| {
        let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
        if stdout.trim().is_empty() {
            Err(TranscribeError::Parse(
                "transcriber produced no output (wrote no {output} file and empty stdout)".into(),
            ))
        } else {
            parse_transcript_text(&stdout, None)
        }
    });

    // `{output}` is scratch space, not part of the public recording package.
    // Keep it on failure for diagnostics, but remove it after a successful parse.
    if result.is_ok() && used_output {
        for path in [&output_base, &output_json, &output_srt] {
            let _ = std::fs::remove_file(path);
        }
    }
    result
}

/// Which transcript wire format a blob is.
enum Blob {
    Json,
    Srt,
}

/// Parse a transcript blob as JSON or SRT. With no `format` hint, JSON is
/// detected by a leading `{`, otherwise the blob is parsed as SRT.
fn parse_transcript_text(raw: &str, format: Option<Blob>) -> Result<Transcript, TranscribeError> {
    let is_json = match format {
        Some(Blob::Json) => true,
        Some(Blob::Srt) => false,
        None => raw.trim_start().starts_with('{'),
    };
    if is_json {
        Ok(serde_json::from_str(raw)?)
    } else {
        Transcript::from_srt(raw)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn srt_timestamp_formatting() {
        assert_eq!(format_srt_timestamp(0), "00:00:00,000");
        assert_eq!(format_srt_timestamp(1_250), "00:00:01,250");
        assert_eq!(format_srt_timestamp(61_000), "00:01:01,000");
        assert_eq!(format_srt_timestamp(3_661_001), "01:01:01,001");
    }

    #[test]
    fn json_roundtrip() {
        let t = Transcript {
            language: Some("en".into()),
            duration_ms: 4800,
            segments: vec![TranscriptSegment {
                start_ms: 1250,
                end_ms: 4800,
                text: "open the settings panel".into(),
            }],
        };
        let json = serde_json::to_string(&t).unwrap();
        let back: Transcript = serde_json::from_str(&json).unwrap();
        assert_eq!(t, back);
    }

    #[test]
    fn srt_roundtrip() {
        let t = Transcript {
            language: None,
            duration_ms: 8200,
            segments: vec![
                TranscriptSegment {
                    start_ms: 1250,
                    end_ms: 4800,
                    text: "first instruction".into(),
                },
                TranscriptSegment {
                    start_ms: 5000,
                    end_ms: 8200,
                    text: "second instruction".into(),
                },
            ],
        };
        let srt = t.to_srt();
        assert!(srt.starts_with("1\n00:00:01,250 --> 00:00:04,800\nfirst instruction\n\n"));
        let back = Transcript::from_srt(&srt).unwrap();
        assert_eq!(back.segments, t.segments);
        assert_eq!(back.duration_ms, t.duration_ms);
    }

    #[test]
    fn parse_detects_json_vs_srt() {
        let json = r#"{"duration_ms":10,"segments":[{"start_ms":0,"end_ms":10,"text":"x"}]}"#;
        assert_eq!(parse_transcript_text(json, None).unwrap().segments.len(), 1);
        let srt = "1\n00:00:00,000 --> 00:00:00,010\nx\n";
        assert_eq!(parse_transcript_text(srt, None).unwrap().segments.len(), 1);
    }

    #[test]
    fn disabled_is_empty() {
        let t = Transcriber::Disabled
            .transcribe(Path::new("nonexistent.wav"), Path::new("."))
            .unwrap();
        assert!(t.is_empty());
        assert_eq!(Transcriber::Disabled.engine_meta().0, "none");
    }

    #[test]
    fn command_rejects_unbalanced_quotes() {
        let t = Transcriber::Command {
            template: "prog \"unterminated".into(),
        };
        let err = t
            .transcribe(Path::new("audio.wav"), Path::new("."))
            .unwrap_err();
        assert!(matches!(err, TranscribeError::Parse(_)));
    }

    #[test]
    fn command_rejects_empty_template() {
        let err = Transcriber::Command {
            template: "   ".into(),
        }
        .transcribe(Path::new("a.wav"), Path::new("."))
        .unwrap_err();
        assert!(matches!(err, TranscribeError::Parse(_)));
    }

    #[test]
    fn from_srt_handles_index_crlf_dot_separator_and_multiline() {
        let srt = "1\r\n00:00:01,000 --> 00:00:02,000\r\nhello\r\nworld\r\n\r\n\
                   2\r\n00:00:03.500 --> 00:00:04,000\r\nbye\r\n";
        let t = Transcript::from_srt(srt).unwrap();
        assert_eq!(t.segments.len(), 2);
        assert_eq!(t.segments[0].text, "hello world"); // multi-line cue joined
        assert_eq!(t.segments[0].start_ms, 1000);
        assert_eq!(t.segments[1].start_ms, 3500); // '.' separator tolerated
        assert_eq!(t.duration_ms, 4000);
    }

    #[test]
    fn from_srt_empty_and_bad_timing() {
        assert!(Transcript::from_srt("").unwrap().is_empty());
        assert!(Transcript::from_srt("1\nnot a timing line\ntext\n").is_err());
        // hours component present.
        let t = Transcript::from_srt("1\n01:02:03,004 --> 01:02:04,000\nx\n").unwrap();
        assert_eq!(t.segments[0].start_ms, 3_723_004);
    }

    #[test]
    fn engine_meta_labels() {
        assert_eq!(Transcriber::Disabled.engine_meta(), ("none", None));
        let (eng, model) = Transcriber::Command {
            template: "whisper-cli -f {audio}".into(),
        }
        .engine_meta();
        assert_eq!(eng, "command");
        assert_eq!(model.as_deref(), Some("whisper-cli -f {audio}"));
        #[cfg(feature = "record")]
        let (eng, model) = Transcriber::ManagedWhisper {
            executable: "whisper-cli".into(),
            model: "ggml-base.en.bin".into(),
        }
        .engine_meta();
        #[cfg(feature = "record")]
        {
            assert_eq!(eng, "whisper.cpp");
            assert_eq!(model.as_deref(), Some("base.en"));
        }
    }
}