VideoHelp Forum
+ Reply to Thread
Results 1 to 15 of 15
Thread
  1. With avisynth, I can edit the audio on videos so that it fades in (from zero) or out (to zero). What I'd like to know is: is there an avisynth function/script that allows fading to a certain level eg the start of a video might be at amplification factor 2 with a five second fade down to the original volume?

    Thanks.
    Quote Quote  
  2. Member
    Join Date
    Feb 2006
    Location
    United States
    Search Comp PM
    Originally Posted by pooksahib View Post
    With avisynth, I can edit the audio on videos so that it fades in (from zero) or out (to zero). What I'd like to know is: is there an avisynth function/script that allows fading to a certain level eg the start of a video might be at amplification factor 2 with a five second fade down to the original volume?

    Thanks.
    see if this thread will help - https://forum.videohelp.com/threads/383792-Using-avisynth-to-fade-out-the-audio
    Quote Quote  
  3. Member Cornucopia's Avatar
    Join Date
    Oct 2001
    Location
    Deep in the Heart of Texas
    Search PM
    If you want to fade up/down from a 2x volume to a 1x volume, etc, and easy cheat is to just use an extra copy of the clip and do the fade only on the copy and combine both clips.

    Scott
    Quote Quote  
  4. Try this:

    Code:
    ###############################################################
    #
    # Fade audio from 2x to 1x over the specified number of frames.
    #
    ###############################################################
    
    function FadeFrom2xTo1x(clip source, int NumFrames)
    {
        source = source.Amplify(2.0)
    
        Trim(source, 0, NumFrames)
        FadeOut0(NumFrames)
    
        Mixaudio(source, last)
    }
    
    ###############################################################

    A call like FadeFrom2xto1x(50) will fade audio from 2x to 1x over 50 frames.

    VirtualDub's audio waveform of a constant amplitude sine wave after FadeFrom2xto1x(50):
    Image
    [Attachment 69897 - Click to enlarge]


    It would be useful to include the amplification amount as a variable too.
    Last edited by jagabo; 21st Mar 2023 at 10:02. Reason: added image and note
    Quote Quote  
  5. My thanks, guys.
    jagabo, special thanks to you. At first, I couldn't see that your function was working so I ended up downloading a clip from youtube of a road drill being used (constant, even sound) and edited it with "trim(200,1000).FadeFrom2xto1x(400)". I could see that it had worked via WavePad (similar to Audacity). Great stuff, thanks again.

    I don't fully understand how the function works - I can see where you specify the starting volume but I don't see the closing volume being set. Presumably avisynth just fades to source volume in the absence of being told anything else. Which leads me to this: Say I wanted to make this edit:

    Code:
    Trim(1000,5000).amplify(6)++trim(5001,6000)#reducing volume#++trim(6001,9000).amplify(2)
    Is that doable?
    Quote Quote  
  6. MixAudio() does (A+B)/2 by default. ie the average of two audio clips. I create two streams from the source. One has the volume amplified by 2, A*2. The other has the Volume amplified to A*2 but it fades over 50 frames (or whatever number of frames your specify) to A*0, or 0. So when you mix the two streams you get (A*2 + A*2) / 2 at the start, or A*2, twice the original volume. The volume of the second clip decrease linearly until it reaches A*0 at the end of the clip. So at the end of the short clip you have (A*2 + A*0) / 2, or (A*2) / 2, or just A, the original volume. Since the second clip ends at 50 frames its audio for the rest of that stream is 0. So the the rest of the clip continues to have (A*2 + 0) / 2, or just A -- the original audio volume.
    Last edited by jagabo; 21st Mar 2023 at 16:23.
    Quote Quote  
  7. Here's a version that lets you specify the initial amplification:

    Code:
    ###############################################################
    #
    # Fade audio from Nx to 1x over the specified number of frames.
    #
    ###############################################################
    
    function FadeFromNxTo1x(clip source, int "NumFrames", float "InitialAmplitude")
    {
        NumFrames = default(NumFrames, 30)
        InitialAmplitude = default(InitialAmplitude, 2.0)
    
        ampd = source.Amplify((InitialAmplitude-1.0)*2.0)
    
        Mixaudio(source.Amplify(2.0), ampd.Trim(0, NumFrames).FadeOut0(NumFrames))
    }
    
    ###############################################################
    Quote Quote  
  8. That's great, many thanks indeed for writing it. I'm guessing that my scenario above (reducing from Amp6 to Amp2) isn't possible as all fades are to Source?
    Quote Quote  
  9. Originally Posted by pooksahib View Post
    That's great, many thanks indeed for writing it. I'm guessing that my scenario above (reducing from Amp6 to Amp2) isn't possible as all fades are to Source?
    Sure it's possible. Here's one way:

    Code:
    Amplify(2.0)
    Trim(1000,5000).Amplify(3)++trim(5001,6000).FadeFromNxTo1x(1000, 3.0)++trim(6001,9000)
    Quote Quote  
  10. Clever. 3x2=6. And by stipulating Amplify(2) at the outset, the fade never falls below that?
    Many thanks (again).
    Quote Quote  
  11. Originally Posted by pooksahib View Post
    Clever. 3x2=6. And by stipulating Amplify(2) at the outset, the fade never falls below that?
    Yes.

    Here's a more general solution where you specify the initial gain and the final gain. The gain is linearly interpolated over the full clip:

    Code:
    #############################################################################
    #
    # Vary audio gain from StartGain to EndGain over the full clip.
    #
    #############################################################################
    
    function VariableAmplify(clip Source, float "StartGain", float "EndGain")
    {
        NumFrames = Source.FrameCount
    
        a1 = Source.Amplify(StartGain*2.0).FadeOut0(Source.FrameCount)
        a2 = Source.Amplify(EndGain*2.0).FadeIn0(Source.FrameCount)
        Mixaudio(a1, a2)
        AudioDub(Source, last)
    }
    
    #############################################################################
    Your earlier example could be achieved with:

    Code:
    Trim(1000,5000).Amplify(6.0)++trim(5001,6000).VariableAmplify(6.0, 2.0)++trim(6001,9000).Amplify(2.0)
    Note that gain values less than 1.0 reduce the volume. So VariableAmplify(1.0, 0.0) is a fade to silence.
    Quote Quote  
  12. That final code is very neat and tidy. Sincerest thanks. I think I did well simply learning how to use avisynth to make a basic edit but when I read your posts I realise how little I know...
    Quote Quote  
  13. I've given that script a workout, this time on a 'Tone Test' video and it's certainly an easy script to use. The video I used it on lasts 48 seconds with 1440 frames and this is my script (I changed the name of the function):
    Code:
    function semifade(clip Source, float "StartGain", float "EndGain")
    {
        NumFrames = Source.FrameCount
    
        a1 = Source.Amplify(StartGain*2.0).FadeOut0(Source.FrameCount)
        a2 = Source.Amplify(EndGain*2.0).FadeIn0(Source.FrameCount)
        Mixaudio(a1, a2)
        AudioDub(Source, last)
    }
    
    LoadPlugin("D:\MeGUI\tools\lsmash\LSMASHSource.dll")
    a=LSMASHVideoSource("D:\Test Tone.mp4")
    b=LSMASHaudioSource("D:\Test Tone.mp4")
    audiodub(a,b)
    
    Trim(0,480).Amplify(2)++trim(481,960).semifade(2, 0.5)++trim(961,1440).Amplify(0.5)
    As you can see, I've split the vid into thirds: loud/fade/low. Attached is the waveform and you can see that the fade ends exactly where it should at 32 seconds. Oddly, the 'loud' section seems to have overrun by 4-6 seconds. Can you identify any reason for that?

    Otherwise, terrific script, exactly what I was after.
    Image
    [Attachment 69918 - Click to enlarge]
    Quote Quote  
  14. You have blown out the audio. The sections are the correct length but the middle section is maxed out for the first ~1/3 of its duration.
    Quote Quote  
  15. You're right. Thanks a lot.
    Code:
    Trim(0,480)++trim(481,960).semifade(1, 0.5)++trim(961,1440).Amplify(0.5)
    Image
    [Attachment 69919 - Click to enlarge]
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!