| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | use std::path::Path; |
| 12 | use std::process::Command; |
| 13 | use std::time::{Duration, Instant}; |
| 14 | |
| 15 | use engine::export::{EncoderSeat, ExportSettings, Format}; |
| 16 | use engine::export_presets::{apply, by_id, presets}; |
| 17 | use engine::scratch::Scratch; |
| 18 | use engine::PlaybackSession; |
| 19 | |
| 20 | fn have_ffmpeg() -> bool { |
| 21 | Command::new("ffmpeg").arg("-version").output().is_ok_and(|o| o.status.success()) |
| 22 | && Command::new("ffprobe").arg("-version").output().is_ok_and(|o| o.status.success()) |
| 23 | } |
| 24 | |
| 25 | |
| 26 | fn make_clip(out: &Path) -> bool { |
| 27 | Command::new("ffmpeg") |
| 28 | .args(["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=320x180:rate=25:duration=2"]) |
| 29 | .args(["-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=2"]) |
| 30 | .args(["-af", "volume=0.05,aformat=channel_layouts=stereo"]) |
| 31 | .args(["-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-ar", "48000", "-shortest"]) |
| 32 | .arg(out) |
| 33 | .status() |
| 34 | .is_ok_and(|s| s.success()) |
| 35 | } |
| 36 | |
| 37 | fn probe(path: &Path, stream: &str, entries: &str) -> String { |
| 38 | let out = Command::new("ffprobe") |
| 39 | .args(["-v", "error", "-select_streams", stream, "-show_entries", entries, "-of", "csv=p=0"]) |
| 40 | .arg(path) |
| 41 | .output() |
| 42 | .expect("ffprobe runs"); |
| 43 | String::from_utf8_lossy(&out.stdout).trim().to_owned() |
| 44 | } |
| 45 | |
| 46 | |
| 47 | fn measure(path: &Path) -> (f64, f64) { |
| 48 | let out = Command::new("ffmpeg") |
| 49 | .args(["-hide_banner", "-nostats", "-i"]) |
| 50 | .arg(path) |
| 51 | .args(["-vn", "-af", "ebur128=peak=true", "-f", "null", "-"]) |
| 52 | .output() |
| 53 | .expect("ffmpeg runs"); |
| 54 | let text = String::from_utf8_lossy(&out.stderr); |
| 55 | let summary = text.rsplit("Summary:").next().expect("a summary"); |
| 56 | let num = |key: &str| -> f64 { |
| 57 | let line = summary.lines().find(|l| l.trim_start().starts_with(key)).unwrap_or_else(|| panic!("{key} in {summary}")); |
| 58 | line.split_whitespace().nth(1).and_then(|v| v.parse().ok()).unwrap_or_else(|| panic!("{key} figure in {line}")) |
| 59 | }; |
| 60 | (num("I:"), num("Peak:")) |
| 61 | } |
| 62 | |
| 63 | fn export(src: &Path, name: &str, id: &str) -> Scratch { |
| 64 | let preset = by_id(id).expect("the preset exists"); |
| 65 | let settings = apply( |
| 66 | preset, |
| 67 | ExportSettings { seat: EncoderSeat::Software, ..ExportSettings::default() }, |
| 68 | ); |
| 69 | let session = PlaybackSession::open(src).expect("open the clip"); |
| 70 | let out = Scratch::file(&format!("ve_preset_{name}"), settings.format.ext()); |
| 71 | let handle = session.export_to_with(&out, &settings); |
| 72 | let deadline = Instant::now() + Duration::from_secs(600); |
| 73 | loop { |
| 74 | if let Some(result) = handle.result() { |
| 75 | result.expect("the export finishes"); |
| 76 | break; |
| 77 | } |
| 78 | assert!(Instant::now() < deadline, "export never settled"); |
| 79 | std::thread::sleep(Duration::from_millis(20)); |
| 80 | } |
| 81 | out |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn every_preset_applies_and_keeps_the_chapters() { |
| 86 | assert!(!presets().is_empty()); |
| 87 | let mut seen = std::collections::HashSet::new(); |
| 88 | for p in presets() { |
| 89 | assert!(seen.insert(p.id), "duplicate id {}", p.id); |
| 90 | let base = ExportSettings { |
| 91 | chapters: vec![(0, "one".into()), (1_000_000_000, "two".into())], |
| 92 | video_bit_depth: Some(8), |
| 93 | subtitles: vec![0], |
| 94 | ..ExportSettings::default() |
| 95 | }; |
| 96 | let got = apply(p, base.clone()); |
| 97 | assert_eq!(got.format, p.format, "{}", p.id); |
| 98 | assert_eq!(got.chapters, base.chapters, "{} cleared the chapters", p.id); |
| 99 | assert_eq!(got.subtitles, base.subtitles, "{}", p.id); |
| 100 | assert_eq!(got.bitrate, p.bitrate, "{}", p.id); |
| 101 | assert_eq!(got.size, p.target.size, "{}", p.id); |
| 102 | assert_eq!(got.loudness, p.loudness, "{}", p.id); |
| 103 | assert_eq!(got.video_bit_depth, p.video_bit_depth.or(Some(8)), "{}", p.id); |
| 104 | } |
| 105 | let shorts = apply(by_id("shorts_reels").unwrap(), ExportSettings::default()); |
| 106 | assert_eq!(shorts.size, Some((1080, 1920))); |
| 107 | let archive = apply(by_id("archive").unwrap(), ExportSettings::default()); |
| 108 | assert_eq!((archive.format, archive.video_bit_depth), (Format::Av1, Some(10))); |
| 109 | let yt = apply(by_id("youtube_1080p").unwrap(), ExportSettings::default()); |
| 110 | assert_eq!((yt.bitrate, yt.audio_kbps), (Some(8_000_000), Some(384))); |
| 111 | let podcast = apply(by_id("podcast").unwrap(), ExportSettings::default()); |
| 112 | let l = podcast.loudness.expect("a loudness target"); |
| 113 | assert_eq!((l.lufs, l.true_peak_dbtp), (-16.0, -1.0)); |
| 114 | assert!(by_id("nope").is_none()); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn youtube_1080p_exports_h264_1080p_with_48k_aac() { |
| 119 | if !have_ffmpeg() { |
| 120 | eprintln!("no ffmpeg: skipped"); |
| 121 | return; |
| 122 | } |
| 123 | let src = Scratch::file("ve_preset_src_yt", "mp4"); |
| 124 | assert!(make_clip(&src), "ffmpeg makes the clip"); |
| 125 | let out = export(&src, "yt", "youtube_1080p"); |
| 126 | assert_eq!(probe(&out, "v:0", "stream=codec_name,width,height"), "h264,1920,1080"); |
| 127 | assert_eq!(probe(&out, "a:0", "stream=codec_name,sample_rate"), "aac,48000"); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn podcast_lands_on_minus_16_lufs_under_the_peak_ceiling() { |
| 132 | if !have_ffmpeg() { |
| 133 | eprintln!("no ffmpeg: skipped"); |
| 134 | return; |
| 135 | } |
| 136 | let src = Scratch::file("ve_preset_src_pod", "mp4"); |
| 137 | assert!(make_clip(&src), "ffmpeg makes the clip"); |
| 138 | let (before, _) = measure(&src); |
| 139 | assert!(before < -20.0, "the source is quiet: {before} LUFS"); |
| 140 | for (id, codec) in [("podcast", "aac"), ("podcast_ogg", "vorbis"), ("podcast_opus", "opus")] { |
| 141 | let out = export(&src, id, id); |
| 142 | assert_eq!(probe(&out, "a:0", "stream=codec_name,sample_rate"), format!("{codec},48000"), "{id}"); |
| 143 | assert!(probe(&out, "v:0", "stream=codec_name").is_empty(), "{id} has no picture"); |
| 144 | let (lufs, peak) = measure(&out); |
| 145 | assert!((lufs + 16.0).abs() <= 1.0, "{id}: {lufs} LUFS is not within 1 LU of -16"); |
| 146 | assert!(peak <= -0.5, "{id}: true peak {peak} dBTP"); |
| 147 | println!("{id}: {lufs} LUFS, peak {peak} dBTP"); |
| 148 | } |
| 149 | } |