oximedia-cli 0.1.5

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
//! Custom preset loading and saving.
//!
//! Handles loading presets from TOML files and saving user-created presets.

use super::{validate, AudioConfig, FilterConfig, Preset, PresetCategory, VideoConfig};
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

/// TOML preset file format.
#[derive(Debug, Serialize, Deserialize)]
struct PresetFile {
    preset: PresetMetadata,
    video: VideoConfig,
    audio: AudioConfig,
    #[serde(skip_serializing_if = "Option::is_none")]
    filters: Option<FilterConfig>,
}

/// Preset metadata section.
#[derive(Debug, Serialize, Deserialize)]
struct PresetMetadata {
    name: String,
    description: String,
    #[serde(default = "default_category")]
    category: String,
    container: String,
    #[serde(default)]
    tags: Vec<String>,
}

fn default_category() -> String {
    "custom".to_string()
}

/// Load a preset from a TOML file.
pub fn load_preset_from_file<P: AsRef<Path>>(path: P) -> Result<Preset> {
    let content = fs::read_to_string(path.as_ref()).context(format!(
        "Failed to read preset file: {}",
        path.as_ref().display()
    ))?;

    let preset_file: PresetFile =
        toml::from_str(&content).context("Failed to parse TOML preset file")?;

    // Convert to internal Preset structure
    let category =
        PresetCategory::from_str(&preset_file.preset.category).unwrap_or(PresetCategory::Custom);

    let preset = Preset {
        name: preset_file.preset.name,
        description: preset_file.preset.description,
        category,
        video: preset_file.video,
        audio: preset_file.audio,
        container: preset_file.preset.container,
        filters: preset_file.filters,
        builtin: false,
        tags: preset_file.preset.tags,
    };

    // Validate preset
    validate::validate_preset(&preset).context("Preset validation failed")?;

    Ok(preset)
}

/// Save a preset to a TOML file.
pub fn save_preset_to_file<P: AsRef<Path>>(preset: &Preset, dir: P) -> Result<()> {
    let path = dir.as_ref().join(format!("{}.toml", preset.name));

    let preset_file = PresetFile {
        preset: PresetMetadata {
            name: preset.name.clone(),
            description: preset.description.clone(),
            category: preset.category.name().to_lowercase(),
            container: preset.container.clone(),
            tags: preset.tags.clone(),
        },
        video: preset.video.clone(),
        audio: preset.audio.clone(),
        filters: preset.filters.clone(),
    };

    let toml_string =
        toml::to_string_pretty(&preset_file).context("Failed to serialize preset to TOML")?;

    fs::write(&path, toml_string)
        .context(format!("Failed to write preset file: {}", path.display()))?;

    Ok(())
}

/// Load all presets from a directory.
#[allow(dead_code)]
pub fn load_presets_from_dir<P: AsRef<Path>>(dir: P) -> Result<Vec<Preset>> {
    let mut presets = Vec::new();

    if !dir.as_ref().exists() {
        return Ok(presets);
    }

    for entry in fs::read_dir(dir.as_ref())? {
        let entry = entry?;
        let path = entry.path();

        if path.extension().and_then(|s| s.to_str()) == Some("toml") {
            match load_preset_from_file(&path) {
                Ok(preset) => presets.push(preset),
                Err(e) => {
                    eprintln!(
                        "Warning: Failed to load preset from {}: {}",
                        path.display(),
                        e
                    );
                }
            }
        }
    }

    Ok(presets)
}

/// Create a new custom preset interactively.
pub fn create_preset_interactive() -> Result<Preset> {
    use std::io::{self, Write};

    println!("Creating a new custom preset...");
    println!();

    // Get name
    print!("Preset name (alphanumeric, hyphens, underscores): ");
    io::stdout().flush()?;
    let mut name = String::new();
    io::stdin().read_line(&mut name)?;
    let name = name.trim().to_string();

    // Get description
    print!("Description: ");
    io::stdout().flush()?;
    let mut description = String::new();
    io::stdin().read_line(&mut description)?;
    let description = description.trim().to_string();

    // Get category
    println!("Category (web, device, quality, archival, streaming, custom): ");
    print!("> ");
    io::stdout().flush()?;
    let mut category_str = String::new();
    io::stdin().read_line(&mut category_str)?;
    let category = PresetCategory::from_str(category_str.trim()).unwrap_or(PresetCategory::Custom);

    // Get video codec
    print!("Video codec (av1, vp9, vp8): ");
    io::stdout().flush()?;
    let mut video_codec = String::new();
    io::stdin().read_line(&mut video_codec)?;
    let video_codec = video_codec.trim().to_string();

    // Get video bitrate or CRF
    print!("Video bitrate (e.g., '5M') or press Enter for CRF mode: ");
    io::stdout().flush()?;
    let mut video_bitrate_str = String::new();
    io::stdin().read_line(&mut video_bitrate_str)?;
    let video_bitrate_str = video_bitrate_str.trim();

    let (video_bitrate, crf) = if video_bitrate_str.is_empty() {
        print!("CRF value (0-63 for VP9/VP8, 0-255 for AV1): ");
        io::stdout().flush()?;
        let mut crf_str = String::new();
        io::stdin().read_line(&mut crf_str)?;
        let crf: u32 = crf_str.trim().parse().unwrap_or(31);
        (None, Some(crf))
    } else {
        (Some(video_bitrate_str.to_string()), None)
    };

    // Get resolution
    print!("Video width (pixels, or press Enter to skip): ");
    io::stdout().flush()?;
    let mut width_str = String::new();
    io::stdin().read_line(&mut width_str)?;
    let width = if width_str.trim().is_empty() {
        None
    } else {
        Some(width_str.trim().parse().unwrap_or(1920))
    };

    print!("Video height (pixels, or press Enter to skip): ");
    io::stdout().flush()?;
    let mut height_str = String::new();
    io::stdin().read_line(&mut height_str)?;
    let height = if height_str.trim().is_empty() {
        None
    } else {
        Some(height_str.trim().parse().unwrap_or(1080))
    };

    // Get frame rate
    print!("Frame rate (e.g., '30', or press Enter to skip): ");
    io::stdout().flush()?;
    let mut fps_str = String::new();
    io::stdin().read_line(&mut fps_str)?;
    let fps = if fps_str.trim().is_empty() {
        None
    } else {
        Some(fps_str.trim().parse().unwrap_or(30.0))
    };

    // Get audio codec
    print!("Audio codec (opus, vorbis, flac, pcm, aac, mp3): ");
    io::stdout().flush()?;
    let mut audio_codec = String::new();
    io::stdin().read_line(&mut audio_codec)?;
    let audio_codec = audio_codec.trim().to_string();

    // Get audio bitrate
    print!("Audio bitrate (e.g., '128k'): ");
    io::stdout().flush()?;
    let mut audio_bitrate = String::new();
    io::stdin().read_line(&mut audio_bitrate)?;
    let audio_bitrate = Some(audio_bitrate.trim().to_string());

    // Get container
    print!("Container format (webm, mkv, ogg): ");
    io::stdout().flush()?;
    let mut container = String::new();
    io::stdin().read_line(&mut container)?;
    let container = container.trim().to_string();

    let preset = Preset {
        name,
        description,
        category,
        video: VideoConfig {
            codec: video_codec,
            bitrate: video_bitrate,
            crf,
            width,
            height,
            fps,
            preset: Some("medium".to_string()),
            pixel_format: Some("yuv420p".to_string()),
            two_pass: false,
            max_bitrate: None,
            min_bitrate: None,
            buffer_size: None,
            keyframe_interval: Some(240),
            min_keyframe_interval: Some(24),
            aspect_ratio: None,
        },
        audio: AudioConfig {
            codec: audio_codec,
            bitrate: audio_bitrate,
            sample_rate: Some(48000),
            channels: Some(2),
            quality: None,
            compression_level: None,
        },
        container,
        filters: None,
        builtin: false,
        tags: vec![],
    };

    // Validate preset
    validate::validate_preset(&preset)?;

    Ok(preset)
}

/// Generate a preset template TOML file.
pub fn generate_template<P: AsRef<Path>>(path: P) -> Result<()> {
    let template = r#"[preset]
name = "my-custom-preset"
description = "My custom transcoding preset"
category = "custom"  # web, device, quality, archival, streaming, custom
container = "webm"   # webm, mkv, ogg, flac, wav
tags = ["custom", "example"]

[video]
codec = "vp9"        # av1, vp9, vp8, theora
# Use either bitrate OR crf (not both)
# bitrate = "5M"     # Target bitrate (e.g., "5M", "2.5M", "500k")
crf = 31             # Constant Rate Factor (0-63 for VP9/VP8, 0-255 for AV1)
width = 1920
height = 1080
fps = 30.0
preset = "medium"    # ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow
pixel_format = "yuv420p"
two_pass = true
# max_bitrate = "7M"
# min_bitrate = "3M"
# buffer_size = "10M"
keyframe_interval = 240
min_keyframe_interval = 24
aspect_ratio = "16:9"

[audio]
codec = "opus"       # opus, vorbis, flac, pcm, aac, mp3
bitrate = "128k"     # Audio bitrate (e.g., "128k", "192k", "256k")
sample_rate = 48000  # 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200, 96000
channels = 2         # 1 (mono), 2 (stereo), 6 (5.1), 8 (7.1)
# quality = 5.0      # Codec-specific quality (0-10)
# compression_level = 8  # For FLAC (0-12)

# Optional filters
# [filters]
# video_filters = ["scale=1920:1080", "fps=30"]
# audio_filters = ["volume=0.5"]
# deinterlace = "yadif"
# denoise = "hqdn3d"
"#;

    fs::write(path.as_ref(), template).context(format!(
        "Failed to write template to {}",
        path.as_ref().display()
    ))?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_load_preset_from_file() {
        let temp_dir = TempDir::new().expect("TempDir::new should succeed");
        let preset_path = temp_dir.path().join("test-preset.toml");

        let toml_content = r#"
[preset]
name = "test-preset"
description = "Test preset"
category = "custom"
container = "webm"
tags = ["test"]

[video]
codec = "vp9"
crf = 31
width = 1920
height = 1080
fps = 30.0
preset = "medium"
pixel_format = "yuv420p"
two_pass = true
keyframe_interval = 240
min_keyframe_interval = 24
aspect_ratio = "16:9"

[audio]
codec = "opus"
bitrate = "128k"
sample_rate = 48000
channels = 2
"#;

        fs::write(&preset_path, toml_content).expect("fs::write should succeed");

        let preset = load_preset_from_file(&preset_path).expect("load should succeed");
        assert_eq!(preset.name, "test-preset");
        assert_eq!(preset.video.codec, "vp9");
        assert_eq!(preset.audio.codec, "opus");
    }

    #[test]
    fn test_save_preset_to_file() {
        let temp_dir = TempDir::new().expect("TempDir::new should succeed");

        let preset = Preset {
            name: "save-test".to_string(),
            description: "Save test preset".to_string(),
            category: PresetCategory::Custom,
            video: VideoConfig {
                codec: "vp9".to_string(),
                bitrate: None,
                crf: Some(31),
                width: Some(1920),
                height: Some(1080),
                fps: Some(30.0),
                preset: Some("medium".to_string()),
                pixel_format: Some("yuv420p".to_string()),
                two_pass: true,
                max_bitrate: None,
                min_bitrate: None,
                buffer_size: None,
                keyframe_interval: Some(240),
                min_keyframe_interval: Some(24),
                aspect_ratio: Some("16:9".to_string()),
            },
            audio: AudioConfig {
                codec: "opus".to_string(),
                bitrate: Some("128k".to_string()),
                sample_rate: Some(48000),
                channels: Some(2),
                quality: None,
                compression_level: None,
            },
            container: "webm".to_string(),
            filters: None,
            builtin: false,
            tags: vec!["test".to_string()],
        };

        save_preset_to_file(&preset, temp_dir.path()).expect("save should succeed");

        let saved_path = temp_dir.path().join("save-test.toml");
        assert!(saved_path.exists());

        let loaded = load_preset_from_file(&saved_path).expect("load should succeed");
        assert_eq!(loaded.name, preset.name);
    }
}