forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB86e9455Tahinli3 months ago398 commits

dump_frame.rs62 lines, 2.4 KiBHistoryRawDownload
1//! Dump decoded frames as PPM for visual inspection.
2//! Usage: cargo run -p engine --release --example dump_frame -- <video.mp4> <frame_index> <out.ppm> [seek]
3//!
4//! With `seek` the frame is reached the way a scrub reaches it -- restart at the
5//! random access point at or before it -- instead of by decoding the file from
6//! its start. The two must be the same picture, byte for byte, which is what
7//! makes a `cmp` of the two dumps a check that seeking into an open GOP decodes
8//! the frame it claims and not a smear of missing references.
9
10use std::env;
11use std::fs::File;
12use std::io::{BufWriter, Write};
13
14fn main() -> engine::Result<()> {
15 let mut args = env::args().skip(1);
16 let path = args
17 .next()
18 .ok_or("usage: dump_frame <video.mp4> <frame_index> <out.ppm> [seek]")?;
19 let target: u32 = args.next().ok_or("missing frame index")?.parse()?;
20 let out = args.next().ok_or("missing output path")?;
21 let seek = args.next().is_some_and(|a| a == "seek");
22
23 let (meta, rx) = engine::DecodeSession::open(&path)?;
24 eprintln!(
25 "{}x{} @ {:.2} fps",
26 meta.width, meta.height, meta.frame_rate
27 );
28 if seek {
29 drop(rx);
30 let mut session = engine::PlaybackSession::open(&path)?;
31 session.seek(f64::from(target) / meta.frame_rate);
32 let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
33 while std::time::Instant::now() < deadline {
34 session.tick();
35 if let Some(frame) = session.try_frame() {
36 write_ppm(&out, frame.width, frame.height, &frame.bgra)?;
37 eprintln!("wrote frame {} (asked {target}) to {out}", frame.index);
38 return Ok(());
39 }
40 std::thread::sleep(std::time::Duration::from_millis(4));
41 }
42 return Err(format!("no frame arrived within 60 s of seeking to {target}").into());
43 }
44
45 for frame in rx {
46 if frame.index == target {
47 write_ppm(&out, frame.width, frame.height, &frame.bgra)?;
48 eprintln!("wrote frame {target} to {out}");
49 return Ok(());
50 }
51 }
52 Err(format!("stream ended before frame {target}").into())
53}
54
55fn write_ppm(out: &str, width: u32, height: u32, bgra: &[u8]) -> engine::Result<()> {
56 let mut w = BufWriter::new(File::create(out)?);
57 writeln!(w, "P6\n{width} {height}\n255")?;
58 for px in bgra.chunks_exact(4) {
59 w.write_all(&[px[2], px[1], px[0]])?; // BGRA -> RGB
60 }
61 Ok(())
62}