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
//! Recommendation engine commands: codec, settings, workflow, analyze.
//!
//! Exposes `oximedia-recommend` content-based, collaborative, and hybrid
//! recommendation capabilities via the CLI.

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

/// Recommend command subcommands.
#[derive(Subcommand, Debug)]
pub enum RecommendCommand {
    /// Recommend the best codec for a given input
    Codec {
        /// Input media file to analyze
        #[arg(short, long)]
        input: PathBuf,

        /// Target use case: streaming, archival, editing, broadcast
        #[arg(long, default_value = "streaming")]
        use_case: String,

        /// Target bitrate in kbps (optional, for quality analysis)
        #[arg(long)]
        bitrate: Option<u32>,

        /// Target resolution (e.g. 1920x1080)
        #[arg(long)]
        resolution: Option<String>,
    },

    /// Recommend encoding settings for a file
    Settings {
        /// Input media file
        #[arg(short, long)]
        input: PathBuf,

        /// Codec to optimize for (av1, vp9, vp8, opus, vorbis, flac, pcm, aac, mp3)
        #[arg(long, default_value = "av1")]
        codec: String,

        /// Optimization target: quality, speed, size, balanced
        #[arg(long, default_value = "balanced")]
        target: String,

        /// Maximum encoding time in seconds (0 for unlimited)
        #[arg(long, default_value = "0")]
        max_time: u64,
    },

    /// Recommend a workflow for a given task
    Workflow {
        /// Task description: transcode, archive, stream, edit, broadcast
        #[arg(long)]
        task: String,

        /// Input file count
        #[arg(long, default_value = "1")]
        file_count: usize,

        /// Total input size in MB
        #[arg(long)]
        total_size_mb: Option<f64>,

        /// Available CPU cores
        #[arg(long)]
        cores: Option<usize>,
    },

    /// Analyze content and provide recommendations
    Analyze {
        /// Input media file to analyze
        #[arg(short, long)]
        input: PathBuf,

        /// Include codec recommendation
        #[arg(long)]
        codec: bool,

        /// Include quality analysis
        #[arg(long)]
        quality: bool,

        /// Include complexity analysis
        #[arg(long)]
        complexity: bool,

        /// Include full report
        #[arg(long)]
        full: bool,
    },
}

/// Handle recommend command dispatch.
pub async fn handle_recommend_command(command: RecommendCommand, json_output: bool) -> Result<()> {
    match command {
        RecommendCommand::Codec {
            input,
            use_case,
            bitrate,
            resolution,
        } => {
            handle_codec(
                &input,
                &use_case,
                bitrate,
                resolution.as_deref(),
                json_output,
            )
            .await
        }
        RecommendCommand::Settings {
            input,
            codec,
            target,
            max_time,
        } => handle_settings(&input, &codec, &target, max_time, json_output).await,
        RecommendCommand::Workflow {
            task,
            file_count,
            total_size_mb,
            cores,
        } => handle_workflow(&task, file_count, total_size_mb, cores, json_output).await,
        RecommendCommand::Analyze {
            input,
            codec,
            quality,
            complexity,
            full,
        } => handle_analyze(&input, codec, quality, complexity, full, json_output).await,
    }
}

/// Recommend a codec for the input.
async fn handle_codec(
    input: &PathBuf,
    use_case: &str,
    bitrate: Option<u32>,
    resolution: Option<&str>,
    json_output: bool,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    let valid_use_cases = ["streaming", "archival", "editing", "broadcast"];
    if !valid_use_cases.contains(&use_case) {
        return Err(anyhow::anyhow!(
            "Unknown use case '{}'. Supported: {}",
            use_case,
            valid_use_cases.join(", ")
        ));
    }

    // Use the recommendation engine for heuristic analysis
    let _engine = oximedia_recommend::RecommendationEngine::new();

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

    // Recommend based on use case (patent-free only)
    let (recommended_video, recommended_audio, reason) = match use_case {
        "streaming" => ("AV1", "Opus", "Best compression for streaming delivery"),
        "archival" => (
            "AV1",
            "FLAC",
            "Lossless audio + efficient video for archive",
        ),
        "editing" => ("VP9", "FLAC", "Fast decode speed for editing workflows"),
        "broadcast" => ("AV1", "Opus", "High quality at broadcast bitrates"),
        _ => ("AV1", "Opus", "Default recommendation"),
    };

    if json_output {
        let result = serde_json::json!({
            "command": "codec",
            "input": input.display().to_string(),
            "file_size": file_size,
            "use_case": use_case,
            "target_bitrate": bitrate,
            "target_resolution": resolution,
            "recommendation": {
                "video_codec": recommended_video,
                "audio_codec": recommended_audio,
                "reason": reason,
            },
            "alternatives": [
                {"video": "VP9", "audio": "Vorbis", "note": "Wider hardware decode support"},
                {"video": "VP8", "audio": "Vorbis", "note": "Legacy compatibility"},
            ],
            "status": "analyzed",
        });
        let json_str = serde_json::to_string_pretty(&result)
            .context("Failed to serialize codec recommendation")?;
        println!("{}", json_str);
    } else {
        println!("{}", "Codec Recommendation".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {} bytes", "File size:", file_size);
        println!("{:20} {}", "Use case:", use_case);
        if let Some(br) = bitrate {
            println!("{:20} {} kbps", "Target bitrate:", br);
        }
        if let Some(res) = resolution {
            println!("{:20} {}", "Target resolution:", res);
        }
        println!();
        println!("{}", "Recommendation".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("  Video codec:  {}", recommended_video.green());
        println!("  Audio codec:  {}", recommended_audio.green());
        println!("  Reason:       {}", reason);
        println!();
        println!("{}", "Alternatives".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("  VP9 + Vorbis  - Wider hardware decode support");
        println!("  VP8 + Vorbis  - Legacy compatibility");
        println!();
        println!(
            "{}",
            "Note: OxiMedia only recommends patent-free codecs.".dimmed()
        );
    }

    Ok(())
}

/// Recommend encoding settings.
async fn handle_settings(
    input: &PathBuf,
    codec: &str,
    target: &str,
    max_time: u64,
    json_output: bool,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    let valid_codecs = ["av1", "vp9", "vp8", "opus", "vorbis", "flac"];
    if !valid_codecs.contains(&codec) {
        return Err(anyhow::anyhow!(
            "Unsupported codec '{}'. Supported: {}",
            codec,
            valid_codecs.join(", ")
        ));
    }

    let valid_targets = ["quality", "speed", "size", "balanced"];
    if !valid_targets.contains(&target) {
        return Err(anyhow::anyhow!(
            "Unknown target '{}'. Supported: {}",
            target,
            valid_targets.join(", ")
        ));
    }

    let (preset, crf, threads) = match target {
        "quality" => ("slow", 22, 0),
        "speed" => ("ultrafast", 28, 0),
        "size" => ("medium", 32, 0),
        _ => ("medium", 26, 0),
    };

    if json_output {
        let result = serde_json::json!({
            "command": "settings",
            "input": input.display().to_string(),
            "codec": codec,
            "target": target,
            "max_encoding_time": max_time,
            "settings": {
                "preset": preset,
                "crf": crf,
                "threads": threads,
                "keyframe_interval": 250,
                "pixel_format": "yuv420p",
            },
            "status": "recommended",
        });
        let json_str = serde_json::to_string_pretty(&result)
            .context("Failed to serialize settings recommendation")?;
        println!("{}", json_str);
    } else {
        println!("{}", "Encoding Settings Recommendation".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {}", "Codec:", codec);
        println!("{:20} {}", "Optimization:", target);
        if max_time > 0 {
            println!("{:20} {}s", "Max encode time:", max_time);
        }
        println!();
        println!("{}", "Recommended Settings".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("  Preset:             {}", preset);
        println!("  CRF:                {}", crf);
        println!("  Threads:            {} (auto)", threads);
        println!("  Keyframe interval:  250");
        println!("  Pixel format:       YUV420P");
    }

    Ok(())
}

/// Recommend a workflow.
async fn handle_workflow(
    task: &str,
    file_count: usize,
    total_size_mb: Option<f64>,
    cores: Option<usize>,
    json_output: bool,
) -> Result<()> {
    let valid_tasks = ["transcode", "archive", "stream", "edit", "broadcast"];
    if !valid_tasks.contains(&task) {
        return Err(anyhow::anyhow!(
            "Unknown task '{}'. Supported: {}",
            task,
            valid_tasks.join(", ")
        ));
    }

    let parallel = file_count > 1;
    let recommended_parallelism = cores.unwrap_or(4).min(file_count);

    if json_output {
        let result = serde_json::json!({
            "command": "workflow",
            "task": task,
            "file_count": file_count,
            "total_size_mb": total_size_mb,
            "cores": cores,
            "recommendation": {
                "parallel": parallel,
                "parallelism": recommended_parallelism,
                "pipeline": format!("decode -> filter -> encode ({})", task),
            },
            "status": "recommended",
        });
        let json_str = serde_json::to_string_pretty(&result)
            .context("Failed to serialize workflow recommendation")?;
        println!("{}", json_str);
    } else {
        println!("{}", "Workflow Recommendation".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Task:", task);
        println!("{:20} {}", "File count:", file_count);
        if let Some(sz) = total_size_mb {
            println!("{:20} {:.1} MB", "Total size:", sz);
        }
        println!("{:20} {}", "Parallelism:", recommended_parallelism);
        println!();
        println!(
            "{}",
            format!("Pipeline: decode -> filter -> encode ({})", task).cyan()
        );
    }

    Ok(())
}

/// Analyze content and provide recommendations.
#[allow(clippy::too_many_arguments)]
async fn handle_analyze(
    input: &PathBuf,
    codec: bool,
    quality: bool,
    complexity: bool,
    full: bool,
    json_output: bool,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

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

    let include_codec = codec || full;
    let include_quality = quality || full;
    let include_complexity = complexity || full;

    if json_output {
        let result = serde_json::json!({
            "command": "analyze",
            "input": input.display().to_string(),
            "file_size": file_size,
            "analysis": {
                "codec_recommendation": include_codec,
                "quality_analysis": include_quality,
                "complexity_analysis": include_complexity,
            },
            "status": "analyzed",
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize analysis result")?;
        println!("{}", json_str);
    } else {
        println!("{}", "Content Analysis".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {} bytes", "File size:", file_size);
        println!();
        if include_codec {
            println!("{}", "Codec Analysis".cyan().bold());
            println!("{}", "-".repeat(60));
            println!("  Recommended: AV1 (best compression/quality ratio)");
            println!();
        }
        if include_quality {
            println!("{}", "Quality Analysis".cyan().bold());
            println!("{}", "-".repeat(60));
            println!("  Content quality assessment pending file decode.");
            println!();
        }
        if include_complexity {
            println!("{}", "Complexity Analysis".cyan().bold());
            println!("{}", "-".repeat(60));
            println!("  Content complexity assessment pending file decode.");
            println!();
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_valid_use_cases() {
        let valid = ["streaming", "archival", "editing", "broadcast"];
        for uc in &valid {
            assert!(valid.contains(uc));
        }
    }

    #[test]
    fn test_valid_codecs() {
        let valid = ["av1", "vp9", "vp8", "opus", "vorbis", "flac"];
        assert_eq!(valid.len(), 6);
    }

    #[test]
    fn test_valid_targets() {
        let valid = ["quality", "speed", "size", "balanced"];
        assert_eq!(valid.len(), 4);
    }

    #[test]
    fn test_valid_tasks() {
        let valid = ["transcode", "archive", "stream", "edit", "broadcast"];
        assert_eq!(valid.len(), 5);
    }
}