Skip to content

Instantly share code, notes, and snippets.

@phiresky
Last active October 9, 2024 00:41
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()
@qyot27
Copy link

qyot27 commented Oct 22, 2021

It's not core = vapoursynth.core(), it's core = vapoursynth.core. No parentheses.

@Ayy-Zee
Copy link

Ayy-Zee commented Oct 22, 2021

It's not core = vapoursynth.core(), it's core = vapoursynth.core. No parentheses.
🤦 Thank you. 🤦 It works again now. Wow...

@TripleK2004
Copy link

TripleK2004 commented Mar 8, 2022

Whats causing this issue

[cplayer] Run command: vf, flags=73, args=[operation="toggle", value="format=yuv420p,vapoursynth=/motioninterpolation.vpy:4:4"]
[cplayer] Passing more than 1 argument to vf-toggle is deprecated!
[user_filter_wrapper] Setting option 'fmt' = 'yuv420p' (flags = 0)
[user_filter_wrapper] Setting option 'file' = '
/motioninterpolation.vpy' (flags = 0)
[user_filter_wrapper] Setting option 'buffered-frames' = '4' (flags = 0)
[user_filter_wrapper] Setting option 'concurrent-frames' = '4' (flags = 0)
[global] config path: 'motioninterpolation.vpy' -> '/home/triplek/.config/mpv/motioninterpolation.vpy'
[global] user path: '~~/motioninterpolation.vpy' -> '/home/triplek/.config/mpv/motioninterpolation.vpy'
[vapoursynth] using 4 concurrent requests.
[vapoursynth] Could not initialize VapourSynth scripting.
[user_filter_wrapper] Creating filter 'vapoursynth' failed.

@hooke007
Copy link

Could not initialize VapourSynth scripting.

No python installed (/ correctly).

@ItsABlackScreen
Copy link

Thanks for the script. Here were the steps required for fedora 35.

  1. sudo dnf install fftw-devel nasm fftw vapoursynth-devel vapoursynth-plugins vapoursynth-tools python3-vapoursynth
  2. git clone https://github.com/dubhater/vapoursynth-mvtools
  3. Then follow the instructions in the repo above under compilation, libmvtools will be built in the build dir inside the repo.
  4. Then see the comment : https://gist.github.com/phiresky/4bfcfbbd05b3c2ed8645?permalink_comment_id=3310945#gistcomment-3310945 and add the path to the library

@Grumbel
Copy link

Grumbel commented Oct 30, 2022

Install instructions for NixOS:

Instead of using pkgs.mpv in environment.systemPackages, use:

(pkgs.wrapMpv
  (pkgs.mpv-unwrapped.override { vapoursynthSupport = true; })
  {
    extraMakeWrapperArgs = [
      "--prefix" "LD_LIBRARY_PATH" ":" "${pkgs.vapoursynth-mvtools}/lib"
    ];
  })

Install the motioninterpolation.vpy as described and replace vapoursynth.get_core() with just vapoursynth.core. And add below (mvtools library won't be found otherwise):

import os
for d in os.environ["LD_LIBRARY_PATH"].split(":"):
    core.std.LoadAllPlugins(os.path.join(d, "vapoursynth"))

@rnhmjoj
Copy link

rnhmjoj commented Oct 2, 2023

The correct way to install vapoursynth plugins on NixOS is to use vapoursynth.withPlugins.
For mpv you would do:

    mpv-unwrapped = super.mpv-unwrapped.override {
      vapoursynthSupport = true;
      vapoursynth = with self; vapoursynth.withPlugins
        [ vapoursynth-mvtools ffms ];
    };

@R8s6
Copy link

R8s6 commented Feb 2, 2024

As of 2024-02-01, on arch, is mpv-git or mpv-vapoursynth still required (instead of just mpv)? thanks!

@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