Skip to content

Commit f75ad67

Browse files
committed
Stop monotonic
1 parent b5ebe80 commit f75ad67

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

auto_editor/make_layers.py

+39-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,30 @@ 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 (clips and clips[-1].start == round(start)):
250+
raise ValueError("")
251+
252+
max_end = start + dur - 1
253+
this_i = round((offset + max_end - start) * chunk[3])
254+
255+
if this_i < last_i:
256+
raise ValueError("")
257+
# import pdb; pdb.set_trace()
258+
last_i = this_i
259+
260+
clips.append(Clip(start, dur, offset, chunk[3], chunk[0]))
244261

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

@@ -281,4 +296,23 @@ def chunkify(arr: NDArray, smap: dict[int, float]) -> Chunks:
281296
else:
282297
v1_compatiable = None
283298

284-
return v3(inp, tb, sr, res, args.background, vtl, atl, v1_compatiable)
299+
tl = v3(inp, tb, sr, res, args.background, vtl, atl, v1_compatiable)
300+
301+
# Assert monotonic here because
302+
# 1. non-monotonic timelines cause performance problems in video rendering.
303+
# 2. it should be linear here.
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+
#print(f"{_i=}, {index=}, {lobj.start=} {lobj.dur=}, {lobj.offset=}, {lobj.speed=}")
313+
if (_i < last_i):
314+
print(_i, last_i)
315+
raise ValueError("not monotinic")
316+
317+
last_i = _i
318+
return tl

0 commit comments

Comments
 (0)