302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
)
def _func_nf(
n: int, f: vs.VideoFrame | Sequence[vs.VideoFrame],
callback: _RangesCallBackNF[vs.VideoFrame | Sequence[vs.VideoFrame]]
) -> vs.VideoNode:
return clip_b if callback(n, f) else clip_a
def _func_f(
n: int, f: vs.VideoFrame | Sequence[vs.VideoFrame],
callback: _RangesCallBackF[vs.VideoFrame | Sequence[vs.VideoFrame]]
) -> vs.VideoNode:
return clip_b if callback(f) else clip_a
def _func_n(n: int, callback: _RangesCallBack) -> vs.VideoNode:
return clip_b if callback(n) else clip_a
_func: Callable[..., vs.VideoNode]
if 'f' in params and 'n' in params:
_func = _func_nf
elif 'f' in params:
_func = _func_f
elif 'n' in params:
_func = _func_n
else:
raise CustomValueError(
'Callback must have signature ((n, f) | (n) | (f)) -> bool!', replace_ranges, callback
)
_func.__callback = callback # type: ignore[attr-defined]
_gc_func_gigacope.append(_func)
return vs.core.std.FrameEval(
base_clip, partial(_func, callback=callback), prop_src if 'f' in params else None, [clip_a, clip_b]
)
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)
|