forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

silent_audio_import.rs74 lines, 3.1 KiBHistoryRawDownload
1//! A video whose sound track no decoder here opens (WavPack) imports with its
2//! picture, its sound silent for as long as the file's header says it plays, and
3//! a returned warning that names the codec in the words a person knows it by.
4//! The file is made with ffmpeg at test time; nothing is committed.
5//!
6//! ```text
7//! cargo test -p engine --release --test silent_audio_import -- --nocapture
8//! ```
9
10use std::path::{Path, PathBuf};
11use std::process::Command;
12
13use engine::{AudioSession, PlaybackSession};
14
15fn asset(name: &str) -> PathBuf {
16 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
17 .join("../../assets")
18 .join(name)
19}
20
21/// Two seconds of test picture at 25 fps, with a 48 kHz sine encoded as WavPack.
22fn wavpack_video(dir: &Path) -> PathBuf {
23 let out = dir.join("wavpack_sound.mkv");
24 let run = Command::new("ffmpeg")
25 .args(["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=duration=2:size=320x240:rate=25"])
26 .args(["-f", "lavfi", "-i", "sine=duration=2:sample_rate=48000"])
27 .args(["-map", "0:v", "-map", "1:a", "-c:v", "libx264", "-pix_fmt", "yuv420p"])
28 .args(["-c:a", "wavpack"])
29 .arg(&out)
30 .output()
31 .expect("ffmpeg runs");
32 assert!(run.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&run.stderr));
33 out
34}
35
36#[test]
37fn an_undecodable_sound_track_imports_silent_with_a_warning_naming_wavpack() {
38 let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("silent-audio");
39 std::fs::create_dir_all(&dir).expect("the scratch directory");
40 let file = wavpack_video(&dir);
41
42 // The sound opens to silence, and its length is the file's own: 2 s.
43 assert!(
44 matches!(AudioSession::open(&file), Ok(None)),
45 "no decoder opens this WavPack track, so it is silent"
46 );
47 let secs = AudioSession::stream_duration_secs(&file, 0)
48 .expect("the length reads")
49 .expect("the header states a length for the silent sound");
50 assert!((secs - 2.0).abs() < 0.1, "the sound is held for the picture's 2 s: {secs}");
51
52 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the picture file");
53 session.set_gain(0.0);
54 let (source, warnings) = session
55 .import_with_warnings(&file)
56 .expect("a picture whose sound nothing decodes still imports");
57 assert_eq!(session.sources()[source].path, file);
58
59 // The picture is all there: two seconds of the timeline's frames. A file at its own rate
60 // covers the same wall time, whatever rate it was shot at.
61 let timeline_fps = session.native_frame_rate();
62 let frames = f64::from(session.file_frames(&file));
63 assert!(
64 (frames - 2.0 * timeline_fps).abs() <= 2.0,
65 "the video frames are present: {frames} frames at a {timeline_fps} fps timeline"
66 );
67
68 // One warning, naming the codec as WavPack and saying the sound is silent.
69 assert_eq!(warnings.len(), 1, "{warnings:?}");
70 let warning = &warnings[0];
71 assert!(warning.contains("WavPack"), "{warning}");
72 assert!(!warning.contains("WAVPACK4"), "the raw tag is not shown: {warning}");
73 assert!(warning.ends_with("its sound will be silent"), "{warning}");
74}