forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

export_audio_cost.rs79 lines, 2.8 KiBHistoryRawDownload
1//! What the *sound* of a copied export costs, on a window of a real film.
2//!
3//! The picture of an untouched Matroska timeline is copied block for block, so
4//! everything an export of a feature film waits for is in the audio pass. This
5//! runs that pass over the first `minutes` of a file and prints the engine's own
6//! stage lines (`export audio: ...`), which is how the per-minute cost of the
7//! mix, the Opus encode and the fidelity gate are told apart.
8//!
9//! ```text
10//! cargo run --release -p engine --example export_audio_cost -- <film.mkv> [minutes] [out.mkv]
11//! ```
12//!
13//! The output file is overwritten; name a scratch path, never a source.
14
15use std::path::PathBuf;
16use std::time::{Duration, Instant};
17
18use engine::PlaybackSession;
19use engine::export::{ExportSettings, Format};
20use engine::project::Lane;
21
22fn main() {
23 let mut args = std::env::args_os().skip(1);
24 let film = PathBuf::from(
25 args.next()
26 .expect("usage: export_audio_cost <film.mkv> [minutes] [out.mkv]"),
27 );
28 let minutes: f64 = args
29 .next()
30 .and_then(|s| s.to_str().and_then(|s| s.parse().ok()))
31 .unwrap_or(5.0);
32 let out = args.next().map_or_else(
33 || std::env::temp_dir().join("ve_export_audio_cost.mkv"),
34 PathBuf::from,
35 );
36
37 // On a sync point, or the picture is re-encoded and the measurement is of
38 // the encoder rather than of the sound.
39 let (_, demuxer) = engine::demux::Demuxer::open(&film).expect("open the film");
40 let engine::demux::Demuxer::Mkv(mut source) = demuxer else {
41 panic!("the film is not Matroska");
42 };
43
44 let mut session = PlaybackSession::open(&film).expect("open the film");
45 let fps = session.meta().frame_rate;
46 let at = ((minutes * 60.0 * fps) as usize..source.block_count())
47 .find(|&i| source.is_sync(i))
48 .expect("a sync point after the mark");
49 if session.cut_at(at as f64 / fps) {
50 // Everything after the cut goes; what is left is the window measured.
51 while session.delete_clip(Lane::V1, 1) {}
52 }
53 println!(
54 "{}: {:.1} min of timeline at {fps:.3} fps -> {}",
55 film.display(),
56 session.timeline_duration() / 60.0,
57 out.display()
58 );
59
60 let settings = ExportSettings {
61 format: Format::Hevc,
62 ..Default::default()
63 };
64 let started = Instant::now();
65 let handle = session.export_to_with(&out, &settings);
66 while !handle.is_finished() {
67 std::thread::sleep(Duration::from_millis(200));
68 }
69 let bytes = std::fs::metadata(&out).map(|m| m.len()).unwrap_or(0);
70 println!(
71 "export: {:.1} s, {} MB ({})",
72 started.elapsed().as_secs_f64(),
73 bytes / 1_000_000,
74 handle.encoders().unwrap_or_default()
75 );
76 if let Some(Err(e)) = handle.result() {
77 println!("export failed: {e}");
78 }
79}