Log in

View Full Version : Introducing DissolveMod.


StifflerStealth
15th December 2007, 00:02
Hi,

This is a rather complicated script that does a lot. It has 15 settings that controls everything on how your two video clips are joined together.

The default settings will just perform the same thing that a clip1 + clip2 does. However you can adjust the settings so that the video of clip1/clip2 fades in/out at a different rate than the audio. The Audio and video can even overlap differently as well: For example, the video is dissolved together in 5 frames, but the audio from clip1 plays for another 20 frames before it fades and allows the audio from clip2 to play. You can even cut the audio from one clip completely and replace it with the audio from the other clip. Also, you can have as many blank frames as you like between the cut. The max blank frames with fadein/fadeout is 2.

There are some things that you can do that may not be so useful, but there may be a need for it. You can have the audio stop playing at the start or end of a clip, so there could be a gap in audio between the two clips. Same with the video. It can be replaced with color of your choosing. Maybe you want to overlay video on that section later and it would be easy if there was no video there.

The two videos are always joined together where clip1 fadeout ends and clip2 fadein begins, Unless the overlap variable is specified. Also, you can choose to insert blank frames if you do not want the audios to overlap. The Audio does not get cut where the video is cut.

However, I need your help. I cannot test ever possible combination of this function, but I have tested it mathematically, and it seems to work.

Requirements:
You need Stickboy's Trim2 function, which can be found Here (http://avisynth.org/stickboy/jdl-util.avsi).
You can use his DLL plugin for min/max, and if you do, delete the min/max/clamp functions from the avsi file. The min/max dll version can be found Here (http://avisynth.org/stickboy/MinMax.zip).

I might be writing more functions later that deal with only one clip instead of two.

I know I don't have all the proper error checking. Some errors can still get by, so if you notice a way to strengthen my error checking, or to make it more simple and do the same thing, please let me know. Also, if you have better name suggestions for the variables, let me know. I would love to hear any general comments or criticisms. Please report bugs to me.

If this was already done, sorry for posting this. I am new to AVS and didn't see something similar when I researched. If this was done before, my apologies to that author.

The complicated function:
# DissolveMod
#
# A version of Dissolve that allows for more flexibility of the fading.
# It can cut video and use fades or just overlap the audio of two clips.
# There are many uses for this function. :) Hope you enjoy it.
#
# There are a lot of options for this function, so pay attention to what you do.
# Reason why I have *FadeIn, *FadeInLen, *FadeOut... variables for cutting
# instead of a defined start and end is so that there are no errors like if the end
# variable is defined long past the fadeout variable. Start and End frames are found
# by StartFadeIn + FadeInLen and FadeOut + FadeOutLen. So, Fade length of 0 means
# That's a hard cut point. :) This helps to eliminate errors on the users end.
#
# Color is for the blank video and transition color for fadein and fadeout functions.
# VidOverlap is how many frames of the video are "dissolved" together. This can be
# affected by the use of BlankVidFrames, which inserts blank vid frames inbetween
# the video cut of type color. This can also produce gaps in audio or affect how audio
# is overlaped. It's best to see what is happening in Debug mode if you want to use
# This parameter. It basically replicats a fadein or out of type 1 or two, but this is
# more powerful since you can select any number that's positive. :)
#
# There are so many options with this function that it might be helpful to produce
# wrapper functions with your most commonly used settings. There are 15 different settings. :|
#
# 20071214 - Initial Release. May contain bugs. Let's call this Beta or Alpha or release 20071214. :P
function DissolveMod(clip a, clip b,
\ int "aVidStartFadeIn", int "aVidFadeInLen", int "aVidStartFadeOut", int "aVidFadeOutLen",
\ int "aAudStartFadeIn", int "aAudFadeInLen", int "aAudStartFadeOut", int "aAudFadeOutLen",
\ int "bVidStartFadeIn", int "bVidFadeInLen", int "bVidStartFadeOut", int "bVidFadeOutLen",
\ int "bAudStartFadeIn", int "bAudFadeInLen", int "bAudStartFadeOut", int "bAudFadeOutLen",
\ int "VidOverlap", int "color", int "BlankVidFrames")
{
# Frame counts needed for calculations. Last frame of clip is FC* - 1.
FCa = a.FrameCount
FCb = b.FrameCount

# Set defaults and check for errors.
aVidStartFadeIn = default(aVidStartFadeIn, 0)
aVidFadeInLen = default(aVidFadeInLen, 0)
aVidStartFadeOut = default(aVidStartFadeOut, FCa - 1)
aVidFadeOutLen = default(aVidFadeOutLen, 0)

aAudStartFadeIn = default(aAudStartFadeIn, 0)
aAudFadeInLen = default(aAudFadeInLen, 0)
aAudStartFadeOut = default(aAudStartFadeOut, FCa - 1)
aAudFadeOutLen = default(aAudFadeOutLen, 0)

bVidStartFadeIn = default(bVidStartFadeIn, 0)
bVidFadeInLen = default(bVidFadeInLen, 0)
bVidStartFadeOut = default(bVidStartFadeOut, FCb - 1)
bVidFadeOutLen = default(bVidFadeOutLen, 0)

bAudStartFadeIn = default(bAudStartFadeIn, 0)
bAudFadeInLen = default(bAudFadeInLen, 0)
bAudStartFadeOut = default(bAudStartFadeOut, FCb - 1)
bAudFadeOutLen = default(bAudFadeOutLen, 0)

VidOverlap = default(VidOverlap, 0)
color = default(color, $000000)
BlankVidFrames = default(BlankVidFrames, 0)

Assert(((aVidStartFadeIn >= 0) && (aVidStartFadeIn < FCa) && (aVidStartFadeIn <= FCa - aVidFadeInLen)),
\ "DissolveMod: <aVidStartFadeIn> or <aVidFadeInLen> is out of bounds.")
Assert(((aVidStartFadeOut >= 0) && (aVidStartFadeOut < FCa) && (aVidStartFadeOut <= FCa - aVidFadeOutLen)),
\ "DissolveMod: <aVidStartFadeOut> or <aVidFadeOutLen> is out of bounds.")

Assert(((aAudStartFadeIn >= 0) && (aAudStartFadeIn < FCa) && (aAudStartFadeIn <= FCa - aAudFadeInLen)),
\ "DissolveMod: <aAudStartFadeIn> or <aAudFadeInLen> is out of bounds.")
Assert(((aAudStartFadeOut >= 0) && (aAudStartFadeOut < FCa) && (aAudStartFadeOut <= FCa - aAudFadeOutLen)),
\ "DissolveMod: <aAudStartFadeOut> or <aAudFadeOutLen> is out of bounds.")

Assert(((bVidStartFadeIn >= 0) && (bVidStartFadeIn < FCb) && (bVidStartFadeIn <= FCb - bVidFadeInLen)),
\ "DissolveMod: <bVidStartFadeIn> or <bVidFadeInLen> is out of bounds.")
Assert(((bVidStartFadeOut >= 0) && (bVidStartFadeOut < FCb) && (bVidStartFadeOut <= FCb - bVidFadeOutLen)),
\ "DissolveMod: <bVidStartFadeOut> or <bVidFadeOutLen> is out of bounds.")

Assert(((bAudStartFadeIn >= 0) && (bAudStartFadeIn < FCb) && (bAudStartFadeIn <= FCb - bAudFadeInLen)),
\ "DissolveMod: <bAudStartFadeIn> or <bAudFadeInLen> is out of bounds.")
Assert(((bAudStartFadeOut >= 0) && (bAudStartFadeOut < FCb) && (bAudStartFadeOut <= FCb - bAudFadeOutLen)),
\ "DissolveMod: <bAudStartFadeOut> or <bAudFadeOutLen> is out of bounds.")

# Used in so many calculations ... Makes the funtions smaller.
aVidEndFadeIn = aVidStartFadeIn + aVidFadeInLen
aAudEndFadeIn = aAudStartFadeIn + aAudFadeInLen
aVidEndFadeOut = aVidStartFadeOut + aVidFadeOutLen
aAudEndFadeOut = aAudStartFadeOut + aAudFadeOutLen

bVidEndFadeIn = bVidStartFadeIn + bVidFadeInLen
bAudEndFadeIn = bAudStartFadeIn + bAudFadeInLen
bVidEndFadeOut = bVidStartFadeOut + bVidFadeOutLen
bAudEndFadeOut = bAudStartFadeOut + bAudFadeOutLen

Assert((VidOverlap <= Min(aVidStartfadeIn + aVidEndFadeOut,bVidStartfadeIn + bVidEndFadeOut)),
\ "DissolveMod: <VidOverlap> exceeds the size of the smallest vid.")

Clamp(color,$000000,$ffffff)

Assert((BlankVidFrames >= 0),
\ "<BlankVIdFrames> needs to be greater than or equal to zero: " + String(BlankVidFrames))

# Needed for Fade Functions.
fps = a.FrameRate

# We need to apply the fades to the audio and video and cut them in the right spots.
# Separate lines so they are not long.
aVid = a.Trim2(aVidStartFadeIn,aVidEndFadeOut)
aVid = FadeIn0(aVid,aVidFadeInLen,color,fps)
aVid = FadeOut0(aVid,aVidFadeOutLen,color,fps)
aAud = a.Trim2(aAudStartFadeIn,aAudEndFadeOut)
aAud = FadeIn0(aAud,aAudFadeInLen,color,fps)
aAud = FadeOut0(aAud,aAudFadeOutLen,color,fps)

bVid = b.Trim2(bVidStartFadeIn,bVidEndFadeOut)
bVid = FadeIn0(bVid,bVidFadeInLen,color,fps)
bVid = FadeOut0(bVid,bVidFadeOutLen,color,fps)
bAud = b.Trim2(bAudStartFadeIn,bAudEndFadeOut)
bAud = FadeIn0(bAud,bAudFadeInLen,color,fps)
bAud = FadeOut0(bAud,bAudFadeOutLen,color,fps)

# This needs to be done to keep Audio/Video sync and debug modes of this function.
Pad = BlankClip(a,length=(Max(aVidStartFadeIn,aAudStartFadeIn) - Min(aVidStartFadeIn,aAudStartFadeIn) + 1),color=color)
aVid = (aVidStartFadeIn > aAudStartFadeIn) ? Pad + aVid : aVid
aAud = (aAudStartFadeIn > aVidStartFadeIn) ? Pad + aVid : aAud

Pad = BlankClip(a,length=(Max(bVidEndFadeOut,bAudEndFadeOut) - Min(bVidEndFadeOut,bAudEndFadeOut) + 1),color=color)
bVid = (bVidEndFadeOut < bAudEndFadeOut) ? bVid + Pad : bVid
bAud = (bAudEndFadeOut < bVidEndFadeOut) ? bVid + Pad : bAud

aVid = aVid + BlankClip(a,length=Floor(Float(BlankVidFrames) / 2.0),color=color)
bVid = BlankClip(b,length=Ceil(Float(BlankVidFrames) / 2.0),color=color) + bVid

V = Dissolve(aVid,bVid,VidOverlap)

# Store the new sizes of "a" and "b" and store V total size.
# In case we need these to convert this function to use three parts
# so that MixAudio is done once.
FCa = aAud.FrameCount
FCb = bAud.FrameCount
FCv = V.FrameCount

# Now to patch Audios to overlap correctly
aAud = aAud + BlankClip(a,length=(FCv - FCa),color=color)
bAud = BlankClip(b,length=(FCv - FCb),color=color) + bAud

#####
# Calculations to help find sections points to run MixAudio on
# as small a segment as possible. :)
aLAF = FCa - 1
bFAF = FCv - FCb

B1Len = Min(aLAF,bFAF - 1) + 1
B2Len = Max(aLAF + 1,bFAF) - B1Len # This length can be zero sometimes. ;)

A = aAud.Trim2(0,length=B1Len) +
\ MixAudio(aAud.Trim2(B1Len,length=B2Len),bAud.Trim2(B1Len,length=B2Len)) +
\ bAud.Trim2(B1Len + B2Len)
#####
#####
# Comment out the above statements if you don't mind running MixAudio
# on the entire clip. Then uncomment the following line.
#A = MixAudio(aAud,bAud,1,1)
#####

### Debug Code ###
# Used to see the audios as two waves with clip a on top.
# This requires the AudioGraph plugin.
#aVid = aVid + BlankClip(a,length=(V.FrameCount - aVid.FrameCount),color=color)
#bVid = BlankClip(b,length=(V.FrameCount - bVid.FrameCount),color=color) + bVid
#aTemp = AudioDub(aVid,aAud)#.Trim2(B1Len + B2Len)
#bTemp = AudioDub(bVid,bAud)#.Trim2(B1Len + B2Len)
#return StackVertical(AudioGraph(aTemp,5),AudioGraph(bTemp,5)).subtitle("aLAF:" + string(aLAF) +
# \ " bFAF:" + string(bFAF) + " B1Len:" + string(B1Len) + " B2Len:" + string(B2Len))
###

return AudioDub(V,A)
}