# vim: set ft=python:

import vapoursynth as vs

core = vs.core
core.std.LoadPlugin(
    path="/usr/lib/python3.14/site-packages/vapoursynth/plugins/mvtools.so"
)
clip = video_in

dst_fps = display_fps
# Interpolating to fps higher than 60 is too CPU-expensive, smoothmotion can handle the rest.
while dst_fps > 60:
    dst_fps = 60

dst_fps = 144

# Skip interpolation for > 4K or 60 Hz content due to performance
if not (clip.width > 3840 or clip.height > 2160 or container_fps > 59):
    src_fps_num = int(container_fps * 1e8)
    src_fps_den = int(1e8)
    dst_fps_num = int(dst_fps * 1e4)
    dst_fps_den = int(1e4)
    # Needed because clip FPS is missing
    clip = core.std.AssumeFPS(clip, fpsnum=src_fps_num, fpsden=src_fps_den)
    print(
        "Reflowing from ",
        src_fps_num / src_fps_den,
        " fps to ",
        dst_fps_num / dst_fps_den,
        " fps.",
    )

    sup = core.mv.Super(clip, pel=2, hpad=16, vpad=16)
    bvec = core.mv.Analyse(
        sup, blksize=16, isb=True, chroma=True, search=3, searchparam=1
    )
    fvec = core.mv.Analyse(
        sup, blksize=16, isb=False, chroma=True, search=3, searchparam=1
    )
    clip = core.mv.BlockFPS(
        clip, sup, bvec, fvec, num=dst_fps_num, den=dst_fps_den, mode=3, thscd2=12
    )

clip.set_output()
