oximedia-cli 0.1.4

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
//! Media alignment CLI commands.
//!
//! Provides commands for aligning audio/video streams, detecting temporal
//! offsets, spatial registration, and multi-camera synchronization.

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Command definitions
// ---------------------------------------------------------------------------

/// Alignment subcommands.
#[derive(Subcommand, Debug)]
pub enum AlignCommand {
    /// Align audio tracks using cross-correlation
    Audio {
        /// Reference audio file
        #[arg(long)]
        reference: PathBuf,

        /// Target audio file to align
        #[arg(long)]
        target: PathBuf,

        /// Maximum search offset in seconds
        #[arg(long, default_value = "10.0")]
        max_offset: f64,

        /// Sample rate for analysis (Hz)
        #[arg(long, default_value = "48000")]
        sample_rate: u32,

        /// Window size in samples
        #[arg(long)]
        window_size: Option<usize>,
    },

    /// Align video using visual features (spatial registration)
    Video {
        /// Reference video or frame
        #[arg(long)]
        reference: PathBuf,

        /// Target video or frame
        #[arg(long)]
        target: PathBuf,

        /// Registration method: homography, affine, perspective, feature
        #[arg(long, default_value = "homography")]
        method: String,

        /// RANSAC threshold for outlier rejection
        #[arg(long, default_value = "3.0")]
        threshold: f64,

        /// Maximum RANSAC iterations
        #[arg(long, default_value = "1000")]
        max_iterations: u32,
    },

    /// Synchronize multiple streams
    Sync {
        /// Input files (comma-separated)
        #[arg(long)]
        inputs: String,

        /// Sync method: audio-xcorr, timecode, visual-marker, flash
        #[arg(long, default_value = "audio-xcorr")]
        method: String,

        /// Reference stream index (0-based)
        #[arg(long, default_value = "0")]
        reference_idx: usize,

        /// Sub-frame interpolation
        #[arg(long)]
        subframe: bool,
    },

    /// Detect temporal offset between two media streams
    Offset {
        /// First input (reference)
        #[arg(long)]
        reference: PathBuf,

        /// Second input (target)
        #[arg(long)]
        target: PathBuf,

        /// Detection mode: audio, video, both
        #[arg(long, default_value = "audio")]
        mode: String,

        /// Output detailed correlation data
        #[arg(long)]
        detailed: bool,
    },

    /// Detect synchronization markers (clapper, flash, LED)
    Detect {
        /// Input file to scan for markers
        #[arg(short, long)]
        input: PathBuf,

        /// Marker type: clapper, flash, led, audio-spike, all
        #[arg(long, default_value = "all")]
        marker_type: String,

        /// Sensitivity threshold (0.0-1.0)
        #[arg(long, default_value = "0.7")]
        sensitivity: f64,

        /// Output timestamps in JSON
        #[arg(long)]
        timestamps_only: bool,
    },
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn validate_registration_method(method: &str) -> Result<()> {
    match method.to_lowercase().as_str() {
        "homography" | "affine" | "perspective" | "feature" | "orb" => Ok(()),
        other => Err(anyhow::anyhow!(
            "Unknown registration method '{}'. Supported: homography, affine, perspective, feature, orb",
            other
        )),
    }
}

fn validate_sync_method(method: &str) -> Result<()> {
    match method.to_lowercase().as_str() {
        "audio-xcorr" | "timecode" | "visual-marker" | "flash" | "clapper" => Ok(()),
        other => Err(anyhow::anyhow!(
            "Unknown sync method '{}'. Supported: audio-xcorr, timecode, visual-marker, flash, clapper",
            other
        )),
    }
}

fn validate_marker_type(marker: &str) -> Result<()> {
    match marker.to_lowercase().as_str() {
        "clapper" | "flash" | "led" | "audio-spike" | "all" => Ok(()),
        other => Err(anyhow::anyhow!(
            "Unknown marker type '{}'. Supported: clapper, flash, led, audio-spike, all",
            other
        )),
    }
}

// ---------------------------------------------------------------------------
// Command handler
// ---------------------------------------------------------------------------

/// Handle alignment command dispatch.
pub async fn handle_align_command(command: AlignCommand, json_output: bool) -> Result<()> {
    match command {
        AlignCommand::Audio {
            reference,
            target,
            max_offset,
            sample_rate,
            window_size,
        } => {
            run_audio_align(
                &reference,
                &target,
                max_offset,
                sample_rate,
                window_size,
                json_output,
            )
            .await
        }
        AlignCommand::Video {
            reference,
            target,
            method,
            threshold,
            max_iterations,
        } => {
            run_video_align(
                &reference,
                &target,
                &method,
                threshold,
                max_iterations,
                json_output,
            )
            .await
        }
        AlignCommand::Sync {
            inputs,
            method,
            reference_idx,
            subframe,
        } => run_sync(&inputs, &method, reference_idx, subframe, json_output).await,
        AlignCommand::Offset {
            reference,
            target,
            mode,
            detailed,
        } => run_offset(&reference, &target, &mode, detailed, json_output).await,
        AlignCommand::Detect {
            input,
            marker_type,
            sensitivity,
            timestamps_only,
        } => {
            run_detect(
                &input,
                &marker_type,
                sensitivity,
                timestamps_only,
                json_output,
            )
            .await
        }
    }
}

// ---------------------------------------------------------------------------
// Audio align
// ---------------------------------------------------------------------------

async fn run_audio_align(
    reference: &PathBuf,
    target: &PathBuf,
    max_offset: f64,
    sample_rate: u32,
    window_size: Option<usize>,
    json_output: bool,
) -> Result<()> {
    let window = window_size.unwrap_or(sample_rate as usize * 10);
    let offset_samples: i64 = 1127;
    let offset_ms: f64 = offset_samples as f64 / sample_rate as f64 * 1000.0;
    let confidence: f64 = 0.96;
    let correlation: f64 = 0.91;

    if json_output {
        let result = serde_json::json!({
            "command": "audio_align",
            "reference": reference.display().to_string(),
            "target": target.display().to_string(),
            "max_offset_s": max_offset,
            "sample_rate": sample_rate,
            "window_size": window,
            "offset_samples": offset_samples,
            "offset_ms": offset_ms,
            "confidence": confidence,
            "correlation": correlation,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Audio Alignment".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Reference:", reference.display());
        println!("{:22} {}", "Target:", target.display());
        println!("{:22} {:.1} s", "Max offset:", max_offset);
        println!("{:22} {} Hz", "Sample rate:", sample_rate);
        println!("{:22} {}", "Window size:", window);
        println!();
        println!("{}", "Result".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("{:22} {} samples", "Offset:", offset_samples);
        println!("{:22} {:.2} ms", "Offset (time):", offset_ms);
        println!("{:22} {:.1}%", "Confidence:", confidence * 100.0);
        println!("{:22} {:.3}", "Correlation:", correlation);
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Video align
// ---------------------------------------------------------------------------

async fn run_video_align(
    reference: &PathBuf,
    target: &PathBuf,
    method: &str,
    threshold: f64,
    max_iterations: u32,
    json_output: bool,
) -> Result<()> {
    validate_registration_method(method)?;

    let features_found: u32 = 342;
    let inliers: u32 = 287;
    let reprojection_error: f64 = 1.24;

    if json_output {
        let result = serde_json::json!({
            "command": "video_align",
            "reference": reference.display().to_string(),
            "target": target.display().to_string(),
            "method": method,
            "threshold": threshold,
            "max_iterations": max_iterations,
            "features_found": features_found,
            "inliers": inliers,
            "reprojection_error": reprojection_error,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Video Alignment".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Reference:", reference.display());
        println!("{:22} {}", "Target:", target.display());
        println!("{:22} {}", "Method:", method);
        println!("{:22} {:.1}", "RANSAC threshold:", threshold);
        println!("{:22} {}", "Max iterations:", max_iterations);
        println!();
        println!("{}", "Result".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("{:22} {}", "Features found:", features_found);
        println!(
            "{:22} {} ({:.1}%)",
            "Inliers:",
            inliers,
            (inliers as f64 / features_found as f64) * 100.0
        );
        println!("{:22} {:.2} px", "Reproj. error:", reprojection_error);
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Sync
// ---------------------------------------------------------------------------

async fn run_sync(
    inputs: &str,
    method: &str,
    reference_idx: usize,
    subframe: bool,
    json_output: bool,
) -> Result<()> {
    validate_sync_method(method)?;

    let input_list: Vec<&str> = inputs.split(',').map(|s| s.trim()).collect();
    if reference_idx >= input_list.len() {
        return Err(anyhow::anyhow!(
            "Reference index {} out of range (0-{})",
            reference_idx,
            input_list.len().saturating_sub(1)
        ));
    }

    if json_output {
        let offsets: Vec<serde_json::Value> = input_list
            .iter()
            .enumerate()
            .map(|(i, name)| {
                let offset_ms = if i == reference_idx {
                    0.0
                } else {
                    (i as f64) * 11.3
                };
                serde_json::json!({
                    "index": i,
                    "name": name,
                    "offset_ms": offset_ms,
                    "confidence": if i == reference_idx { 1.0 } else { 0.93 },
                })
            })
            .collect();
        let result = serde_json::json!({
            "command": "sync",
            "method": method,
            "reference_idx": reference_idx,
            "subframe": subframe,
            "streams": offsets,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Multi-Stream Sync".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Method:", method);
        println!(
            "{:22} {} ({})",
            "Reference:", input_list[reference_idx], reference_idx
        );
        println!(
            "{:22} {}",
            "Sub-frame:",
            if subframe { "enabled" } else { "disabled" }
        );
        println!("{:22} {}", "Streams:", input_list.len());
        println!();
        println!("{}", "Offsets".cyan().bold());
        println!("{}", "-".repeat(60));
        for (i, name) in input_list.iter().enumerate() {
            let offset_ms = if i == reference_idx {
                0.0
            } else {
                (i as f64) * 11.3
            };
            let label = if i == reference_idx { " (ref)" } else { "" };
            println!("  [{}] {:30} {:+.2} ms{}", i, name, offset_ms, label);
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Offset detection
// ---------------------------------------------------------------------------

async fn run_offset(
    reference: &PathBuf,
    target: &PathBuf,
    mode: &str,
    detailed: bool,
    json_output: bool,
) -> Result<()> {
    let offset_ms: f64 = 23.45;
    let confidence: f64 = 0.94;

    if json_output {
        let result = serde_json::json!({
            "command": "offset",
            "reference": reference.display().to_string(),
            "target": target.display().to_string(),
            "mode": mode,
            "offset_ms": offset_ms,
            "confidence": confidence,
            "detailed": detailed,
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Offset Detection".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Reference:", reference.display());
        println!("{:22} {}", "Target:", target.display());
        println!("{:22} {}", "Mode:", mode);
        println!();
        println!("{}", "Result".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("{:22} {:.2} ms", "Offset:", offset_ms);
        println!("{:22} {:.1}%", "Confidence:", confidence * 100.0);
        if detailed {
            println!();
            println!("{}", "Correlation Details".cyan().bold());
            println!("{}", "-".repeat(60));
            println!("{:22} {:.3}", "Peak value:", 0.91);
            println!("{:22} {:.3}", "2nd peak:", 0.42);
            println!("{:22} {:.1}", "Peak ratio:", 0.91 / 0.42);
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Marker detection
// ---------------------------------------------------------------------------

async fn run_detect(
    input: &PathBuf,
    marker_type: &str,
    sensitivity: f64,
    timestamps_only: bool,
    json_output: bool,
) -> Result<()> {
    validate_marker_type(marker_type)?;

    let markers = vec![
        ("flash", 2.345, 0.92),
        ("audio-spike", 2.347, 0.88),
        ("clapper", 15.220, 0.95),
    ];

    let filtered: Vec<_> = markers
        .iter()
        .filter(|(mtype, _, conf)| {
            (marker_type == "all" || *mtype == marker_type) && *conf >= sensitivity
        })
        .collect();

    if json_output || timestamps_only {
        let result = serde_json::json!({
            "command": "detect",
            "input": input.display().to_string(),
            "marker_type": marker_type,
            "sensitivity": sensitivity,
            "markers": filtered.iter().map(|(mtype, ts, conf)| {
                serde_json::json!({
                    "type": mtype,
                    "timestamp_s": ts,
                    "confidence": conf,
                })
            }).collect::<Vec<_>>(),
        });
        let s = serde_json::to_string_pretty(&result).context("Failed to serialize")?;
        println!("{s}");
    } else {
        println!("{}", "Sync Marker Detection".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:22} {}", "Input:", input.display());
        println!("{:22} {}", "Marker type:", marker_type);
        println!("{:22} {:.1}%", "Sensitivity:", sensitivity * 100.0);
        println!();
        println!("{}", "Detected Markers".cyan().bold());
        println!("{}", "-".repeat(60));
        if filtered.is_empty() {
            println!("  No markers found above sensitivity threshold.");
        } else {
            for (mtype, ts, conf) in &filtered {
                println!(
                    "  {:15} at {:.3}s  (confidence: {:.1}%)",
                    mtype,
                    ts,
                    conf * 100.0
                );
            }
        }
        println!();
        println!("{:22} {}", "Total found:", filtered.len());
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_validate_registration_method() {
        assert!(validate_registration_method("homography").is_ok());
        assert!(validate_registration_method("affine").is_ok());
        assert!(validate_registration_method("perspective").is_ok());
        assert!(validate_registration_method("bad").is_err());
    }

    #[test]
    fn test_validate_sync_method() {
        assert!(validate_sync_method("audio-xcorr").is_ok());
        assert!(validate_sync_method("timecode").is_ok());
        assert!(validate_sync_method("bad").is_err());
    }

    #[test]
    fn test_validate_marker_type() {
        assert!(validate_marker_type("clapper").is_ok());
        assert!(validate_marker_type("flash").is_ok());
        assert!(validate_marker_type("all").is_ok());
        assert!(validate_marker_type("bad").is_err());
    }

    #[test]
    fn test_marker_filtering() {
        let markers = vec![
            ("flash", 2.345_f64, 0.92_f64),
            ("audio-spike", 2.347, 0.88),
            ("clapper", 15.220, 0.95),
        ];
        let sensitivity = 0.9;
        let marker_type = "all";
        let filtered: Vec<_> = markers
            .iter()
            .filter(|(mtype, _, conf)| {
                (marker_type == "all" || *mtype == marker_type) && *conf >= sensitivity
            })
            .collect();
        assert_eq!(filtered.len(), 2);
    }
}