oximedia-cli 0.1.8

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
//! Gaming capture, clipping, and overlay commands.
//!
//! Provides game capture configuration, highlight clip creation,
//! and webcam/stats overlay compositing via `oximedia-gaming`.

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

/// Gaming command subcommands.
#[derive(Subcommand, Debug)]
pub enum GamingCommand {
    /// Configure game capture settings
    Capture {
        /// Output file path
        #[arg(short, long)]
        output: PathBuf,

        /// Capture width in pixels
        #[arg(long, default_value = "1920")]
        width: u32,

        /// Capture height in pixels
        #[arg(long, default_value = "1080")]
        height: u32,

        /// Target framerate
        #[arg(long, default_value = "60")]
        fps: u32,

        /// Video codec (av1, vp9, vp8)
        #[arg(long, default_value = "av1")]
        codec: String,

        /// Bitrate in kbps
        #[arg(long)]
        bitrate: Option<u32>,

        /// Recording duration in seconds (omit for unlimited)
        #[arg(long)]
        duration: Option<f64>,

        /// Container format (webm, mkv)
        #[arg(long, default_value = "webm")]
        format: String,
    },

    /// Create a highlight clip from a recording
    Clip {
        /// Input recording file
        #[arg(short, long)]
        input: PathBuf,

        /// Output clip file
        #[arg(short, long)]
        output: PathBuf,

        /// Clip start time in seconds
        #[arg(long)]
        start: f64,

        /// Clip end time in seconds
        #[arg(long)]
        end: f64,

        /// Boost highlight intensity (auto color grade)
        #[arg(long)]
        highlight_boost: bool,

        /// Apply slow-motion factor (e.g. 0.5 for half speed)
        #[arg(long)]
        slow_motion: Option<f64>,
    },

    /// Apply gaming overlay (webcam, stats, chat)
    Overlay {
        /// Input video file
        #[arg(short, long)]
        input: PathBuf,

        /// Output file with overlay applied
        #[arg(short, long)]
        output: PathBuf,

        /// Webcam footage file for picture-in-picture
        #[arg(long)]
        webcam: Option<PathBuf>,

        /// Webcam PiP position: top-left, top-right, bottom-left, bottom-right
        #[arg(long)]
        webcam_position: Option<String>,

        /// Webcam size as percentage of main video (1-100)
        #[arg(long)]
        webcam_size: Option<u32>,

        /// Show performance stats bar overlay
        #[arg(long)]
        stats_bar: bool,

        /// Corner radius for webcam PiP (pixels)
        #[arg(long)]
        border_radius: Option<u32>,
    },
}

/// Handle gaming command dispatch.
pub async fn handle_gaming_command(command: GamingCommand, json_output: bool) -> Result<()> {
    match command {
        GamingCommand::Capture {
            output,
            width,
            height,
            fps,
            codec,
            bitrate,
            duration,
            format,
        } => {
            handle_capture(
                &output,
                width,
                height,
                fps,
                &codec,
                bitrate,
                duration,
                &format,
                json_output,
            )
            .await
        }
        GamingCommand::Clip {
            input,
            output,
            start,
            end,
            highlight_boost,
            slow_motion,
        } => {
            handle_clip(
                &input,
                &output,
                start,
                end,
                highlight_boost,
                slow_motion,
                json_output,
            )
            .await
        }
        GamingCommand::Overlay {
            input,
            output,
            webcam,
            webcam_position,
            webcam_size,
            stats_bar,
            border_radius,
        } => {
            handle_overlay(
                &input,
                &output,
                webcam.as_deref(),
                webcam_position.as_deref(),
                webcam_size,
                stats_bar,
                border_radius,
                json_output,
            )
            .await
        }
    }
}

/// Configure and validate game capture settings.
#[allow(clippy::too_many_arguments)]
async fn handle_capture(
    output: &PathBuf,
    width: u32,
    height: u32,
    fps: u32,
    codec: &str,
    bitrate: Option<u32>,
    duration: Option<f64>,
    format: &str,
    json_output: bool,
) -> Result<()> {
    // Validate codec is patent-free
    let valid_codecs = ["av1", "vp9", "vp8"];
    if !valid_codecs.contains(&codec) {
        return Err(anyhow::anyhow!(
            "Unsupported codec '{}'. OxiMedia only supports patent-free codecs: {}",
            codec,
            valid_codecs.join(", ")
        ));
    }

    // Validate format
    let valid_formats = ["webm", "mkv"];
    if !valid_formats.contains(&format) {
        return Err(anyhow::anyhow!(
            "Unsupported format '{}'. Supported: {}",
            format,
            valid_formats.join(", ")
        ));
    }

    // Validate resolution
    if width == 0 || height == 0 || width > 7680 || height > 4320 {
        return Err(anyhow::anyhow!(
            "Invalid resolution {}x{}. Must be between 1x1 and 7680x4320",
            width,
            height
        ));
    }

    // Validate fps
    if fps == 0 || fps > 240 {
        return Err(anyhow::anyhow!(
            "Invalid framerate {}. Must be between 1 and 240",
            fps
        ));
    }

    // Build StreamConfig using the gaming crate
    let mut builder = oximedia_gaming::StreamConfig::builder()
        .source(oximedia_gaming::CaptureSource::PrimaryMonitor)
        .resolution(width, height)
        .framerate(fps);

    if let Some(br) = bitrate {
        builder = builder.bitrate(br);
    }

    let _config = builder
        .build()
        .map_err(|e| anyhow::anyhow!("Invalid capture configuration: {}", e))?;

    // Compute recommended bitrate if not specified
    let effective_bitrate = bitrate.unwrap_or_else(|| recommend_bitrate(width, height, fps));

    if json_output {
        let result = serde_json::json!({
            "command": "capture",
            "output": output.display().to_string(),
            "resolution": format!("{}x{}", width, height),
            "fps": fps,
            "codec": codec,
            "bitrate_kbps": effective_bitrate,
            "duration": duration,
            "format": format,
            "status": "configured",
            "message": "Capture pipeline configured; awaiting system capture integration",
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize capture config")?;
        println!("{}", json_str);
    } else {
        println!("{}", "Game Capture Configuration".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Output:", output.display());
        println!("{:20} {}x{}", "Resolution:", width, height);
        println!("{:20} {} fps", "Framerate:", fps);
        println!("{:20} {}", "Codec:", codec);
        println!("{:20} {} kbps", "Bitrate:", effective_bitrate);
        println!("{:20} {}", "Format:", format);
        if let Some(dur) = duration {
            println!("{:20} {:.1}s", "Duration:", dur);
        } else {
            println!("{:20} unlimited", "Duration:");
        }
        println!();

        println!("{}", "Encoder Recommendations".cyan().bold());
        println!("{}", "-".repeat(60));
        let preset = recommend_preset(fps);
        println!("  Encoder preset:   {}", preset);
        println!("  Keyframe interval: {} frames ({:.1}s)", fps * 2, 2.0);
        println!("  Pixel format:     YUV420P (BT.709)");
        println!();

        println!(
            "{}",
            "Note: System screen capture integration pending.".yellow()
        );
        println!(
            "{}",
            "Capture pipeline configured and ready for frame input.".dimmed()
        );
    }

    Ok(())
}

/// Create a highlight clip from a recording.
#[allow(clippy::too_many_arguments)]
async fn handle_clip(
    input: &PathBuf,
    output: &PathBuf,
    start: f64,
    end: f64,
    highlight_boost: bool,
    slow_motion: Option<f64>,
    json_output: bool,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    if start >= end {
        return Err(anyhow::anyhow!(
            "Start time ({:.3}s) must be before end time ({:.3}s)",
            start,
            end
        ));
    }

    if start < 0.0 {
        return Err(anyhow::anyhow!(
            "Start time must be non-negative, got {:.3}s",
            start
        ));
    }

    if let Some(sm) = slow_motion {
        if sm <= 0.0 || sm > 1.0 {
            return Err(anyhow::anyhow!(
                "Slow motion factor must be between 0.0 (exclusive) and 1.0, got {:.3}",
                sm
            ));
        }
    }

    let clip_duration = end - start;
    let effective_duration = if let Some(sm) = slow_motion {
        clip_duration / sm
    } else {
        clip_duration
    };

    let file_size = std::fs::metadata(input)
        .context("Failed to read file metadata")?
        .len();

    if json_output {
        let result = serde_json::json!({
            "command": "clip",
            "input": input.display().to_string(),
            "output": output.display().to_string(),
            "start_time": start,
            "end_time": end,
            "clip_duration": clip_duration,
            "effective_duration": effective_duration,
            "highlight_boost": highlight_boost,
            "slow_motion": slow_motion,
            "input_file_size": file_size,
            "status": "configured",
            "message": "Clip extraction configured; awaiting frame decoding pipeline",
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize clip config")?;
        println!("{}", json_str);
    } else {
        println!("{}", "Highlight Clip Creation".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {}", "Output:", output.display());
        println!("{:20} {} bytes", "Input size:", file_size);
        println!();

        println!("{}", "Clip Settings".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("{:20} {:.3}s", "Start time:", start);
        println!("{:20} {:.3}s", "End time:", end);
        println!("{:20} {:.3}s", "Clip duration:", clip_duration);
        if let Some(sm) = slow_motion {
            println!("{:20} {:.2}x", "Slow motion:", sm);
            println!("{:20} {:.3}s", "Output duration:", effective_duration);
        }
        println!("{:20} {}", "Highlight boost:", highlight_boost);
        println!();

        println!(
            "{}",
            "Note: Clip extraction requires frame decoding pipeline.".yellow()
        );
        println!(
            "{}",
            "Clip configuration validated and ready for processing.".dimmed()
        );
    }

    Ok(())
}

/// Apply gaming overlay to a video.
#[allow(clippy::too_many_arguments)]
async fn handle_overlay(
    input: &PathBuf,
    output: &PathBuf,
    webcam: Option<&std::path::Path>,
    webcam_position: Option<&str>,
    webcam_size: Option<u32>,
    stats_bar: bool,
    border_radius: Option<u32>,
    json_output: bool,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    if let Some(wc) = webcam {
        if !wc.exists() {
            return Err(anyhow::anyhow!("Webcam file not found: {}", wc.display()));
        }
    }

    // Validate webcam position
    let valid_positions = ["top-left", "top-right", "bottom-left", "bottom-right"];
    if let Some(pos) = webcam_position {
        if !valid_positions.contains(&pos) {
            return Err(anyhow::anyhow!(
                "Invalid webcam position '{}'. Supported: {}",
                pos,
                valid_positions.join(", ")
            ));
        }
    }

    // Validate webcam size
    if let Some(size) = webcam_size {
        if size == 0 || size > 100 {
            return Err(anyhow::anyhow!(
                "Webcam size must be between 1 and 100 (percent), got {}",
                size
            ));
        }
    }

    let file_size = std::fs::metadata(input)
        .context("Failed to read file metadata")?
        .len();

    let pos = webcam_position.unwrap_or("bottom-right");
    let size = webcam_size.unwrap_or(25);

    if json_output {
        let result = serde_json::json!({
            "command": "overlay",
            "input": input.display().to_string(),
            "output": output.display().to_string(),
            "input_file_size": file_size,
            "webcam": webcam.map(|p| p.display().to_string()),
            "webcam_position": pos,
            "webcam_size_percent": size,
            "stats_bar": stats_bar,
            "border_radius": border_radius,
            "status": "configured",
            "message": "Overlay pipeline configured; awaiting frame decoding integration",
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize overlay config")?;
        println!("{}", json_str);
    } else {
        println!("{}", "Gaming Overlay".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {}", "Output:", output.display());
        println!("{:20} {} bytes", "File size:", file_size);
        println!();

        println!("{}", "Overlay Layers".cyan().bold());
        println!("{}", "-".repeat(60));

        if let Some(wc) = webcam {
            println!("  [+] Webcam PiP:     {}", wc.display());
            println!("      Position:       {}", pos);
            println!("      Size:           {}% of main video", size);
            if let Some(br) = border_radius {
                println!("      Border radius:  {}px", br);
            }
        } else {
            println!("  [-] Webcam PiP:     not configured");
        }

        if stats_bar {
            println!("  [+] Stats bar:      enabled (FPS, bitrate, encoding latency)");
        } else {
            println!("  [-] Stats bar:      disabled");
        }

        println!();
        println!(
            "{}",
            "Note: Overlay compositing requires frame decoding pipeline.".yellow()
        );
        println!(
            "{}",
            "Overlay configuration validated and ready for compositing.".dimmed()
        );
    }

    Ok(())
}

/// Recommend a bitrate based on resolution and framerate.
fn recommend_bitrate(width: u32, height: u32, fps: u32) -> u32 {
    let pixels = (width as u64) * (height as u64);
    let base = match pixels {
        0..=921_600 => 2500,            // up to 720p
        921_601..=2_073_600 => 6000,    // up to 1080p
        2_073_601..=3_686_400 => 12000, // up to 1440p
        _ => 20000,                     // 4K+
    };
    // Scale for high framerate
    if fps > 60 {
        base * 3 / 2
    } else {
        base
    }
}

/// Recommend an encoder preset based on framerate.
fn recommend_preset(fps: u32) -> &'static str {
    if fps >= 120 {
        "ultra-low-latency"
    } else if fps >= 60 {
        "low-latency"
    } else {
        "balanced"
    }
}

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

    #[test]
    fn test_recommend_bitrate_720p() {
        let br = recommend_bitrate(1280, 720, 60);
        assert_eq!(br, 2500);
    }

    #[test]
    fn test_recommend_bitrate_1080p() {
        let br = recommend_bitrate(1920, 1080, 60);
        assert_eq!(br, 6000);
    }

    #[test]
    fn test_recommend_bitrate_4k_high_fps() {
        let br = recommend_bitrate(3840, 2160, 120);
        assert_eq!(br, 30000);
    }

    #[test]
    fn test_recommend_preset_values() {
        assert_eq!(recommend_preset(30), "balanced");
        assert_eq!(recommend_preset(60), "low-latency");
        assert_eq!(recommend_preset(144), "ultra-low-latency");
    }

    #[test]
    fn test_recommend_bitrate_1440p() {
        let br = recommend_bitrate(2560, 1440, 60);
        assert_eq!(br, 12000);
    }
}