320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396 | def replace_ranges(
clip_a: vs.VideoNode,
clip_b: vs.VideoNode,
ranges: FrameRangeN | FrameRangesN | _RangesCallBackT | None,
exclusive: bool = False,
mismatch: bool = False,
*,
prop_src: vs.VideoNode | Sequence[vs.VideoNode] | None = None
) -> vs.VideoNode:
from ..functions import invert_ranges, normalize_ranges
if ranges != 0 and not ranges or clip_a is clip_b:
return clip_a
if not mismatch:
check_ref_clip(clip_a, clip_b)
if callable(ranges):
from inspect import Signature
signature = Signature.from_callable(ranges, eval_str=True)
params = set(signature.parameters.keys())
base_clip = clip_a.std.BlankClip(keep=True, varformat=mismatch, varsize=mismatch)
callback = ranges
if 'f' in params and not prop_src:
raise CustomValueError(
'To use frame properties in the callback (parameter "f"), '
'you must specify one or more source clips via `prop_src`!',
replace_ranges
)
if _is_cb_nf(callback, params):
return vs.core.std.FrameEval(
base_clip, lambda n, f: clip_b if callback(n, f) else clip_a, prop_src, [clip_a, clip_b]
)
if _is_cb_f(callback, params):
return vs.core.std.FrameEval(
base_clip, lambda n, f: clip_b if callback(f) else clip_a, prop_src, [clip_a, clip_b]
)
if _is_cb_n(callback, params):
return vs.core.std.FrameEval(
base_clip, lambda n: clip_b if callback(n) else clip_a, None, [clip_a, clip_b]
)
raise CustomValueError(
'Callback must have signature ((n, f) | (n) | (f)) -> bool!', replace_ranges, callback
)
shift = 1 - exclusive
b_ranges = normalize_ranges(clip_b, ranges)
if hasattr(vs.core, 'vszip'):
return vs.core.vszip.RFS(
clip_a, clip_b,
[y for (s, e) in b_ranges
for y in range(
s, e + (not exclusive if s != e else 1) + (1 if e == clip_b.num_frames - 1 and exclusive else 0)
)
],
mismatch=mismatch
)
a_ranges = invert_ranges(clip_a, clip_b, b_ranges)
a_trims = [clip_a[max(0, start - exclusive):end + shift + exclusive] for start, end in a_ranges]
b_trims = [clip_b[start:end + shift] for start, end in b_ranges]
if a_ranges:
main, other = (a_trims, b_trims) if (a_ranges[0][0] == 0) else (b_trims, a_trims)
else:
main, other = (b_trims, a_trims) if (b_ranges[0][0] == 0) else (a_trims, b_trims)
return vs.core.std.Splice(list(interleave_arr(main, other, 1)), mismatch)
|