Log in

View Full Version : Script and Time?


NLPguy
25th February 2015, 02:12
Hi folks,

I'm using two AviSynth scripts, posted below, to improve/clean the video of many old VHS (NTSC) tapes that I have. It has many horizontal waves (lines) or fixed lines on the image.

I'm capturing VHS tapes with a Panasonic AG-1980 videotape. Also, I'm using a DVD Panasonic DMR-ES10 to pass through (with all the filters 'on') aiming to strike as many horizontal wave as possible.

My PC is an AMD 8350 4.0 Ghz 16GB is taking 6 hours to run the script. And my laptop - an Intel Core I7 2630QM 2.0 Ghz 8GB is taking 20 hours to run it.


So, please:

Do you know why is taking so much time to execute the script using the Core i7 laptop?

Is there some way to decrease the 'process time' modifying the script below (trying to keep the quality output)?


Thank you very much!

Script 1
Script 1 - source script2 Jucilda.avs

SetMemoryMax(512)
SetMTMode(3)
mpeg2source("M:\Jucilda e Joel\01 12 V1\01.d2v")
import("m:\temp2\ChubbyRain2 functions.avs")
import("m:\temp2\RemoveDirtMC.avs")
SetMTMode(2)
AssumeTFF()
Crop(12,0,-4,-8,true)
ChubbyRain2()

SeparateFields()
f1=SelectEven().RemoveDirtMC(60,false).FFT3DFilter(sigma=4,plane=3,bt=2).LSFMod(strength=100)
f2=SelectOdd().RemoveDirtMC(60,false).FFT3DFilter(sigma=4,plane=3,bt=2).LSFMod(strength=100)
Interleave(f1,f2)
Weave()

#SmoothLevels(30,1,255,0,245)

base=last
base.crop(0,0,-630,0,true)
Smoothuv(radius=3)
left=last

Overlay(base,left)

AddBorders(8,4,8,4)



Script 2

Script 2

Import("m:\temp2\source script2 Jucilda.avs")
# Plugins: - RemoveGrain
# - MaskTools2
# - Depan + DepanEstimate
# - MVTools2

SetMemoryMax(512)
SetMTMode(3)
LoadPlugin("m:\temp2\RemoveGrain.dll")
LoadPlugin("m:\temp2\MaskTools2-25.dll")
LoadPlugin("m:\temp2\Depan.dll")
LoadPlugin("m:\temp2\DepanEstimate.dll")
LoadPlugin("m:\temp2\MVTools2.dll")

# mpeg2source("M:\Rosangela\20 4 JVC\\20.d2v")

SetMTMode(2)
# crop(0,64,-0,-64,true)

bob(0,0.5)
a=last

clense(reduceflicker=false).merge(last,0.5).clense(reduceflicker=false)
mot=removegrain(11,0).removegrain(20,0).DepanEstimate(range=2)
take2=a.depaninterleave(mot,prev=2,next=2,subpixel=2)
clean1=take2.TMedian2().selectevery(5,2)

sup1 = clean1.minblur(1).removegrain(11,0).removegrain(11,0)
\ .mt_lutxy(clean1,"x 1 + y < x 2 + x 1 - y > x 2 - y ? ?",U=2,V=2)
\ .msuper(pel=2,sharp=0)
sup2 = a.msuper(pel=2,levels=1,sharp=2)
#bv23=sup1.manalyse(isb=true, truemotion=false,global=true,delta=3,blksize=16,overlap=8,search=5,searchparam=4,DCT=5)
bv22=sup1.manalyse(isb=true, truemotion=false,global=true,delta=2,blksize=16,overlap=8,search=5,searchparam=4,DCT=5)
bv21=sup1.manalyse(isb=true, truemotion=false,global=true,delta=1,blksize=16,overlap=8,search=5,searchparam=4,DCT=5)
fv21=sup1.manalyse(isb=false,truemotion=false,global=true,delta=1,blksize=16,overlap=8,search=5,searchparam=4,DCT=5)
fv22=sup1.manalyse(isb=false,truemotion=false,global=true,delta=2,blksize=16,overlap=8,search=5,searchparam=4,DCT=5)
#fv23=sup1.manalyse(isb=false,truemotion=false,global=true,delta=3,blksize=16,overlap=8,search=5,searchparam=4,DCT=5)

#a.mdegrain3(sup2,bv21,fv21,bv22,fv22,bv23,fv23,thSAD=499) # << this is the "2-fold" variant

interleave(a.mcompensate(sup2,fv22),a.mcompensate(sup2,fv21),a,a.mcompensate(sup2,bv21),a.mcompensate(sup2,bv22))
TMedian2().selectevery(5,2)

sup3 = last.msuper(pel=2,sharp=2)
bv33=sup3.manalyse(isb=true, truemotion=false,global=true,delta=3,blksize=8,overlap=4,search=5,searchparam=4,DCT=5)
bv32=sup3.manalyse(isb=true, truemotion=false,global=true,delta=2,blksize=8,overlap=4,search=5,searchparam=4,DCT=5)
bv31=sup3.manalyse(isb=true, truemotion=false,global=true,delta=1,blksize=8,overlap=4,search=5,searchparam=4,DCT=5)
fv31=sup3.manalyse(isb=false,truemotion=false,global=true,delta=1,blksize=8,overlap=4,search=5,searchparam=4,DCT=5)
fv32=sup3.manalyse(isb=false,truemotion=false,global=true,delta=2,blksize=8,overlap=4,search=5,searchparam=4,DCT=5)
fv33=sup3.manalyse(isb=false,truemotion=false,global=true,delta=3,blksize=8,overlap=4,search=5,searchparam=4,DCT=5)

last.mdegrain3(sup3,bv31,fv31,bv32,fv32,bv33,fv33,thSAD=499)

#stackvertical(a,last) # Alterado por mim.
return(last)


#--------------------------------------------------------------
# Helper functions

function MinBlur(clip clp, int r, int "uv")
{
uv = default(uv,3)
uv2 = (uv==2) ? 1 : uv
rg4 = (uv==3) ? 4 : -1
rg11 = (uv==3) ? 11 : -1
rg20 = (uv==3) ? 20 : -1
medf = (uv==3) ? 1 : -200

RG11D = (r==0) ? mt_makediff(clp,clp.sbr(),U=uv2,V=uv2)
\ : (r==1) ? mt_makediff(clp,clp.removegrain(11,rg11),U=uv2,V=uv2)
\ : (r==2) ? mt_makediff(clp,clp.removegrain(11,rg11).removegrain(20,rg20),U=uv2,V=uv2)
\ : mt_makediff(clp,clp.removegrain(11,rg11).removegrain(20,rg20).removegrain(20,rg20),U=uv2,V=uv2)
RG4D = (r<=1) ? mt_makediff(clp,clp.removegrain(4,rg4),U=uv2,V=uv2)
\ : (r==2) ? mt_makediff(clp,clp.medianblur(2,2*medf,2*medf),U=uv2,V=uv2)
\ : mt_makediff(clp,clp.medianblur(3,3*medf,3*medf),U=uv2,V=uv2)
DD = mt_lutxy(RG11D,RG4D,"x 128 - y 128 - * 0 < 128 x 128 - abs y 128 - abs < x y ? ?",U=uv2,V=uv2)
clp.mt_makediff(DD,U=uv,V=uv)
return(last)
}

# median of 5 clips from Helpers.avs by G-force
Function Median2(clip "input_1", clip "input_2", clip "input_3", clip "input_4", clip "input_5", string "chroma")
{
chroma = default(chroma,"process") #default is "process". Alternates: "copy first" or "copy second"
#MEDIAN(i1,i3,i5)
Interleave(input_1,input_3,input_5)
chroma == "process" ? Clense(reduceflicker=false) : Clense(reduceflicker=false,grey=true)
m1 = selectevery(3,1)
#MAX(MIN(i1,i3,i5),i2)
m2 = input_1.MT_Logic(input_3,"min",chroma=chroma).MT_Logic(input_5,"min",chroma=chroma).MT_Logic(input_2,"max",chroma=chroma)
#MIN(MAX(i1,i3,i5),i4)
m3 = input_1.MT_Logic(input_3,"max",chroma=chroma).MT_Logic(input_5,"max",chroma=chroma).MT_Logic(input_4,"min",chroma=chroma)
Interleave(m1,m2,m3)
chroma == "process" ? Clense(reduceflicker=false) : Clense(reduceflicker=false,grey=true)
selectevery(3,1)
chroma == "copy first" ? last.MergeChroma(input_1) : chroma == "copy second" ? last.MergeChroma(input_2) : last
Return(last)
}

function TMedian2(clip c) {
Median2( c.selectevery(1,-2), c.selectevery(1,-1), c, c.selectevery(1,1), c.selectevery(1,2) ) }

johnmeyer
25th February 2015, 02:24
Boy, this sure seems like the wrong script for fixing VHS tapes! Post a few seconds of one of your captures and let's see what you're trying to fix.

I see two reasons this script runs slow:

1. You are doing WAY too much to your poor video.

2. You are using DCT=5 in the Manalyze calls. Set that to DCT=0 instead.

NLPguy
25th February 2015, 15:20
Hi John,

I tried to upload the video but the forum doesn't permitted. It has a m2v extension.

So, I upload it to youtube. Maybe it can help.

Video Link: https://www.youtube.com/watch?v=MqknHaOm_wk

Thank you.


Boy, this sure seems like the wrong script for fixing VHS tapes! Post a few seconds of one of your captures and let's see what you're trying to fix.

I see two reasons this script runs slow:

1. You are doing WAY too much to your poor video.

2. You are using DCT=5 in the Manalyze calls. Set that to DCT=0 instead.

StainlessS
25th February 2015, 15:28
Hi there.
The guys aint gonna like the Youtube messed with version. You need to post on some file host site eg MediaFire or SendSpace (SendSpace dont need registration
I think). Max file size for forum is about 200/300 KB so probably no good anyways.

NLPguy
25th February 2015, 16:02
Thank you for your help Stainless,

I did remember that I have a mega.nz account. Thus, I've just uploaded the video there.

Link: https://mega.nz/#!sF1xXTwS!BBWmWB5UNApVOSwEywGe6ctR587V6fz3BLoRTpyKo14



Hi John,

I tried to upload the video but the forum doesn't permitted. It has a m2v extension.

So, I upload it to youtube. Maybe it can help.

Video Link: https://www.youtube.com/watch?v=MqknHaOm_wk

Thank you.

Hi there.
The guys aint gonna like the Youtube messed with version. You need to post on some file host site eg MediaFire or SendSpace (SendSpace dont need registration
I think). Max file size for forum is about 200/300 KB so probably no good anyways.

johnmeyer
25th February 2015, 18:42
The noise in the script can be fixed by the usual scripts that many of us have posted before. You can search on my name and "VHS" for some of my contributions.

The bounce can be removed by Deshaker, a plugin for VirtualDub.

The tearing is the most interesting problem. I separated the video into fields, and in most cases, the noise is only present in one field. This opens up the possibility of doing the following:

1. Create motion estimated versions of both fields;
2. Use one of StainlessS' functions to detect an even field that doesn't match either the previous or following even field. This indicates a glitch.
3. Replace that glitch with the motion estimated version, using conditional logic.

I didn't have time to figure out how to use StainlessS' code to do the detection, so I just did it manually. I bobbed, and then replaced each broken field with a motion estimated version, using a delta of 2 in the MVTools2 call (this uses the adjacent "fields" when working on bobbed video). I used the old "filldrops" code to do this. I then re-weaved using the selectevery(4,0,3) followed by a weave.

I then ran this through the VHS denoising script that I've posted many times (basic MDegrain2 stuff).

I tried both Deshaker and also the commercial Mercalli stabilization tools. They both did a good job stabilizing the footage, but the borders were too funky, or I had to zoom in too much to cover the borders, so I didn't do any stabilization.

This is the result, in DV format:

Deglitched & denoised (http://www.mediafire.com/watch/4vtvzt78ppdx5td/Final.avi)

I used lossless codecs for my intermediate renders.

There are still a couple of glitches that were more than one field in duration.

Nothing is perfect ...

NLPguy
25th February 2015, 22:03
Hi John,

I really liked what you did with the video.

I'm a beginner with Avisynt and the script that I was using I did copy from another member.

I'm searching for your old posts. I need to learn more about these effects that you told about.

Please, if it's possible, Could you write the script that you used?

Thank you very much (again)!


The noise in the script can be fixed by the usual scripts that many of us have posted before. You can search on my name and "VHS" for some of my contributions.

The bounce can be removed by Deshaker, a plugin for VirtualDub.

The tearing is the most interesting problem. I separated the video into fields, and in most cases, the noise is only present in one field. This opens up the possibility of doing the following:

1. Create motion estimated versions of both fields;
2. Use one of StainlessS' functions to detect an even field that doesn't match either the previous or following even field. This indicates a glitch.
3. Replace that glitch with the motion estimated version, using conditional logic.

I didn't have time to figure out how to use StainlessS' code to do the detection, so I just did it manually. I bobbed, and then replaced each broken field with a motion estimated version, using a delta of 2 in the MVTools2 call (this uses the adjacent "fields" when working on bobbed video). I used the old "filldrops" code to do this. I then re-weaved using the selectevery(4,0,3) followed by a weave.

I then ran this through the VHS denoising script that I've posted many times (basic MDegrain2 stuff).

I tried both Deshaker and also the commercial Mercalli stabilization tools. They both did a good job stabilizing the footage, but the borders were too funky, or I had to zoom in too much to cover the borders, so I didn't do any stabilization.

This is the result, in DV format:

Deglitched & denoised (http://www.mediafire.com/watch/4vtvzt78ppdx5td/Final.avi)

I used lossless codecs for my intermediate renders.

There are still a couple of glitches that were more than one field in duration.

Nothing is perfect ...

johnmeyer
25th February 2015, 22:52
StainlessS may be able to suggest which of his functions might be used to spot the "glitch" frames on the bobbed version of the video. It would be much nicer not to have to do the glitch detection manually.

I'll post the noise reduction script when I get back to my main editing computer.

johnmeyer
25th February 2015, 23:59
This is my basic VHS video denoising script, more or less from the MVTools2 documentation. I've posted more or less this same thing several times. It provides a good starting point.

This script is designed for removing "snow" type noise. Other problems will require additional plugins and code. You always have to "tune" any script to suit your particular video. There is no "best" or one-size-fits-all.

#Denoiser script for interlaced video using MDegrain2

SetMemoryMax(768)

Loadplugin("C:\Program Files\AviSynth 2.5\plugins\MVTools\mvtools2.dll")
LoadPlugin("c:\Program Files\AviSynth 2.5\plugins\CNR\Cnr2.dll")

SetMTMode(5,6)
source=AVISource("E:\fs.avi").killaudio().AssumeBFF()
SetMTMode(2)

chroma=source.Cnr2("oxx",8,16,191,100,255,32,255,false) #VHS
output=MDegrain2i2(chroma,8,2,0)

#stackhorizontal(source,output)

return output

#-------------------------------

function MDegrain2i2(clip source, int "blksize", int "overlap", int "dct")
{
Vshift=0 # 2 lines per bobbed-field per tape generation (PAL); original=2; copy=4 etc
Hshift=0 # determine experimentally
overlap=default(overlap,0) # overlap value (0 to 4 for blksize=8)
dct=default(dct,0) # use dct=1 for clip with light flicker

fields=source.SeparateFields() # separate by fields

#This line gets rid of vertical chroma halo. Don't use unless you have the problem
#fields=MergeChroma(fields,crop(fields,Hshift,Vshift,0,0).addborders(0,0,Hshift,Vshift))

super = fields.MSuper(pel=2, sharp=1)
backward_vec2 = super.MAnalyse(isb = true, delta = 2, blksize=blksize, overlap=overlap, dct=dct)
forward_vec2 = super.MAnalyse(isb = false, delta = 2, blksize=blksize, overlap=overlap, dct=dct)
backward_vec4 = super.MAnalyse(isb = true, delta = 4, blksize=blksize, overlap=overlap, dct=dct)
forward_vec4 = super.MAnalyse(isb = false, delta = 4, blksize=blksize, overlap=overlap, dct=dct)

#Increase thSAD for more denoising. Won't do much beyone about 1500
MDegrain2(fields,super, backward_vec2,forward_vec2,backward_vec4,forward_vec4,thSAD=400)

Weave()
}

StainlessS
27th February 2015, 18:37
Sorry guys, Been having some hardware problems for a few days.
This is about as good as I could find, but is far from perfect and does make some mistakes. (req RT_Stats and Zebra).


AVISource("D:\NLPGuy\source.avi").Assumetff.SeparateFields.SelectEven
CMD = "EvenCmd.txt"
RT_FileDelete(CMD)

ZEBRA_LO=220
ZEBRA_HI=255
LOCATE_LO=200
LOCATE_HI=255

Z =Last.Zebra(lo=ZEBRA_LO,hi=ZEBRA_HI).Crop(5*48,0,-0,-0) # EDIT: Crop to select YInRange Band
ZP=Z.DeleteFrame(Z.Framecount()-1).DuplicateFrame(0) # Shift clip to align previous with current frame
ZN=Z.DuplicateFrame(Z.Framecount()-1).DeleteFrame(0) # Shift clip to align next with current frame
X=Subtract(Z,Merge(ZP,ZN))
X=X.ScriptClip("""
n=current_frame
Bingo=RT_YInRangeLocate(Last,n=n,Baffle_W=48,Baffle_H=1,lo=LOCATE_LO,hi=LOCATE_HI)
# Got to make coords even for YV12
(BINGO) ? Overlay(Last.BlankClip(Width=24,height=(YIRL_H+1)/2*2,color=$FF0000),x=48/2-12,y=RT_BitClr(YIRL_Y,0)) : NOP
(BINGO) ? RT_WriteFile(CMD,"1 %d",n,Append=True) : NOP
Return Last
""")
StackHorizontal(Last,X)


Above just the script for even fields.

EDIT:
https://s20.postimg.cc/x8y9lytfh/NLPGuy_zpsvxf4cw7u.jpg (https://postimg.cc/image/etdsokfax/)

johnmeyer
27th February 2015, 20:15
StainlessS,

Thanks for posting that. The noise detection problem in this video is a little bit similar to the noise bars on that old Kinescope that you helped me me restore a few years ago, so I thought perhaps some of the same code would work here.

Hopefully the OP will be able to make good use of it.

Here are the results of that old project, in case you forgot:

1955 NFL Football Noise Bars Before-After (https://www.youtube.com/watch?v=qx26T6WOZ_4)

StainlessS
27th February 2015, 20:53
Nope, have not forgotten, still quite amazing results.
If of interest to OP, here was thread:- http://forum.doom9.org/showthread.php?t=167591&highlight=RT_yinrangeLocate

Here script for odd field detect

AVISource("D:\NLPGuy\source.avi").Assumetff.SeparateFields.SelectOdd
CMD = "OddCmd.txt"
RT_FileDelete(CMD)

ZEBRA_LO=220
ZEBRA_HI=255
LOCATE_LO=200
LOCATE_HI=255

Z =Last.Zebra(lo=ZEBRA_LO,hi=ZEBRA_HI).Crop(5*48,0,-0,-0) # EDIT: Crop to select YInRange Band
ZP=Z.DeleteFrame(Z.Framecount()-1).DuplicateFrame(0) # Shift clip to align previous with current frame
ZN=Z.DuplicateFrame(Z.Framecount()-1).DeleteFrame(0) # Shift clip to align next with current frame
X=Subtract(Z,Merge(ZP,ZN))
X=X.ScriptClip("""
n=current_frame
Bingo=RT_YInRangeLocate(Last,n=n,Baffle_W=48,Baffle_H=1,lo=LOCATE_LO,hi=LOCATE_HI)
# Got to make coords even for YV12
(BINGO) ? Overlay(Last.BlankClip(Width=24,height=(YIRL_H+1)/2*2,color=$FF0000),x=48/2-12,y=RT_BitClr(YIRL_Y,0)) : NOP
(BINGO) ? RT_WriteFile(CMD,"1 %d",n,Append=True) : NOP
Return Last
""")
StackHorizontal(Last,X)


And script to fix the clip

AVISource("D:\NLPGuy\source.avi")
ECMD="EvenCmd.txt"
OCMD="OddCmd.txt"
Assumetff
SeparateFields
E=SelectEven
O=SelectOdd


pel = 2 # Default as for MSuper(), ie 2, range=1 or 2 or 4
sharp = 2 # Default as for MSuper(), ie 2, range=0 -> 2
rfilter= 2 # Default as for MSuper(), ie 2, range=0 -> 4
ml = 100.0 # Default as for MFlowInter(), ie 100.0, range=greater than 0.0.

E_CP = E.DeleteFrame(E.Framecount()-1).DuplicateFrame(0)
E_sup = E_CP.MSuper(pel=pel, sharp=sharp, rfilter=rfilter)
E_bv = E_sup.MAnalyse(isb=true, delta=2)
E_fv = E_sup.MAnalyse(isb=false, delta=2)
EC = E_CP.MFlowInter(E_sup,E_bv,E_fv, time=100.0 * 1 / 2 , ml=ml)

O_CP = O.DeleteFrame(O.Framecount()-1).DuplicateFrame(0)
O_sup = O_CP.MSuper(pel=pel, sharp=sharp, rfilter=rfilter)
O_bv = O_sup.MAnalyse(isb=true, delta=2)
O_fv = O_sup.MAnalyse(isb=false, delta=2)
OC = O_CP.MFlowInter(O_sup,O_bv,O_fv, time=100.0 * 1 / 2 , ml=ml)

SHOW=False
E_RESULT=ClipClop(E,EC,cmd=ECMD,Show=SHOW)
O_RESULT=ClipClop(O,OC,cmd=OCMD,Show=SHOW)

Interleave(E_RESULT,O_RESULT)
Weave


EDIT: 1st script edited, added clipclop clip index to text file.

NOTE, I used AssumeTFF.

EDIT: You need to run both detection scripts to create the text command files, and then the fix script.