vidcapture 0.2.0

macOS CLI to record screen and audio, cut videos precisely, and add timed text labels with ffmpeg.
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
use std::process::{Command, Output};

fn vidcapture(args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_vidcapture"))
        .args(args)
        .output()
        .expect("vidcapture should run")
}

fn create_test_video(path: &std::path::Path, duration_secs: u64) {
    let status = Command::new("ffmpeg")
        .args([
            "-y",
            "-f", "lavfi",
            "-i", &format!("color=c=red:s=320x240:d={}", duration_secs),
            "-c:v", "libx264",
            "-preset", "ultrafast",
            "-pix_fmt", "yuv420p",
            path.to_str().unwrap(),
        ])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .expect("ffmpeg should be available for test fixture creation");
    assert!(status.success(), "failed to create test video fixture");
}

#[test]
fn no_subcommand_shows_help() {
    let output = vidcapture(&[]);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(stderr.contains("Usage: vidcapture <COMMAND>"));
    assert!(stderr.contains("Commands:"));
    assert!(stderr.contains("start"));
    assert!(stderr.contains("help"));
}

#[test]
fn start_subcommand_is_recognized() {
    let output = vidcapture(&["start", "--help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(output.status.success());
    assert!(stdout.contains("Start capturing screen and audio"));
    assert!(stdout.contains("Usage: vidcapture start"));
}

#[test]
fn help_subcommand_describes_commands_and_flags() {
    let output = vidcapture(&["help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(output.status.success());
    assert!(stdout.contains("COMMANDS"));
    assert!(stdout.contains("start"));
    assert!(stdout.contains("-d, --duration"));
    assert!(stdout.contains("-e, --every"));
    assert!(stdout.contains("-o, --output"));
    assert!(stdout.contains("-v, --verbose"));
}

#[test]
fn invalid_subcommand_shows_error_and_help() {
    let output = vidcapture(&["invalid"]);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(!output.status.success());
    assert!(stderr.contains("unrecognized subcommand 'invalid'"));
    assert!(stderr.contains("Usage: vidcapture <COMMAND>"));
    assert!(stderr.contains("For more information, try '--help'."));
}

#[test]
fn cut_end_to_end_with_from_and_to() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_to");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 30);

    let output = dir.join("cut_out.mp4");
    let result = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
        "--from", "5s",
        "--to", "10s",
        "-o", output.to_str().unwrap(),
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        result.status.success(),
        "cut should succeed, stderr: {}",
        stderr
    );
    assert!(
        output.exists(),
        "output file should exist after successful cut"
    );
    let meta = std::fs::metadata(&output).unwrap();
    assert!(meta.len() > 0, "output file should not be empty");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn cut_end_to_end_with_length() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_length");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 30);

    let output = dir.join("cut_out.mp4");
    let result = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
        "--from", "10s",
        "--length", "1500ms",
        "-o", output.to_str().unwrap(),
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        result.status.success(),
        "cut with --length should succeed, stderr: {}",
        stderr
    );
    assert!(output.exists(), "output file should exist");
    let meta = std::fs::metadata(&output).unwrap();
    assert!(meta.len() > 0, "output file should not be empty");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn cut_range_past_end_warns_and_succeeds() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_past_end");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 5);

    let output = dir.join("cut_out.mp4");
    let result = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
        "--from", "3s",
        "--to", "20s",
        "-o", output.to_str().unwrap(),
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        result.status.success(),
        "cut past end should still succeed, stderr: {}",
        stderr
    );
    assert!(
        output.exists(),
        "output file should exist even when range extends past source"
    );
    assert!(
        stderr.contains("warning"),
        "should print a warning about short range, stderr: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn cut_nonexistent_source_errors() {
    let result = vidcapture(&[
        "cut",
        "/nonexistent/video.mp4",
        "--length", "5s",
    ]);
    assert!(!result.status.success());
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        stderr.contains("Source file not found"),
        "should report missing source, stderr: {}",
        stderr
    );
}

#[test]
fn cut_no_range_specified_errors() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_no_range");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 5);

    let result = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
    ]);
    assert!(!result.status.success());

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn cut_help_documents_flags() {
    let output = vidcapture(&["cut", "--help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(output.status.success());
    assert!(stdout.contains("--from"));
    assert!(stdout.contains("--to"));
    assert!(stdout.contains("--length"));
    assert!(stdout.contains("--fast"));
    assert!(stdout.contains("--output"));
}

#[test]
fn cut_success_reports_the_saved_cut_path() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_saved_msg");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 5);

    let output = dir.join("clip.mp4");
    let result = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
        "--length",
        "1s",
        "-o",
        output.to_str().unwrap(),
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        result.status.success(),
        "cut should succeed, stderr: {}",
        stderr
    );
    assert!(
        stderr.contains(&format!("Cut saved to {}", output.display())),
        "success message should name the cut, got: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// The short-range warning is about the cut, not about the log level: `-v`
/// must not swallow it.
#[test]
fn cut_range_past_end_warns_in_verbose_mode_too() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_past_end_verbose");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 5);

    let output = dir.join("cut_out.mp4");
    let result = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
        "--from",
        "3s",
        "--to",
        "20s",
        "-o",
        output.to_str().unwrap(),
        "-v",
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        result.status.success(),
        "cut past end should still succeed, stderr: {}",
        stderr
    );
    assert!(
        stderr.contains("warning"),
        "-v should not swallow the short-range warning, stderr: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// `-v` shows ffmpeg's own output; without it the run stays quiet apart from
/// vidcapture's own messages.
#[test]
fn cut_verbose_shows_ffmpeg_output_and_quiet_run_does_not() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_verbosity");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 5);

    let verbose = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
        "--length",
        "1s",
        "-o",
        dir.join("verbose.mp4").to_str().unwrap(),
        "-v",
    ]);
    let verbose_stderr = String::from_utf8_lossy(&verbose.stderr);
    assert!(
        verbose_stderr.contains("ffmpeg version"),
        "-v should show ffmpeg output, got: {}",
        verbose_stderr
    );

    let quiet = vidcapture(&[
        "cut",
        source.to_str().unwrap(),
        "--length",
        "1s",
        "-o",
        dir.join("quiet.mp4").to_str().unwrap(),
    ]);
    let quiet_stderr = String::from_utf8_lossy(&quiet.stderr);
    assert!(
        !quiet_stderr.contains("ffmpeg version"),
        "a quiet run should not show ffmpeg output, got: {}",
        quiet_stderr
    );
    assert_eq!(
        quiet_stderr.lines().count(),
        1,
        "a quiet run should print only the saved-cut line, got: {}",
        quiet_stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// An ffmpeg failure exits 1, leaves no partial file, and reports the error
/// once — `-v` already streamed ffmpeg's output, so the error message must not
/// repeat it.
#[test]
fn cut_failure_reports_the_ffmpeg_error_once_in_both_modes() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_failure");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    // A file ffmpeg cannot decode: passes vidcapture's own source checks and
    // fails inside ffmpeg.
    let source = dir.join("broken.mp4");
    std::fs::write(&source, b"this is not a video").unwrap();

    let count_errors = |out: &Output| {
        String::from_utf8_lossy(&out.stderr)
            .matches("Invalid data found")
            .count()
    };

    let output = dir.join("cut_out.mp4");
    let quiet = vidcapture(&[
        "cut", source.to_str().unwrap(),
        "--length", "1s",
        "-o", output.to_str().unwrap(),
    ]);
    assert!(!quiet.status.success(), "a broken source should fail");
    assert!(
        !output.exists(),
        "a failed cut should leave no partial file behind"
    );

    let verbose = vidcapture(&[
        "cut", source.to_str().unwrap(),
        "--length", "1s",
        "-o", output.to_str().unwrap(),
        "-v",
    ]);
    assert!(!verbose.status.success(), "a broken source should fail under -v too");
    assert_eq!(
        count_errors(&verbose),
        count_errors(&quiet),
        "-v should not report ffmpeg's error twice, stderr: {}",
        String::from_utf8_lossy(&verbose.stderr)
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// `cut` shells out to ffmpeg; when it is not on PATH, say so instead of
/// leaking a bare OS error.
#[test]
fn cut_without_ffmpeg_on_path_explains_the_missing_dependency() {
    let dir = std::env::temp_dir().join("vidcapture_cut_e2e_no_ffmpeg");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 5);

    let result = Command::new(env!("CARGO_BIN_EXE_vidcapture"))
        .args(["cut", source.to_str().unwrap(), "--length", "1s"])
        .env("PATH", dir.to_str().unwrap())
        .output()
        .expect("vidcapture should run");

    assert!(!result.status.success());
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        stderr.contains("ffmpeg not found"),
        "should name the missing dependency, got: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// Two labels on one pass: both are drawn, the source is left byte-for-byte
/// alone, and the labeled video lands where the success message says it did.
#[test]
fn label_end_to_end_with_two_labels() {
    let dir = std::env::temp_dir().join("vidcapture_label_e2e_two");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 10);
    let source_before = std::fs::read(&source).unwrap();

    let output = dir.join("labeled.mp4");
    let result = vidcapture(&[
        "label",
        source.to_str().unwrap(),
        "-l", "text=Intro,from=1s,to=4s,position=top,background=black@0.5",
        "-l", "text=Demo,from=4s,length=3s,color=#ffcc00,size=48",
        "-o", output.to_str().unwrap(),
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(result.status.success(), "label should succeed, stderr: {}", stderr);
    assert!(output.exists(), "labeled video should exist");
    assert!(std::fs::metadata(&output).unwrap().len() > 0);
    assert!(
        stderr.contains(&format!("Labeled video saved to {}", output.display())),
        "success message should name the labeled video, got: {}",
        stderr
    );
    assert_eq!(
        std::fs::read(&source).unwrap(),
        source_before,
        "a label pass must leave the source video untouched"
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// With no -o the labeled video lands beside the source as
/// `<source-stem>_labeled.mp4`.
#[test]
fn label_without_output_writes_beside_the_source() {
    let dir = std::env::temp_dir().join("vidcapture_label_e2e_default_path");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("talk.mp4");
    create_test_video(&source, 5);

    let result = vidcapture(&[
        "label",
        source.to_str().unwrap(),
        "-l", "text=Hello,to=2s",
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(result.status.success(), "label should succeed, stderr: {}", stderr);
    assert!(
        dir.join("talk_labeled.mp4").exists(),
        "should write talk_labeled.mp4 beside the source, stderr: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// A label window past the end of the source draws nothing, so say so instead
/// of handing back a video that silently looks unlabeled.
#[test]
fn label_window_past_end_of_source_warns_and_succeeds() {
    let dir = std::env::temp_dir().join("vidcapture_label_e2e_past_end");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 3);

    let output = dir.join("labeled.mp4");
    let result = vidcapture(&[
        "label",
        source.to_str().unwrap(),
        "-l", "text=Never seen,from=30s,to=40s",
        "-o", output.to_str().unwrap(),
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        result.status.success(),
        "a label past the end should still succeed, stderr: {}",
        stderr
    );
    assert!(output.exists(), "labeled video should still be written");
    assert!(
        stderr.contains("warning") && stderr.contains("never appears"),
        "should warn that the label never appears, stderr: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// A label whose window fits inside the source is not warned about.
#[test]
fn label_within_the_source_does_not_warn() {
    let dir = std::env::temp_dir().join("vidcapture_label_e2e_inside");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 5);

    let output = dir.join("labeled.mp4");
    let result = vidcapture(&[
        "label",
        source.to_str().unwrap(),
        "-l", "text=Hello,from=1s,to=3s",
        "-o", output.to_str().unwrap(),
    ]);

    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(result.status.success(), "label should succeed, stderr: {}", stderr);
    assert!(
        !stderr.contains("warning"),
        "a label inside the source should not warn, stderr: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn label_nonexistent_source_errors() {
    let result = vidcapture(&[
        "label",
        "/nonexistent/video.mp4",
        "-l", "text=Hello,to=5s",
    ]);
    assert!(!result.status.success());
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        stderr.contains("Source file not found"),
        "should report missing source, stderr: {}",
        stderr
    );
}

#[test]
fn label_without_a_label_spec_errors() {
    let dir = std::env::temp_dir().join("vidcapture_label_e2e_no_spec");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 3);

    let result = vidcapture(&["label", source.to_str().unwrap()]);
    assert!(!result.status.success());
    let stderr = String::from_utf8_lossy(&result.stderr);
    assert!(
        stderr.contains("--label"),
        "should name the required flag, stderr: {}",
        stderr
    );

    let _ = std::fs::remove_dir_all(&dir);
}

/// An invalid spec is rejected before ffmpeg runs, so no file is written.
#[test]
fn label_invalid_spec_errors_without_writing_anything() {
    let dir = std::env::temp_dir().join("vidcapture_label_e2e_bad_spec");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let source = dir.join("source.mp4");
    create_test_video(&source, 3);

    let output = dir.join("labeled.mp4");
    let result = vidcapture(&[
        "label",
        source.to_str().unwrap(),
        "-l", "text=Hello,from=10s,to=5s",
        "-o", output.to_str().unwrap(),
    ]);

    assert!(!result.status.success());
    assert!(!output.exists(), "an invalid spec should write no file");

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn label_help_documents_flags() {
    let output = vidcapture(&["label", "--help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(output.status.success());
    assert!(stdout.contains("--label"));
    assert!(stdout.contains("--font"));
    assert!(stdout.contains("--output"));
    assert!(stdout.contains("--verbose"));
}

#[test]
fn help_subcommand_describes_the_label_command_and_spec() {
    let output = vidcapture(&["help"]);
    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(output.status.success());
    assert!(stdout.contains("-l, --label"));
    assert!(stdout.contains("LABEL SPEC"));
    assert!(stdout.contains("position=top|bottom"));
    assert!(stdout.contains("background=<COLOR>"));
}