Skip to content

Instantly share code, notes, and snippets.

@city96
Created August 5, 2023 15:39
Show Gist options
  • Save city96/153db27d0a1ffd7a7dc69e812a4b4110 to your computer and use it in GitHub Desktop.
Save city96/153db27d0a1ffd7a7dc69e812a4b4110 to your computer and use it in GitHub Desktop.
rife_deflicker
#
# Deflicker videos by only grabbing the interpolated frames.
# Helps to smoothe over AI animations, at least somewhat.
# Edit the cmd on line 40 to change the model from whatever the default is.
#
# You'll need this in the same folder https://github.com/nihui/rife-ncnn-vulkan + ffmpeg in path
#
import os
from shutil import copyfile
# Settings
video = "test.webm"
framerate = "8"
rife_dir = "rife-ncnn-vulkan"
ffmpeg = "ffmpeg"
# Folder check
if not os.path.isdir("src"):
os.mkdir("src")
if len(os.listdir("src")) > 0:
input("folder 'src' not empty")
# exit(1)
if not os.path.isdir("tmp"):
os.mkdir("tmp")
if len(os.listdir("tmp")) > 0:
input("folder 'tmp' not empty")
exit(1)
if not os.path.isdir("dst"):
os.mkdir("dst")
if len(os.listdir("dst")) > 0:
input("folder 'dst' not empty")
exit(1)
# Video to image
cmd = f"{ffmpeg} -i \"{video}\" src\%04d.png"
os.system(cmd)
# Interpolate
cmd = f"{os.path.join(rife_dir,'rife-ncnn-vulkan.exe')} -i src -o tmp -m {os.path.join(rife_dir,'rife-v4.6')} -f %04d.png"
os.system(cmd)
# Copy frames
i = 1
while True:
a = os.path.join("tmp",f"{i*2:04d}.png")
b = os.path.join("dst",f"{i:04d}.png")
if os.path.isfile(a):
print((a,b))
copyfile(a,b)
else:
break
i += 1
# Image to video
cmd = f"{ffmpeg} -i \"{video}\" -r {framerate} -i dst\%04d.png -map 0:a:0? -map 1:v:0 -c:v libx264 -crf 21 -pix_fmt yuv420p \"{video+'_out.mp4'}\""
os.system(cmd)
input("\nDone!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment