forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

stills.rs139 lines, 6.1 KiBHistoryRawDownload
1//! Still-picture exports and numbered runs: a PNG sequence, a poster frame, an animated GIF, and
2//! a folder of numbered PNGs imported (and opened) as one clip.
3//!
4//! ```text
5//! cargo test -p engine --test stills -- --test-threads=1 --nocapture
6//! ```
7
8use std::path::PathBuf;
9use std::time::{Duration, Instant};
10
11use engine::export::{ExportSettings, Format, GifSettings};
12use engine::project::Lane;
13use engine::scratch::Scratch;
14use engine::{ExportHandle, PlaybackSession};
15
16fn asset(name: &str) -> PathBuf {
17 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets").join(name)
18}
19
20fn wait(handle: &ExportHandle, limit: Duration) -> engine::Result<()> {
21 let started = Instant::now();
22 while !handle.is_finished() {
23 assert!(started.elapsed() < limit, "export did not finish in {limit:?}");
24 std::thread::sleep(Duration::from_millis(20));
25 }
26 handle.result().expect("a finished export has an outcome")
27}
28
29/// Frame `k` of a generated run: a horizontal ramp, a vertical one, and a level that climbs with `k`.
30fn ramp(k: u32, width: u32, height: u32) -> Vec<u8> {
31 let mut px = Vec::with_capacity((width * height * 3) as usize);
32 for y in 0..height {
33 for x in 0..width {
34 px.extend_from_slice(&[(x * 255 / width) as u8, (k * 20) as u8, (y * 255 / height) as u8]);
35 }
36 }
37 px
38}
39
40/// A numbered run of ten PNGs at 1280x720, written with the codec's own encoder.
41fn ten_frames(dir: &std::path::Path) -> PathBuf {
42 let (w, h) = (1280, 720);
43 for k in 1..=10u32 {
44 let bytes = ec_image::png_encode::encode_rgb8(w, h, &ramp(k, w, h)).expect("encode");
45 std::fs::write(dir.join(format!("frame_{k:04}.png")), bytes).expect("write");
46 }
47 dir.join("frame_0004.png")
48}
49
50#[test]
51fn a_png_sequence_and_a_still_are_pictures_at_project_size() {
52 let session = PlaybackSession::open(asset("test_25fps.mp4")).expect("open the fixture");
53 let (project, meta) = session.export_snapshot();
54 let dir = Scratch::dir("ve_stills_sequence");
55 let out = dir.join("shot");
56 let settings = ExportSettings {
57 format: Format::PngSequence,
58 range: Some((0, 50)), // two seconds at 25 fps
59 ..ExportSettings::default()
60 };
61 wait(&engine::export::start(project.clone(), meta, &out, &settings, None), Duration::from_secs(300))
62 .expect("the sequence");
63
64 let mut names: Vec<String> = std::fs::read_dir(&out)
65 .expect("the folder exists")
66 .map(|e| e.expect("entry").file_name().to_string_lossy().into_owned())
67 .collect();
68 names.sort();
69 assert_eq!(names.len(), 50, "one picture per frame: {names:?}");
70 assert_eq!(names[0], "shot_00001.png");
71 assert_eq!(names[49], "shot_00050.png");
72 let first = ec_image::open(out.join("shot_00001.png")).expect("a PNG");
73 assert_eq!(first.dimensions(), (meta.width, meta.height), "the project's own size");
74
75 let poster = Scratch::file("ve_stills_poster", "png");
76 let still = ExportSettings { format: Format::Still, range: Some((25, 26)), ..ExportSettings::default() };
77 wait(&engine::export::start(project, meta, &poster, &still, None), Duration::from_secs(300))
78 .expect("the still");
79 let img = ec_image::open(&poster).expect("a PNG");
80 assert_eq!(img.dimensions(), (meta.width, meta.height));
81 assert!(!poster.with_extension("png.part").exists(), "no temporary left behind");
82}
83
84#[test]
85fn a_gif_of_a_second_is_fifteen_frames_at_480_wide_and_a_wide_one_is_refused() {
86 let session = PlaybackSession::open(asset("test_25fps.mp4")).expect("open the fixture");
87 let (project, meta) = session.export_snapshot();
88 let out = Scratch::file("ve_stills_gif", "gif");
89 let settings = ExportSettings {
90 format: Format::Gif,
91 range: Some((0, 25)),
92 gif: GifSettings::default(),
93 ..ExportSettings::default()
94 };
95 wait(&engine::export::start(project.clone(), meta, &out, &settings, None), Duration::from_secs(300))
96 .expect("the GIF");
97 let bytes = std::fs::read(&out).expect("the GIF file");
98 let frames = ec_image::gif::decode_animation(&bytes, ec_image::Limits::default()).expect("a GIF that decodes");
99 assert_eq!(frames.len(), 15, "one second at 15 frames a second");
100 assert_eq!(frames[0].image.dimensions().0, 480, "the default width");
101
102 let refused = Scratch::file("ve_stills_gif_refused", "gif");
103 let wide = ExportSettings {
104 format: Format::Gif,
105 range: Some((0, 25)),
106 gif: GifSettings { width: 1280, ..GifSettings::default() },
107 ..ExportSettings::default()
108 };
109 let err = wait(&engine::export::start(project, meta, &refused, &wide, None), Duration::from_secs(60))
110 .expect_err("too wide");
111 assert!(err.to_string().contains("pixels wide"), "{err}");
112 assert!(!refused.exists());
113}
114
115#[test]
116fn a_numbered_run_opens_and_imports_as_one_clip_of_its_frames() {
117 let dir = Scratch::dir("ve_stills_run_import");
118 let one = ten_frames(&dir);
119 let spelled = dir.join("frame_[0001-0010].png");
120
121 // Opened as the timeline's first file.
122 let opened = PlaybackSession::open(&one).expect("a numbered run opens");
123 assert_eq!(opened.sources()[0].path, spelled, "the run is the source, not one frame of it");
124 assert_eq!(opened.lane_clips(Lane::V1).first().expect("placed on V1").len(), 10, "ten frames");
125
126 // Imported into a timeline that already has footage.
127 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
128 session.set_gain(0.0);
129 let before = session.sources().len();
130 let source = session.import(&one).expect("a numbered run imports");
131 assert_eq!(session.sources().len(), before + 1, "one source for the whole run");
132 assert_eq!(session.sources()[source].path, spelled);
133 assert_eq!(session.file_frames(&spelled), 10, "ten frames, a clip of ten frames");
134 assert_eq!(session.import(&dir.join("frame_0007.png")).expect("again"), source, "the same source");
135 let end = session.timeline_duration();
136 assert!(session.place_stream_at(end, &spelled, 0, Some(Lane::V1)).expect("placed"));
137 let placed = session.lane_clips(Lane::V1).last().expect("the run's clip").clone();
138 assert_eq!(placed.len(), 10, "the clip is ten frames long");
139}