Rescale a stream's initial_padding into its own time_base before subtracting it from duration
Browse files at this commit28b9982d8625245fe12fb0889eccf7d86d0add77Parent: dc51bff- fix
- rescale a stream's initial_padding into its own time_base before subtracting it from duration
1 changed file +37 -2
- crates/ec-probe/src/lib.rs +37 -2
crates/ec-probe/src/lib.rsmodified+37 -2
| @@ -347,8 +347,25 @@ impl Reader { | |||
| 347 | self.streams() | 347 | self.streams() |
| 348 | .iter() | 348 | .iter() |
| 349 | .filter_map(|s| { | 349 | .filter_map(|s| { |
| 350 | - s.duration | 350 | + s.duration.map(|d| { |
| 351 | - .map(|d| Timestamp::new(d - i64::from(s.initial_padding), s.time_base)) | 351 | + // `initial_padding` is a sample count, not a `time_base` |
| 352 | + // tick count — the two only coincide when the stream's | ||
| 353 | + // time base happens to be `1/sample_rate`, which a | ||
| 354 | + // container's own duration field usually is not. | ||
| 355 | + let padding_ticks = s | ||
| 356 | + .params | ||
| 357 | + .audio() | ||
| 358 | + .filter(|_| s.initial_padding > 0) | ||
| 359 | + .map(|a| { | ||
| 360 | + TimeBase::from_rate(a.sample_rate).rescale( | ||
| 361 | + i64::from(s.initial_padding), | ||
| 362 | + s.time_base, | ||
| 363 | + Rounding::Down, | ||
| 364 | + ) | ||
| 365 | + }) | ||
| 366 | + .unwrap_or(0); | ||
| 367 | + Timestamp::new(d - padding_ticks, s.time_base) | ||
| 368 | + }) | ||
| 352 | }) | 369 | }) |
| 353 | .max_by(|a, b| { | 370 | .max_by(|a, b| { |
| 354 | a.as_secs_f64() | 371 | a.as_secs_f64() |
| @@ -719,6 +736,24 @@ mod tests { | |||
| 719 | assert_eq!(sniff(b"not a media file at all"), None); | 736 | assert_eq!(sniff(b"not a media file at all"), None); |
| 720 | } | 737 | } |
| 721 | 738 | ||
| 739 | + /// `initial_padding` is a *sample* count while a stream's `duration` is in | ||
| 740 | + /// `time_base` ticks -- for a Matroska file whose `TimestampScale` is not | ||
| 741 | + /// `1/sample_rate` (the common case: ticks are milliseconds), subtracting | ||
| 742 | + /// one from the other unconverted lopped a third off an AAC track's | ||
| 743 | + /// stated length (3.021 s of `CodecDelay`-bearing audio reported as 1.997 | ||
| 744 | + /// s: `1024` samples read straight off as `1024` one-millisecond ticks). | ||
| 745 | + #[test] | ||
| 746 | + fn duration_converts_initial_padding_out_of_sample_units() { | ||
| 747 | + let path = Path::new(env!("CARGO_MANIFEST_DIR")) | ||
| 748 | + .join("../../fixtures/audio/aac-mka-stereo-48000.mka"); | ||
| 749 | + if !path.exists() { | ||
| 750 | + return; // fixtures are generated, not checked in | ||
| 751 | + } | ||
| 752 | + let reader = Reader::open(&path).unwrap(); | ||
| 753 | + let d = reader.duration().expect("a stated duration").as_secs_f64(); | ||
| 754 | + assert!((d - 3.0).abs() < 0.1, "duration {d}, want ~3.0s"); | ||
| 755 | + } | ||
| 756 | + | ||
| 722 | /// An `.aac` or `.mp3` that opens with a tag is still an `.aac` or `.mp3`. | 757 | /// An `.aac` or `.mp3` that opens with a tag is still an `.aac` or `.mp3`. |
| 723 | #[test] | 758 | #[test] |
| 724 | fn an_id3_tag_does_not_hide_the_format() { | 759 | fn an_id3_tag_does_not_hide_the_format() { |