Post 4 of 4.
Code:
myName = "LocateFrames: "
ver = "v1.10 - 25 June 2014" # Version Number
Assert(RT_FunctionExist("GScript"),myName+"Essential GScript plugin installed, http://forum.doom9.org/showthread.php?t=147846")
c = c.Killaudio() f = f.Killaudio() # Audio not needed.
Thresh = Default(Thresh, 0.0) # Threshold for declaring match, Default==0.0 to search for best possible match.
Thresh2 = Default(Thresh2,0.0) # Threshold2 Default==0.0, find clip f, not time ordered.
Start = Default(Start,0) # Frame to start search from.
Stop = Default(Stop,0) # Frame to stop search (0 == last frame + 1).
XP = Default(XP,true) # Extra Paranoid, dont fully trust Thresh.
Chroma = Default(Chroma,False) # Default Luma Only.
ChromaWeight= Default(ChromaWeight,1.0/3.0) # Default, 33% of Total Difference (Combined U and V).
Prefix = Default(Prefix,"LF_")
Debug = Default(Debug,True) # v1.10, changed True
DB = Default(DB,"") # DBase not default
Start = (Start<0)?0:Start # Silent check
Stop=(Stop<=0||Stop>c.FrameCount)?c.FrameCount:Stop # Silent check
cfrms = Stop-Start # Number of frames to scan in c
ChromaWeight=(Chroma)?ChromaWeight:0.0 # If Not Needed, ignore default weight.
Assert(c.Width==f.Width,myName+"- Clip Width mismatch")
Assert(c.Height==f.Height,myName+"- Clip Height mismatch")
Assert(RT_VarIsSame(c,f),myName+"- ColorSpace Mismatch") # Same colorspace ?
Assert(cfrms>0,myName+"- Invalid Start>=Stop")
Assert(Thresh>=0.0,myName+"- Invalid Thresh")
Assert(f.Framecount<=cfrms,myName+"- f Frames > c[START,STOP] Frames")
Assert(ChromaWeight>=0.0&&ChromaWeight<=1.0,myName+"- ChromaWeight Range 0.0->1.0")
MODE = (DB!="")
Ordered = Thresh < Thresh2 # If true then: Find frames are in time order (match(f[n]) searches from match(f[n-1])+1
FMT="%sFOUND=%-6d %sFIND=%-6d %sDIFF=%f %sSTART=%d %sEND=%-6d %sXPCNT=%-3d %sMATCH=%d"
Result = ""
GScript("""
(DEBUG) ? RT_DebugF("\n%s %s (%s Mode)\n",myName, Ver,(!MODE)? "STRING" : "DBASE",name=myName) : NOP
if(MODE) {RT_DBaseAlloc(DB,0,"iifiiii")}
for (ix = 0, f.Framecount-1) { # Iterate through find frames clip.
s = Start # Init Start search position
BestSoFar = s # Search start position
BestSoFarDiff = 255.0 + 1.0 # Maximum Possible + 1
sS =s # Start offset this time
mS= 0 # " (BM: )", Init to 'scanned all the way' message (Flag Best Match).
eterm=(Ordered)?Stop-f.Framecount+ix:Stop-1 # Init last frame to compare for this iteration.
e=eterm # Init to 'scanned all the way'
Assert(s<=e,myName+"- Internal Error, s > e") # Oops.
xpCnt=0 # Prep XP skip Count string
for(n=s, eterm) { # Search c clip(s to eterm) or until satisfactory match found. (eterm evaluated on entry only)
# Difference to current find frame.
CurrDiff=RT_FrameDifference(c,f,n=n,n2=ix,ChromaWeight=ChromaWeight)
if(CurrDiff < BestSoFarDiff) { # Better match than found so far ?
BestSoFar = n # Remember frame number
BestSoFarDiff = CurrDiff # Remember difference
if(BestSoFarDiff <= thresh) { # Good enough to satisfy user requirement ?
e=BestSoFar # Where we've scanned to so far
mS=2 # " (T1: )", Found satisfactory frame, (Flag Thresh)
if(XP && BestSoFar < eterm && BestSoFarDiff > 0.0) { # DBug will NOT enter here!, eterm condition will fail.
tmp = BestSoFar # Remember
for(n=BestSoFar+1,eterm) {
CurrDiff=RT_FrameDifference(c,f,n=n,n2=ix,ChromaWeight=ChromaWeight)
e=n # We scanned more frames past original BestSoFar
if(CurrDiff < BestSoFarDiff) { # Better match ?
BestSoFar = n # Remember frame number
BestSoFarDiff= CurrDiff # Remember difference
} else {
n = eterm+1 # Force Exit with current_frame == eterm + 2 (normally exit @ eterm+1)
}
}
if(BestSoFar != tmp) {
mS=3 # " (T1:XP)", Found satisfactory frame, (Flag Thresh, and Xtra Paranoid found better frame)
XpCnt=BestSoFar-tmp # How many frames Xtra Paranoid skipped to find better match than Thresh.
}
}
Start=(Ordered)?BestSoFar+1:Start # If required, Next time search from BestSoFar + 1
n = eterm + 1 # Force Exit with current_frame == eterm + 2 (normally exit @ eterm + 1)
}
}
}
if(Ordered && n == eterm+1 && BestSoFarDiff < Thresh2) {
mS = 1 # " (BM:T2)", For Log, Time ordered, (Flag, Best Match and Thresh2)
Start = BestSoFar + 1 # Next time start from BEST MATCH found + 1.
}
if(MODE) {
(DEBUG) ? RT_DebugF(FMT,Prefix,BestSoFar,Prefix,ix,Prefix,BestSoFarDiff,Prefix,sS,Prefix,e,Prefix,XpCnt,Prefix,mS,name=myName) : NOP
RT_DBaseAppend(DB,BestSoFar,ix,BestSoFarDiff,sS,e,XpCnt,mS)
} Else {
Text = RT_String(FMT, Prefix,BestSoFar,Prefix,ix,Prefix,BestSoFarDiff,Prefix,sS,Prefix,e,Prefix,XpCnt,Prefix,mS)
Result = RT_TxtAddStr(Result,Text) # Avoid string concatenation bug in v2.58 and v2.6a3.
(DEBUG) ? RT_DebugF("%s",Text,name=myName) : NOP
}
}
""") # End Of GScript
return (MODE) ? f.FrameCount : Result # Int return for Dbase and String if not DBase
}