Code:
#VIDEO FILE
#----------------------------------------------------------------------------------------------------------------------------
#Change the following line to point to your video file
film="E:\EXPORT_PREMIERE\SUPER8\xmas okanogan 1975\XMAS 1975 OKANOGAN_xvdb980.avi"
#GENERAL PARAMETERS
#----------------------------------------------------------------------------------------------------------------------------
result="result1" #specify the wanted output here; valid results are "result1" through "result4" and "resultS1" through "resultS4" for before/after
trim_begin=20 trim_end=10 play_speed=18 #play speed (8mm=16; Super8=18;)
#COLOR AND LEVELS PARAMATERS
#----------------------------------------------------------------------------------------------------------------------------
saturation = 1.0 #for all outputs
gamma = 1.0 #for all outputs
#GamMac Parameters
LockChan = 1 #(0=red channel)
LockVal = 128.0 #default 128 -- Used when LockChan = -1 (for flicker)
Scale = 2 #Fred recommended 2 instead of 1
RedMul = 1.0
GrnMul = 1.0
BluMul = 1.0
Th = 0.1
GMx = 0
GMy = 0
GMw = 0
GMh = 0
LOTH = 0.2
HITH = 0.2
OMIN = 0 #limiting the output a little bit makes it a little 'softer' to look at
OMAX = 255
Al2 = 20
autolev_bord1 = 50
borderV=10 borderH=10
#SIZE, CROP AND BORDERS PARAMETERS
#----------------------------------------------------------------------------------------------------------------------------
CLeft=8 CTop=8 CRight=8 CBottom=8 #crop values after Depan and before final resizing
W=1470 H=1080 #final size after cropping
bord_left=0 bord_top=0 bord_right=0 bord_bot=0 #720p= borders 150
in_bord_left=0 in_bord_top=0 in_bord_right=0 in_bord_bot=0 #Borders around input that must be removed
#STABILISING PARAMETERS
#----------------------------------------------------------------------------------------------------------------------------
maxstabH=10 #maximum values for the stabiliser (in pixels) 20 is a good start value
maxstabV=10
est_left=40 est_top=40 est_right=40 est_bottom=40 #crop and contast values for special Estimate clip
est_cont=1.1 #Too large a value defeats stabilization
#DENOISING PARAMETERS
#----------------------------------------------------------------------------------------------------------------------------
#denoising_strength=600 #denoising level of first denoiser: MVDegrain()
#block_size= 16 #block size of MVDegrain
#block_size_v= 16
#block_over= 8 #block overlapping of MVDegrainMulti()
#Alternate denoising using smaller block size
#denoising_strength= 600 #denoising level of first denoiser: MDegrain()
#block_size= 8 #block size of MVDegrain
#block_size_v= 8
#block_over= 4 #block overlapping of MVDegrainMulti()
#Alternate denoising using smallest block size
denoising_strength= 300 #denoising level of first denoiser: MDegrain()
block_size= 4 #block size of MVDegrain
block_size_v= 4
block_over= 2 #block overlapping of MVDegrainMulti()
dirt_strength=25 #sets amount of dirt removal (big spots)
#FOUR STEP SHARPENING PARAMETERS
#--------------------------------------------------------------------------------------------------------------------------------
#120 is pretty aggressive; 85 seems more subtle
#PRE_sharp_ness= 120 PRE_radi_us= 3 #presharpening (UnsharpMask) just after first denoising
#PRE_sharp_ness= 85 PRE_radi_us= 3 #presharpening (UnsharpMask) just after first denoising
PRE_sharp_ness= 105 PRE_radi_us= 3 #presharpening (UnsharpMask) just after first denoising
Sharp_Strength = 0.1 #0.05 is often better (avoids too much sharpening)
#AUTO LEVELS PARAMETER
#--------------------------------------------------------------------------------------------------------------------------------
X=4 #X parameter for reducing the autolevels effect on the whites
X2=4 #X2 parameter for reducing the autolevels effect on the blacks
#NUMBER OF THREADS
#--------------------------------------------------------------------------------------------------------------------------------
threads=4
# END VARIABLES, BEGIN SCRIPT
#=================================================================================================================================
#Memory Settings Sets the maximum memory that AviSynth uses (in MB) to the value of amount.
SetMemoryMax(1024)
/*In the 2.5 series the default Memory Max value is 25% of the free physical memory, with a minimum of 16MB.
#The default Memory Max is also limited to 512MB.
#Too high values can result in crashes because of 2GB address space limit
*/
#Load plugins explicitly
LoadPlugin("C:\Program Files\AviSynth+\plugins+\ffms2.dll")
LoadPlugin("C:\Program Files\AviSynth+\plugins+\mvtools2.dll") #Version 2.5.11.9 2/24/2012
LoadPlugin("C:\Program Files\AviSynth+\plugins+\Deflicker.dll") #Version 0.4.0.0 8/16/2004
Loadplugin("C:\Program Files\AviSynth+\plugins+\DePan.dll") #Version 1.10.0.0 4/09/2007
LoadPlugin("C:\Program Files\AviSynth+\plugins+\DePanEstimate.dll") #Version 1.9.2.0 3/25/2007
LoadPlugin("C:\Program Files\AviSynth+\plugins+\fft3dfilter.dll") #Version 2.1.1.0 2/20/2007
Loadplugin("C:\Program Files\AviSynth+\plugins+\masktools2.dll") #Version 2.0.23.0 3/14/2008
Loadplugin("C:\Program Files\AviSynth+\plugins+\RemoveDirt.dll") #Version 0.9 5/05/2005
Loadplugin("C:\Program Files\AviSynth+\plugins+\RgTools.dll") #Version 0.9 5/05/2005
Loadplugin("C:\Program Files\AviSynth+\plugins+\warpsharp.dll") # 4/05/2010
Loadplugin("C:\Program Files\AviSynth+\plugins+\FredAverage_x86.dll") #Version 0.3.0.0 01/19/2019
Loadplugin("C:\Program Files\AviSynth+\plugins+\GamMac_x86.dll") #Version 1.10.0.0 06/15/2018
#Use the following to open with FFVideo using ffm2 plugin
#source1= FFVideoSource(film).killaudio().assumefps(play_speed).converttoYV12()
#Use the following to open with Avisource()
#source1= Avisource(film).killaudio().assumefps(play_speed).converttoYV12()
#Open & using the Clip Trim Options
source= FFVideoSource(film).killaudio().assumefps(play_speed).trim(trim_begin,0).converttoYV12()
trimming= framecount(source)-trim_end
source1= trim(source,0,trimming)
cropped_source=source1.crop(in_bord_left,in_bord_top,-in_bord_right,-in_bord_bot) #remove any black borders on input video
#cropped_source=filldrops(cropped_source) #Use this when removing bad frames that have been removed by duplicating previous frame
#STABILIZING
#....................................................................................................................................................................
stab_reference= cropped_source.crop(est_left,est_top,-est_right,-est_bottom).tweak(cont=est_cont).MT_binarize(threshold=80).greyscale().invert()
mdata=DePanEstimate(stab_reference,trust=1.0,dxmax=maxstabH,dymax=maxstabV)
#stab=DePanStabilize(cropped_source,data=mdata,cutoff=0.5,dxmax=maxstabH,dymax=maxstabV,method=1,mirror=15).deflicker()
stab=DePanStabilize(cropped_source,data=mdata,cutoff=0.5,dxmax=maxstabH,dymax=maxstabV,method=1,mirror=15)
#DENOISING
#...................................................................................................................................................................
input_to_removedirt=stab.crop(CLeft,CTop,-CRight,-CBottom)
stabcrop=RemoveDirtMC(input_to_removedirt,dirt_strength,false)
prefiltered = RemoveGrain(stabcrop,2)
superfilt = MSuper(prefiltered, hpad=32, vpad=32,pel=2)
super= MSuper(stabcrop, hpad=32, vpad=32,pel=2)
halfblksize= (block_size>4) ? block_size/2 : 4
halfoverlap= (block_over>2) ? block_over/2 : 2
bvec1 = MAnalyse(superfilt, isb = true, delta = 1, blksize=block_size, overlap=block_over,dct=0)
bvec1 = MRecalculate(super, bvec1, blksize=halfblksize, overlap=halfoverlap,thSAD=100)
fvec1 = MAnalyse(super, isb = false, delta = 1, blksize=block_size, overlap=block_over,dct=0)
fvec1 = MRecalculate(super, fvec1, blksize=halfblksize, overlap=halfoverlap,thSAD=100)
bvec2 = MAnalyse(super, isb = true, delta = 2, blksize=block_size, overlap=block_over,dct=0)
bvec2 = MRecalculate(super, bvec2, blksize=halfblksize, overlap=halfoverlap,thSAD=100)
fvec2 = MAnalyse(super, isb = false, delta = 2, blksize=block_size, overlap=block_over,dct=0)
fvec2 = MRecalculate(super, fvec2, blksize=halfblksize, overlap=halfoverlap,thSAD=100)
denoised=stabcrop.MDegrain2(super, bvec1,fvec1,bvec2,fvec2,thSAD=denoising_strength).levels(0,gamma,255,0,255).tweak(sat=saturation).unsharpmask(PRE_sharp_ness,PRE_radi_us,0)
#SHARPENING
#...................................................................................................................................................................
sharp1=denoised.sharpen(Sharp_Strength)
PreBorderFrame = sharp1.Lanczos4Resize(W - bord_left - in_bord_left - bord_right - in_bord_right, H - bord_top - in_bord_top - bord_bot - in_bord_bot)
#FRAME INTERPOLATION (optional)
#...................................................................................................................................................................
#Everything in this section is commented out because I usually choose NOT to do frame interpolation because
#it causes too many unexpected artifacts on certain frames. It also makes the film look like video which
#aesthetically is not always pleasing.
/*
prefiltered = RemoveGrain(PreBorderFrame,2)
super = MSuper(PreBorderFrame,hpad=16, vpad=16, levels=1) # one level is enough for MRecalculate
superfilt = MSuper(prefiltered, hpad=16, vpad=16) # all levels for MAnalyse
backward = MAnalyse(superfilt, isb = true, blksize=16,overlap=8,search=3,dct=0)
forward = MAnalyse(superfilt, isb = false, blksize=16,overlap=8,search=3,dct=0)
forward_re = MRecalculate(super, forward, blksize=8, thSAD=100)
backward_re = MRecalculate(super, backward, blksize=8, thSAD=100)
PreBorderFrame = MFlowFps(PreBorderFrame,super, backward_re, forward_re, num=30000, den=1001,ml=200,mask=2)
*/
#Alternative interpolation
/*
superfps= MSuper(PreBorderFrame,pel=2)
backward_vec2 = MAnalyse(superfps, isb = true,blksize=16)
forward_vec2 = MAnalyse(superfps, isb = false,blksize=16)
PreBorderFrame = MFlowFps(PreBorderFrame,superfps, backward_vec2, forward_vec2, num=60000, den=1001, ml=200).SeparateFields().SelectEvery(4, 0, 3).Weave()
*/
#Yet another way to do interpolation
#The following alternative can provide synthesized frames for hi-def material that causes MSuper to choke
#////// Begin alternative to MFlowFPS ////
#SmoothFPS2(PreBorderFrame,threads)
#////// End alternative to MFlowFPS ////
#A final way to do interpolation, although Interframe is just a front-end to SmoothFPS, so I don't think the results are any different.
#The following is another alternative that can provide synthesized frames for hi-def material that causes MSuper to choke. Probably better than the one above.
#////// Begin alternative to MFlowFPS ////
#PreBorderFrame=InterFrame(NewNum=30000,NewDen=1001,PreBorderFrame,GPU=true,Cores=threads)
#////// End alternative to MFlowFPS ////
#RESULT1: AUTOLEVELS,AUTOWHITE
#......................................................................................................................................................................
Baseclip = PreBorderFrame.crop(borderV,borderH,-borderV,-borderH,align=true).bicubicresize(W,H)
blank_black = Blankclip(baseclip, width=autolev_bord1,height=autolev_bord1)
blank_white= Blankclip(baseclip, width=autolev_bord1,height=autolev_bord1, color=$FFFFFF)
Average= baseclip.FredAverage().invert()
over1 = overlay(baseclip,blank_black, x=40,y=300)
over2 = overlay (over1,blank_white, x=160, y=300) \
.bicubicresize(width(baseclip)-(al2)*2,height(baseclip)-(al2)*2)
Detect = (al2 >1) ? overlay (Average, over2,x=al2,y=al2) \
.converttoRGB24(matrix="rec709") : over2.converttoRGB24(matrix="rec709")
result1= PreBorderFrame.ConvertToRGB24.GamMac(verbosity=4,DC=Detect,Show=False, \
LockChan=LockChan, Th=Th, LockVal=LockVal, Scale=Scale, RedMul=RedMul,\
GrnMul=GrnMul, BluMul=BluMul, loTh=LOTH,hiTh=HITH,oMin=OMIN,oMax=OMAX,\
x=GMx,y=GMy,w=GMw,h=GMh).converttoYV12().deflicker().addborders(X,0,0,0,$FFFFFF) \
.addborders(0,0,X2,0,$000000).autolevels(filterRadius=2).crop(X,0,-X2,-0) \
.addborders(bord_left+in_bord_left, bord_top+in_bord_top, \
bord_right+in_bord_right, bord_bot+in_bord_bot)
#RESULT2: AUTOWHITE
#......................................................................................................................................................................
result2= PreBorderFrame.ConvertToRGB24.GamMac(DC=Detect,Show=False, Dither=TRUE,verbosity=5,LockChan=LockChan, \
Th=Th, LockVal=LockVal, Scale=Scale, RedMul=RedMul, GrnMul=GrnMul, BluMul=BluMul, \
loTh=LOTH,hiTh=HITH,oMin=OMIN,oMax=OMAX,x=GMx,y=GMy,w=GMw,h=GMh).converttoYV12() \
.deflicker().addborders(bord_left+in_bord_left, bord_top+in_bord_top, \
bord_right+in_bord_right, bord_bot+in_bord_bot)
#RESULT3: AUTOLEVELS
#.....................................................................................................................................................................
result3= PreBorderFrame.addborders(X,0,0,0,$FFFFFF).addborders(0,0,X2,0,$000000).autolevels(filterRadius=2).crop(X,0,-X2,-0).addborders(bord_left+in_bord_left, bord_top+in_bord_top, bord_right+in_bord_right, bord_bot+in_bord_bot)
#RESULT4: NO CORRECTIONS
#.....................................................................................................................................................................
result4= PreBorderFrame.addborders(bord_left+in_bord_left, bord_top+in_bord_top, bord_right+in_bord_right, bord_bot+in_bord_bot)
#PARAMETERS FOR THE COMPARISONS
#.....................................................................................................................................................................
W2= W+bord_left+bord_right
H2= H+bord_top+bord_bot
source4=Lanczos4Resize(source1,W2,H2)