pub fn track_to_segments_timed(trak: &TrackBox, timing_segments: &[Segment]) -> io::Result<Vec<Segment>> {
    let media = trak.media();
    let table = media.media_info().sample_table();

    let timescale = media.media_header().timescale as f64;
    let comp_time_shift = trak.composition_time_shift().unwrap_or(0);

    let mut stts_iter = table.time_to_sample().iter();
    let mut ctss_iter = table.composition_time_to_sample().map(|ctts| ctts.iter());

    let mut cur_time = 0;
    let mut seg_duration = 0;
    let mut segments = Vec::new();
    let mut cur_segment = Segment::default();
    cur_segment.start_sample = 1;

    // If our timing_segments track is shorter than this track, keep on
    // going - generate intervals of 6 seconds.
    let mut timing_segments_iter = timing_segments.iter();
    let mut segment_end_time = 0_f64;
    let mut next_segment_end_time = move || {
        timing_segments_iter
            .next()
            .map(|s| s.start_time + s.duration)
            .unwrap_or_else(|| segment_end_time + 6_f64)
    };
    segment_end_time = next_segment_end_time();

    for cur_sample in 1..=u32::MAX {
        let delta = ctss_iter.as_mut().and_then(|iter| iter.next()).unwrap_or(0);

        let sample_duration = match stts_iter.next() {
            Some((d, _)) => d,
            None => {
                // No more samples, we're done. Finish the last segment.
                if cur_segment.start_sample > 0 {
                    cur_segment.end_sample = cur_sample - 1;
                    cur_segment.duration = seg_duration as f64 / timescale;
                    segments.push(cur_segment.clone());
                }
                break;
            },
        };

        // calculate composition time of this sample.
        let tm = cur_time as i64 + (delta as i64) - (comp_time_shift as i64);
        let tm = std::cmp::max(0, tm);
        let cur_comp_time = tm as f64 / timescale;

        // if composition time >= current segment start + duration,
        // we have to start a new segment.
        if cur_comp_time.partial_cmp(&segment_end_time) != Some(Ordering::Less) {
            // Finish the previous segment and push it onto the vec.
            cur_segment.end_sample = cur_sample - 1;
            cur_segment.duration = seg_duration as f64 / timescale;
            segments.push(cur_segment.clone());

            // start new segment.
            cur_segment.start_sample = cur_sample;
            cur_segment.start_time = cur_comp_time;
            seg_duration = 0;
            segment_end_time = next_segment_end_time();
        }
        seg_duration += sample_duration;
        cur_time += sample_duration;
    }

    Ok(segments)
}
/// Parse a track into segments, based on a list of segment time/duration.
///
/// Used for audio tracks.
Parse a track into segments, based on a list of segment time/duration.
Used for audio tracks.
Parse a track into segments based on a list of segment time / duration . Used for audio tracks .
pub fn track_to_segments_timed(trak: &TrackBox, timing_segments: &[Segment]) -> io::Result<Vec<Segment>> {
    let media = trak.media();
    let table = media.media_info().sample_table();
    let timescale = media.media_header().timescale as f64;
    let comp_time_shift = trak.composition_time_shift().unwrap_or(0);
    let mut stts_iter = table.time_to_sample().iter();
    let mut ctss_iter = table.composition_time_to_sample().map(|ctts| ctts.iter());
    let mut cur_time = 0;
    let mut seg_duration = 0;
    let mut segments = Vec::new();
    let mut cur_segment = Segment::default();
    cur_segment.start_sample = 1;
    // If our timing_segments track is shorter than this track, keep on
    // going - generate intervals of 6 seconds.
    let mut timing_segments_iter = timing_segments.iter();
    let mut segment_end_time = 0_f64;
    let mut next_segment_end_time = move || {
        timing_segments_iter
            .next()
            .map(|s| s.start_time + s.duration)
            .unwrap_or_else(|| segment_end_time + 6_f64)
    };
    segment_end_time = next_segment_end_time();
    for cur_sample in 1..=u32::MAX {
        let delta = ctss_iter.as_mut().and_then(|iter| iter.next()).unwrap_or(0);
        let sample_duration = match stts_iter.next() {
            Some((d, _)) => d,
            None => {
                // No more samples, we're done. Finish the last segment.
                if cur_segment.start_sample > 0 {
                    cur_segment.end_sample = cur_sample - 1;
                    cur_segment.duration = seg_duration as f64 / timescale;
                    segments.push(cur_segment.clone());
                }
                break;
            },
        };
        // calculate composition time of this sample.
        let tm = cur_time as i64 + (delta as i64) - (comp_time_shift as i64);
        let tm = std::cmp::max(0, tm);
        let cur_comp_time = tm as f64 / timescale;
        // if composition time >= current segment start + duration,
        // we have to start a new segment.
        if cur_comp_time.partial_cmp(&segment_end_time) != Some(Ordering::Less) {
            // Finish the previous segment and push it onto the vec.
            cur_segment.end_sample = cur_sample - 1;
            cur_segment.duration = seg_duration as f64 / timescale;
            segments.push(cur_segment.clone());
            // start new segment.
            cur_segment.start_sample = cur_sample;
            cur_segment.start_time = cur_comp_time;
            seg_duration = 0;
            segment_end_time = next_segment_end_time();
        }
        seg_duration += sample_duration;
        cur_time += sample_duration;
    }
    Ok(segments)
}
pub fn track_to_segments_timed ( trak : & TrackBox , timing_segments : & [ Segment ] ) -> io :: Result < Vec < Segment > > { let media = trak . media ( ) ; let table = media . media_info ( ) . sample_table ( ) ; let timescale = media . media_header ( ) . timescale as f64 ; let comp_time_shift = trak . composition_time_shift ( ) . unwrap_or ( 0 ) ; let mut stts_iter = table . time_to_sample ( ) . iter ( ) ; let mut ctss_iter = table . composition_time_to_sample ( ) . map ( | ctts | ctts . iter ( ) ) ; let mut cur_time = 0 ; let mut seg_duration = 0 ; let mut segments = Vec :: new ( ) ; let mut cur_segment = Segment :: default ( ) ; cur_segment . start_sample = 1 ; // If our timing_segments track is shorter than this track, keep on // going - generate intervals of 6 seconds. let mut timing_segments_iter = timing_segments . iter ( ) ; let mut segment_end_time = 0_f64 ; let mut next_segment_end_time = move | | { timing_segments_iter . next ( ) . map ( | s | s . start_time + s . duration ) . unwrap_or_else ( | | segment_end_time + 6_f64 ) } ; segment_end_time = next_segment_end_time ( ) ; for cur_sample in 1 ..= u32 :: MAX { let delta = ctss_iter . as_mut ( ) . and_then ( | iter | iter . next ( ) ) . unwrap_or ( 0 ) ; let sample_duration = match stts_iter . next ( ) { Some ( ( d , _ ) ) => d , None => { // No more samples, we're done. Finish the last segment. if cur_segment . start_sample > 0 { cur_segment . end_sample = cur_sample - 1 ; cur_segment . duration = seg_duration as f64 / timescale ; segments . push ( cur_segment . clone ( ) ) ; } break ; } , } ; // calculate composition time of this sample. let tm = cur_time as i64 + ( delta as i64 ) - ( comp_time_shift as i64 ) ; let tm = std :: cmp :: max ( 0 , tm ) ; let cur_comp_time = tm as f64 / timescale ; // if composition time >= current segment start + duration, // we have to start a new segment. if cur_comp_time . partial_cmp ( & segment_end_time ) != Some ( Ordering :: Less ) { // Finish the previous segment and push it onto the vec. cur_segment . end_sample = cur_sample - 1 ; cur_segment . duration = seg_duration as f64 / timescale ; segments . push ( cur_segment . clone ( ) ) ; // start new segment. cur_segment . start_sample = cur_sample ; cur_segment . start_time = cur_comp_time ; seg_duration = 0 ; segment_end_time = next_segment_end_time ( ) ; } seg_duration += sample_duration ; cur_time += sample_duration ; } Ok ( segments ) }
Parse a track into segments, based on a list of segment time/duration.
Parse a track into segments based on a list of segment time / duration .
trak
&TrackBox
timing_segments
&[Segment]
trak
&TrackBox
timing_segments
&[Segment]