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
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);
}