Forums > Encoding > Source vs. Encode with VapourSynth natively under Linux or macOS

#1363964 by BigMittens (Power User) [ Jun 13 2017, 05:20 ] - [Quote] [Report Post]  
BigMittens's avatar
Just finished with a quick script for Source vs. Encode screenshots using VapourSynth.

Most of this comes from keyfan from his guide, so you may want to use his post as a reference for some of the things not mentioned here. I've added some minor changes to use the subtitle filter rather than the bitmap text filter and I'm reposting this because the old thread has pruned. Using the subtitle filter this should give you nearly identical results to AviSynth + AvsPmod.

First, install vapoursynth and (optionally) vapoursynth-editor. Depending on your distribution or OS this may involve adding repositories or compiling from source.

Then save this as a vapoursynth script:
import vapoursynth as vs
import functools

core = vs.get_core()

# frame number and picture type
def frameInfo(n, clip):
clip = core.sub.Subtitle(clip, "Frame Number: " + str(n) + " of " + str(clip.num_frames)
+ "\nPicture Type: " + clip.get_frame(n).props._PictType.decode())
return clip

source = core.ffms2.Source('path/to/source')
# crop to match encode
source = core.std.CropRel(source, LEFT, RIGHT, TOP, BOTTOM)
# source text
source = core.sub.Subtitle(source, text=[' \n \n Source'])
# trim source to match encode if needed
#source = core.std.Trim(source, first=FIRST, last=(encode.num_frames - 1))

encode = core.ffms2.Source('path/to/encode')
# encode text
encode = core.sub.Subtitle(encode, text=[' \n \nEncode'])
# fixes "unrecognized color primaries" errors in vsedit
#encode = core.resize.Spline16(encode, matrix_in_s="709", primaries_in_s="709")

# interleave source/encode
res = core.std.Interleave([encode, source])
# adds info to each frame
res = core.std.FrameEval(res, functools.partial(frameInfo, clip=res))
# for anamorphic resize (not necessarily needed for anamorphic clips)
#par = source.get_frame(0).props._SARNum / source.get_frame(0).props._SARDen
#res = core.resize.Spline36(res, int(res.width * par / 2) * 2, res.height)

res.set_output()

Simply edit in the paths + crop values + optional filters, and remove comments for what you need if you're having problems. Then use the script with vsedit or vspipe, and the screenshots will end up looking like this:
Source, Encode: Show comparison

If this is too brief, there are many things in keyfan's post above that I didn't mention and go into greater detail. The VapourSynth documentation will also be helpful as a reference.
Last edited by BigMittens [ Mar 10 2018, 21:53 ]
#1364003 by Exemplu (Torrent Master) [ Jun 13 2017, 08:48 ] - [Quote] [Report Post]  
Exemplu's avatar
Thank you for this tutorial
#1364277 by BigMittens (Power User) [ Jun 13 2017, 23:27 ] - [Quote] [Report Post]  
BigMittens's avatar
Exemplu wrote: View Post
Thank you for this tutorial

Hope it helps.
#1465971 by chacalbhzDonor (Torrent Master) [ Feb 03 2018, 02:25 ] - [Quote] [Report Post]  
chacalbhz's avatar
#1686709 by DigitalJosee (Elite) (Custom what?) [ Sep 29 2019, 20:02 ] - [Quote] [Report Post]  
DigitalJosee's avatar
I'm dumb or this doesn't work?

Probably the first option tho, but may I ask how to use it?
Would vspipe script.vpy ./ work?

EDIT: Ok, I managed to make it work, but there's no info on the image, maybe I'm missing something?

https://i.imgur.com/YkJV8n7.jpg

nvmd, It's working, thx!
Last edited by DigitalJosee [ Sep 29 2019, 21:56 ]
#1818628 by dosocat (Power User) [ Sep 01 2020, 15:26 ] - [Quote] [Report Post]  
dosocat's avatar
@BigMittens THANK YOU SO MUCH FOR THIS POST. I was wrestling around with AvsBmod and having absolutely no luck, but this made it super easy. You should turn this into a wiki article! so useful.

Would you describe what's happening in these lines?


# for anamorphic resize (not necessarily needed for anamorphic clips)
#par = source.get_frame(0).props._SARNum / source.get_frame(0).props._SARDen
#res = core.resize.Spline36(res, int(res.width * par / 2) * 2, res.height)


I'm not sure I 100% grasp when to use it, or why.

Also, is there a reason to choose Spline36 for the resizes instead of Bicubic? I only ask because the docs say that one is "default", and I want to understand more!

#1818644 by Aicha (Power User) [ Sep 01 2020, 16:10 ] - [Quote] [Report Post]  
Aicha's avatar
dosocat wrote: View Post
Would you describe what's happening in these lines?


# for anamorphic resize (not necessarily needed for anamorphic clips)
#par = source.get_frame(0).props._SARNum / source.get_frame(0).props._SARDen
#res = core.resize.Spline36(res, int(res.width * par / 2) * 2, res.height)


I'm not sure I 100% grasp when to use it, or why.

Also, is there a reason to choose Spline36 for the resizes instead of Bicubic? I only ask because the docs say that one is "default", and I want to understand more!
I believe you would do something like that if you're dealing with a DVD or similar, where metadata tells the video player to stretch the frame to the given aspect ratio. If you're only working with Blu-rays or similar HD content, you should (almost) never encounter this.

Bicubic is usually considered too blurry compared to Spline36. This is probably more obvious if you upscale clips. You can do this to visualize it:
spline = src.resize.Spline36(src.width * 2, src.height * 2, dither_type="error_diffusion")
spline = spline.sub.Subtitle("Spline36")
# mitchell is usually the go-to, iirc bicubic defaults to catmull so that the default's an interpolation filter
bicubic = src.resize.Bicubic(src.width * 2, src.height * 2, dither_type="error_diffusion", filter_param_a=1/3, filter_param_b=1/3)
bicubic = bicubic.sub.Subtitle("Mitchell Bicubic")
out = core.std.Interleave([spline, bicubic])

Here's a quick rundown of bicubic resampling (spline is underneath): https://guide.encode.moe/encoding/resampling.html#mitchell-netravali--bicubic
#1818682 by dosocat (Power User) [ Sep 01 2020, 19:10 ] - [Quote] [Report Post]  
dosocat's avatar
Much appreciated @Aicha!! Thanks for the in depth response and sources.
#1818753 by BigMittens (Power User) [ Sep 01 2020, 23:11 ] - [Quote] [Report Post]  
BigMittens's avatar
Thank you dosocat and Aicha. I would also recommend checking out the new wiki article's section on doing comparisons with svgsfunc.
#1818797 by dosocat (Power User) [ Sep 02 2020, 03:08 ] - [Quote] [Report Post]  
dosocat's avatar
Incredible source! I've been using Handbrake, but I think I'll try an encode entirely with VS next time.

Luckily my source was pretty good, so no real issues that needed filtering for this one.

That link is going in my bookmarks for sure!

Forums > Encoding > Source vs. Encode with VapourSynth natively under Linux or macOS