Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
3106 lines (2612 loc) · 86.4 KB
/
Copy pathtest_cli.py
File metadata and controls
3106 lines (2612 loc) · 86.4 KB
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import json
import platform
import shlex
import shutil
import subprocess
import sys
from datetime import timedelta
from importlib.metadata import entry_points
from io import StringIO
from pathlib import Path
from typing import TextIO
import pytest
from flowmpeg import cli, diagnostics
from flowmpeg.artifacts import ArtifactSet, FrameSet, FrameWorkflow, SegmentWorkflow
from flowmpeg.batch import BatchItemResult, BatchResult
from flowmpeg.black import BlackInterval, BlackReport
from flowmpeg.catalog import COMMAND_CATALOG
from flowmpeg.comparison import MediaComparison, MediaSummary
from flowmpeg.crop_detection import CropCandidate, CropReport
from flowmpeg.demo import DemoMediaResult
from flowmpeg.errors import (
BinaryNotFoundError,
BinaryUnusableError,
CompilationError,
ExecutionError,
FlowmpegError,
JobCancelledError,
JobTimeoutError,
OutputExistsError,
ProbeError,
)
from flowmpeg.loudness import LoudnessMeasurement
from flowmpeg.plan import Plan
from flowmpeg.probe import (
AudioStreamInfo,
FormatInfo,
MediaInfo,
VideoStreamInfo,
)
from flowmpeg.progress import Progress
from flowmpeg.quality import (
PsnrScore,
QualityComponent,
QualityReport,
SsimScore,
VmafScore,
)
from flowmpeg.runner import RunResult
from flowmpeg.scenes import SceneChange, SceneReport
from flowmpeg.silence import SilenceInterval, SilenceReport
from flowmpeg.ui.config import UiAddress, UiLaunchOptions
from flowmpeg.workflows import LoudnessWorkflow, LoudnessWorkflowResult
def test_ui_command_starts_the_local_application(
monkeypatch: pytest.MonkeyPatch,
) -> None:
launches: list[UiLaunchOptions] = []
def record(options: UiLaunchOptions, output: TextIO) -> None:
del output
launches.append(options)
monkeypatch.setattr("flowmpeg.ui.launcher.serve_ui", record)
assert cli.main(["ui", "--no-browser", "--port", "8123"]) == 0
assert launches == [
UiLaunchOptions(
address=UiAddress(port=8123),
open_browser=False,
)
]
def test_ui_command_opens_the_browser_by_default(
monkeypatch: pytest.MonkeyPatch,
) -> None:
launches: list[UiLaunchOptions] = []
def record(options: UiLaunchOptions, output: TextIO) -> None:
del output
launches.append(options)
monkeypatch.setattr("flowmpeg.ui.launcher.serve_ui", record)
assert cli.main(["ui"]) == 0
assert launches == [UiLaunchOptions()]
def test_ui_help_does_not_mark_no_browser_as_default(
capsys: pytest.CaptureFixture[str],
) -> None:
with pytest.raises(SystemExit) as exit_info:
cli.main(["ui", "--help"])
assert exit_info.value.code == 0
assert "(default: True)" not in capsys.readouterr().out
def test_demo_media_command_reports_created_files(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
result = DemoMediaResult(
directory=tmp_path,
files=(tmp_path / "sample.mp4", tmp_path / "voice.wav"),
video_duration=2.0,
)
monkeypatch.setattr(
"flowmpeg.cli.generate_demo_media", lambda *args, **kwargs: result
)
assert cli.main(["demo-media", str(tmp_path)]) == 0
output = capsys.readouterr().out
assert "Created 2 demo files" in output
assert "sample.mp4" in output
assert "flowmpeg cut sample.mp4" in output
def test_demo_media_command_prints_json(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
result = DemoMediaResult(
directory=tmp_path,
files=(tmp_path / "sample.mp4",),
video_duration=2.0,
)
monkeypatch.setattr(
"flowmpeg.cli.generate_demo_media", lambda *args, **kwargs: result
)
assert cli.main(["demo", str(tmp_path), "--json"]) == 0
assert json.loads(capsys.readouterr().out) == result.as_dict()
@pytest.mark.parametrize("port", ["-1", "65536"])
def test_ui_command_rejects_invalid_ports(port: str) -> None:
with pytest.raises(SystemExit):
cli.main(["ui", "--port", port])
@pytest.mark.parametrize(
"arguments",
[
["transcode", "in.mov", "-o", "out.mp4"],
["transcode-webm", "in.mov", "-o", "out.webm"],
["transcode-hevc", "in.mov", "-o", "out.mp4"],
["transcode-av1", "in.mov", "-o", "out.webm"],
["trim", "in.mp4", "--start", "2", "--duration", "5", "-o", "out.mp4"],
["loop-video", "in.mp4", "--duration", "30", "-o", "out.mp4"],
["resize", "in.mp4", "--width", "640", "-o", "out.mp4"],
["remove-audio", "in.mp4", "-o", "out.mp4"],
["extract-audio", "in.mp4", "-o", "out.mp3"],
["replace-audio", "in.mp4", "music.wav", "-o", "out.mp4"],
["watermark", "in.mp4", "mark.png", "-o", "out.mp4"],
["add-music", "in.mp4", "music.mp3", "-o", "out.mp4"],
["join-matching", "one.mp4", "two.mp4", "-o", "out.mp4"],
["join-normalized", "one.mp4", "two.mp4", "-o", "out.mp4"],
["mix-audio", "one.wav", "two.wav", "-o", "out.wav"],
["grid", "one.mp4", "two.mp4", "-o", "out.mp4"],
["thumbnail", "in.mp4", "-o", "out.jpg"],
["make-gif", "in.mp4", "-o", "out.gif"],
["rotate", "in.mp4", "-o", "out.mp4"],
["crop", "in.mp4", "--width", "320", "--height", "180", "-o", "out.mp4"],
["change-speed", "in.mp4", "--factor", "1.5", "-o", "out.mp4"],
["normalize-loudness", "in.wav", "-o", "out.wav"],
["fit-canvas", "in.mp4", "-o", "out.mp4"],
["picture-in-picture", "main.mp4", "inset.mp4", "-o", "out.mp4"],
["waveform-image", "in.mp3", "-o", "out.png"],
["spectrum-image", "in.mp3", "-o", "out.png"],
["still-image-video", "cover.jpg", "in.mp3", "-o", "out.mp4"],
["contact-sheet", "in.mp4", "-o", "out.jpg"],
["duck-music", "in.mp4", "music.mp3", "-o", "out.mp4"],
["fade-edges", "in.mp4", "--duration", "8", "-o", "out.mp4"],
["blurred-background", "in.mp4", "-o", "out.mp4"],
["reverse-clip", "in.mp4", "--duration", "5", "-o", "out.mp4"],
["compress-video", "in.mov", "-o", "out.mp4"],
["shrink-video", "in.mov", "-o", "out.mp4"],
["reframe", "in.mp4", "-o", "out.mp4"],
["social-video", "in.mp4", "-o", "out.mp4"],
["set-frame-rate", "in.mp4", "-o", "out.mp4"],
["deinterlace", "in.mp4", "-o", "out.mp4"],
["flip-video", "in.mp4", "-o", "out.mp4"],
["adjust-colors", "in.mp4", "-o", "out.mp4"],
["sharpen", "in.mp4", "-o", "out.mp4"],
["freeze-end", "in.mp4", "-o", "out.mp4"],
[
"mute-section",
"in.mp4",
"--start",
"1",
"--end",
"2",
"-o",
"out.mp4",
],
[
"blur-region",
"in.mp4",
"--x",
"0",
"--y",
"0",
"--width",
"100",
"--height",
"100",
"-o",
"out.mp4",
],
["boomerang", "in.mp4", "--duration", "2", "-o", "out.mp4"],
["denoise-audio", "in.wav", "-o", "out.wav"],
["compress-audio", "in.wav", "-o", "out.wav"],
["podcast-voice", "in.wav", "-o", "out.wav"],
["trim-silence", "in.wav", "--duration", "120", "-o", "out.wav"],
["trim-audio", "in.wav", "--duration", "5", "-o", "out.wav"],
["mono-audio", "in.wav", "-o", "out.wav"],
["resample-audio", "in.wav", "--sample-rate", "48000", "-o", "out.wav"],
["volume-audio", "in.wav", "--gain-db", "4", "-o", "out.wav"],
["fade-audio", "in.wav", "--duration", "30", "-o", "out.wav"],
["delay-audio", "in.wav", "--seconds", "0.5", "-o", "out.wav"],
["speed-audio", "in.wav", "--factor", "1.5", "-o", "out.wav"],
["crossfade-audio", "one.wav", "two.wav", "-o", "out.wav"],
["join-audio", "one.wav", "two.wav", "-o", "out.wav"],
["extract-subtitles", "in.mkv", "-o", "out.srt"],
["add-subtitles", "in.mp4", "captions.srt", "-o", "out.mp4"],
["burn-subtitles", "in.mp4", "captions.srt", "-o", "out.mp4"],
["remove-subtitles", "in.mkv", "-o", "out.mp4"],
["image-sequence-video", "frame-%04d.png", "-o", "out.mp4"],
["podcast-audiogram", "in.wav", "cover.jpg", "-o", "out.mp4"],
["strip-metadata", "in.mkv", "-o", "out.mkv"],
["remux", "in.mp4", "-o", "out.mkv"],
["tag-media", "in.mp4", "--title", "Camera master", "-o", "out.mp4"],
["tag-audio", "in.m4a", "--title", "Episode 1", "-o", "out.m4a"],
],
)
def test_every_media_command_builds_a_dry_run(
arguments: list[str],
capsys: pytest.CaptureFixture[str],
) -> None:
code = cli.main([*arguments, "--dry-run"])
captured = capsys.readouterr()
assert code == 0
assert captured.out.startswith("ffmpeg ")
assert captured.err == ""
def test_audio_commands_accept_opus_output(
capsys: pytest.CaptureFixture[str],
) -> None:
assert (
cli.main(
[
"extract-audio",
"in.mp4",
"--codec",
"opus",
"--bitrate",
"96k",
"-o",
"out.opus",
"--dry-run",
]
)
== 0
)
output = capsys.readouterr().out
assert "-c:a libopus" in output
assert "-b:a 96k" in output
def test_shrink_command_builds_small_phone_export(
capsys: pytest.CaptureFixture[str],
) -> None:
assert (
cli.main(
[
"shrink",
"IMG_9357.MOV",
"--codec",
"hevc",
"--max-height",
"720",
"--fps",
"30",
"--crf",
"28",
"--audio-codec",
"opus",
"--audio-bitrate",
"32k",
"-o",
"IMG_9357.mp4",
"--dry-run",
]
)
== 0
)
output = capsys.readouterr().out
assert "scale=w=-2:h=trunc(min(ih\\,720)/2)*2" in output
assert "fps=fps=30" in output
assert "-c:v libx265" in output
assert "-crf 28" in output
assert "-c:a libopus" in output
assert "-b:a 32k" in output
assert "-max_muxing_queue_size 1024" in output
def test_shrink_command_can_keep_size_and_frame_rate(
capsys: pytest.CaptureFixture[str],
) -> None:
assert (
cli.main(
[
"shrink",
"input.mov",
"--keep-size",
"--keep-fps",
"-o",
"output.mp4",
"--dry-run",
]
)
== 0
)
output = capsys.readouterr().out
assert "scale=w=trunc(iw/2)*2:h=trunc(ih/2)*2" in output
assert "fps=fps=" not in output
@pytest.mark.parametrize(
"arguments",
[
["convert", "in.mov", "-o", "out.mp4"],
["webm", "in.mov", "-o", "out.webm"],
["h265", "in.mov", "-o", "out.mp4"],
["av1", "in.mov", "-o", "out.webm"],
["cut", "in.mp4", "--duration", "2", "-o", "out.mp4"],
["loop", "in.mp4", "--duration", "30", "-o", "out.mp4"],
["mute", "in.mp4", "-o", "out.mp4"],
["strip-audio", "in.mp4", "-o", "out.mp4"],
["audio", "in.mp4", "-o", "out.mp3"],
["pip", "main.mp4", "inset.mp4", "-o", "out.mp4"],
["gif", "in.mp4", "-o", "out.gif"],
["waveform", "in.mp3", "-o", "out.png"],
["spectrum", "in.mp3", "-o", "out.png"],
["sheet", "in.mp4", "-o", "out.jpg"],
["reverse", "in.mp4", "--duration", "2", "-o", "out.mp4"],
["mix-audio-files", "one.wav", "two.wav", "-o", "out.wav"],
["compress", "in.mov", "-o", "out.mp4"],
["shrink", "in.mov", "-o", "out.mp4"],
["reduce-size", "in.mov", "-o", "out.mp4"],
["social", "in.mp4", "-o", "out.mp4"],
["fps", "in.mp4", "-o", "out.mp4"],
["mirror", "in.mp4", "-o", "out.mp4"],
["color", "in.mp4", "-o", "out.mp4"],
["freeze", "in.mp4", "-o", "out.mp4"],
[
"privacy-blur",
"in.mp4",
"--x",
"0",
"--y",
"0",
"--width",
"100",
"--height",
"100",
"-o",
"out.mp4",
],
["bounce", "in.mp4", "--duration", "2", "-o", "out.mp4"],
["denoise", "in.wav", "-o", "out.wav"],
["voice", "in.wav", "-o", "out.wav"],
["mono", "in.wav", "-o", "out.wav"],
["cut-audio", "in.wav", "--end", "5", "-o", "out.wav"],
["audio-standard", "in.wav", "--layout", "mono", "-o", "out.wav"],
["gain", "in.wav", "--gain-db", "-3", "-o", "out.wav"],
["audio-fade", "in.wav", "--duration", "30", "-o", "out.wav"],
["sync-audio", "in.wav", "--seconds", "0.5", "-o", "out.wav"],
["tempo", "in.wav", "--factor", "1.5", "-o", "out.wav"],
["crossfade", "one.wav", "two.wav", "-o", "out.wav"],
["audio-join", "one.wav", "two.wav", "-o", "out.wav"],
["captions", "in.mp4", "captions.srt", "-o", "out.mp4"],
["burn-captions", "in.mp4", "captions.srt", "-o", "out.mp4"],
["join-any", "one.mp4", "two.mp4", "-o", "out.mp4"],
["timelapse", "frame-%04d.png", "-o", "out.mp4"],
["audiogram", "in.wav", "cover.jpg", "-o", "out.mp4"],
["clean-metadata", "in.mkv", "-o", "out.mkv"],
["rewrap", "in.mp4", "-o", "out.mkv"],
["label-media", "in.mp4", "--title", "Camera master", "-o", "out.mp4"],
["tag", "in.m4a", "--title", "Episode 1", "-o", "out.m4a"],
],
)
def test_short_aliases_build_the_same_kind_of_plan(
arguments: list[str],
capsys: pytest.CaptureFixture[str],
) -> None:
assert cli.main([*arguments, "--dry-run"]) == 0
assert capsys.readouterr().out.startswith("ffmpeg ")
def test_dry_run_does_not_start_ffmpeg(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
def fail_run(self: Plan, **kwargs: object) -> RunResult:
del self, kwargs
raise AssertionError("Plan.run was called")
monkeypatch.setattr(Plan, "run", fail_run)
code = cli.main(["mute", "in.mp4", "-o", "out.mp4", "--dry-run"])
assert code == 0
assert "-c:v copy" in capsys.readouterr().out
def test_media_command_runs_and_reports_output(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
received: dict[str, object] = {}
def fake_run(self: Plan, **kwargs: object) -> RunResult:
received.update(kwargs)
return RunResult(0, 1.25, "", None, (self.outputs[0].destination,))
monkeypatch.setattr(Plan, "run", fake_run)
code = cli.main(
[
"trim",
"in.mp4",
"--duration",
"2",
"-o",
"out.mp4",
"--ffmpeg",
"custom-ffmpeg",
"--ffprobe",
"custom-ffprobe",
"--probe-timeout",
"4",
"--timeout",
"12",
"--expected-duration",
"2",
"--no-progress",
]
)
captured = capsys.readouterr()
assert code == 0
assert captured.out == "Finished in 1.25s: out.mp4\n"
assert received == {
"ffmpeg": "custom-ffmpeg",
"ffprobe": "custom-ffprobe",
"probe_timeout": 4.0,
"on_progress": None,
"expected_duration": 2.0,
"timeout": 12.0,
}
def test_finished_output_hides_url_secrets(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
def fake_run(self: Plan, **kwargs: object) -> RunResult:
del self, kwargs
return RunResult(
0,
0.1,
"",
None,
("https://example.com/upload?token=REDACT_ME",),
)
monkeypatch.setattr(Plan, "run", fake_run)
assert cli.main(["mute", "in.mp4", "-o", "out.mp4"]) == 0
output = capsys.readouterr().out
assert "REDACT_ME" not in output
assert "token=<redacted>" in output
def test_known_duration_drives_progress(
monkeypatch: pytest.MonkeyPatch,
) -> None:
received: dict[str, object] = {}
def fake_run(self: Plan, **kwargs: object) -> RunResult:
received.update(kwargs)
return RunResult(0, 0.1, "", None, (self.outputs[0].destination,))
monkeypatch.setattr(Plan, "run", fake_run)
assert cli.main(["reverse", "in.mp4", "--duration", "4", "-o", "out.mp4"]) == 0
assert received["expected_duration"] == 4.0
def test_trim_range_drives_progress_duration(
monkeypatch: pytest.MonkeyPatch,
) -> None:
received: dict[str, object] = {}
def fake_run(self: Plan, **kwargs: object) -> RunResult:
received.update(kwargs)
return RunResult(0, 0.1, "", None, (self.outputs[0].destination,))
monkeypatch.setattr(Plan, "run", fake_run)
assert (
cli.main(["trim", "in.mp4", "--start", "5", "--end", "12", "-o", "out.mp4"])
== 0
)
assert received["expected_duration"] == 7.0
def test_boomerang_doubles_progress_duration(
monkeypatch: pytest.MonkeyPatch,
) -> None:
received: dict[str, object] = {}
def fake_run(self: Plan, **kwargs: object) -> RunResult:
received.update(kwargs)
return RunResult(0, 0.1, "", None, (self.outputs[0].destination,))
monkeypatch.setattr(Plan, "run", fake_run)
assert cli.main(["bounce", "in.mp4", "--duration", "2.5", "-o", "out.mp4"]) == 0
assert received["expected_duration"] == 5.0
def test_rotate_accepts_270_degrees(capsys: pytest.CaptureFixture[str]) -> None:
code = cli.main(
["rotate", "in.mp4", "--degrees", "270", "-o", "out.mp4", "--dry-run"]
)
assert code == 0
assert "transpose=dir=cclock" in capsys.readouterr().out
def test_no_audio_drops_the_audio_mapping(capsys: pytest.CaptureFixture[str]) -> None:
assert (
cli.main(["transcode", "in.mov", "-o", "out.mp4", "--no-audio", "--dry-run"])
== 0
)
assert "0:a:0" not in capsys.readouterr().out
def test_batch_dry_run_discovers_directory_in_order(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
source_dir = tmp_path / "recordings"
source_dir.mkdir()
(source_dir / "b.mov").touch()
(source_dir / "a.mkv").touch()
(source_dir / "notes.txt").touch()
output_dir = tmp_path / "converted"
code = cli.main(
[
"batch",
str(source_dir),
"-o",
str(output_dir),
"--dry-run",
]
)
output = capsys.readouterr().out
assert code == 0
assert output.index("a.mkv") < output.index("b.mov")
assert "a.mp4" in output
assert "b.mp4" in output
assert "notes.txt" not in output
assert not output_dir.exists()
def test_batch_dry_run_expands_quoted_pattern(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
(tmp_path / "one.mov").touch()
(tmp_path / "two.mov").touch()
code = cli.main(
[
"batch-convert",
str(tmp_path / "*.mov"),
"-o",
str(tmp_path / "converted"),
"--name-suffix=-web",
"--dry-run",
]
)
output = capsys.readouterr().out
assert code == 0
assert "one-web.mp4" in output
assert "two-web.mp4" in output
def test_batch_recursive_directory_discovery(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
source_dir = tmp_path / "recordings"
nested = source_dir / "day-one"
nested.mkdir(parents=True)
(nested / "clip.mov").touch()
output_dir = tmp_path / "converted"
code = cli.main(["batch", str(source_dir), "-o", str(output_dir), "--dry-run"])
assert code == 2
assert "No supported videos" in capsys.readouterr().err
code = cli.main(
[
"batch",
str(source_dir),
"-o",
str(output_dir),
"--recursive",
"--dry-run",
]
)
assert code == 0
assert "clip.mov" in capsys.readouterr().out
def test_batch_rejects_duplicate_output_names(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
first = tmp_path / "first" / "clip.mov"
second = tmp_path / "second" / "clip.mkv"
first.parent.mkdir()
second.parent.mkdir()
first.touch()
second.touch()
code = cli.main(
[
"batch",
str(first),
str(second),
"-o",
str(tmp_path / "converted"),
"--dry-run",
]
)
assert code == 2
assert "same output name" in capsys.readouterr().err
def test_batch_preflights_existing_outputs(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
source = tmp_path / "clip.mov"
source.touch()
output_dir = tmp_path / "converted"
output_dir.mkdir()
(output_dir / "clip.mp4").touch()
monkeypatch.setattr(
cli,
"run_batch",
lambda *args, **kwargs: pytest.fail("Preflight must run before the batch"),
)
code = cli.main(["batch", str(source), "-o", str(output_dir)])
assert code == 4
assert "already exists" in capsys.readouterr().err
def test_batch_prints_result_summary(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
source = tmp_path / "clip.mov"
source.touch()
output_dir = tmp_path / "converted"
result = BatchResult(
(
BatchItemResult(
"clip.mov",
"completed",
0.4,
(str(output_dir / "clip.mp4"),),
),
),
0.5,
)
monkeypatch.setattr(cli, "run_batch", lambda *args, **kwargs: result)
code = cli.main(
["batch-transcode", str(source), "-o", str(output_dir), "--no-progress"]
)
output = capsys.readouterr().out
assert code == 0
assert "COMPLETED clip.mov" in output
assert "1 completed, 0 failed" in output
assert output_dir.is_dir()
def test_batch_json_reports_failure_exit_code(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
source = tmp_path / "clip.mov"
source.touch()
result = BatchResult(
(
BatchItemResult(
"clip.mov",
"failed",
0.1,
error="FFmpeg exited with code 1",
error_type="ExecutionError",
),
),
0.1,
)
monkeypatch.setattr(cli, "run_batch", lambda *args, **kwargs: result)
code = cli.main(
[
"batch",
str(source),
"-o",
str(tmp_path / "converted"),
"--json",
"--no-progress",
]
)
report = json.loads(capsys.readouterr().out)
assert code == 6
assert report["ok"] is False
assert report["counts"]["failed"] == 1
assert report["items"][0]["error_type"] == "ExecutionError"
def test_batch_json_dry_run_lists_commands(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
source = tmp_path / "clip.mov"
source.touch()
code = cli.main(
[
"batch",
str(source),
"-o",
str(tmp_path / "converted"),
"--json",
"--dry-run",
]
)
report = json.loads(capsys.readouterr().out)
assert code == 0
assert report["jobs"][0]["name"] == "clip.mov"
assert report["jobs"][0]["command"].startswith("ffmpeg ")
def test_batch_shrink_dry_run_uses_size_controls(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
source = tmp_path / "IMG_9357.MOV"
source.touch()
code = cli.main(
[
"shrink-batch",
str(source),
"-o",
str(tmp_path / "small"),
"--codec",
"hevc",
"--max-height",
"720",
"--fps",
"30",
"--crf",
"28",
"--audio-codec",
"opus",
"--audio-bitrate",
"32k",
"--dry-run",
]
)
output = capsys.readouterr().out
assert code == 0
assert "IMG_9357-small.mp4" in output
assert "scale=w=-2:h=trunc(min(ih\\,720)/2)*2" in output
assert "fps=fps=30" in output
assert "-c:v libx265" in output
assert "-c:a libopus" in output
assert "-b:a 32k" in output
def test_batch_shrink_json_dry_run_lists_commands(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
source = tmp_path / "clip.mov"
source.touch()
code = cli.main(
[
"batch-shrink",
str(source),
"-o",
str(tmp_path / "small"),
"--keep-size",
"--keep-fps",
"--json",
"--dry-run",
]
)
report = json.loads(capsys.readouterr().out)
assert code == 0
assert report["jobs"][0]["name"] == "clip.mov"
assert "clip-small.mp4" in report["jobs"][0]["outputs"][0]
assert "--keep-size" not in report["jobs"][0]["command"]
assert "fps=fps=" not in report["jobs"][0]["command"]
def test_gif_full_omits_trim_filter(capsys: pytest.CaptureFixture[str]) -> None:
assert cli.main(["gif", "in.mp4", "--full", "-o", "out.gif", "--dry-run"]) == 0
assert "trim=" not in capsys.readouterr().out
def test_gif_original_width_omits_scale(capsys: pytest.CaptureFixture[str]) -> None:
assert (
cli.main(
[
"gif",
"in.mp4",
"--original-width",
"-o",
"out.gif",
"--dry-run",
]
)
== 0
)
assert "scale=" not in capsys.readouterr().out
def test_boolean_shortcut_options_reach_the_plan(
capsys: pytest.CaptureFixture[str],
) -> None:
assert (
cli.main(
[
"add-music",
"silent.mp4",
"music.mp3",
"--no-source-audio",
"-o",
"out.mp4",
"--dry-run",
]
)
== 0
)
assert "0:a:0" not in capsys.readouterr().out
assert (
cli.main(
[
"duck",
"in.mp4",
"music.mp3",
"--no-loop-music",
"-o",
"out.mp4",
"--dry-run",
]
)
== 0
)
assert "-stream_loop" not in capsys.readouterr().out
assert (
cli.main(
[
"add-music",
"silent.mp4",
"music.mp3",
"--silent-source",
"-o",
"out.mp4",
"--dry-run",
]
)
== 0
)
assert "0:a:0" not in capsys.readouterr().out
def test_overwrite_changes_the_ffmpeg_policy(
capsys: pytest.CaptureFixture[str],
) -> None:
assert (
cli.main(["mute", "in.mp4", "-o", "out.mp4", "--overwrite", "--dry-run"]) == 0
)
assert " -y " in capsys.readouterr().out
def test_explain_prints_plan_before_dry_run(
capsys: pytest.CaptureFixture[str],
) -> None:
code = cli.main(
[
"resize",
"in.mp4",
"--width",
"640",
"-o",
"out.mp4",
"--dry-run",
"--explain",
]
)
output = capsys.readouterr().out
assert code == 0
assert "Inputs:" in output
assert "Outputs:" in output
assert output.rstrip().endswith("out.mp4")
def test_dry_run_hides_url_credentials(
capsys: pytest.CaptureFixture[str],
) -> None:
code = cli.main(
[
"transcode",
"https://example-user:REDACT_ME@example.com/live.mp4",
"-o",
"out.mp4",
"--dry-run",
]
)
output = capsys.readouterr().out
assert code == 0
assert "REDACT_ME" not in output
assert "<redacted>@example.com" in output
@pytest.mark.parametrize(
("error", "expected_code"),
[
(CompilationError("bad compile"), 2),
(BinaryNotFoundError("missing binary"), 3),
(BinaryUnusableError("blocked binary"), 3),
(OutputExistsError("output exists"), 4),
(
ExecutionError(
"failed",
returncode=1,
stderr="failure",
command="ffmpeg",
),
6,
),
(JobTimeoutError("too slow"), 7),
(FlowmpegError("other failure"), 1),
],