oxframe 0.8.0

Procedural animation in Rust: scenes are coroutines, frames are await points
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
//! Command line of a project binary. `oxframe-cli` drives these commands;
//! they also work directly via `cargo run -- <command>`.

use std::path::PathBuf;
use std::time::Instant;

use oxframe_export::{Audio, ExportOptions, Exporter, Format};
use oxframe_render::{CpuRenderer, Renderer};

use oxframe_core::FrameSource;

use crate::{Aspect, Engine, PreviewAdapter, Project};

pub(crate) const EXIT_OK: i32 = 0;
pub(crate) const EXIT_ERROR: i32 = 1;
pub(crate) const EXIT_ENV: i32 = 2;

const HELP: &str = "\
usage: <project> <command> [options]

commands:
  render   render the project to a video or PNG sequence
             --out <path>        output file or directory (default: out/<project>.mp4)
             --format <f>        mp4 | webm | prores | png (default: from --out extension)
             --scene <name>      render only this scene
             --range <a>..<b>    time range in seconds, e.g. 0.5..2 (either side optional)
             --at <seconds>      only this frame, as PNG (repeatable); with one --at and
                                 --out file.png writes that file, else out/snapshots/
             --size <w>x<h>      output size (default: project size)
             --aspect <shape>    16:9, 9:16, 1:1, or all (every Project::aspects format)
             --cpu               use the CPU renderer (tiny-skia) instead of the GPU
             --ffmpeg <path>     ffmpeg binary (default: from PATH)
             --no-audio          do not mux the project's soundtrack
  check    write out/check/storyboard.png and report.json (CPU, exits 1 on errors)
             --out <dir>         output directory (default: out/check)
             --frames            also write each storyboard frame at full size
             --interval <s>      seconds between storyboard frames, plus every marker
             --safe-inset <px>   safe area; 0 disables that warning
             --min-contrast <n>  WCAG ratio for text (default 4.5)
             --min-motion <s>    large moves shorter than this are too_fast
             --max-still <s>     pause with nothing changing (needs no soundtrack)
             --chars-per-second <n>
             --min-chars <n>     too_fast ignores shorter text
             --cell-width <px>   storyboard cell width (default 320)
  inspect  print the scene tree at a time as JSON
             --at <seconds>      required
             --scene <name>      --at is measured from the start of this scene
             --graph             also list render passes (vector, then composite)
  preview  run the preview server for the editor
             --port <n>          port on 127.0.0.1 (0 = pick a free one; printed on stdout)
  info     print the project timeline as JSON

  --theme <name>     override the project theme: dark, light, midnight, paper, vivid
  --accent <#hex>    rebuild that theme from one brand color
";

pub(crate) fn run(mut project: Project, args: Vec<String>) -> i32 {
    let args = match apply_theme_flags(&mut project, args) {
        Ok(a) => a,
        Err(e) => return usage_error(&e),
    };
    let mut args = args.into_iter();
    match args.next().as_deref() {
        Some("render") => match RenderArgs::parse(args) {
            Ok(a) => render(project, a),
            Err(e) => usage_error(&e),
        },
        Some("check") => crate::feedback::command_check(project, args),
        Some("inspect") => crate::feedback::command_inspect(project, args),
        Some("preview") => crate::preview::run(project, args.collect()),
        Some("info") => info(project),
        Some("-h" | "--help" | "help") | None => {
            print!("{HELP}");
            EXIT_OK
        }
        Some(other) => usage_error(&format!("unknown command `{other}`")),
    }
}

/// `--theme <preset>` and `--accent <#hex>` may appear anywhere. They override
/// the theme compiled into the project (what `oxframe.toml` `[theme]` forwards).
fn apply_theme_flags(project: &mut Project, args: Vec<String>) -> Result<Vec<String>, String> {
    let mut kept = Vec::with_capacity(args.len());
    let mut args = args.into_iter();
    while let Some(flag) = args.next() {
        match flag.as_str() {
            "--theme" => {
                let name = args
                    .next()
                    .ok_or_else(|| "--theme needs a name".to_owned())?;
                project.apply_preset(&name)?;
            }
            "--accent" => {
                let hex = args
                    .next()
                    .ok_or_else(|| "--accent needs a #rrggbb color".to_owned())?;
                project.apply_accent(&hex)?;
            }
            _ => kept.push(flag),
        }
    }
    Ok(kept)
}

pub(crate) fn usage_error(msg: &str) -> i32 {
    eprintln!("error: {msg}\n\n{HELP}");
    EXIT_ENV
}

#[derive(Debug, Default, Clone)]
struct RenderArgs {
    out: Option<PathBuf>,
    format: Option<Format>,
    scene: Option<String>,
    range: Option<(f64, f64)>,
    /// Single frames to write as PNG (seconds); see `snapshot`.
    at: Vec<f64>,
    size: Option<(u32, u32)>,
    /// One shape, or every shape the project declares (`all`).
    aspect: Option<AspectPick>,
    cpu: bool,
    ffmpeg: Option<PathBuf>,
    no_audio: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AspectPick {
    One(Aspect),
    All,
}

impl RenderArgs {
    fn parse(mut args: impl Iterator<Item = String>) -> Result<Self, String> {
        let mut a = Self::default();
        while let Some(flag) = args.next() {
            let mut value = || args.next().ok_or_else(|| format!("{flag} needs a value"));
            match flag.as_str() {
                "--out" => a.out = Some(value()?.into()),
                "--scene" => a.scene = Some(value()?),
                "--format" => {
                    let f = value()?;
                    a.format = Some(Format::parse(&f).ok_or_else(|| {
                        format!("unknown format `{f}`; use mp4, webm, prores or png")
                    })?);
                }
                "--at" => {
                    let v = value()?;
                    a.at.push(
                        v.parse()
                            .map_err(|_| format!("bad time `{v}`, expected seconds"))?,
                    );
                }
                "--range" => {
                    let v = value()?;
                    let (s, e) = v
                        .split_once("..")
                        .ok_or_else(|| format!("bad range `{v}`, expected a..b"))?;
                    let parse = |x: &str, default: f64| {
                        if x.is_empty() {
                            Ok(default)
                        } else {
                            x.parse::<f64>().map_err(|_| format!("bad range `{v}`"))
                        }
                    };
                    a.range = Some((parse(s, 0.0)?, parse(e, f64::INFINITY)?));
                }
                "--size" => {
                    let v = value()?;
                    let (w, h) = v
                        .split_once('x')
                        .and_then(|(w, h)| Some((w.parse().ok()?, h.parse().ok()?)))
                        .ok_or_else(|| format!("bad size `{v}`, expected WxH"))?;
                    a.size = Some((w, h));
                }
                "--aspect" => {
                    let v = value()?;
                    a.aspect =
                        Some(if v == "all" {
                            AspectPick::All
                        } else {
                            AspectPick::One(Aspect::parse(&v).ok_or_else(|| {
                                format!("bad aspect `{v}` (16:9, 9:16, 1:1, or all)")
                            })?)
                        });
                }
                "--cpu" => a.cpu = true,
                "--gpu" => a.cpu = false,
                "--ffmpeg" => a.ffmpeg = Some(value()?.into()),
                "--no-audio" => a.no_audio = true,
                other => return Err(format!("unknown option `{other}`")),
            }
        }
        Ok(a)
    }
}

/// GPU renderer if compiled in and an adapter exists, else CPU.
pub(crate) fn make_renderer(cpu: bool) -> (Box<dyn Renderer>, &'static str) {
    #[cfg(feature = "gpu")]
    if !cpu {
        match oxframe_render::GpuRenderer::new() {
            Ok(r) => return (Box::new(r), "gpu (vello)"),
            Err(e) => eprintln!("warning: {e}; falling back to the CPU renderer"),
        }
    }
    let _ = cpu;
    (Box::new(CpuRenderer::new()), "cpu (tiny-skia)")
}

fn render(project: Project, args: RenderArgs) -> i32 {
    if !args.at.is_empty() {
        return snapshot(project, &args);
    }
    if let Some(jobs) = expand_aspects(&project, &args) {
        if jobs.len() == 1 {
            let mut project = project;
            project.size = jobs[0].1;
            return render_video(project, args);
        }
        let format = args
            .format
            .or_else(|| args.out.as_deref().map(Format::from_path))
            .unwrap_or(Format::Mp4);
        for (aspect, size) in jobs {
            let mut sized = project.clone();
            sized.size = size;
            let mut job = args.clone();
            job.aspect = None;
            job.out = Some(with_tag(
                output_path(&project, &args, format),
                &aspect.label().replace(':', "x"),
            ));
            let code = render_video(sized, job);
            if code != EXIT_OK {
                return code;
            }
        }
        return EXIT_OK;
    }
    render_video(project, args)
}

fn expand_aspects(project: &Project, args: &RenderArgs) -> Option<Vec<(Aspect, (u32, u32))>> {
    if args.size.is_some() {
        return None;
    }
    let pick = args.aspect?;
    let aspects = match pick {
        AspectPick::One(aspect) => vec![aspect],
        AspectPick::All => {
            if project.aspects.is_empty() {
                vec![Aspect::Landscape, Aspect::Portrait, Aspect::Square]
            } else {
                project.aspects.clone()
            }
        }
    };
    Some(
        aspects
            .into_iter()
            .map(|aspect| {
                let size = aspect.frame(project.size);
                (aspect, size)
            })
            .collect(),
    )
}

fn output_path(project: &Project, args: &RenderArgs, format: Format) -> PathBuf {
    args.out.clone().unwrap_or_else(|| {
        let base = PathBuf::from("out").join(project.name());
        match format {
            Format::Png => base,
            f => base.with_extension(f.extension()),
        }
    })
}

fn with_tag(path: PathBuf, tag: &str) -> PathBuf {
    let name = path.file_name().and_then(|s| s.to_str()).unwrap_or("out");
    let tagged = match path.extension().and_then(|e| e.to_str()) {
        Some(ext) => {
            let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or(name);
            format!("{stem}-{tag}.{ext}")
        }
        None => format!("{name}-{tag}"),
    };
    path.with_file_name(tagged)
}

fn render_video(project: Project, args: RenderArgs) -> i32 {
    let format = args
        .format
        .or_else(|| args.out.as_deref().map(Format::from_path))
        .unwrap_or(Format::Mp4);
    let out = args.out.clone().unwrap_or_else(|| {
        let base = PathBuf::from("out").join(project.name());
        match format {
            Format::Png => base,
            f => base.with_extension(f.extension()),
        }
    });
    if format != Format::Png
        && let Err(e) = oxframe_export::find_ffmpeg(args.ffmpeg.as_deref())
    {
        eprintln!("error: {e}");
        return EXIT_ENV;
    }

    let mut engine = match Engine::new(project) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("error: {e}");
            return EXIT_ERROR;
        }
    };
    // One scene, on its own: no blend with the scene before it.
    if args.scene.is_some() {
        engine.skip_transitions(true);
    }
    let fps = engine.fps();
    let mut frames = 0..engine.total_frames();
    if let Some(name) = &args.scene {
        let Some(s) = engine.scenes().into_iter().find(|s| &s.name == name) else {
            eprintln!("error: no scene named `{name}`");
            return EXIT_ERROR;
        };
        frames = s.start..s.start + s.frames;
    }
    if let Some((a, b)) = args.range {
        let first = frames.start + (a * f64::from(fps)).round() as u32;
        let last = if b.is_finite() {
            frames.start + (b * f64::from(fps)).round() as u32
        } else {
            frames.end
        };
        frames = first.max(frames.start)..last.min(frames.end);
    }
    if frames.is_empty() {
        eprintln!("error: nothing to render (empty range)");
        return EXIT_ERROR;
    }

    let size = args.size.unwrap_or(engine.size());
    let audio = (!args.no_audio && format != Format::Png)
        .then(|| engine.audio())
        .flatten()
        .map(|(file, offset)| Audio {
            file: file.into(),
            // The video starts at `frames.start`, so shift the audio too.
            offset: offset - f64::from(frames.start) / f64::from(fps),
        });
    let opts = ExportOptions {
        out: out.clone(),
        format,
        width: size.0,
        height: size.1,
        fps,
        audio,
        ffmpeg: args.ffmpeg.clone(),
    };
    let mut exporter = match Exporter::start(&opts) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("error: {e}");
            return EXIT_ENV;
        }
    };
    let (renderer, backend) = make_renderer(args.cpu);
    // From here on frames come through the generic `FrameSource`.
    let mut source = PreviewAdapter::new(engine, renderer);
    eprintln!(
        "rendering {} frames at {}x{} @ {fps} fps with {backend} → {}",
        frames.len(),
        size.0,
        size.1,
        out.display()
    );

    let started = Instant::now();
    let count = frames.len();
    let mut last_report = 0;
    let (mut t_render, mut t_queue) = (0.0, 0.0);
    for (n, frame) in frames.enumerate() {
        let t0 = Instant::now();
        let image = source.render(frame, size);
        let t1 = Instant::now();
        t_render += (t1 - t0).as_secs_f64();
        let pushed = exporter.push(image);
        t_queue += t1.elapsed().as_secs_f64();
        if let Err(e) = pushed {
            eprintln!("error: {e}");
            return EXIT_ENV;
        }
        let pct = (n + 1) * 100 / count;
        if pct >= last_report + 10 || n + 1 == count {
            last_report = pct - pct % 10;
            eprintln!("  {pct:3}%  frame {}/{count}", n + 1);
        }
    }
    if let Err(e) = exporter.finish() {
        eprintln!("error: {e}");
        return EXIT_ENV;
    }
    let secs = started.elapsed().as_secs_f64();
    eprintln!(
        "done in {secs:.2}s ({:.1} frames/s; per frame: simulate+render {:.1} ms, waiting for encoder {:.1} ms): {}",
        count as f64 / secs.max(1e-9),
        1000.0 * t_render / count as f64,
        1000.0 * t_queue / count as f64,
        out.display()
    );
    EXIT_OK
}

fn info(project: Project) -> i32 {
    let engine = match Engine::new(project) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("error: {e}");
            return EXIT_ERROR;
        }
    };
    let json = serde_json::json!({
        "protocol_version": oxframe_protocol::PROTOCOL_VERSION,
        "engine_version": env!("CARGO_PKG_VERSION"),
        "fps": engine.fps(),
        "size": [engine.size().0, engine.size().1],
        "frames": engine.total_frames(),
        "scenes": engine.scenes(),
        "markers": engine.markers(),
    });
    println!("{}", serde_json::to_string_pretty(&json).expect("json"));
    EXIT_OK
}

/// `render --at <s> [--at <s> …]`: single frames as PNG, no ffmpeg. One
/// time and an `--out` ending in `.png` writes exactly that file; otherwise
/// files `<project>-<time>.png` go into `--out` (default `out/snapshots`).
fn snapshot(project: Project, args: &RenderArgs) -> i32 {
    let name = project.name().to_owned();
    let engine = match Engine::new(project) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("error: {e}");
            return EXIT_ERROR;
        }
    };
    let fps = f64::from(engine.fps());
    let offset = match &args.scene {
        Some(scene) => match engine.scenes().into_iter().find(|s| &s.name == scene) {
            Some(s) => s.start,
            None => {
                eprintln!("error: no scene named `{scene}`");
                return EXIT_ERROR;
            }
        },
        None => 0,
    };
    let single_file = args.at.len() == 1
        && args
            .out
            .as_ref()
            .is_some_and(|o| o.extension().is_some_and(|e| e.eq_ignore_ascii_case("png")));
    let dir = if single_file {
        None
    } else {
        Some(
            args.out
                .clone()
                .unwrap_or_else(|| PathBuf::from("out/snapshots")),
        )
    };
    if let Some(d) = &dir
        && let Err(e) = std::fs::create_dir_all(d)
    {
        eprintln!("error: cannot create {}: {e}", d.display());
        return EXIT_ENV;
    }
    let size = args.size.unwrap_or(engine.size());
    let (renderer, _) = make_renderer(args.cpu);
    let mut source = PreviewAdapter::new(engine, renderer);
    let last = source.total_frames().saturating_sub(1);
    for &t in &args.at {
        let frame = (offset + (t.max(0.0) * fps).round() as u32).min(last);
        let path = match &dir {
            Some(d) => d.join(format!("{name}-{t:.2}.png")),
            None => args.out.clone().expect("single file has --out"),
        };
        if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
            let _ = std::fs::create_dir_all(parent);
        }
        if let Err(e) = std::fs::write(&path, source.render(frame, size).encode_png()) {
            eprintln!("error: cannot write {}: {e}", path.display());
            return EXIT_ENV;
        }
        // Machine-readable for tools and agents: one line per file.
        println!("{} frame={frame} t={t:.3}", path.display());
    }
    EXIT_OK
}