forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

media_streams.rs151 lines, 5.0 KiBHistoryRawDownload
1//! `probe_media`'s per-stream facts: every video, audio and subtitle stream the
2//! header names, with the codec identifier the file spells (an mp4 sample entry
3//! fourcc or a Matroska CodecID), and each stream's own header length. The old
4//! single-stream fields are unchanged; these are additions beside them.
5
6use std::path::{Path, PathBuf};
7
8use engine::{StreamKind, probe_media};
9
10fn asset(name: &str) -> PathBuf {
11 Path::new(env!("CARGO_MANIFEST_DIR"))
12 .join("../../assets")
13 .join(name)
14}
15
16#[test]
17fn an_mp4_names_each_stream_by_its_sample_entry_fourcc() {
18 let f = probe_media(&asset("test_av.mp4"));
19 let video: Vec<_> = f
20 .streams
21 .iter()
22 .filter(|s| s.kind == StreamKind::Video)
23 .collect();
24 assert_eq!(video.len(), 1);
25 assert_eq!(video[0].codec_id.as_deref(), Some("avc1"));
26 assert_eq!(video[0].index, 0);
27
28 // An hvc1-tagged HEVC track keeps its own fourcc, not the hev1 the crate knows.
29 let hvc1 = probe_media(&asset("test_hevc_hvc1.mp4"));
30 assert_eq!(hvc1.streams[0].codec_id.as_deref(), Some("hvc1"));
31}
32
33#[test]
34fn a_matroska_names_each_stream_by_its_codec_id() {
35 let f = probe_media(&asset("test_subs.mkv"));
36 let ids: Vec<(StreamKind, Option<&str>)> = f
37 .streams
38 .iter()
39 .map(|s| (s.kind, s.codec_id.as_deref()))
40 .collect();
41 assert!(ids.contains(&(StreamKind::Video, Some("V_AV1"))), "{ids:?}");
42 assert!(
43 ids.contains(&(StreamKind::Subtitle, Some("S_TEXT/UTF8"))),
44 "{ids:?}"
45 );
46 assert!(
47 ids.contains(&(StreamKind::Subtitle, Some("S_TEXT/ASS"))),
48 "{ids:?}"
49 );
50}
51
52#[test]
53fn every_audio_track_is_listed_not_just_the_first() {
54 let mp4 = probe_media(&asset("test_multiaudio.mp4"));
55 let audio: Vec<_> = mp4
56 .streams
57 .iter()
58 .filter(|s| s.kind == StreamKind::Audio)
59 .map(|s| (s.index, s.codec_id.clone().unwrap_or_default()))
60 .collect();
61 assert_eq!(
62 audio,
63 vec![
64 (0, "mp4a".to_string()),
65 (1, "mp4a".to_string()),
66 (2, "ac-3".to_string())
67 ]
68 );
69
70 let mkv = probe_media(&asset("test_multiaudio.mkv"));
71 let n = mkv
72 .streams
73 .iter()
74 .filter(|s| s.kind == StreamKind::Audio)
75 .count();
76 assert_eq!(n, 2);
77}
78
79#[test]
80fn each_audio_stream_states_its_own_header_length() {
81 // Track 2 of the mp4 is a little longer than track 1: each keeps its own
82 // mdhd duration, not the file's.
83 let f = probe_media(&asset("test_multiaudio.mp4"));
84 let secs: Vec<f64> = f
85 .streams
86 .iter()
87 .filter(|s| s.kind == StreamKind::Audio)
88 .map(|s| s.duration_secs.expect("mp4 states a track length"))
89 .collect();
90 assert_eq!(secs.len(), 3);
91 assert!((secs[0] - 2.0232).abs() < 0.01, "{secs:?}");
92 assert!((secs[1] - 2.0464).abs() < 0.01, "{secs:?}");
93
94 // A long commentary track is reported as its own, beside a short picture.
95 let long = probe_media(&asset("test_short_video_long_audio.mp4"));
96 let audio = long
97 .streams
98 .iter()
99 .find(|s| s.kind == StreamKind::Audio)
100 .expect("an audio stream");
101 assert!(audio.duration_secs.unwrap() > 59.0);
102 assert!(
103 long.duration_secs.unwrap() < 3.0,
104 "picture length unchanged"
105 );
106}
107
108#[test]
109fn matroska_gives_every_track_the_files_length() {
110 // Matroska has no per-track length, so each track reports the file's
111 // `Info.Duration` times its `TimestampScale`: the same value for every one.
112 let f = probe_media(&asset("test_subs.mkv"));
113 assert_eq!(f.streams.len(), 3);
114 for s in &f.streams {
115 let secs = s.duration_secs.expect("the Info length");
116 assert!((secs - 5.0).abs() < 0.01, "{secs} for {:?}", s.codec_id);
117 }
118
119 let multi = probe_media(&asset("test_multiaudio.mkv"));
120 let secs: Vec<Option<f64>> = multi.streams.iter().map(|s| s.duration_secs).collect();
121 assert!(secs.len() >= 2, "{secs:?}");
122 assert!(secs[0].is_some(), "{secs:?}");
123 assert!(secs.iter().all(|s| *s == secs[0]), "{secs:?}");
124}
125
126#[test]
127fn a_picture_the_decoder_refuses_still_lists_its_streams() {
128 // Playback refuses a display matrix that is not a quarter turn by name,
129 // and the stream list must not go with the picture.
130 let rot = probe_media(&asset("test_rotation45.mkv"));
131 assert_eq!(rot.streams.len(), 1, "{:?}", rot.streams);
132 assert_eq!(rot.streams[0].kind, StreamKind::Video);
133 assert_eq!(rot.streams[0].codec_id.as_deref(), Some("V_MPEG4/ISO/AVC"));
134}
135
136#[test]
137fn a_wav_has_one_audio_stream_named_by_its_codec() {
138 let f = probe_media(&asset("test_tone_48k.wav"));
139 assert_eq!(f.streams.len(), 1);
140 assert_eq!(f.streams[0].kind, StreamKind::Audio);
141 assert_eq!(f.streams[0].codec_id.as_deref(), Some("pcm_s16le"));
142 assert!(f.streams[0].duration_secs.is_some());
143}
144
145#[test]
146fn the_existing_fields_are_unchanged() {
147 let f = probe_media(&asset("test_av.mp4"));
148 assert_eq!(f.container.as_deref(), Some("mp4"));
149 assert!(f.video.is_some());
150 assert!(f.duration_secs.is_some());
151}