vidcapture 0.1.0

CLI screen and audio recorder for macOS, with millisecond-precise cutting of existing recordings.
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use std::time::Duration;

#[derive(Parser, Debug)]
#[command(
    name = "vidcapture",
    about = "CLI screen and audio recorder for macOS",
    disable_help_subcommand = true
)]
pub struct Args {
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Subcommand, Debug)]
pub enum Command {
    /// Start capturing screen and audio
    Start(StartArgs),
    /// Cut a range out of an existing video file
    Cut(CutArgs),
    /// Show help with setup instructions
    Help,
}

#[derive(Parser, Debug, Clone)]
pub struct StartArgs {
    /// Capture duration (e.g., 10s, 1.5s, 1500ms, 1h30m, 00:01:30.500)
    #[arg(short, long, value_parser = parse_positive_timespec)]
    pub duration: Option<Duration>,

    /// Interval mode — split into segments of this duration (e.g., 10s, 2m)
    #[arg(short, long, value_parser = parse_positive_timespec)]
    pub every: Option<Duration>,

    /// Output directory (default: current directory)
    #[arg(short, long)]
    pub output: Option<PathBuf>,

    /// Show ffmpeg output (shortcut for RUST_LOG debug)
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Parser, Debug, Clone)]
pub struct CutArgs {
    /// Path to the source video file
    pub source: PathBuf,

    /// Start offset of the cut range (default: 0s)
    #[arg(short, long, value_parser = parse_timespec, default_value = "0s")]
    pub from: Duration,

    /// End offset of the cut range (conflicts with --length)
    #[arg(short, long, value_parser = parse_positive_timespec, conflicts_with = "length")]
    pub to: Option<Duration>,

    /// Cut length (conflicts with --to)
    #[arg(short, long, value_parser = parse_positive_timespec, conflicts_with = "to")]
    pub length: Option<Duration>,

    /// Output file or directory
    #[arg(short, long)]
    pub output: Option<PathBuf>,

    /// Stream-copy instead of re-encoding
    #[arg(long)]
    pub fast: bool,

    /// Show ffmpeg output
    #[arg(short, long)]
    pub verbose: bool,
}

/// Validated cut range: a start offset and a length.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CutRange {
    pub start: Duration,
    pub length: Duration,
}

impl CutArgs {
    /// Validate the cut arguments and return a normalized cut range.
    ///
    /// Returns an error if:
    /// - Neither `--to` nor `--length` is provided
    /// - `--from` >= `--to`
    /// - `--length` is zero
    pub fn validate_cut_range(&self) -> Result<CutRange, String> {
        let length = match (self.to, self.length) {
            (None, None) => {
                return Err(
                    "Neither --to nor --length specified. \
                     One of them is required."
                        .to_string(),
                );
            }
            (Some(to), None) => {
                if self.from >= to {
                    return Err(format!(
                        "--from ({}) must be less than --to ({})",
                        format_duration(self.from),
                        format_duration(to),
                    ));
                }
                to - self.from
            }
            (None, Some(len)) => {
                if len.is_zero() {
                    return Err("--length must be greater than zero".to_string());
                }
                len
            }
            // Both --to and --length: clap's conflicts_with prevents this.
            (Some(_), Some(_)) => unreachable!("clap conflicts_with should have rejected this"),
        };

        Ok(CutRange {
            start: self.from,
            length,
        })
    }
}

/// Format a duration for user-facing error messages.
fn format_duration(d: Duration) -> String {
    let secs = d.as_secs_f64();
    if secs.fract() == 0.0 && secs < 60.0 {
        format!("{}s", secs as u64)
    } else {
        format!("{:.3}s", secs)
    }
}

/// Parse a timespec into a `std::time::Duration`, at millisecond precision.
///
/// Two notations are accepted:
/// - Unit-suffixed: a sequence of `<number><unit>` pairs, units `ms`, `s`,
///   `m`, `h`, with an optional decimal point. E.g. `10s`, `1500ms`, `1.5s`,
///   `1h30m10s`.
/// - Timestamp: `HH:MM:SS[.mmm]` or `MM:SS[.mmm]`. E.g. `00:01:30.500`,
///   `01:30`, `1:02:03.250`.
///
/// `0s` parses successfully — callers that require a positive value should
/// use [`parse_positive_timespec`] instead.
pub fn parse_timespec(input: &str) -> Result<Duration, String> {
    if input.is_empty() {
        return Err("Timespec cannot be empty".to_string());
    }

    if input.contains(':') {
        parse_timestamp(input)
    } else {
        parse_unit_suffixed(input)
    }
}

/// Like [`parse_timespec`], but rejects a zero duration. Use this for flags
/// where a zero-length value is meaningless (`-d`, `-e`, `--length`).
pub fn parse_positive_timespec(input: &str) -> Result<Duration, String> {
    let duration = parse_timespec(input)?;
    if duration.is_zero() {
        return Err(format!(
            "Timespec must be greater than zero, got: '{}'",
            input
        ));
    }
    Ok(duration)
}

fn parse_unit_suffixed(input: &str) -> Result<Duration, String> {
    let chars: Vec<char> = input.chars().collect();
    let mut i = 0;
    let mut total_millis: f64 = 0.0;

    while i < chars.len() {
        let number_start = i;
        let mut seen_dot = false;
        while i < chars.len() && (chars[i].is_ascii_digit() || (chars[i] == '.' && !seen_dot)) {
            seen_dot = seen_dot || chars[i] == '.';
            i += 1;
        }
        if i == number_start {
            return Err(format!("Invalid timespec: '{}'", input));
        }

        let number_str: String = chars[number_start..i].iter().collect();
        let value: f64 = number_str
            .parse()
            .map_err(|_| format!("Invalid number in timespec: '{}'", number_str))?;

        if i + 1 < chars.len() && chars[i] == 'm' && chars[i + 1] == 's' {
            total_millis += value;
            i += 2;
        } else if i < chars.len() {
            let unit = chars[i];
            i += 1;
            match unit {
                's' => total_millis += value * 1_000.0,
                'm' => total_millis += value * 60_000.0,
                'h' => total_millis += value * 3_600_000.0,
                _ => return Err(format!("Unknown timespec unit: '{}'", unit)),
            }
        } else {
            return Err(format!(
                "Timespec must end with a unit (ms, s, m, or h), got: '{}'",
                input
            ));
        }
    }

    Ok(Duration::from_millis(total_millis.round() as u64))
}

fn parse_timestamp(input: &str) -> Result<Duration, String> {
    let parts: Vec<&str> = input.split(':').collect();
    let (hours, minutes, seconds_str) = match parts.as_slice() {
        [h, m, s] => (
            parse_timestamp_int(h, input)?,
            parse_timestamp_int(m, input)?,
            *s,
        ),
        [m, s] => (0, parse_timestamp_int(m, input)?, *s),
        _ => return Err(format!("Invalid timestamp: '{}'", input)),
    };

    if !is_valid_decimal(seconds_str) {
        return Err(format!("Invalid timestamp: '{}'", input));
    }
    let seconds: f64 = seconds_str
        .parse()
        .map_err(|_| format!("Invalid timestamp: '{}'", input))?;

    if minutes >= 60 {
        return Err(format!(
            "Invalid timestamp: '{}' (minutes must be < 60)",
            input
        ));
    }
    if seconds >= 60.0 {
        return Err(format!(
            "Invalid timestamp: '{}' (seconds must be < 60)",
            input
        ));
    }

    let millis = hours * 3_600_000 + minutes * 60_000 + (seconds * 1_000.0).round() as u64;
    Ok(Duration::from_millis(millis))
}

fn parse_timestamp_int(component: &str, original: &str) -> Result<u64, String> {
    if component.is_empty() || !component.chars().all(|c| c.is_ascii_digit()) {
        return Err(format!("Invalid timestamp: '{}'", original));
    }
    component
        .parse()
        .map_err(|_| format!("Invalid timestamp: '{}'", original))
}

fn is_valid_decimal(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }
    let mut seen_dot = false;
    for (idx, ch) in s.chars().enumerate() {
        if ch == '.' {
            if seen_dot || idx == 0 {
                return false;
            }
            seen_dot = true;
        } else if !ch.is_ascii_digit() {
            return false;
        }
    }
    !s.ends_with('.')
}

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

    #[test]
    fn parse_timespec_unit_suffixed_table() {
        let cases: &[(&str, u64)] = &[
            ("1500ms", 1_500),
            ("1.5s", 1_500),
            ("10s", 10_000),
            ("2m", 120_000),
            ("1h", 3_600_000),
            ("1h30m10s", 5_410_000),
            ("0.25m", 15_000),
            ("1h30m", 5_400_000),
            ("0s", 0),
        ];
        for (input, expected_millis) in cases {
            let duration = parse_timespec(input)
                .unwrap_or_else(|e| panic!("expected '{}' to parse, got error: {}", input, e));
            assert_eq!(
                duration,
                Duration::from_millis(*expected_millis),
                "input: '{}'",
                input
            );
        }
    }

    #[test]
    fn parse_timespec_timestamp_table() {
        let cases: &[(&str, u64)] = &[
            ("00:01:30.500", 90_500),
            ("01:30", 90_000),
            ("1:02:03.250", 3_723_250),
        ];
        for (input, expected_millis) in cases {
            let duration = parse_timespec(input)
                .unwrap_or_else(|e| panic!("expected '{}' to parse, got error: {}", input, e));
            assert_eq!(
                duration,
                Duration::from_millis(*expected_millis),
                "input: '{}'",
                input
            );
        }
    }

    #[test]
    fn parse_timespec_ms_not_confused_with_minutes() {
        // 1500ms must be 1.5 seconds, not 1500 minutes.
        let duration = parse_timespec("1500ms").unwrap();
        assert_eq!(duration, Duration::from_millis(1_500));
        assert_ne!(duration, Duration::from_secs(1500 * 60));
    }

    #[test]
    fn parse_timespec_rounds_sub_millisecond_fractions() {
        let duration = parse_timespec("1.0004s").unwrap();
        assert_eq!(duration, Duration::from_millis(1_000));

        let duration = parse_timespec("1.0006s").unwrap();
        assert_eq!(duration, Duration::from_millis(1_001));
    }

    #[test]
    fn parse_timespec_rejects_invalid_input() {
        let cases = ["", "10", "10x", "abcs"];
        for input in cases {
            assert!(
                parse_timespec(input).is_err(),
                "expected '{}' to be rejected",
                input
            );
        }
    }

    #[test]
    fn parse_timespec_zero_is_accepted() {
        assert_eq!(parse_timespec("0s").unwrap(), Duration::ZERO);
    }

    #[test]
    fn parse_positive_timespec_rejects_zero() {
        for input in ["0s", "0ms", "00:00:00"] {
            assert!(
                parse_positive_timespec(input).is_err(),
                "expected '{}' to be rejected as non-positive",
                input
            );
        }
    }

    #[test]
    fn parse_positive_timespec_accepts_nonzero() {
        assert_eq!(
            parse_positive_timespec("1.5s").unwrap(),
            Duration::from_millis(1_500)
        );
    }

    #[test]
    fn parse_start_with_duration() {
        let args = Args::try_parse_from(["vidcapture", "start", "-d", "10s"]).unwrap();
        match args.command {
            Command::Start(start_args) => {
                assert_eq!(start_args.duration, Some(Duration::from_secs(10)));
                assert_eq!(start_args.every, None);
                assert_eq!(start_args.output, None);
                assert!(!start_args.verbose);
            }
            _ => panic!("expected Start command"),
        }
    }

    #[test]
    fn parse_start_with_fractional_duration() {
        let args = Args::try_parse_from(["vidcapture", "start", "-d", "1.5s"]).unwrap();
        match args.command {
            Command::Start(start_args) => {
                assert_eq!(start_args.duration, Some(Duration::from_millis(1_500)));
            }
            _ => panic!("expected Start command"),
        }
    }

    #[test]
    fn parse_start_rejects_zero_duration() {
        let result = Args::try_parse_from(["vidcapture", "start", "-d", "0s"]);
        assert!(result.is_err(), "expected -d 0s to be rejected");
    }

    #[test]
    fn parse_start_rejects_zero_interval() {
        let result = Args::try_parse_from(["vidcapture", "start", "-e", "0s"]);
        assert!(result.is_err(), "expected -e 0s to be rejected");
    }

    #[test]
    fn parse_start_defaults() {
        let args = Args::try_parse_from(["vidcapture", "start"]).unwrap();
        match args.command {
            Command::Start(start_args) => {
                assert_eq!(start_args.duration, None);
                assert_eq!(start_args.every, None);
                assert_eq!(start_args.output, None);
                assert!(!start_args.verbose);
            }
            _ => panic!("expected Start command"),
        }
    }

    #[test]
    fn parse_start_with_interval() {
        let args = Args::try_parse_from(["vidcapture", "start", "-e", "30s"]).unwrap();
        match args.command {
            Command::Start(start_args) => {
                assert_eq!(start_args.duration, None);
                assert_eq!(start_args.every, Some(Duration::from_secs(30)));
            }
            _ => panic!("expected Start command"),
        }
    }

    #[test]
    fn parse_start_with_all_flags() {
        let args = Args::try_parse_from([
            "vidcapture",
            "start",
            "-d",
            "1m",
            "-e",
            "10s",
            "-o",
            "/tmp/output",
            "-v",
        ])
        .unwrap();
        match args.command {
            Command::Start(start_args) => {
                assert_eq!(start_args.duration, Some(Duration::from_secs(60)));
                assert_eq!(start_args.every, Some(Duration::from_secs(10)));
                assert_eq!(start_args.output, Some(PathBuf::from("/tmp/output")));
                assert!(start_args.verbose);
            }
            _ => panic!("expected Start command"),
        }
    }

    #[test]
    fn parse_help_subcommand() {
        let args = Args::try_parse_from(["vidcapture", "help"]).unwrap();
        assert!(matches!(args.command, Command::Help));
    }

    #[test]
    fn parse_no_subcommand_shows_help() {
        // clap's default behavior is to print help and exit when no subcommand is provided.
        let result = Args::try_parse_from(["vidcapture"]);
        assert!(result.is_err(), "expected error when no subcommand given");
    }

    // ---- cut subcommand tests (issue #17) ----

    #[test]
    fn parse_cut_with_from_and_to() {
        let args = Args::try_parse_from(["vidcapture", "cut", "talk.mp4", "--from", "10s", "--to", "25s"]).unwrap();
        match args.command {
            Command::Cut(cut_args) => {
                assert_eq!(cut_args.from, Duration::from_secs(10));
                assert_eq!(cut_args.to, Some(Duration::from_secs(25)));
                assert_eq!(cut_args.length, None);
            }
            _ => panic!("expected Cut command"),
        }
    }

    #[test]
    fn parse_cut_with_from_and_length() {
        let args = Args::try_parse_from(["vidcapture", "cut", "talk.mp4", "--from", "10s", "--length", "1500ms"]).unwrap();
        match args.command {
            Command::Cut(cut_args) => {
                assert_eq!(cut_args.from, Duration::from_secs(10));
                assert_eq!(cut_args.to, None);
                assert_eq!(cut_args.length, Some(Duration::from_millis(1500)));
            }
            _ => panic!("expected Cut command"),
        }
    }

    #[test]
    fn parse_cut_defaults_from_to_zero() {
        let args = Args::try_parse_from(["vidcapture", "cut", "talk.mp4", "--length", "5s"]).unwrap();
        match args.command {
            Command::Cut(cut_args) => {
                assert_eq!(cut_args.from, Duration::ZERO);
                assert_eq!(cut_args.length, Some(Duration::from_secs(5)));
            }
            _ => panic!("expected Cut command"),
        }
    }

    #[test]
    fn parse_cut_short_flags() {
        let args = Args::try_parse_from(["vidcapture", "cut", "talk.mp4", "-f", "10s", "-t", "25s"]).unwrap();
        match args.command {
            Command::Cut(cut_args) => {
                assert_eq!(cut_args.from, Duration::from_secs(10));
                assert_eq!(cut_args.to, Some(Duration::from_secs(25)));
            }
            _ => panic!("expected Cut command"),
        }
    }

    #[test]
    fn parse_cut_both_to_and_length_errors() {
        let result = Args::try_parse_from([
            "vidcapture", "cut", "talk.mp4", "--to", "25s", "--length", "5s",
        ]);
        assert!(result.is_err(), "expected --to and --length to conflict");
    }

    #[test]
    fn parse_cut_with_output_and_verbose() {
        let args = Args::try_parse_from([
            "vidcapture", "cut", "talk.mp4", "--length", "5s", "-o", "out.mp4", "-v", "--fast",
        ])
        .unwrap();
        match args.command {
            Command::Cut(cut_args) => {
                assert_eq!(cut_args.output, Some(PathBuf::from("out.mp4")));
                assert!(cut_args.verbose);
                assert!(cut_args.fast);
            }
            _ => panic!("expected Cut command"),
        }
    }

    #[test]
    fn cut_validate_neither_to_nor_length_errors() {
        let cut_args = CutArgs {
            source: PathBuf::from("talk.mp4"),
            from: Duration::ZERO,
            to: None,
            length: None,
            output: None,
            fast: false,
            verbose: false,
        };
        assert!(cut_args.validate_cut_range().is_err());
    }

    #[test]
    fn cut_validate_from_greater_than_to_errors() {
        let cut_args = CutArgs {
            source: PathBuf::from("talk.mp4"),
            from: Duration::from_secs(25),
            to: Some(Duration::from_secs(10)),
            length: None,
            output: None,
            fast: false,
            verbose: false,
        };
        assert!(cut_args.validate_cut_range().is_err());
    }

    #[test]
    fn cut_validate_from_equals_to_errors() {
        let cut_args = CutArgs {
            source: PathBuf::from("talk.mp4"),
            from: Duration::from_secs(10),
            to: Some(Duration::from_secs(10)),
            length: None,
            output: None,
            fast: false,
            verbose: false,
        };
        assert!(cut_args.validate_cut_range().is_err());
    }

    #[test]
    fn cut_validate_zero_length_errors() {
        let cut_args = CutArgs {
            source: PathBuf::from("talk.mp4"),
            from: Duration::ZERO,
            to: None,
            length: Some(Duration::ZERO),
            output: None,
            fast: false,
            verbose: false,
        };
        assert!(cut_args.validate_cut_range().is_err());
    }

    #[test]
    fn cut_validate_from_to_range() {
        let cut_args = CutArgs {
            source: PathBuf::from("talk.mp4"),
            from: Duration::from_secs(10),
            to: Some(Duration::from_secs(25)),
            length: None,
            output: None,
            fast: false,
            verbose: false,
        };
        let range = cut_args.validate_cut_range().unwrap();
        assert_eq!(range.start, Duration::from_secs(10));
        assert_eq!(range.length, Duration::from_secs(15));
    }

    #[test]
    fn cut_validate_from_length_range() {
        let cut_args = CutArgs {
            source: PathBuf::from("talk.mp4"),
            from: Duration::from_secs(10),
            to: None,
            length: Some(Duration::from_millis(1500)),
            output: None,
            fast: false,
            verbose: false,
        };
        let range = cut_args.validate_cut_range().unwrap();
        assert_eq!(range.start, Duration::from_secs(10));
        assert_eq!(range.length, Duration::from_millis(1500));
    }

    #[test]
    fn cut_validate_defaults_from_zero_with_length() {
        let cut_args = CutArgs {
            source: PathBuf::from("talk.mp4"),
            from: Duration::ZERO,
            to: None,
            length: Some(Duration::from_secs(5)),
            output: None,
            fast: false,
            verbose: false,
        };
        let range = cut_args.validate_cut_range().unwrap();
        assert_eq!(range.start, Duration::ZERO);
        assert_eq!(range.length, Duration::from_secs(5));
    }
}