diff --git a/auto_editor/make_layers.py b/auto_editor/make_layers.py index 329b84b37..a0b229021 100644 --- a/auto_editor/make_layers.py +++ b/auto_editor/make_layers.py @@ -13,6 +13,7 @@ from auto_editor.timeline import ASpace, TlAudio, TlVideo, VSpace, v1, v3 from auto_editor.utils.func import mut_margin from auto_editor.utils.types import Args, CoerceError, time +from math import ceil if TYPE_CHECKING: from numpy.typing import NDArray @@ -234,16 +235,27 @@ def echunk( clips: list[Clip] = [] i = 0 start = 0 + + last_i = 0 + for chunk in echunk(speed_index, src_index): if chunk[3] != 99999: - dur = round((chunk[2] - chunk[1]) / chunk[3]) + dur = int((chunk[2] - chunk[1]) / chunk[3]) if dur == 0: continue - offset = int(chunk[1] / chunk[3]) + offset = ceil(chunk[1] / chunk[3]) + + if len(sources) == 1: + # Try to detect non-monotonically tl's early + max_end = start + dur - 1 + this_i = round((offset + max_end - start) * chunk[3]) + if this_i < last_i: + raise ValueError("not monotonic", sources, this_i, last_i) + last_i = this_i + + clips.append(Clip(start, dur, offset, chunk[3], chunk[0])) - if not (clips and clips[-1].start == round(start)): - clips.append(Clip(start, dur, offset, chunk[3], chunk[0])) start += dur i += 1 @@ -281,4 +293,25 @@ def chunkify(arr: NDArray, smap: dict[int, float]) -> Chunks: else: v1_compatiable = None - return v3(inp, tb, sr, res, args.background, vtl, atl, v1_compatiable) + tl = v3(inp, tb, sr, res, args.background, vtl, atl, v1_compatiable) + + # Assert monotonic timeline because non-monotonic timelines because it's + # incorrect here and causes back-seeking (perf. issue) in video rendering. + + # We don't properly check monotonically for multiple sources. skip. + if len(sources) != 1: + return tl + + last_i = 0 + for index in range(tl.end): + for layer in tl.v: + for lobj in layer: + assert isinstance(lobj, TlVideo) + if index >= lobj.start and index < (lobj.start + lobj.dur): + _i = round((lobj.offset + index - lobj.start) * lobj.speed) + if (_i < last_i): + print(_i, last_i) + raise ValueError("not monotonic") + + last_i = _i + return tl