|
13 | 13 | from auto_editor.timeline import ASpace, TlAudio, TlVideo, VSpace, v1, v3
|
14 | 14 | from auto_editor.utils.func import mut_margin
|
15 | 15 | from auto_editor.utils.types import Args, CoerceError, time
|
| 16 | +from math import ceil |
16 | 17 |
|
17 | 18 | if TYPE_CHECKING:
|
18 | 19 | from numpy.typing import NDArray
|
@@ -234,16 +235,27 @@ def echunk(
|
234 | 235 | clips: list[Clip] = []
|
235 | 236 | i = 0
|
236 | 237 | start = 0
|
| 238 | + |
| 239 | + last_i = 0 |
| 240 | + |
237 | 241 | for chunk in echunk(speed_index, src_index):
|
238 | 242 | if chunk[3] != 99999:
|
239 |
| - dur = round((chunk[2] - chunk[1]) / chunk[3]) |
| 243 | + dur = int((chunk[2] - chunk[1]) / chunk[3]) |
240 | 244 | if dur == 0:
|
241 | 245 | continue
|
242 | 246 |
|
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-monotonically tl's 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])) |
244 | 258 |
|
245 |
| - if not (clips and clips[-1].start == round(start)): |
246 |
| - clips.append(Clip(start, dur, offset, chunk[3], chunk[0])) |
247 | 259 | start += dur
|
248 | 260 | i += 1
|
249 | 261 |
|
@@ -281,4 +293,25 @@ def chunkify(arr: NDArray, smap: dict[int, float]) -> Chunks:
|
281 | 293 | else:
|
282 | 294 | v1_compatiable = None
|
283 | 295 |
|
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 monotonic timeline because non-monotonic timelines because it's |
| 299 | + # incorrect here and causes back-seeking (perf. issue) in video rendering. |
| 300 | + |
| 301 | + # We don't properly check monotonically 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