<p>The easiest way to do this is to use an expression. First you convert audio to keyframes. Then set an expression on your opacity channel. If you drag the pick-whip for the expression to the audio keyframe property you'll get an opacity that bounces all over the place like crazy, so we're going to have to do a bit more to it.</p>

<p>First, in the opacity channel for your layer, set up a variable like so:</p>

<pre><code>var audioAmp = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");
</code></pre>

<p>(the expression on the right of the <code>=</code> is just what you get if you drag the pickwhip to the 'Both Channels' property). </p>

<p>We want to sample that property over time, to allow for slow fadeouts. </p>

<p>First, to make it easier to tweak we can set some properties that we can edit until we get the best results. </p>

<pre><code>var numSamples = 200;
var sampleSpacing = thisComp.frameDuration;
var threshold = 5.0 //&lt;--you set this
</code></pre>

<ul>
<li><p><code>numSamples</code> is the number of times we sample the amplitude to get a smooth average. The higher it is, the smoother the result, but also the slower the script is. If you set it really high you may want to convert the expression to keyframes once you've got it working right. The values I put in here are just pulled out of my hat, you'll have to tweak them until they work for you. This is the main setting that you can tweak. Have a go at setting it really high, like 2000 or low like 10, and see how it works.</p></li>
<li><p><code>sampleSpacing</code> is the time between samples. Setting it to <code>thisComp.frameDuration</code> means one sample per frame. You could do a smaller sample, but given that the audio amplitude property only has one keyframe per frame it might not really help.</p></li>
<li><p><code>Threshold</code> is the audio threshold above which no fading happens. Again you <em>will</em> have to tweak this depending on the audio level of your material.</p></li>
</ul>

<p>Ok, here comes the hoo-hah.</p>

<pre><code>var sum = 0;
for (var i = 0; i &lt; numSamples; i++){
  sum += audioAmp.valueAtTime(time - i * sampleSpacing);
}
avAudio = sum / numSamples; //the average audio for all the samples
//now decide if the audio is low enough to start fading
if (avAudio &lt; threshold) { 
  //normalises it to 0-100% where 100% = the threshold
  100 * avAudio / threshold; 
} else { //audio is above threshold
  100;
}
</code></pre>

<p>Basically it just averages the audio out for the last <code>numSamples</code> samples. When you have a sound followed by silence the average will go from the volume of the sound to zero in <code>numSamples</code> * <code>sampleSpacing</code> seconds, or in this case 200 frames.</p>

CC BY-SA 3.0
2017-02-28T04:16:15.570Z
2017-03-03T05:48:14.500Z
2017-03-03T05:48:14.500Z
stib
<p>I have <code>3 audio files</code> from a podcast. 1 file from Steve, one from john, and the other is from me. </p>

<p>I have a composition with the three audio files and also <code>3 png images</code> of our faces, one for each of our faces. <strong>Each image</strong> has <code>an opacity keyframe set to 50%</code> by default. Now i want to connect each audio track to the right image and animate as follows:</p>

<blockquote>
  <p>✖ When audio intensity of a track is <code>more than x decibels</code> then gradually animate the image that's linked to that track <code>from 50% opacity to 100% opacity</code>. <strong>To give the
  visual effect of who is currently talking</strong>.</p>
  
  <p>✖ When the sound intensity <code>drops below the x decibels</code> i have set before, then gradually <code>animate the opacity back to 50%</code></p>
</blockquote>

<p>How can i achieve that?</p>

CC BY-SA 3.0
2017-02-25T22:18:02.917Z
Have you seen [this article](https://www.reddit.com/r/AfterEffects/comments/4cy3hq/is_there_a_way_to_control_opacity_dictated_by_the/) on Reddit?
bret7600
CC BY-SA 3.0
2017-02-27T11:37:20.103Z
@bret7600 Thank you, but that didn't quite give me the full answer :)
GuntherKushner
<after-effects><adobe><after-effects-expressions><after-effects-scripting>
Link audio to opacity effect of an image in After Effects
GuntherKushner
# Link audio to opacity effect of an image in After Effects

(Asked by: username_0 on 2017-02-25)

I have `3 audio files` from a podcast. 1 file from Steve, one from john, and the other is from me.

I have a composition with the three audio files and also `3 png images` of our faces, one for each of our faces. **Each image** has `an opacity keyframe set to 50%` by default. Now i want to connect each audio track to the right image and animate as follows:

> ✖ When audio intensity of a track is `more than x decibels` then gradually animate the image that's linked to that track `from 50% opacity to 100% opacity`. **To give the
> visual effect of who is currently talking**.
>
> ✖ When the sound intensity `drops below the x decibels` i have set before, then gradually `animate the opacity back to 50%`

How can i achieve that?

**Tags:** after-effects, adobe, after-effects-expressions, after-effects-scripting

> **Comments:**
> **username_1**: Have you seen [this article](https://www.reddit.com/r/AfterEffects/comments/4cy3hq/is_there_a_way_to_control_opacity_dictated_by_the/) on Reddit?  
> **username_0**: @username_1 Thank you, but that didn't quite give me the full answer :)  

---

## 1 answers

### Answer by username_2 on 2017-02-28. Score: 2 | Accepted answer

The easiest way to do this is to use an expression. First you convert audio to keyframes. Then set an expression on your opacity channel. If you drag the pick-whip for the expression to the audio keyframe property you'll get an opacity that bounces all over the place like crazy, so we're going to have to do a bit more to it.
First, in the opacity channel for your layer, set up a variable like so:
var audioAmp = thisComp.layer("Audio Amplitude").effect("Both Channels")("Slider");

(the expression on the right of the = is just what you get if you drag the pickwhip to the 'Both Channels' property). 
We want to sample that property over time, to allow for slow fadeouts. 
First, to make it easier to tweak we can set some properties that we can edit until we get the best results. 
var numSamples = 200;
var sampleSpacing = thisComp.frameDuration;
var threshold = 5.0 //<--you set this


numSamples is the number of times we sample the amplitude to get a smooth average. The higher it is, the smoother the result, but also the slower the script is. If you set it really high you may want to convert the expression to keyframes once you've got it working right. The values I put in here are just pulled out of my hat, you'll have to tweak them until they work for you. This is the main setting that you can tweak. Have a go at setting it really high, like 2000 or low like 10, and see how it works.
sampleSpacing is the time between samples. Setting it to thisComp.frameDuration means one sample per frame. You could do a smaller sample, but given that the audio amplitude property only has one keyframe per frame it might not really help.
Threshold is the audio threshold above which no fading happens. Again you will have to tweak this depending on the audio level of your material.

Ok, here comes the hoo-hah.
var sum = 0;
for (var i = 0; i < numSamples; i++){
  sum += audioAmp.valueAtTime(time - i * sampleSpacing);
}
avAudio = sum / numSamples; //the average audio for all the samples
//now decide if the audio is low enough to start fading
if (avAudio < threshold) { 
  //normalises it to 0-100% where 100% = the threshold
  100 * avAudio / threshold; 
} else { //audio is above threshold
  100;
}

Basically it just averages the audio out for the last numSamples samples. When you have a sound followed by silence the average will go from the volume of the sound to zero in numSamples * sampleSpacing seconds, or in this case 200 frames.


---

