oximedia-cli 0.1.7

Command-line interface for OxiMedia
Documentation
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
//! OxiMedia CLI — Patent-free multimedia processing framework.
//!
//! `oximedia` is a command-line tool for working with media files using only
//! royalty-free codecs. It is part of the [OxiMedia](https://github.com/cool-japan/oximedia)
//! sovereign media framework — a pure-Rust reconstruction of both FFmpeg and OpenCV.
//!
//! # Quick Start
//!
//! ```notrust
//! # Probe a media file
//! oximedia probe -i input.mkv
//!
//! # Transcode video (alias: convert)
//! oximedia transcode -i input.mkv -o output.webm --video-codec vp9 --video-bitrate 2M
//!
//! # Extract frames
//! oximedia extract -i input.mkv -o frames_%04d.png
//!
//! # Batch process a directory
//! oximedia batch input_dir/ output_dir/ config.toml
//!
//! # FFmpeg compatibility layer
//! oximedia ff -i input.mp4 -c:v libvpx-vp9 output.webm
//! ```
//!
//! # Supported Formats
//!
//! OxiMedia uses only patent-free codecs:
//! - **Video:** AV1, VP9, VP8, Theora, FFV1
//! - **Audio:** Opus, Vorbis, FLAC, PCM, WAV
//! - **Containers:** Matroska (.mkv), WebM, Ogg, FLAC, WAV
//!
//! ## Subcommand Families
//!
//! ### Inspection
//!
//! | Subcommand | Description |
//! |---|---|
//! | `probe` | Inspect media file metadata: codec, resolution, duration, tracks, chapters |
//! | `info` | Show supported formats, codecs, and feature flags |
//! | `version` | Print OxiMedia version and build info |
//! | `doctor` | Diagnose the runtime environment: GPU adapters, temp dir writability |
//! | `analyze` | Video/audio quality metrics analysis |
//!
//! ### Transcode and Convert
//!
//! | Subcommand | Description |
//! |---|---|
//! | `transcode` | Re-encode video/audio with codec and quality control (alias: `convert`) |
//! | `extract` | Extract individual frames to PNG, JPEG, or PPM |
//! | `thumbnail` | Generate single thumbnails from a video file |
//! | `sprite` | Generate thumbnail sprite sheets for video scrubbing |
//! | `concat` | Concatenate multiple video/audio files |
//! | `batch` | Batch-process multiple files in a directory |
//! | `batch-engine` | SQLite-backed job queue: submit, status, list, cancel, report |
//! | `scaling` | Upscale, downscale, analyze, compare, batch scale operations |
//! | `optimize` | Codec complexity analysis, CRF sweep, quality ladder, benchmark |
//!
//! ### Audio Processing
//!
//! | Subcommand | Description |
//! |---|---|
//! | `audio` | Loudness metering, normalization, spectrum, and beat detection |
//! | `loudness` | EBU R128 / ITU-R BS.1770 loudness analysis and validation |
//! | `normalize` | Normalize audio to a target loudness standard |
//! | `mixer` | Mix multiple audio tracks with a declarative routing bus |
//! | `audiopost` | ADR, stem mixing, delivery, and audio restoration |
//! | `filter` | Standalone filter graph processing |
//!
//! ### Video Analysis and Enhancement
//!
//! | Subcommand | Description |
//! |---|---|
//! | `scopes` | Waveform, vectorscope, histogram, parade, false-colour scopes |
//! | `quality` | Perceptual quality metrics: VMAF, SSIM, PSNR comparison |
//! | `dedup` | Detect near-duplicate clips via perceptual hashing |
//! | `restore` | Audio/video restoration: de-noise, de-click, upscale, deinterlace |
//! | `denoise` | Video noise reduction with spatial and temporal modes |
//! | `stabilize` | Remove camera shake (translation, affine, perspective, 3D models) |
//! | `scene` | Shot detection, classification, and storyboard generation |
//! | `lut` | Apply, inspect, convert, or generate LUT files (.cube, Hald) |
//! | `graphics` | 2D broadcast graphics: lower-thirds, tickers, overlays, templates |
//! | `image` | Professional image operations (DPX, EXR, TIFF, sequences) |
//! | `color` | Color management: convert, inspect matrix, compute Delta E |
//! | `forensics` | Tamper detection, integrity analysis, media provenance |
//! | `mir` | Music Information Retrieval: tempo, key, chords, segmentation |
//! | `ml` | Sovereign ML pipelines via Pure-Rust ONNX (oximedia-ml) |
//!
//! ### Broadcast and Live Production
//!
//! | Subcommand | Description |
//! |---|---|
//! | `playout` | Schedule and play out media files on an output timeline |
//! | `switcher` | Software vision mixer with keying, transitions, and macro recording |
//! | `ndi` | NDI source discovery, streaming, and monitoring |
//! | `videoip` | Video-over-IP: send, receive, discover (RTP/SRT/RIST) |
//! | `multicam` | Multi-camera sync, switch, composite, color-match, export |
//! | `monitor` | Real-time system and pipeline health monitoring with alerting |
//! | `stream` | HLS/DASH adaptive streaming: serve, ingest, record |
//! | `timesync` | Time synchronisation: analyze, align, offset, drift, report |
//! | `gaming` | Gaming: capture, clip, overlay |
//!
//! ### MAM and Archive
//!
//! | Subcommand | Description |
//! |---|---|
//! | `mam` | Media asset management: ingest, tag, search, export, catalogue |
//! | `search` | Full-text and visual similarity search over the MAM catalogue |
//! | `archive` | IMF/archive packaging and extraction |
//! | `archive-pro` | Archive Pro: ingest, verify, migrate, report, policy |
//! | `clips` | Clip management: create, list, export, trim, merge, tag |
//! | `playlist` | Playlist generation, validation, and playout automation |
//! | `cloud` | Cloud storage: upload, download, transcode (S3/GCS/Azure/R2/B2) |
//! | `storage` | Inspect and manage local storage backends |
//!
//! ### Post Production
//!
//! | Subcommand | Description |
//! |---|---|
//! | `timeline` | Timeline editing: inspect, edit, export (OTIO, EDL, FCP XML) |
//! | `edl` | EDL parse, validate, export, and conform |
//! | `conform` | QC/conformance checking and automated fixing |
//! | `qc` | Quality Control: check, validate, report, rules, fix |
//! | `imf` | IMF: validate, package, info, extract, create |
//! | `aaf` | AAF: info, extract, convert, validate, merge |
//! | `vfx` | Visual effects and compositing |
//! | `subtitle` | Subtitle conversion, extraction, burn-in, and synchronization |
//! | `captions` | Generate and synchronise closed captions |
//! | `dolby-vision` | Dolby Vision: analyze, convert, metadata, validate, info |
//! | `review` | Review: create, annotate, approve, reject, export, status |
//! | `collab` | Collaboration: create, join, share, comment, export, status |
//! | `align` | Audio/video alignment: sync, offset detection |
//! | `calibrate` | Display, audio, color calibration and pattern generation |
//!
//! ### Rights and Security
//!
//! | Subcommand | Description |
//! |---|---|
//! | `drm` | DRM: encrypt, decrypt, manage keys, validate |
//! | `watermark` | Digital audio/video watermarking |
//! | `access` | Access control: grant, revoke, list, policy, audit |
//! | `rights` | Digital rights: register, check, transfer, license, search |
//!
//! ### Infrastructure
//!
//! | Subcommand | Description |
//! |---|---|
//! | `distributed` | Distributed encoding: coordinator, workers, job submission |
//! | `farm` | Render farm: start, submit, status, cancel, nodes |
//! | `renderfarm` | Render farm cluster: init, add-node, submit, status, dashboard |
//! | `routing` | Signal routing: create, connect, validate, list |
//! | `virtual` | Virtual production: LED volume, camera tracking, configure |
//! | `profiler` | Profiler: run, report, compare, export, bottleneck analysis |
//! | `package` | HLS/DASH adaptive-bitrate packaging with optional encryption |
//!
//! ### Analysis and Recommendations
//!
//! | Subcommand | Description |
//! |---|---|
//! | `recommend` | Codec, settings, and workflow recommendations |
//! | `workflow` | Workflow orchestration: create, run, status, list, template |
//! | `auto` | Auto editing: run, schedule, create, delete, log |
//! | `benchmark` | Run encoding benchmarks |
//! | `validate` | Validate media file integrity |
//! | `metadata` | Edit media metadata and tags |
//!
//! ### Compatibility
//!
//! | Subcommand | Description |
//! |---|---|
//! | `ff` / `ffcompat` | FFmpeg argument compatibility layer — translates FFmpeg CLI flags to OxiMedia |
//!
//! ### Tooling
//!
//! | Subcommand | Description |
//! |---|---|
//! | `completions` | Print shell completion script (bash, zsh, fish, powershell, elvish) |
//! | `man-page` | Print roff man page to stdout |
//! | `tui` | Launch the interactive terminal UI |
//! | `plugin` | Manage OxiMedia plugins: list, info, codecs, validate, paths |
//! | `preset` | List and manage encoding and processing presets |
//! | `timecode` | Timecode convert, calculate, validate, burn-in |
//! | `proxy` | Proxy media: generate, list, link, info, clean |
//! | `concat` | Concatenate multiple media files |
//! | `repair` | Media file repair and recovery |
//!
//! ## Global Flags
//!
//! | Flag | Default | Description |
//! |---|---|---|
//! | `--json` | off | Output pure JSON on stdout; suppress coloured text |
//! | `--ndjson` | off | Emit one NDJSON record per item (conflicts with `--json`) |
//! | `--no-color` | off | Disable colour output |
//! | `--log-format json` | plain | Structured JSON log output via `tracing-subscriber` |
//! | `--progress <plain\|json>` | plain | Progress reporting format |
//! | `-v` / `--verbose` | off | Enable debug/trace log output (repeatable: `-vv`, `-vvv`) |
//! | `-q` / `--quiet` | off | Suppress all non-error output |
//!
//! ## See Also
//!
//! - Plugin system: `plugin_cmd` — see `$OXIMEDIA_PLUGIN_PATH` for search paths.
//! - Preset system: `presets` — see built-in categories (Web, Device, Streaming, Archival, Custom).
//! - FFmpeg compatibility: `ffcompat_cmd` and the `ff` alias.

#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::unused_async)]
#![allow(clippy::unnested_or_patterns)]
#![allow(clippy::unnecessary_wraps)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::trivially_copy_pass_by_ref)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::similar_names)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::if_not_else)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::case_sensitive_file_extension_comparisons)]
#![allow(clippy::doc_link_with_quotes)]
#![allow(clippy::match_same_arms)]
#![allow(clippy::needless_continue)]
#![allow(clippy::single_char_pattern)]

mod aaf_cmd;
mod access_cmd;
mod align_cmd;
mod analyze;
mod archive_cmd;
mod archivepro_cmd;
mod completions_cmd;
mod doctor_cmd;
mod exit_codes;
mod man_cmd;
mod output;

mod audio_cmd;
mod audiopost_cmd;
mod auto_cmd;
mod batch;
mod batch_cmd;
mod benchmark;
mod calibrate_cmd;
mod captions_cmd;
mod clips_cmd;
mod cloud_cmd;
mod collab_cmd;
mod color_cmd;
mod commands;
mod concat;
mod conform_cmd;
mod decode_helper;
mod dedup_cmd;
mod denoise_cmd;
mod distributed_cmd;
mod dolbyvision_cmd;
mod drm_cmd;
mod edl_cmd;
mod extract;
mod farm_cmd;
mod ffcompat_cmd;
mod filter_cmd;
mod forensics_cmd;
mod frame_extract;
mod gaming_cmd;
mod graphics_cmd;
mod handlers;
mod image_cmd;
mod imf_cmd;
mod loudness_cmd;
mod lut_cmd;
mod mam_cmd;
mod metadata;
mod mir_cmd;
mod mixer_cmd;
mod ml_cmd;
mod monitor_cmd;
mod multicam_cmd;
mod ndi_cmd;
mod normalize_cmd;
mod optimize_cmd;
mod package_cmd;
mod playlist_cmd;
mod profiler_cmd;

mod playout_cmd;
mod plugin_cmd;
mod presets;
mod progress;
mod proxy_cmd;
mod qc_cmd;
mod quality_cmd;
mod recommend_cmd;
mod renderfarm_cmd;
mod repair_cmd;
mod restore_cmd;
mod review_cmd;
mod rights_cmd;
mod routing_cmd;
mod scaling_cmd;
mod scene;
mod scopes_cmd;
mod search_cmd;
mod sprite;
mod stabilize_cmd;
mod stream_cmd;
mod subtitle_cmd;
mod switcher_cmd;
mod thumbnail;
mod timecode_cmd;
mod timeline_cmd;
mod timesync_cmd;
mod transcode;
mod tui_cmd;
mod validate;
mod vfx_cmd;
mod videoip_cmd;
mod virtual_cmd;
mod watermark_cmd;
mod workflow_cmd;

use anyhow::Result;
use clap::Parser;
use colored::Colorize;

use commands::Commands;
use handlers::{
    handle_captions_command, handle_monitor_command, handle_preset_command, handle_restore_command,
    init_color, init_logging, probe_file, show_info, show_version, LogFormat,
};
use progress::ProgressFormat;

/// Patent-free multimedia framework CLI
#[derive(Parser)]
#[command(name = "oximedia")]
#[command(author, version, about, long_about = None)]
struct Cli {
    /// Subcommand to execute
    #[command(subcommand)]
    command: Commands,

    /// Enable verbose output (can be used multiple times: -v, -vv, -vvv)
    #[arg(short, long, action = clap::ArgAction::Count, global = true)]
    verbose: u8,

    /// Suppress all output except errors
    #[arg(short, long, global = true)]
    quiet: bool,

    /// Disable colored output
    #[arg(long, global = true)]
    no_color: bool,

    /// Output results in JSON format
    #[arg(long, global = true)]
    json: bool,

    /// Output as NDJSON (one record per line); conflicts with --json
    #[arg(long, global = true, conflicts_with = "json")]
    ndjson: bool,

    /// Log output format
    #[arg(long, global = true, default_value = "plain", value_enum)]
    log_format: LogFormat,

    /// Progress output format (plain bar or streaming JSON to stderr)
    #[arg(long, global = true, default_value = "plain", value_enum)]
    progress: ProgressFormat,
}

/// Dispatch CLI subcommands and return a `Result`.
async fn run(cli: Cli) -> Result<()> {
    // Resolve the output format once so individual handlers can query it.
    let _output_fmt = output::resolve_format(cli.json, cli.ndjson);

    let result = match cli.command {
        Commands::Probe {
            input,
            detail,
            streams,
            hash: _hash,
            quality_snapshot: _quality_snapshot,
            format,
            chapters,
            metadata,
        } => {
            probe_file(
                &input, detail, streams, &format, chapters, metadata, cli.ndjson,
            )
            .await
        }

        Commands::Info => {
            show_info();
            Ok(())
        }

        Commands::Version => {
            show_version(cli.json);
            Ok(())
        }

        Commands::Transcode {
            input,
            output,
            preset_name,
            video_codec,
            audio_codec,
            video_bitrate,
            audio_bitrate,
            scale,
            video_filter,
            start_time,
            duration,
            framerate,
            preset,
            two_pass,
            crf,
            threads,
            overwrite,
            resume,
            audio_filter: _audio_filter,
            map: _map,
            normalize_audio: _normalize_audio,
        } => {
            let options = transcode::TranscodeOptions {
                input,
                output,
                preset_name,
                video_codec,
                audio_codec,
                video_bitrate,
                audio_bitrate,
                scale,
                video_filter,
                audio_filter: _audio_filter,
                start_time,
                duration,
                framerate,
                preset,
                two_pass,
                crf,
                threads,
                overwrite,
                resume,
                progress_format: cli.progress,
            };
            transcode::transcode(options).await
        }

        Commands::Extract {
            input,
            output_pattern,
            format,
            start_time,
            frames,
            every,
            quality,
        } => {
            let options = extract::ExtractOptions {
                input,
                output_pattern,
                format,
                start_time,
                frames,
                every,
                quality,
            };
            extract::extract_frames(options).await
        }

        Commands::Batch {
            input_dir,
            output_dir,
            config,
            jobs,
            continue_on_error,
            dry_run,
        } => {
            let options = batch::BatchOptions {
                input_dir,
                output_dir,
                config,
                jobs,
                continue_on_error,
                dry_run,
                progress_format: cli.progress,
            };
            batch::batch_process(options).await
        }

        Commands::Concat {
            inputs,
            output,
            method,
            validate,
            overwrite,
        } => {
            let concat_method = concat::ConcatMethod::from_str(&method)?;
            let options = concat::ConcatOptions {
                inputs,
                output,
                method: concat_method,
                validate,
                overwrite,
                json_output: cli.json,
                transition: concat::TransitionType::CleanCut,
                chapter_options: concat::ChapterOptions::default(),
                stream_selection: None,
                trim_ranges: Vec::new(),
                edl_file: None,
                force_format: None,
                keyframe_align: false,
                max_audio_desync_ms: 100.0,
            };
            concat::concat_videos(options).await
        }

        Commands::Thumbnail {
            input,
            output,
            mode,
            timestamp,
            count,
            rows,
            cols,
            width,
            height,
            format,
            quality,
        } => {
            let thumb_mode = match mode.to_lowercase().as_str() {
                "single" => {
                    let ts = if let Some(ref ts_str) = timestamp {
                        thumbnail::parse_timestamp(ts_str)?
                    } else {
                        return Err(anyhow::anyhow!(
                            "Timestamp is required for single mode (use --timestamp)"
                        ));
                    };
                    thumbnail::ThumbnailMode::Single { timestamp: ts }
                }
                "multiple" => thumbnail::ThumbnailMode::Multiple { count },
                "grid" => thumbnail::ThumbnailMode::Grid { rows, cols },
                "auto" => thumbnail::ThumbnailMode::Auto,
                _ => {
                    return Err(anyhow::anyhow!(
                        "Invalid mode. Use: single, multiple, grid, or auto"
                    ))
                }
            };

            let thumb_format = thumbnail::ThumbnailFormat::from_str(&format)?;

            let options = thumbnail::ThumbnailOptions {
                input,
                output,
                mode: thumb_mode,
                width,
                height,
                quality,
                format: thumb_format,
                json_output: cli.json,
            };
            thumbnail::generate_thumbnails(options).await
        }

        Commands::Sprite {
            input,
            output,
            interval,
            count,
            cols,
            rows,
            width,
            height,
            format,
            quality,
            compression,
            strategy,
            layout,
            spacing,
            margin,
            vtt,
            vtt_output,
            manifest,
            manifest_output,
            timestamps,
            aspect,
        } => {
            // Parse interval if provided
            let interval_secs = if let Some(ref interval_str) = interval {
                Some(sprite::parse_duration(interval_str)?)
            } else {
                None
            };

            // Parse format
            let img_format = sprite::ImageFormat::from_str(&format)?;

            // Parse sampling strategy
            let sampling_strategy = sprite::SamplingStrategy::from_str(&strategy)?;

            // Parse layout mode
            let layout_mode = sprite::LayoutMode::from_str(&layout)?;

            // Create sprite sheet configuration
            let mut config = sprite::SpriteSheetConfig {
                interval: interval_secs,
                count,
                thumbnail_width: width,
                thumbnail_height: height,
                columns: cols,
                rows,
                format: img_format,
                quality,
                strategy: sampling_strategy,
                layout: layout_mode,
                spacing,
                margin,
                maintain_aspect_ratio: aspect,
                compression,
            };

            // Validate and adjust configuration
            config = sprite::validate_and_adjust_config(config)?;

            // Create options
            let options = sprite::SpriteSheetOptions {
                input,
                output,
                config,
                generate_vtt: vtt,
                vtt_output,
                generate_manifest: manifest,
                manifest_output,
                show_timestamps: timestamps,
                json_output: cli.json,
            };

            sprite::generate_sprite_sheet(options).await
        }

        Commands::Metadata {
            input,
            output,
            show,
            set,
            remove,
            clear,
            copy_from,
        } => {
            use std::collections::HashMap;

            let operation = if show {
                metadata::MetadataOperation::Show
            } else if !set.is_empty() {
                let mut fields = HashMap::new();
                for (key, value) in set {
                    fields.insert(key, value);
                }
                metadata::MetadataOperation::Set { fields }
            } else if !remove.is_empty() {
                metadata::MetadataOperation::Remove { fields: remove }
            } else if clear {
                metadata::MetadataOperation::Clear
            } else if let Some(source) = copy_from {
                metadata::MetadataOperation::Copy { source }
            } else {
                // Default to show if no operation specified
                metadata::MetadataOperation::Show
            };

            let options = metadata::MetadataOptions {
                input,
                output,
                operation,
                json_output: cli.json,
            };
            metadata::manage_metadata(options).await
        }

        Commands::Benchmark {
            input,
            codecs,
            presets,
            duration,
            iterations,
            output_dir,
        } => {
            let options = benchmark::BenchmarkOptions {
                input,
                codecs,
                presets,
                duration,
                iterations,
                output_dir,
                json_output: cli.json,
            };
            benchmark::run_benchmark(options).await
        }

        Commands::Validate {
            inputs,
            checks,
            strict,
            fix,
            loudness_check: _loudness_check,
            gamut_check: _gamut_check,
        } => {
            let validation_checks: Result<Vec<validate::ValidationCheck>> = checks
                .iter()
                .map(|s| validate::ValidationCheck::from_str(s))
                .collect();

            let options = validate::ValidateOptions {
                inputs,
                checks: validation_checks?,
                strict,
                fix,
                json_output: cli.json,
            };
            validate::validate_files(options).await
        }

        Commands::Analyze {
            input,
            reference,
            metrics,
            output_format,
            per_frame,
            summary,
        } => {
            let options = analyze::AnalyzeOptions {
                input,
                reference,
                metrics,
                output_format,
                per_frame,
                summary,
                json_output: cli.json,
            };
            analyze::analyze_quality(options).await
        }

        Commands::Scene { command } => scene::handle_scene_command(command, cli.json).await,

        Commands::Scopes { command } => scopes_cmd::handle_scopes_command(command, cli.json).await,

        Commands::Audio { command } => audio_cmd::handle_audio_command(command, cli.json).await,

        Commands::Subtitle { command } => {
            subtitle_cmd::handle_subtitle_command(command, cli.json).await
        }

        Commands::Filter { command } => filter_cmd::handle_filter_command(command, cli.json).await,

        Commands::Lut { command } => lut_cmd::handle_lut_command(command, cli.json).await,

        Commands::Denoise {
            input,
            output,
            mode,
            strength,
            spatial,
            temporal,
            preserve_grain,
        } => {
            let opts = denoise_cmd::DenoiseOptions {
                input,
                output,
                mode,
                strength,
                spatial,
                temporal,
                preserve_grain,
            };
            denoise_cmd::run_denoise(opts, cli.json).await
        }

        Commands::Stabilize {
            input,
            output,
            mode,
            quality,
            smoothing,
            zoom,
        } => {
            let opts = stabilize_cmd::StabilizeOptions {
                input,
                output,
                mode,
                quality,
                smoothing,
                zoom,
            };
            stabilize_cmd::run_stabilize(opts, cli.json).await
        }

        Commands::Edl { command } => edl_cmd::handle_edl_command(command, cli.json).await,

        Commands::Package {
            input,
            output,
            format,
            segments,
            ladders,
            encrypt,
            low_latency,
        } => {
            let opts = package_cmd::PackageOptions {
                input,
                output,
                format,
                segments,
                ladders,
                encrypt,
                low_latency,
            };
            package_cmd::run_package(opts, cli.json).await
        }

        Commands::Forensics {
            input,
            all,
            tests,
            output_format,
            report,
        } => {
            let opts = forensics_cmd::ForensicsOptions {
                input,
                all,
                tests,
                output_format,
                report,
            };
            forensics_cmd::run_forensics(opts, cli.json).await
        }

        Commands::Monitor { command } => handle_monitor_command(command, cli.json).await,
        Commands::Restore { command } => handle_restore_command(command, cli.json).await,
        Commands::Captions { command } => handle_captions_command(command, cli.json).await,

        Commands::Preset { command } => handle_preset_command(command, cli.json).await,

        Commands::Stream { command } => stream_cmd::run_stream(command, cli.json).await,
        Commands::Search { command } => search_cmd::run_search(command, cli.json).await,
        Commands::Timecode { command } => timecode_cmd::run_timecode(command, cli.json).await,
        Commands::Repair { command } => repair_cmd::run_repair(command, cli.json).await,
        Commands::Color { command } => color_cmd::run_color(command, cli.json).await,
        Commands::Playlist { command } => {
            playlist_cmd::handle_playlist_command(command, cli.json).await
        }
        Commands::Conform { command } => {
            conform_cmd::handle_conform_command(command, cli.json).await
        }
        Commands::Archive { command } => {
            archive_cmd::handle_archive_command(command, cli.json).await
        }
        Commands::Watermark { command } => {
            watermark_cmd::handle_watermark_command(command, cli.json).await
        }
        Commands::Image { command } => image_cmd::handle_image_command(command, cli.json).await,
        Commands::Graphics { command } => {
            graphics_cmd::handle_graphics_command(command, cli.json).await
        }
        Commands::Multicam { command } => {
            multicam_cmd::handle_multicam_command(command, cli.json).await
        }
        Commands::Timeline { command } => {
            timeline_cmd::handle_timeline_command(command, cli.json).await
        }
        Commands::Vfx { command } => vfx_cmd::handle_vfx_command(command, cli.json).await,
        Commands::Ffcompat { args } => ffcompat_cmd::run(args).await,
        Commands::Tui => {
            tui_cmd::run_tui()?;
            Ok(())
        }
        Commands::Optimize { command } => {
            optimize_cmd::handle_optimize_command(command, cli.json).await
        }
        Commands::Mixer { command } => mixer_cmd::handle_mixer_command(command, cli.json).await,
        Commands::Audiopost { command } => {
            audiopost_cmd::handle_audiopost_command(command, cli.json).await
        }
        Commands::Distributed { command } => {
            distributed_cmd::handle_distributed_command(command, cli.json).await
        }
        Commands::Farm { command } => farm_cmd::handle_farm_command(command, cli.json).await,
        Commands::Ndi { command } => ndi_cmd::handle_ndi_command(command, cli.json).await,
        Commands::Videoip { command } => {
            videoip_cmd::handle_videoip_command(command, cli.json).await
        }
        Commands::Gaming { command } => gaming_cmd::handle_gaming_command(command, cli.json).await,
        Commands::Mam { command } => mam_cmd::handle_mam_command(command, cli.json).await,
        Commands::Cloud { command } => cloud_cmd::handle_cloud_command(command, cli.json).await,
        Commands::Plugin { command } => plugin_cmd::handle_plugin_command(command, cli.json).await,
        Commands::Mir { command } => mir_cmd::handle_mir_command(command, cli.json).await,
        Commands::Qc { command } => qc_cmd::handle_qc_command(command, cli.json).await,
        Commands::Imf { command } => imf_cmd::handle_imf_command(command, cli.json).await,
        Commands::Aaf { command } => aaf_cmd::handle_aaf_command(command, cli.json).await,
        Commands::Playout { command } => {
            playout_cmd::handle_playout_command(command, cli.json).await
        }
        Commands::Switcher { command } => {
            switcher_cmd::handle_switcher_command(command, cli.json).await
        }
        Commands::Workflow { command } => {
            workflow_cmd::handle_workflow_command(command, cli.json).await
        }
        Commands::Collab { command } => collab_cmd::handle_collab_command(command, cli.json).await,
        Commands::Proxy { command } => proxy_cmd::handle_proxy_command(command, cli.json).await,
        Commands::Clips { command } => clips_cmd::handle_clips_command(command, cli.json).await,
        Commands::Review { command } => review_cmd::handle_review_command(command, cli.json).await,
        Commands::Drm { command } => drm_cmd::handle_drm_command(command, cli.json).await,
        Commands::Dedup { command } => dedup_cmd::handle_dedup_command(command, cli.json).await,
        Commands::ArchivePro { command } => {
            archivepro_cmd::handle_archivepro_command(command, cli.json).await
        }
        Commands::DolbyVision { command } => {
            dolbyvision_cmd::handle_dolbyvision_command(command, cli.json).await
        }
        Commands::TimeSync { command } => {
            timesync_cmd::handle_timesync_command(command, cli.json).await
        }
        Commands::Align { command } => align_cmd::handle_align_command(command, cli.json).await,
        Commands::Routing { command } => {
            routing_cmd::handle_routing_command(command, cli.json).await
        }
        Commands::Calibrate { command } => {
            calibrate_cmd::handle_calibrate_command(command, cli.json).await
        }
        Commands::Virtual { command } => {
            virtual_cmd::handle_virtual_command(command, cli.json).await
        }
        Commands::Profiler { command } => {
            profiler_cmd::handle_profiler_command(command, cli.json).await
        }
        Commands::Recommend { command } => {
            recommend_cmd::handle_recommend_command(command, cli.json).await
        }
        Commands::Scaling { command } => {
            scaling_cmd::handle_scaling_command(command, cli.json).await
        }
        Commands::Renderfarm { command } => {
            renderfarm_cmd::handle_renderfarm_command(command, cli.json).await
        }
        Commands::Access { command } => access_cmd::handle_access_command(command, cli.json).await,
        Commands::Rights { command } => rights_cmd::handle_rights_command(command, cli.json).await,
        Commands::Auto { command } => auto_cmd::handle_auto_command(command, cli.json).await,
        Commands::Loudness { command } => {
            loudness_cmd::run_loudness(command, cli.json, cli.ndjson).await
        }
        Commands::Quality { command } => {
            quality_cmd::run_quality(command, cli.json, cli.ndjson).await
        }
        Commands::Normalize { command } => normalize_cmd::run_normalize(command, cli.json).await,
        Commands::BatchEngine { command } => batch_cmd::run_batch_engine(command, cli.json).await,
        Commands::Ml { command } => ml_cmd::run_ml(command, cli.json).await,
        Commands::Completions { shell } => completions_cmd::run(shell),
        Commands::ManPage => man_cmd::run(),
        Commands::Doctor { json, full } => doctor_cmd::run(json, full),
    };

    result
}

/// Main entry point for the OxiMedia CLI.
#[tokio::main]
async fn main() -> std::process::ExitCode {
    let cli = Cli::parse();

    // Disable colours before anything else — must run before any Colorize use.
    // JSON / NDJSON output also forces no-colour for clean machine-readable output.
    init_color(cli.no_color || cli.json || cli.ndjson);

    // Initialize logging before dispatching subcommands
    if let Err(e) = init_logging(cli.verbose, cli.quiet, cli.log_format) {
        eprintln!("error: failed to initialise logging: {:#}", e);
        return exit_codes::OxiExitCode::GenericError.into();
    }

    // Dispatch and map errors to exit codes
    match run(cli).await {
        Ok(()) => exit_codes::OxiExitCode::Ok.into(),
        Err(ref e) => {
            eprintln!("{} {:#}", "Error:".red().bold(), e);
            if let Some(source) = e.source() {
                eprintln!("{} {}", "Caused by:".yellow(), source);
            }
            exit_codes::classify_error(e).into()
        }
    }
}