Skip to content

Instantly share code, notes, and snippets.

@alemonmk
Last active February 21, 2021 15:48
Show Gist options
  • Save alemonmk/4182404c083a2a25d33a to your computer and use it in GitHub Desktop.
Save alemonmk/4182404c083a2a25d33a to your computer and use it in GitHub Desktop.
Use VapourSynth to extract video frames for comparision
import vapoursynth as vs
import random
from os import mkdir
from os.path import exists, basename, dirname, splitext
import numpy as np
import cv2 as cv
import mvsfunc as mvs
core = vs.get_core()
encodeddir = r''
#copy as much as you need
bdmvdirs = [
r''
]
compare_vids_group = [
[
#This order is not hard requirement
bdmvdirs[0] + '',
encodeddir + '',
],
]
comppicdir = encodeddir + 'com/'
if not exists(comppicdir): mkdir(comppicdir)
for group in compare_vids_group:
print('Reading ' + group[0])
src_o = core.lsmas.LWLibavSource(group[0])
print('Reading ' + group[1])
src_e1 = core.lsmas.LWLibavSource(group[1])
#print('Reading ' + group[2])
#src_e2 = core.lsmas.LWLibavSource(group[2])
epdir = comppicdir + '{ep}/'.format(ep=splitext(basename(group[2]))[0])
if not exists(epdir): mkdir(epdir)
total_frames = src_o.num_frames
frames = []
#Randomly insert or just fill the list above
for i in range(10): frames.append(random.randint(0, total_frames))
frames.sort()
for fr in frames:
clips = [
src_o[fr],
src_e1[fr],
#src_e2[fr]
]
for cl in clips:
#10 bit source with depth=8 will make cv.merge() give you picture with only darkness
rgbcl = mvs.ToRGB(input=cl, depth=16, kernel='spline64')
rgbcl = rgbcl.get_frame(0)
#final path to output image file:
#%ep% derived your encoded filename
#%frame% self explaintory
#%source% where this frame come from
#%encodeddir%/com/%ep%/%frame%-%source%.png
fntmpl = epdir + '{fr}-{srcf}.png'
srcbase = basename(group[clips.index(cl)])
nbase = splitext(srcbase)[0]
nfn = fntmpl.format(srcf=nbase, fr=fr)
print('Saving frame {n} of {src} to {fn}'.format(n=fr, src=srcbase, fn=nfn))
planes_count = rgbcl.format.num_planes
v = cv.merge([np.array(rgbcl.get_read_array(i), copy=False) for i in reversed(range(planes_count))])
cv.imwrite(nfn, v)
@alemonmk
Copy link
Author

Get OpenCV and NumPy here for easy install
mvsfunc

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