View Full Version : First\last frame of Overlay
ale_x
7th February 2016, 09:33
Hi everyone
I have question regarding Overlay function
this my simple code
a1 = FFVideoSource("Oh!.mp4").ConvertToRGB32().AssumeFPS(24000,1001)
a2 = AVISource("logo.avi").ConvertToRGB32()
mask_clip = Mask(a2, a2.GreyScale.Levels(0, 1.112, 35, 0, 255)).blur(1.5)
Overlay(a1, a2, mask= showalpha(mask_clip), y=0, x=0, mode="blend", opacity=1.0)
is there anyway I can make a First\last frame for my logo.avi clip or just int first frame?
or if it's possible to edit overlay script ?!
I know there's insertsign function, but I want to use overlay only for some reasons.
thank you. :)
raffriff42
7th February 2016, 17:41
You can cut the overlay in and out with this function:# http://atlas.kennesaw.edu/~dhirschl/avisynth.html
function sceneinsert(clip a1, clip a2, int f1, int f2)
{
# a1=main movie, a2=angle2
segment=a2.trim(f1,f2)
return a1.trim(0,f1-1) + segment + a1.trim(f2+1,0)
}
(one of the first scripts I ever downloaded (http://wayback.archive.org/web/20080719102522/http://atlas.kennesaw.edu/~dhirschl/avisynth.html))
You use it like this:## test clip with visible frame numbers
base = ColorBars.Trim(0, 299).ShowFrameNumber(x=16, y=32)
## test overlay
over = base.Overlay(base.Invert.BicubicResize(base.Width/2, base.Height/2), x=128, y=128)
## cut to overlay at frame 30; cut back to non-overlay at frame 91
return sceneinsert(base, over, 30, 90)
This works with any alternate clip that is synchronized to the base clip -- for example, base.Subtitle, base.AudioDub, ...
...but wait, wouldn't it be great to *fade* the alternate clip in and out? Yeah, I thought so too. ## fade in
return UUInsertFade(base, over, markIn=30, fadeIn=15)
## fade out
return UUInsertFade(base, over, markOut=90, fadeOut=35)
## fade in, fade out
return UUInsertFade(base, over, markIn=30, markOut=90, fadeIn=10, fadeOut=35)
## cut in, cut out (same as 'sceneinsert')
return UUInsertFade(base, over, 30, 90)
## fade out (negative markOut sets duration)
return UUInsertFade(base, over, 0, -30, fadeOut=15)
## default: show overlay clip always
return UUInsertFade(base, over)
#######################################
### insert an alternate clip
### (cut or dissolve to alternate clip and back again)
##
## @ A - main movie
## @ B - alternate clip (synchronized with clip A)
## @ markIn - frame where A->B fade begins
## (default 0; start of clip 'A')
## @ markOut - frame where B->A fade begins
## (default 0; B remains 'on' until end of clip 'A')
## (negative markOut sets duration)
## @ fadeIn - number of frames for A->B fade
## (default 0; cut to clip 'B' at frame markIn)
## @ fadeOut - number of frames for B->A fade
## (default 0; cut to clip 'A' at frame markOut+1)
##
## DISALLOWED INPUTS:
## * if A.FrameCount < markIn+fadeIn+1, an error occurs
## * if B.FrameCount < markOut+fadeOut+1, an error occurs
## * if markOut>=0 and markIn > markOut, an error occurs
## * if duration is negative, an error occurs
##
function UUInsertFade(clip A, clip B,
\ int "markIn", int "markOut",
\ int "fadeIn", int "fadeOut")
{
markIn = Max(0, Default(markIn, 0))
markOut = Default(markOut, 0)
fadeIn = Max(0, Default(fadeIn, 0))
fadeOut = Max(0, Default(fadeOut, 0))
Assert(markIn>0 || Abs(markOut)!=1,
\ "UUInsertFade: markOut cannot be +/-1 if markIn=0")
markOut = (markOut>=0) ? markOut : (markIn - markOut - 1)
Assert(A.FrameCount>=markIn+fadeIn+1,
\ "UUInsertFade: clip A too short"+Chr(10)
\ + "(FrameCount < markIn+fadeIn+1)")
Assert(B.FrameCount>=markOut+fadeOut+1,
\ "UUInsertFade: clip B too short"+Chr(10)
\ + "(FrameCount < markOut+fadeOut+1)")
Assert(markIn<=markOut,
\ "UUInsertFade: duration is negative")
R = ((markIn+fadeIn)==0) ? B
\ : Dissolve(A.Trim(0, ((markIn+fadeIn)==1) ? -1 : markIn+fadeIn-1),
\ B.Trim(markIn, 0),
\ fadeIn)
R = ((markOut+fadeOut)==0) ? R
\ : Dissolve(R.Trim(0, markOut+fadeOut),
\ A.Trim(markOut+1, 0),
\ fadeOut)
return R
}
ale_x
7th February 2016, 18:47
Thank you very much man
I used it, but I got 1 more frame, my video frame is 5551, but I got 1 more when I used sceneinsert 5552
so do you think it's ok to use trim like
return sceneinsert(last, over, 0, 300).trim(1,0)
Regards.
raffriff42
7th February 2016, 19:04
Aha, it seems frame 0 is repeated.
(UUInsertFade handles this case correctly, ...EDIT but has trouble with markIn=1 ...EDIT fixed)
EDIT agree, Trim(1, 0) seems to be the right way to fix sceneinsert where markIn=0. For markIn=1, this does not work however.
ale_x
7th February 2016, 19:25
I just did this
function sceneinsert(clip a1, clip a2, int f1, int f2)
{
segment=a2.trim(f1,f2)
return a1.trim(0,f1-1) + segment + a1.trim(f2+2,0)
}
Ok I have another question, If I want add another logo, I used insertsign, but only one logo appeared, which is a2 !_!
FFVideoSource("Oh! Rival - Detective Conan Movie 19 Theme Song.mp4").ConvertToRGB32().AssumeFPS(24000,1001)
a1 = FFVideoSource("Oh.mp4").ConvertToRGB32().AssumeFPS(24000,1001)
a2 = AVISource("tt1.avi").blur(0.3)
a3 = AVISource("kara.avi")
mask_clip = Mask(a2, a2.GreyScale.Levels(0, 1.112, 35, 0, 255)).blur(1.5)
over = Overlay(a1, a2, mask= showalpha(mask_clip), y=0, x=0, mode="blend", opacity=1.0)
return sceneinsert(last, over, 0, 300).trim(1,0)
insertsign(last,a3,0,300)
raffriff42
7th February 2016, 19:36
I had never heard of insertsign, but here it is (http://forum.doom9.org/showthread.php?p=1739036#post1739036).
It seems to require a valid alpha channel for the overlay to work. mask=overlayclip.showalpha())
EDIT see related post here (http://forum.doom9.org/showthread.php?p=1755162#post1755162), where I create a "fake" mask, which you would then add to a3's alpha channel with Mask (http://avisynth.nl/index.php/Layer#Mask).
ale_x
7th February 2016, 20:12
Thank you Bro
I just did something different
v= sceneinsert(last, over, 0, 300).trim(1,0)
v = insertsign(v,a3,0,300)
return v
it works fine
thank you again =)
شكرا جزيلا لك
Gavino
14th February 2016, 09:36
You can cut the overlay in and out with this function:# http://atlas.kennesaw.edu/~dhirschl/avisynth.html
function sceneinsert(clip a1, clip a2, int f1, int f2)
{
# a1=main movie, a2=angle2
segment=a2.trim(f1,f2)
return a1.trim(0,f1-1) + segment + a1.trim(f2+1,0)
}
(one of the first scripts I ever downloaded (http://wayback.archive.org/web/20080719102522/http://atlas.kennesaw.edu/~dhirschl/avisynth.html))
Unfortunately - as you have seen - this function doesn't work for certain values of the input parameters.
It gives the wrong result if f1 is 0 or 1, or if f2 is the last frame.
See stickboy's classic Caveats of using Trim in functions.
In functions, you should generally use the form Trim(0, -N) instead of Trim(0, N-1), which gives the wrong result for N=1.
An improved version (but still without error checking) is
function sceneinsert(clip a1, clip a2, int f1, int f2)
{
# a1=main movie, a2=angle2
segment=a2.trim(f1,f2)
result = (f1==0 ? segment : a1.trim(0,-f1) + segment)
result = (f2+1 < a1.framecount ? result + a1.trim(f2+1,0) : result)
return result
}
I just did this
function sceneinsert(clip a1, clip a2, int f1, int f2)
{
segment=a2.trim(f1,f2)
return a1.trim(0,f1-1) + segment + a1.trim(f2+2,0)
}
No, that's also wrong, for the same reasons.
StainlessS
16th February 2016, 00:13
I needed a mod of this (a few posts after RaffRiff link in post #6) just now and so thought I might post it.
Feel free to improve.
EDIT: SEE LATER Post #16.
Made mask externally supplied, and removed RGB requirement.
Function OverlayClip(clip c,clip c2,Int "S", Int "E", Int "X", Int "Y",Float "Opacity",String "Mode",clip "Mask") {
/*
OverlayClip v1.01, http://forum.doom9.org/showthread.php?p=1757408#post1757408
ColorSpace, as for Overlay.
Overlay clip c with clip c2 using Mask mask if supplied, frames S to E, @ clip c overlay coords X and Y.
Start and End Args S & E, are similar but not exactly like trim.
OverlayClip(c,c2, 0,0) # Entire clip
OverlayClip(c,c2, 100,0) # Frame 100 to End of Clip
OverlayClip(c,c2, 0,-1) # Frame 0 Only
OverlayClip(c,c2, 1,1) # Frame 1 Only
OverlayClip(c,c2, 1,-1) # Frame 1 Only
OverlayClip(c,c2, 1) # Frame 1 Only [Not same as Trim()], E defaults to -1 ie single frame.
OverlayClip(c,c2, 1,-3) # Frames 1 to 3 (ie 3 frames)
OverlayClip(c,c2, 100,200) # Frames 100 to 200
OverlayClip(c,c2, 100,-50) # Frames 100 to 149 ie 50 frames
X and Y are OverLay x & y offsets, and can be -ve where is relative to c.Width & c.Height, use eg x = -(c2.Width+16)
If c2.Framecount is shorter than S,E specified range, then final c2 frame will be used for remainder of overlay range,
so if c2 clip is single frame, then that frame will be used for entire overlay range.
Opacity=Overlay::Opacity and Mode=Overlay::Mode.
Mask, This will be used as the transparency mask for the overlay image. The mask must be the same size as the overlay clip, only the
greyscale (luma) components are used from the image. The darker the image is, the more transparent will the overlay image be.
Not specitying Mask is equivalent to supplying a 255 clip.
*/
FMX=c.FrameCount-1
S = Min(Max(Default(S,0),0),FMX) E = Default(E,-1)
X=Default(X,0) Y=Default(Y,0)
Opacity=Float(Default(Opacity,1.0)) Mode=Default(Mode,"Blend")
X = (X<0) ? c.Width + X : X
Y = (Y<0) ? c.Height + Y : Y
E = (E==0) ? FMX : E
E = Min(((E < 0) ? S-E-1 : E),FMX) # S <= E <= FMX : E is +ve END Frame number (may be 0)
Empty = c.BlankClip(Length=0)
CS = (S==0) ? Empty : c.Trim(0,-S)
CM = c.Trim(S,E==0?FMX:E).Overlay(c2,x=X,y=Y,mask=Mask,opacity=Opacity,mode=Mode)
CE = (E==FMX) ? Empty : c.Trim(E+1,0)
CS ++ CM ++ CE
}
Demo Client
Avisource("D:\V\StarWars.avi") # Source YV12
Sym=Avisource("D:\V\XMen2.avi").Trim(5000,0) # Overlay clip
W=(Width/16) * 4
H=(Height/16) * 4
Sym=Sym.BilinearResize(W,H)
#S="(((x-.5)^2 +(y-.5)^2) < .25 ? 255 : 0" # Elliptical Disk (Hard Edge)
S="((x-.5)^2+(y-.5)^2)>.25?0:(((x-.5)^2+(y-.5)^2)<.2?255:(.25-((x-.5)^2+(y-.5)^2))*5100)" # Elliptical Disk (Soft Edge)
# Create Elliptical Disk
ElipDisk = Sym.trim(0,-1).mt_lutspa(mode = "relative", expr = Mt_Polish(S), chroma = "-128" ).Loop(Sym.framecount,0,0)
# Start @ frame 100, End at end of Sym Clip, Rel Bottom RHS with 16 pixels GAP
Return OverlayClip(Last,Sym,S=100,E= -Sym.FrameCount,x= -(Sym.Width+16),y= -(Sym.Height+16),mask=ElipDisk)
raffriff42
17th February 2016, 01:59
X = (X<0) ? c.Width + X : XSo x=(-16) puts the left edge of my overlay 16 pix from the right edge of my base clip? Perhaps you wanted x<0 to right-justify?X = (X<0) ? c.Width - c2.Width + X : X
But maybe I wanted my left edge off screen...
StainlessS
17th February 2016, 03:04
But maybe I wanted my left edge off screen...
Then you are screwed :)
No, I did not want nor intend any right justification, -ve is only relative to source width/height but still for overlay top/left x/y coords.
(I guess that it is not such a bad idea though).
Post your modified version + any other mods you may have made. (A sister function to use same range of start S and end E could be used
as for InsertSign, with an alternate angle of same scene from the Overlay clip).
Gavino
17th February 2016, 09:45
But maybe I wanted my left edge off screen...
Then just provide a larger negative number.
X=-(16+c.width) will put the left edge 16 pixels off screen.
raffriff42
17th February 2016, 10:09
...Doh! (https://www.google.com/search?q=Doh!&tbm=isch)
StainlessS
17th February 2016, 17:52
RR, We must never forget that he is eternally watching for our mistakes, we really have to be more careful.
Gavino
17th February 2016, 18:39
Be careful, be very careful ...
But seriously, you're both doing such a good job that I rarely have to post these days (and don't really have the time any more).
StainlessS
11th October 2019, 02:06
Mod of post #9. Added args, Bool "Rgt_Rel"=False,Bool "Bot_Rel"=False,Bool "AltAngle"=False.
OverlayClip.avsi
# OverlayClip.avsi
Function OverlayClip(clip c,clip c2,Int "S", Int "E", Int "X", Int "Y",Float "Opacity",String "Mode",clip "Mask",Bool "Rgt_Rel",Bool "Bot_Rel",Bool "AltAngle") {
/*
OverlayClip v1.02, https://forum.doom9.org/showthread.php?p=1887123#post1887123
ColorSpace, as for Overlay.
Overlay clip c with clip c2 using Mask mask if supplied, frames S to E, @ clip c overlay coords X and Y.
Start and End Args S & E, are similar but not exactly like trim.
OverlayClip(c,c2, 0,0) # Entire clip
OverlayClip(c,c2, 100,0) # Frame 100 to End of Clip
OverlayClip(c,c2, 0,-1) # Frame 0 Only
OverlayClip(c,c2, 1,1) # Frame 1 Only
OverlayClip(c,c2, 1,-1) # Frame 1 Only
OverlayClip(c,c2, 1) # Frame 1 Only [Not same as Trim()], E defaults to -1 ie single frame.
OverlayClip(c,c2, 1,-3) # Frames 1 to 3 (ie 3 frames)
OverlayClip(c,c2, 100,200) # 101 Frames, 100 to 200
OverlayClip(c,c2, 100,-50) # Frames 100 to 149 ie 50 frames
NOTE, X and Y, changed functionality, we use Rgt_Rel and Bot_Rel for Right/Bot Justify Overlay clip.
X is OverLay x offsets, and where Rgt_Rel = true and X = 0, then is hard against right hand side edge. Where Rgt_Rel=true and eg x = -16, then leaves 16 pixel gap between overlay clip and Right edge.
Y is OverLay y offsets, and where Bot_Rel = true and Y = 0, then is hard against Bottom edge. Where Bot_Rel=true and eg y = -16, then leaves 16 pixel gap between overlay clip and bottom edge.
If c2.Framecount is shorter than S,E specified range, then final c2 frame will be used for remainder of overlay range,
so if c2 clip is single frame, then that frame will be used for entire overlay range.
Opacity=Overlay::Opacity and Mode=Overlay::Mode.
Mask, This will be used as the transparency mask for the overlay image. The mask must be the same size as the overlay clip, only the
greyscale (luma) components are used from the image. The darker the image is, the more transparent will the overlay image be.
Not specitying Mask is equivalent to supplying a 255 clip.
Rgt_Rel, Bot_Rel, Default false, if both true, then x and y, are relative Right Bottom aligned.
AltAngle, Default false. If true, then c2 and Mask are trim'ed to match c clip so that they correspond to same time as c when Overlaying. (Otherwise, overlays from beginning of c2 clip)
*/
FMX=c.FrameCount-1
S = Min(Max(Default(S,0),0),FMX) E = Default(E,-1)
X=Default(X,0) Y=Default(Y,0)
Opacity=Float(Default(Opacity,1.0)) Mode=Default(Mode,"Blend")
Rgt_Rel = Default(Rgt_rel,False)
Bot_Rel = Default(Bot_rel,False)
AltAngle = Default(AltAngle,False)
X = (RGT_REL) ? (c.Width - c2.Width + X) : X
Y = (BOT_REL) ? (c.Height - c2.Height + Y) : Y
E = (E==0) ? FMX : E
E = Min(((E < 0) ? S-E-1 : E),FMX) # S <= E <= FMX : E is +ve END Frame number (may be 0)
Empty = c.BlankClip(Length=0)
CS = (S==0) ? Empty : c.Trim(0,-S)
C2E = (AltAngle) ? (E==0?FMX:E) : 0
c2 = (AltAngle) ? c2.Trim(S,C2E) : c2
Mask = (AltAngle && Mask.Defined) ? Mask.Trim(S,C2E) : Mask
CM = c.Trim(S,C2E).Overlay(c2,x=X,y=Y,mask=Mask,opacity=Opacity,mode=Mode)
CE = (E==FMX) ? Empty : c.Trim(E+1,0)
CS ++ CM ++ CE
}
Demo client
# OverlayClip_Client.avs
#Import(".\OverlayClip.avsi")
#Avisource("D:\Parade.avi") # Source YV12
#Sym=Avisource("D:\Parade.avi") # Overlay clip
Colorbars(Pixel_Type="YV12")
Sym=Last
X = -16
Y = -16
RGT_REL = True
BOT_REL = True
ALTANGLE = False # If true, then Sym clip is temporally aligned (trim'ed) to match source clip (START,END) when Overlay (otherwise overlayed from beginning of Sym clip).
START = 100 # Start frame of overlay
END = 1000 # End frame of overlay
######
W = (Width/16) * 4
H = (Height/16) * 4
Sym=Sym.BilinearResize(W,H) # .Loop() # Very long clip
#INFIX="(((x-.5)^2 +(y-.5)^2) < .25 ? 255 : 0" # INFIX: Elliptical Disk (Hard Edge) : Req mt_Polish()
#INFIX="((x-.5)^2+(y-.5)^2)>.25?0:(((x-.5)^2+(y-.5)^2)<.2?255:(.25-((x-.5)^2+(y-.5)^2))*5100)" # INFIX: Elliptical Disk (Soft Edge) : Req mt_Polish()
#RPN="x 0.5 - 2 ^ y 0.5 - 2 ^ + 0.25 < 255 0 ?" # RPN: Elliptical Disk (Hard Edge)
RPN="x 0.5 - 2 ^ y 0.5 - 2 ^ + 0.25 > 0 x 0.5 - 2 ^ y 0.5 - 2 ^ + 0.2 < 255 0.25 x 0.5 - 2 ^ y 0.5 - 2 ^ + - 5100 * ? ?" # RPN: Elliptical Disk (Soft Edge)
# Create Elliptical Disk
ElipDisk = Sym.BlankClip(Length=1).mt_lutspa(mode = "relative", expr = RPN, chroma = "-128" ) # .Loop() # Very long clip
# Start @ frame START, End at END, Rel Bottom RHS with 16 pixels GAP
Return OverlayClip(Last,Sym,S=START,E=END,x=X,y=Y,mask=ElipDisk,Rgt_rel=RGT_REL,bot_Rel=BOT_REL,Altangle=ALTANGLE)
EDIT: For Demo Client, converted mt_lutspa expr from INFIX to RPN, allow use of current versions of mt_lutspa to work on XP. [mt_Polish() dont work on XP]
https://i.postimg.cc/mk8bKfP6/Overlay-Clip-00.jpg (https://postimages.org/)
EDIT:
AltAngle=False
https://i.postimg.cc/pyhChjnQ/Alt-Angle-False.jpg (https://postimg.cc/pyhChjnQ)
AltAngle=True
https://i.postimg.cc/xJbmCKFc/Alt-Angle-True.jpg (https://postimg.cc/xJbmCKFc)
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.