forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

probe_fps.rs117 lines, 4.6 KiBHistoryRawDownload
1//! What the demuxer reads a file's frame rate as, and what that means for the
2//! frame the master clock asks for ten minutes in -- the arithmetic behind A/V
3//! desync on NTSC-rate files, on a file too big to keep as a fixture.
4//!
5//! `cargo run --release -p engine --example probe_fps -- <file> [secs] [play] [play_secs]`
6//!
7//! With `play` it also seeks there, plays `play_secs` (two by default) against the
8//! real audio device and reports the skew between the picture on screen and the
9//! master clock -- the machine-checkable half of "lip sync holds".
10
11use std::path::PathBuf;
12use std::time::{Duration, Instant};
13
14fn main() {
15 let mut args = std::env::args_os().skip(1);
16 let path = PathBuf::from(args.next().expect("usage: probe_fps <file.mp4> [secs]"));
17 let secs: f64 = args
18 .next()
19 .and_then(|s| s.to_str().and_then(|s| s.parse().ok()))
20 .unwrap_or(600.0);
21
22 let (meta, _) = engine::demux::Demuxer::open(&path).expect("open");
23 println!(
24 "{}: {}x{} @ {:.9} fps, {} frames",
25 path.display(),
26 meta.width,
27 meta.height,
28 meta.frame_rate,
29 meta.frame_count
30 );
31 println!(" ntsc 24000/1001 = {:.9}", 24_000.0 / 1001.0);
32 println!(
33 " target frame at t={secs}s: {:.3} (mp4 0.14's flat 23.0 would say {:.3}, {:.1} frames out)",
34 secs * meta.frame_rate,
35 secs * 23.0,
36 secs * (meta.frame_rate - 23.0)
37 );
38 // Straight from the container, to show what the demuxer's frame 0 skipped.
39 // mp4 only: a Matroska file has no `moov` and reading one out of it is a
40 // panic, which used to make this tool unusable on exactly the films in a
41 // library that the demuxer most needs checking against.
42 if !engine::demux::is_matroska(&path) {
43 mp4_boxes(&path);
44 }
45 match engine::AudioSession::open(&path) {
46 Ok(Some((audio, _))) => println!(" audio: {} Hz {} ch", audio.sample_rate, audio.channels),
47 Ok(None) => println!(
48 " audio: none usable ({:?})",
49 engine::AudioSession::unsupported(&path)
50 ),
51 Err(e) => println!(" audio: refused ({e})"),
52 }
53
54 // `play [secs]`: how long to play for, two seconds by default.
55 if args.next().is_some_and(|a| a == *"play") {
56 let seconds = args
57 .next()
58 .and_then(|s| s.to_str().and_then(|s| s.parse().ok()))
59 .unwrap_or(2.0);
60 play(&path, secs, meta.frame_rate, seconds);
61 }
62}
63
64/// The edit list and sample counts of an mp4, per track.
65fn mp4_boxes(path: &std::path::Path) {
66 let file = std::fs::File::open(path).expect("open");
67 let reader = ec_mp4::Mp4Demuxer::new(std::io::BufReader::new(file)).expect("read header");
68 for track in reader.tracks() {
69 let samples = track.stream.map_or(0, |s| reader.samples(s).len());
70 println!(
71 " track {}: {:?}, {samples} samples, media_time {:?}, {} edit entries, rotation {:?}",
72 track.track_id,
73 track.media,
74 track.media_time,
75 track.edits.len(),
76 track.rotation,
77 );
78 }
79}
80
81/// Seeks in, plays two seconds against the real device, and reports where the
82/// picture stands against the master clock. The clock counts audio samples, so
83/// `frame / fps - clock` *is* the A/V skew: a truncated frame rate shows up
84/// here as tens of seconds of it, ten minutes in.
85fn play(path: &std::path::Path, secs: f64, fps: f64, seconds: f64) {
86 let mut session = engine::PlaybackSession::open(path).expect("open for playback");
87 session.seek(secs);
88 session.play();
89 let mut last = None;
90 let mut held: Option<u32> = None;
91 let deadline = Instant::now() + Duration::from_secs_f64(seconds);
92 while Instant::now() < deadline {
93 session.tick();
94 // The app's own drop-when-behind rule (`Player::pump`): show every frame
95 // already due and hold the first one that is not, or this would measure
96 // how far the decoder runs ahead instead of what is on screen.
97 let target = session.now() * fps;
98 while let Some(index) = held.take().or_else(|| session.try_frame().map(|f| f.index)) {
99 if f64::from(index) > target {
100 held = Some(index);
101 break;
102 }
103 last = Some(index);
104 }
105 std::thread::sleep(Duration::from_millis(4));
106 }
107 let now = session.now();
108 let Some(index) = last else {
109 println!(" played from {secs}s: no frame arrived, clock at {now:.3}s");
110 return;
111 };
112 let media = f64::from(index) / fps;
113 println!(
114 " played from {secs}s: clock {now:.3}s, frame {index} = media {media:.3}s, skew {:+.3}s",
115 media - now
116 );
117}