View Full Version : Find specific solid color frames
Aegwyn11
24th May 2013, 01:02
I have clips that have a single green frame at frame 0, then the video. My issue is that frame 0 isn't at the start of the file....it might be 200 frames into the file.
Is there a way to have AVISynth automagically go find that green frame and report back the frame number? If so, I could write a script that would automatically align my clips without having to do it manually with Trim().
StainlessS
24th May 2013, 01:50
Not sure this should be in Development section but,
Write a function that scans first n frames using GScript and returns frame number+1 of found frame, or 0 (not found).
z=fn(250)
Trim(z,0)
There are a number of methods to find color of frame, eg
AverageLuma, AverageChomaU, AverageChomaV
ShowChannels,
RT_RgbChanAve from RT_Stats.
EDIT: Ignore above, color difference wanted not eg averageluma.
Gavino
24th May 2013, 09:16
You can use the FindFrame function from this post, or something similar derived from it.
(Since it pre-dates the release of GScript, it uses recursion instead.)
wonkey_monkey
24th May 2013, 13:36
My issue is that frame 0 isn't at the start of the file....it might be 200 frames into the file.
What do you mean by this? :confused:
Aegwyn11
24th May 2013, 17:14
Thanks for the suggestions! I'll look into both and post back if I get stuck.
Not sure this should be in Development section but,
You're right! I goofed when I made the post...it should be in the usage forum. Mods: please move this if you can. Thanks.
What do you mean by this? :confused:
Imagine you have a source that has green frames at the beginning prior to the content. Now imagine that source playing out in a linear, real time fashion, looping when it gets to the end of the file. Now imagine that feeding a real time encoder, where the output is being captured for analysis/evaluation. The green frames would be at frame 0 of the source, but would be somewhere else in the output capture (unless your timing was REALLY good!).
Make sense?
StainlessS
24th May 2013, 20:50
Ignore what I said earlier, you would want to use color difference rather than eg averageluma, as suggested in Gavino link.
Here, something I just knocked up, not much testing. If not to your liking, then maybe take parts of it and
integrate into Gavino FindFrame.
Anyway, here tis:
Import("LocateFrames.avs") # Included in RT_Stats zip
# Based on FindFrame() (c) Gavino: http://forum.doom9.org/showthread.php?p=1569622#post1569622
# Required: GScript (c) Gavino: http://forum.doom9.org/showthread.php?t=147846
# Required: RT_Stats (c) StainlesS:http://forum.doom9.org/showthread.php?t=165479
# ---------------------------------------------------------------------------------
# Testclip, Replace 10 Green frames at frames 100 to 109
ColorBars().ConvertToYV12().ShowFrameNumber()
G=Last.BlankClip(Length=10,color=$00FF00)
Last=Trim(0,99) ++ G ++ Trim(110,-(1000-110))
Function SkipGreen(clip c,float "th",int "frames",int "R",int "G",int "B",bool "LastGreen") {
# Supply args OR change defaults (easier)
# Should supply non zero th OR change SkipGreen default.
c
th=Float(Default(th,0.0)) # 0.0==Search for 1st exact match or best possible if no exact match found]. Threshold for declaring match (diff <= Thresh).
frames=Default(frames,250) # Frames count to search
R=Default(R,0) G=Default(G,$FF) B=Default(B,0)
LastGreen=Default(LastGreen,false)
Assert(R>=0 && R<=255 && G>=0 && G<=255 && B>=0 && B<=255,"SkipGreen: Bad color")
Dclip=Trim(0,-frames)
DClip = (!LastGreen) ? DClip : DClip.Reverse
Green=c.BlankClip(Length=1,color=(R*256+G)*256+B)
# Return Green
s=DClip.LocateFrames(Green,thresh=th,Chroma=True)
Eval(s)
Start = (LF_MATCH>=2) ? ((!LastGreen)?LF_FOUND:frames-1-LF_FOUND) + 1 : 0
# RT_Debug("SkipGreen:","Trim(" + String(Start) + ",0)")
Trim(Start,0)
# Subtitle("Frame="+String(Start),Last_frame=0)
Return Last
}
# These will search for EXACT match only, set th non zero
return SkipGreen(LastGreen=False) # Should trim @ 101, ie second green frame
return SkipGreen(LastGreen=True) # Should trim @ 110, ie frame after last green frame
EDITED: Change to RGB specification, Added LastGreen to find last +1 where multiple green frames.
StainlessS
25th May 2013, 20:39
Previous post updated script.
Aegwyn11
28th May 2013, 16:00
That is fantastic. Very nifty...this is going to make my life much much easier.
Thanks StainlessS and Gavino!!
Aegwyn11
17th June 2013, 21:59
Thanks again for the awesome script. I modified it slightly to allow for use of pixel types other than YV12 (without altering the input clip pixel type).
Import("LocateFrames.avs") # Included in RT_Stats zip
# Based on FindFrame() (c) Gavino: http://forum.doom9.org/showthread.php?p=1569622#post1569622
# Required: GScript (c) Gavino: http://forum.doom9.org/showthread.php?t=147846
# Required: RT_Stats (c) StainlesS:http://forum.doom9.org/showthread.php?t=165479
# ---------------------------------------------------------------------------------
# Modified by Aegwyn11 - 6/17/2013 - Enabled so that any pixel type will work, not just YV12
Function SkipGreen(clip c,float "th",int "frames",int "R",int "G",int "B",bool "LastGreen") {
# Supply args OR change defaults (easier)
# Should supply non zero th OR change SkipGreen default.
c
th=Float(Default(th,0.0)) # 0.0==Search for 1st exact match or best possible if no exact match found]. Threshold
for declaring match (diff <= Thresh).
frames=Default(frames,250) # Frames count to search
R=Default(R,0) G=Default(G,$FF) B=Default(B,0)
LastGreen=Default(LastGreen,false)
Assert(R>=0 && R<=255 && G>=0 && G<=255 && B>=0 && B<=255,"SkipGreen: Bad color")
Dclip=Trim(0,-frames)
DClip = (!LastGreen) ? DClip : DClip.Reverse
Green=c.BlankClip(Length=1,color=(R*256+G)*256+B,pixel_type="YV12")
# Return Green
s=DClip.ConvertToYV12().LocateFrames(Green,thresh=th,Chroma=True)
Eval(s)
Start = (LF_MATCH>=2) ? ((!LastGreen)?LF_FOUND:frames-1-LF_FOUND) + 1 : 0
# RT_Debug("SkipGreen:","Trim(" + String(Start) + ",0)")
Trim(Start,0)
# Subtitle("Frame="+String(Start),Last_frame=0)
Return Last
}
StainlessS
17th June 2013, 23:43
Nice. :)
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.