Realm

Video viewer

Source-owned programs from the current OA SDK. Rust and Python examples follow their respective public APIs.

Rust · Python

Example

1use oa::video::{VideoPlayer, VideoPlayerConfig};
2use oa::{Engine, Result, Viewer, ViewerConfig, ViewerMode};
3
4fn main() -> Result<()> {
5 let args: Vec<String> = std::env::args().collect();
6 let positional = |index: usize| args.get(index).filter(|arg| !arg.starts_with("--"));
7 let flag_value = |flag: &str| {
8 args
9 .iter()
10 .position(|arg| arg == flag)
11 .and_then(|index| args.get(index + 1))
12 };
13
14 // Headless capture mode: decode one frame through the level-1 player and
15 // save it — the same native decode + GPU presentation path the Viewer
16 // uses, without opening a window. Documentation assets are generated this
17 // way (see asset/poster-frame.png).
18 if let Some(output) = flag_value("--poster") {
19 let frame = flag_value("--frame")
20 .and_then(|value| value.parse().ok())
21 .unwrap_or(30_u64);
22 let default_path = oa::Path::asset_rel("clip/shibuya_720p_30fps_h264_high_8bit_420.mp4");
23 let default_str = default_path.to_string_lossy().into_owned();
24 let input = positional(1).map(String::as_str).unwrap_or(&default_str);
25 return write_poster(input, output, frame);
26 }
27
28 // Default to the H.264 fixture when no path is supplied.
29 let default_path = oa::Path::asset_rel("clip/shibuya_720p_30fps_h264_high_8bit_420.mp4");
30 let default_str = default_path.to_string_lossy().into_owned();
31 let path = positional(1).map(String::as_str).unwrap_or(&default_str);
32 let title = positional(2)
33 .map(String::as_str)
34 .unwrap_or("oa::Viewer · Video");
35
36 let config = ViewerConfig {
37 mode: ViewerMode::Video,
38 path: path.to_owned(),
39 title: title.to_owned(),
40 loop_media: false,
41 ..ViewerConfig::default()
42 };
43
44 Viewer::new(config).run_standalone()
45}
46
47/// Decode `frame` of `input` through the native Vulkan Video path, convert
48/// the decoded YUV planes to an RGBA texture on-device, and write a PNG.
49fn write_poster(input: &str, output: &str, frame: u64) -> Result<()> {
50 let engine = Engine::new()?;
51 Engine::set_default(engine.clone());
52 let mut player = VideoPlayer::new_native(
53 &engine,
54 std::path::Path::new(input),
55 VideoPlayerConfig {
56 start_playing: false,
57 loop_playback: false,
58 ..VideoPlayerConfig::default()
59 },
60 )?;
61 player.seek_frame(frame)?;
62 let texture = player.current_frame_texture()?;
63 let rgba = texture.read_rgba8()?;
64 let width = u32::try_from(texture.width())
65 .map_err(|_| oa::Error::out_of_range("video frame width exceeds u32"))?;
66 let height = u32::try_from(texture.height())
67 .map_err(|_| oa::Error::out_of_range("video frame height exceeds u32"))?;
68 oa::image::save_rgba_file(&rgba, width, height, output, 90)?;
69 player.close();
70 Ok(())
71}
72

Run Rust tutorials from the OA checkout with cargo run --release --example <name>. Run Python with the environment containing the OA native extension. Consult each program's arguments and capability checks before running it.