Skip to content

Instantly share code, notes, and snippets.

@phiresky
Last active November 19, 2024 17:24
Show Gist options
  • Save phiresky/4bfcfbbd05b3c2ed8645 to your computer and use it in GitHub Desktop.
Save phiresky/4bfcfbbd05b3c2ed8645 to your computer and use it in GitHub Desktop.
Realtime motion interpolating 60fps playback in mpv

Realtime motion interpolating playback in mpv

Edit 2016-09: SVP now has a free (but proprietary) version of their software, which includes a GPU port of the MVTools functions, making this much less resource intense: https://www.svp-team.com/wiki/SVP:Linux (AUR)


run this for SVP-like motion interpolation:

cd ~/.config/mpv # or ~/.mpv

wget https://gist.githubusercontent.com/phiresky/4bfcfbbd05b3c2ed8645/raw/motioninterpolation.vpy

echo 'I vf toggle format=yuv420p,vapoursynth=~~/motioninterpolation.vpy:4:4' >> input.conf

# to enable it by default
echo 'vf=format=yuv420p,vapoursynth=~~/motioninterpolation.vpy:4:4' >> mpv.conf

now press shift+i while mpv is playing a video to toggle beautiful motion vector interpolation of the video framerate to your display refresh rate.

You need mpv compiled with VapourSynth support. Not sure what distributions do this, in Arch Linux you can get mpv-git or mpv-vapoursynth (AUR) and vapoursynth-plugin-mvtools.

It should also be possible to make this work through SMPlayer.

Warning: This is CPU intensive. The settings work on a i5-2500k cpu. If it lags you will have to change some parameters, look for 2500k in the script. If your CPU is faster remove that block.

If your CPU is really fast, try removing the 'mode' parameter and changing BlockFPS to FlowFPS, this gives much better results.

See the reddit post for more information

Encoding video to a higher framerate

Run this:

vspipe --arg in_filename=input.mkv --arg display_fps=60 --y4m motioninterpolation.vpy -|ffmpeg -i - -crf 18 output.mkv
# vim: set ft=python:
# see the README at https://gist.github.com/phiresky/4bfcfbbd05b3c2ed8645
# source: https://github.com/mpv-player/mpv/issues/2149
# source: https://github.com/mpv-player/mpv/issues/566
# source: https://github.com/haasn/gentoo-conf/blob/nanodesu/home/nand/.mpv/filters/mvtools.vpy
import vapoursynth
core = vapoursynth.get_core()
# ref: http://avisynth.org.ru/mvtools/mvtools2.html#functions
# default is 400, less means interpolation will only happen when it will work well
ignore_threshold=140
# if n% of blocks change more than threshold then don't interpolate at all (default is 51%)
scene_change_percentage=15
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 /= 2
if "video_in" in globals():
# realtime
clip = video_in
# Needed because clip FPS is missing
src_fps_num = int(container_fps * 1e8)
src_fps_den = int(1e8)
clip = core.std.AssumeFPS(clip, fpsnum = src_fps_num, fpsden = src_fps_den)
else:
# run with vspipe
clip = core.ffms2.Source(source=in_filename)
dst_fps=float(dst_fps)
# resolution in megapixels. 1080p ≈ 2MP, 720p ≈ 1MP
mpix = clip.width * clip.height / 1000000
# Skip interpolation for >1080p or 60 Hz content due to performance
if not (mpix > 2.5 or clip.fps_num/clip.fps_den > 59):
analParams = {
'overlap': 0,
'search': 3,
'truemotion': True,
#'chrome': True,
#'blksize':16,
#'searchparam':1
}
blockParams = {
'thscd1': ignore_threshold,
'thscd2': int(scene_change_percentage*255/100),
'mode': 3,
}
if mpix > 1.5:
# can't handle these on Full HD with Intel i5-2500k
# see the description of these parameters in http://avisynth.org.ru/mvtools/mvtools2.html#functions
analParams['search'] = 0
blockParams['mode'] = 0
quality = 'low'
else:
quality = 'high'
dst_fps_num = int(dst_fps * 1e4)
dst_fps_den = int(1e4)
print("Reflowing from {} fps to {} fps (quality={})".format(clip.fps_num/clip.fps_den,dst_fps_num/dst_fps_den,quality))
sup = core.mv.Super(clip, pel=2)
bvec = core.mv.Analyse(sup, isb=True, **analParams)
fvec = core.mv.Analyse(sup, isb=False, **analParams)
clip = core.mv.BlockFPS(clip, sup, bvec, fvec,
num=dst_fps_num, den=dst_fps_den,
**blockParams)
clip.set_output()
@daher13
Copy link

daher13 commented Apr 11, 2024

I'm trying to use the encode command:
vspipe --arg in_filename=input.mkv --arg display_fps=60 --y4m motioninterpolation.vpy -|ffmpeg -i - -crf 18 output.mkv
It's returning the error:
[in#0 @ 0x62f666cc84c0] Error opening input: Invalid data found when processing input Error opening input file -. Error opening input files: Invalid data found when processing input
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment