Skip to content

Commit c1f759f

Browse files
Update examples (alfg#16)
* Add ReadBox trait * Add boxtype macro * Remove offset in BoxHeader * Fix parsing error when box has largesize * Remove duplicated codes reading version and flags * Update examples * Fix test failure Co-authored-by: Byungwan Jun <[email protected]>
1 parent af49902 commit c1f759f

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

examples/mp4copy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ fn copy<P: AsRef<Path>>(src_filename: &P, dst_filename: &P) -> Result<()> {
7070

7171
let track_id = track_idx as u32 + 1;
7272
let sample_count = mp4_reader.sample_count(track_id)?;
73-
for six in 0..sample_count {
74-
let sample_id = six + 1;
73+
for sample_idx in 0..sample_count {
74+
let sample_id = sample_idx + 1;
7575
let sample = mp4_reader.read_sample(track_id, sample_id)?.unwrap();
7676
mp4_writer.write_sample(track_id, &sample)?;
7777
// println!("copy {}:({})", sample_id, sample);

examples/mp4info.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,7 @@ fn info<P: AsRef<Path>>(filename: &P) -> Result<()> {
3535
compatible_brands.push_str(",");
3636
}
3737
println!(" compatible_brands: {}", compatible_brands);
38-
println!(
39-
"Duration: {}, timescale: {}",
40-
mp4.duration(),
41-
mp4.timescale()
42-
);
38+
println!("Duration: {:?}", mp4.duration());
4339

4440
for track in mp4.tracks().iter() {
4541
let media_info = match track.track_type()? {

src/reader.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::io::{Read, Seek, SeekFrom};
2+
use std::time::Duration;
23

34
use crate::mp4box::*;
45
use crate::*;
@@ -95,8 +96,8 @@ impl<R: Read + Seek> Mp4Reader<R> {
9596
&self.ftyp.compatible_brands
9697
}
9798

98-
pub fn duration(&self) -> u64 {
99-
self.moov.mvhd.duration
99+
pub fn duration(&self) -> Duration {
100+
Duration::from_millis(self.moov.mvhd.duration * 1000 / self.moov.mvhd.timescale as u64)
100101
}
101102

102103
pub fn timescale(&self) -> u32 {

tests/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use mp4::{AudioObjectType, AvcProfile, ChannelConfig, MediaType, SampleFreqIndex, TrackType};
22
use std::fs::File;
33
use std::io::BufReader;
4+
use std::time::Duration;
45

56
#[test]
67
fn test_read_mp4() {
@@ -29,7 +30,7 @@ fn test_read_mp4() {
2930
assert_eq!(t, true);
3031
}
3132

32-
assert_eq!(mp4.duration(), 62);
33+
assert_eq!(mp4.duration(), Duration::from_millis(62));
3334
assert_eq!(mp4.timescale(), 1000);
3435
assert_eq!(mp4.tracks().len(), 2);
3536

0 commit comments

Comments
 (0)