subclip method

dynamic subclip(
  1. dynamic sourceClip,
  2. dynamic name,
  3. dynamic startFrame,
  4. dynamic endFrame, {
  5. int fps = 30,
})

Implementation

subclip(sourceClip, name, startFrame, endFrame, {int fps = 30}) {
  var clip = sourceClip.clone();

  clip.name = name;

  var tracks = [];

  for (var i = 0; i < clip.tracks.length; ++i) {
    var track = clip.tracks[i];
    var valueSize = track.getValueSize();

    var times = [];
    var values = [];

    for (var j = 0; j < track.times.length; ++j) {
      var frame = track.times[j] * fps;

      if (frame < startFrame || frame >= endFrame) continue;

      times.add(track.times[j]);

      for (var k = 0; k < valueSize; ++k) {
        values.add(track.values[j * valueSize + k]);
      }
    }

    if (times.isEmpty) continue;

    track.times = AnimationUtils.convertArray(times, track.times.constructor);
    track.values = AnimationUtils.convertArray(values, track.values.constructor);

    tracks.add(track);
  }

  clip.tracks = tracks;

  // find minimum .times value across all tracks in the trimmed clip

  var minStartTime = double.infinity;

  for (var i = 0; i < clip.tracks.length; ++i) {
    if (minStartTime > clip.tracks[i].times[0]) {
      minStartTime = clip.tracks[i].times[0];
    }
  }

  // shift all tracks such that clip begins at t=0

  for (var i = 0; i < clip.tracks.length; ++i) {
    clip.tracks[i].shift(-1 * minStartTime);
  }

  clip.resetDuration();

  return clip;
}