Skip to content

Commit 91af75a

Browse files
committed
Assert monotonic timelines
1 parent b5ebe80 commit 91af75a

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

auto_editor/make_layers.py

+38-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from auto_editor.timeline import ASpace, TlAudio, TlVideo, VSpace, v1, v3
1414
from auto_editor.utils.func import mut_margin
1515
from auto_editor.utils.types import Args, CoerceError, time
16+
from math import ceil
1617

1718
if TYPE_CHECKING:
1819
from numpy.typing import NDArray
@@ -234,16 +235,27 @@ def echunk(
234235
clips: list[Clip] = []
235236
i = 0
236237
start = 0
238+
239+
last_i = 0
240+
237241
for chunk in echunk(speed_index, src_index):
238242
if chunk[3] != 99999:
239-
dur = round((chunk[2] - chunk[1]) / chunk[3])
243+
dur = int((chunk[2] - chunk[1]) / chunk[3])
240244
if dur == 0:
241245
continue
242246

243-
offset = int(chunk[1] / chunk[3])
247+
offset = ceil(chunk[1] / chunk[3])
248+
249+
if len(sources) == 1:
250+
# Try to detect non-monotonicity early
251+
max_end = start + dur - 1
252+
this_i = round((offset + max_end - start) * chunk[3])
253+
if this_i < last_i:
254+
raise ValueError("not monotonic", sources, this_i, last_i)
255+
last_i = this_i
256+
257+
clips.append(Clip(start, dur, offset, chunk[3], chunk[0]))
244258

245-
if not (clips and clips[-1].start == round(start)):
246-
clips.append(Clip(start, dur, offset, chunk[3], chunk[0]))
247259
start += dur
248260
i += 1
249261

@@ -281,4 +293,25 @@ def chunkify(arr: NDArray, smap: dict[int, float]) -> Chunks:
281293
else:
282294
v1_compatiable = None
283295

284-
return v3(inp, tb, sr, res, args.background, vtl, atl, v1_compatiable)
296+
tl = v3(inp, tb, sr, res, args.background, vtl, atl, v1_compatiable)
297+
298+
# Assert timeline is monotonic because non-monotonic timelines are incorrect
299+
# here and causes back-seeking (performance issue) in video rendering.
300+
301+
# We don't properly check monotonicity for multiple sources. skip.
302+
if len(sources) != 1:
303+
return tl
304+
305+
last_i = 0
306+
for index in range(tl.end):
307+
for layer in tl.v:
308+
for lobj in layer:
309+
assert isinstance(lobj, TlVideo)
310+
if index >= lobj.start and index < (lobj.start + lobj.dur):
311+
_i = round((lobj.offset + index - lobj.start) * lobj.speed)
312+
if (_i < last_i):
313+
print(_i, last_i)
314+
raise ValueError("not monotonic")
315+
316+
last_i = _i
317+
return tl

0 commit comments

Comments
 (0)